diff options
author | palldredge | 2016-10-10 22:01:12 +0000 |
---|---|---|
committer | palldredge | 2016-10-12 13:30:51 +0000 |
commit | fd8c9a5daf54d6126fdb0ac84e656da3aa6d7135 (patch) | |
tree | 91ed25d193f4639ac866483c6ee636722a03b51e | |
parent | 16afdb91090a825ee06a33a753fc445a0493920d (diff) | |
download | org.eclipse.graphiti-fd8c9a5daf54d6126fdb0ac84e656da3aa6d7135.tar.gz org.eclipse.graphiti-fd8c9a5daf54d6126fdb0ac84e656da3aa6d7135.tar.xz org.eclipse.graphiti-fd8c9a5daf54d6126fdb0ac84e656da3aa6d7135.zip |
Bug 465675 - Improve SWT Font management
Change-Id: Ifa192e702a493515f17948a2b449e499be66427e
Signed-off-by: palldredge <pwa0001@uah.edu>
3 files changed, 44 insertions, 47 deletions
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/IResourceRegistry.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/IResourceRegistry.java index 938b7288..a32e83f5 100644 --- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/IResourceRegistry.java +++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/IResourceRegistry.java @@ -9,6 +9,7 @@ * * Contributors: * SAP AG - initial API, implementation and documentation + * palldredge - Bug 465675 - Improve SWT Font management * * </copyright> * @@ -16,6 +17,8 @@ package org.eclipse.graphiti.ui.internal; import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; /** * @noimplement This interface is not intended to be implemented by clients. @@ -43,4 +46,14 @@ public interface IResourceRegistry extends IDisposable { * @see org.eclipse.swt.graphics.Color */ Color getSwtColor(String hexRGBString); + + /** + * Provides an SWT font instance with the given font data. + * + * @param fontData the font data describing the desired font + * @return an SWT font instance + * + * @see org.eclipse.swt.graphics.Font + */ + Font getSwtFont(FontData fontData); }
\ No newline at end of file diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/ResourceRegistry.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/ResourceRegistry.java index d524ea10..e670684b 100644 --- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/ResourceRegistry.java +++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/ResourceRegistry.java @@ -9,6 +9,7 @@ * * Contributors: * SAP AG - initial API, implementation and documentation + * palldredge - Bug 465675 - Improve SWT Font management * * </copyright> * @@ -20,6 +21,8 @@ import java.util.Map; import org.eclipse.graphiti.util.ColorUtil; import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.Resource; @@ -90,4 +93,24 @@ public class ResourceRegistry implements IResourceRegistry { return getSwtColor(ColorUtil.getRedFromHex(hexRGBString), ColorUtil.getGreenFromHex(hexRGBString), ColorUtil .getBlueFromHex(hexRGBString)); } + + /** + * Provides an SWT font instance with the given font data. + * + * @param fontData the font data describing the desired font + * @return an SWT font instance + * + * @see org.eclipse.swt.graphics.Font + */ + @Override + public Font getSwtFont(final FontData fontData) { + Resource ret = registry.get(fontData); + if (ret == null) { + ret = new Font(null, fontData); + registry.put(fontData, ret); + ResourceManager.getResourceManager().manageResource(this, ret); + } + + return ret instanceof Font ? ((Font) ret) : null; + } } diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/parts/PictogramElementDelegate.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/parts/PictogramElementDelegate.java index daeda54a..aed2f479 100644 --- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/parts/PictogramElementDelegate.java +++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/parts/PictogramElementDelegate.java @@ -28,7 +28,7 @@ * mwenz - Bug 459386 - Refresh Connection when getDiagramBehavior().refreshRenderingDecorators(PEInstance) is called * mwenz - Bug 464857 - Images created by GFImageFigure are not destroyed * mwenz - Bug 481619 - Graphiti is not using custom PlatformGraphicsAlgorithm's layout manager; enforces XYLayout - * + * palldredge - Bug 465675 - Improve SWT Font management * </copyright> * *******************************************************************************/ @@ -37,7 +37,6 @@ package org.eclipse.graphiti.ui.internal.parts; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.List; @@ -140,6 +139,7 @@ import org.eclipse.graphiti.util.IColorConstant; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; import org.eclipse.ui.ISharedImages; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.model.IWorkbenchAdapter; @@ -164,9 +164,6 @@ public class PictogramElementDelegate implements IPictogramElementDelegate { private final Hashtable<GraphicsAlgorithm, IFigure> elementFigureHash = new Hashtable<GraphicsAlgorithm, IFigure>(); - private final HashSet<Font> fontList = new HashSet<Font>(); - private final HashSet<Font> decoratorFontList = new HashSet<Font>(); - private PictogramElement pictogramElement; private final HashMap<IFigure, List<IFigure>> decoratorMap = new HashMap<IFigure, List<IFigure>>(); @@ -239,8 +236,6 @@ public class PictogramElementDelegate implements IPictogramElementDelegate { ((ImageFigure) figure).setImage(null); } } - - disposeFonts(); } /* @@ -682,23 +677,8 @@ public class PictogramElementDelegate implements IPictogramElementDelegate { // create/change swt-font of label org.eclipse.graphiti.mm.algorithms.styles.Font font = Graphiti.getGaService().getFont(text, true); if (font != null && font.getName() != null) { - - Font currentSwtFont = label.getFont(); - if (currentSwtFont == null || currentSwtFont.isDisposed()) { - Font newSwtFont = DataTypeTransformation.toSwtFont(font); - fontList.add(newSwtFont); - label.setFont(newSwtFont); - } else { - Font newSwtFont = DataTypeTransformation.syncToSwtFont(font, currentSwtFont); - if (newSwtFont != currentSwtFont) { - fontList.add(newSwtFont); - label.setFont(newSwtFont); - boolean wasInList = fontList.remove(currentSwtFont); - if (wasInList) { - currentSwtFont.dispose(); - } - } - } + IResourceRegistry resourceRegistry = getConfigurationProvider().getResourceRegistry(); + label.setFont(resourceRegistry.getSwtFont(DataTypeTransformation.toFontData(font))); } } @@ -994,15 +974,15 @@ public class PictogramElementDelegate implements IPictogramElementDelegate { String text = textDecorator.getText(); decoratorFigure = new Label(text); - Font font = new org.eclipse.swt.graphics.Font(null, textDecorator.getFontName(), - textDecorator.getFontSize(), SWT.NORMAL); - decoratorFontList.add(font); + IResourceRegistry resourceRegistry = getConfigurationProvider().getResourceRegistry(); + final Font font = resourceRegistry + .getSwtFont(new FontData(textDecorator.getFontName(), textDecorator.getFontSize(), SWT.NORMAL)); + Dimension dimension = TextUtilities.INSTANCE.getStringExtents(text, font); boundsForDecoratorFigure.setSize(dimension.width, dimension.height); decoratorFigure.setFont(font); - IResourceRegistry resourceRegistry = getConfigurationProvider().getResourceRegistry(); IColorConstant backgroundColor = textDecorator.getBackgroundColor(); if (backgroundColor != null) { decoratorFigure.setOpaque(true); @@ -1106,24 +1086,6 @@ public class PictogramElementDelegate implements IPictogramElementDelegate { return decoratorFigure; } - /* - * must be called from the edit-part if this edit-part is de-activated - */ - private void disposeFonts() { - for (Iterator<Font> iter = fontList.iterator(); iter.hasNext();) { - Font font = iter.next(); - font.dispose(); - } - disposeDecoratorFonts(); - } - - private void disposeDecoratorFonts() { - for (Iterator<Font> iter = decoratorFontList.iterator(); iter.hasNext();) { - Font font = iter.next(); - font.dispose(); - } - } - /** * @param figure * @param updateNeeded @@ -1654,7 +1616,6 @@ public class PictogramElementDelegate implements IPictogramElementDelegate { decFigureList.clear(); decoratorMap.remove(figure); } - disposeDecoratorFonts(); } public boolean isValid() { |