Skip to content

Commit

Permalink
ZSTD-4724 [Notifications] Invalid thread access when closing Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
wgalanciak committed Dec 5, 2014
1 parent c6b3bde commit 414b735
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.zend.core.notifications.util.Fonts;
import org.zend.core.notifications.util.ImageCache;

/**
* The activator class controls the plug-in life cycle
Expand Down Expand Up @@ -65,16 +64,6 @@ public void start(BundleContext context) throws Exception {
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);

Display d = Display.getDefault();
if (! d.isDisposed()) {
d.asyncExec(new Runnable() {
public void run() {
ImageCache.dispose();
Fonts.dispose();
}
});
}
}

/**
Expand Down Expand Up @@ -113,6 +102,33 @@ public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(PLUGIN_ID, path);
}

public Image getImage(String path) {
Image image = getImageRegistry().get(path);
if (image == null) {
getImageRegistry().put(path, getImageDescriptor(path));
image = getImageRegistry().get(path);
}
return image;
}

/**
* Returns an image for notification close button focused out.
*
* @return {@link Image} or null if image was not found
*/
public static Image getCloseOut() {
return Activator.getDefault().getImage("icons/close.gif"); //$NON-NLS-1$
}

/**
* Returns an image for notification close button focused in.
*
* @return {@link Image} or null if image was not found
*/
public static Image getCloseIn() {
return Activator.getDefault().getImage("icons/close_active.gif"); //$NON-NLS-1$
}

private Shell getWorkbenchShell(Shell previousShell) {
IWorkbenchWindow[] windows = PlatformUI.getWorkbench()
.getWorkbenchWindows();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.PlatformUI;
import org.zend.core.notifications.Activator;
import org.zend.core.notifications.ui.IActionListener;
import org.zend.core.notifications.ui.IBody;
import org.zend.core.notifications.ui.NotificationSettings;
import org.zend.core.notifications.util.FontName;
import org.zend.core.notifications.util.Fonts;

/**
Expand Down Expand Up @@ -54,13 +52,13 @@ public Composite createContent(Composite container,
layout.horizontalSpacing = layout.verticalSpacing = 2;
composite.setLayout(layout);
Link text = new Link(composite, SWT.WRAP);
text.setFont(Fonts.get(FontName.DEFAULT));
text.setFont(Fonts.DEFAULT.getFont());
text.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, true));
text.setText(message);
if (helpContextId != null) {
text.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent selectionEvent) {
public void widgetSelected(SelectionEvent selectionEvent) {
org.eclipse.swt.program.Program.launch(helpContextId);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
import org.zend.core.notifications.ui.NotificationSettings;
import org.zend.core.notifications.util.Colors;
import org.zend.core.notifications.util.EnvironmentUtils;
import org.zend.core.notifications.util.FontName;
import org.zend.core.notifications.util.Fonts;
import org.zend.core.notifications.util.ImageCache;

public class Notification implements IActionListener, INotification {

Expand Down Expand Up @@ -291,7 +289,7 @@ protected void createBody(Composite container) {
layout.horizontalSpacing = layout.verticalSpacing = 2;
body.setLayout(layout);
Label text = new Label(body, SWT.WRAP);
text.setFont(Fonts.get(FontName.DEFAULT));
text.setFont(Fonts.DEFAULT.getFont());
text.setLayoutData(new GridData(GridData.FILL_BOTH));
text.setForeground(Display.getDefault().getSystemColor(
SWT.COLOR_BLACK));
Expand Down Expand Up @@ -348,7 +346,7 @@ protected void createTitle(Composite container) {
titleLabel.setText(settings.getTitle());
titleLabel.setForeground(Colors.getColorRegistry().get(
"org.eclipse.ui.workbench.INACTIVE_TAB_TEXT_COLOR")); //$NON-NLS-1$
titleLabel.setFont(Fonts.get(FontName.BOLD));
titleLabel.setFont(Fonts.BOLD.getFont());
}

protected void createImage(Composite container) {
Expand All @@ -367,22 +365,22 @@ protected void createClose(Composite container) {
final CLabel button = new CLabel(container, SWT.NONE);
button.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING
| GridData.HORIZONTAL_ALIGN_BEGINNING));
button.setImage(ImageCache.getCloseOut());
button.setImage(Activator.getCloseOut());
button.addMouseTrackListener(new MouseTrackAdapter() {
@Override
public void mouseEnter(MouseEvent e) {
button.setImage(ImageCache.getCloseIn());
button.setImage(Activator.getCloseIn());
}

@Override
public void mouseExit(MouseEvent e) {
button.setImage(ImageCache.getCloseOut());
button.setImage(Activator.getCloseOut());
}
});
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
button.setImage(ImageCache.getCloseOut());
button.setImage(Activator.getCloseOut());
hide();
statusChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.zend.core.notifications.NotificationManager;
import org.zend.core.notifications.internal.ui.Notification;
import org.zend.core.notifications.ui.NotificationSettings;
import org.zend.core.notifications.util.FontName;
import org.zend.core.notifications.util.Fonts;

/**
Expand Down Expand Up @@ -92,7 +91,7 @@ protected void createBody(Composite container) {
layout.horizontalSpacing = layout.verticalSpacing = 2;
composite.setLayout(layout);
message = new Label(composite, SWT.WRAP);
message.setFont(Fonts.get(FontName.DEFAULT));
message.setFont(Fonts.DEFAULT.getFont());
message.setLayoutData(new GridData(GridData.FILL_BOTH));
message.setForeground(Display.getDefault().getSystemColor(
SWT.COLOR_BLACK));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package org.zend.core.notifications.ui;

import org.eclipse.swt.graphics.Image;
import org.zend.core.notifications.util.ImageCache;
import org.zend.core.notifications.Activator;

/**
* Represents notification type.
Expand All @@ -18,11 +18,11 @@
*/
public enum NotificationType {

ERROR(ImageCache.getImage("icons/error.png")), //$NON-NLS-1$
ERROR(Activator.getDefault().getImage("icons/error.png")), //$NON-NLS-1$

WARNING(ImageCache.getImage("icons/warn.png")), //$NON-NLS-1$
WARNING(Activator.getDefault().getImage("icons/warn.png")), //$NON-NLS-1$

INFO(ImageCache.getImage("icons/info.png")), //$NON-NLS-1$
INFO(Activator.getDefault().getImage("icons/info.png")), //$NON-NLS-1$

CUSTOM(null);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,125 +1,38 @@
/*******************************************************************************
* Copyright (c) 2012 Zend Technologies Ltd.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.zend.core.notifications.util;

import static org.zend.core.notifications.util.FontName.BOLD;
import static org.zend.core.notifications.util.FontName.DEFAULT;
import static org.zend.core.notifications.util.FontName.ITALIC;
import static org.zend.core.notifications.util.FontName.ITALIC_BOLD;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;

/**
* Caching class for fonts. Also deals with re-creating fonts should they have
* been disposed when the caller asks for a font.
*
* @author Wojciech Galanciak, 2012
*
*/
public class Fonts {

private static Map<String, Font> cache = new HashMap<String, Font>();

static {
Display.getDefault().syncExec(new Runnable() {

public void run() {
Font defaultFont = JFaceResources.getFontRegistry()
.defaultFont();
cache.put(DEFAULT.getName(), defaultFont);
Font boldFont = JFaceResources.getFontRegistry().getBold(
JFaceResources.DEFAULT_FONT);
cache.put(BOLD.getName(), boldFont);
Font italicFont = JFaceResources.getFontRegistry().getItalic(
JFaceResources.DEFAULT_FONT);
cache.put(ITALIC.getName(), italicFont);
Font italicBold = new Font(Display.getCurrent(),
getModifiedFontData(get(ITALIC).getFontData(), SWT.BOLD
| SWT.ITALIC));
cache.put(ITALIC_BOLD.getName(), italicBold);
}
});
}

/**
* Disposes all fonts and clears out the cache. Never call this unless you
* are shutting down your code/client/etc.
*/
public static void dispose() {
if (cache.size() > 0) {
Set<String> keys = cache.keySet();
for (String key : keys) {
Font font = cache.get(key);
if (font != null && !font.isDisposed()) {
font.dispose();
}
}
}
cache.clear();
}

/**
* Get font for specified {@link FontName}.
*
* @param name
* font name
* @return font
*/
public static Font get(FontName name) {
return cache.get(name.getName());
}

/**
* Get font for specified name.
*
* @param name
* font name
* @return font or <code>null</code> if font with specified name is
* unavailable
*/
public static Font get(String name) {
return cache.get(name);
}

/**
* Add new font to change with specified name.
*
* @param name
* new font name
* @param font
* @return <code>true</code> if font added successfully; otherwise return
* <code>false</code>
*/
public static boolean add(String name, Font font) {
if (cache.containsKey(name)) {
return false;
}
cache.put(name, font);
return true;
}

private static FontData[] getModifiedFontData(FontData[] baseData, int style) {
FontData[] styleData = new FontData[baseData.length];
for (int i = 0; i < styleData.length; i++) {
FontData base = baseData[i];
styleData[i] = new FontData(base.getName(), base.getHeight(),
base.getStyle() | style);
}

return styleData;
}

}
/*******************************************************************************
* Copyright (c) 2012 Zend Technologies Ltd.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.zend.core.notifications.util;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.graphics.Font;

/**
* Set of default fonts available in {@link Fonts} cache.
*
* @author Wojciech Galanciak, 2012
*
*/
public enum Fonts {

DEFAULT(JFaceResources.getFontRegistry().defaultFont()),

BOLD(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT)),

ITALIC(JFaceResources.getFontRegistry().getItalic(
JFaceResources.DEFAULT_FONT));

private Font font;

private Fonts(Font font) {
this.font = font;
}

public Font getFont() {
return font;
}

}
Loading

0 comments on commit 414b735

Please sign in to comment.