Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6449012db9bcffe54e6512688a7e146751744a09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 *
 */
package org.eclipse.papyrus.infra.ui.extension.diagrameditor;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.papyrus.infra.ui.editorsfactory.IEditorIconFactoryExtended;
import org.eclipse.swt.graphics.Image;

/**
 * A factory used to create the Icon associated to an editor TODO Lets have a
 * common ancestor for {@link EditorIconFactory} and {@link EditorFactoryProxy}
 *
 * @author cedric dumoulin
 *
 */
public class EditorIconFactory implements IEditorIconFactoryExtended {

	/**
	 * The concrete implementation.
	 */
	private IPluggableEditorFactory editorFactory;

	/**
	 * EditorDescriptor associated to the factory.
	 */
	protected EditorDescriptor editorDescriptor;

	/**
	 * Cached image for reuse.
	 */
	protected Image cachedImage;

	/**
	 * Constructor.
	 *
	 * @param serviceRegistry
	 * @param editorDescriptor
	 */
	public EditorIconFactory(EditorDescriptor editorDescriptor) {
		this.editorDescriptor = editorDescriptor;
	}

	/**
	 * @see org.eclipse.papyrus.infra.ui.editorsfactory.IEditorIconFactory#getEditorIcon(java.lang.Object)
	 *
	 * @param pageIdentifier
	 * @return
	 */
	@Override
	public Image getEditorIcon(Object pageIdentifier) {

		if (cachedImage == null) {
			cachedImage = createEditorIcon(pageIdentifier);
		}

		return cachedImage;
	}

	/**
	 * Create an Image associated to the editor used to render the specified
	 * pageIdentifier
	 *
	 * @return
	 */
	@Override
	public Image createEditorIcon(Object pageIdentifier) {
		ImageDescriptor imageDescriptor = editorDescriptor.getIcon();
		if (imageDescriptor == null) {
			return null;
		}
		Image image = imageDescriptor.createImage();
		return image;
	}

	/**
	 * @see org.eclipse.papyrus.infra.ui.editorsfactory.IEditorFactory#isPageModelFactoryFor(java.lang.Object)
	 *
	 * @param pageIdentifier
	 * @return
	 */
	@Override
	public boolean isPageModelFactoryFor(Object pageIdentifier) {
		return getEditorFactory().isPageModelFactoryFor(pageIdentifier);
	}

	/**
	 * @return the editorFactory
	 */
	protected IPluggableEditorFactory getEditorFactory() {

		if (editorFactory == null) {
			editorFactory = createEditorFactory();
		}

		return editorFactory;

	}

	/**
	 * Create an instance of IPluggableEditorFactory as described in the
	 * editorDescriptor. TODO let propagate the exceptions.
	 *
	 * @return
	 */
	private IPluggableEditorFactory createEditorFactory() {
		// Create the requested class.
		try {
			editorFactory = editorDescriptor.getEditorFactoryClass().newInstance();
			// Set the descriptor. USed by the factory to get the ActionBarId
			// and Icon
			// editorFactory.init(serviceRegistry, editorDescriptor);
			return editorFactory;
		} catch (InstantiationException e) {
			// Lets propagate. This is an implementation problem that should be
			// solved by programmer.
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			// Lets propagate. This is an implementation problem that should be
			// solved by programmer.
			throw new RuntimeException(e);
		}

	}

	/**
	 * Return the URL of the main icon used to create this icon
	 *
	 * @see org.eclipse.papyrus.infra.ui.editorsfactory.IEditorIconFactory#getURLMainIcon(java.lang.Object)
	 *
	 * @param pageIdentifier
	 * @return
	 */
	@Override
	public String getURLMainIcon(Object pageIdentifier) {
		return editorDescriptor.getIconURL();
	}

	/**
	 * {@inheritDoc}
	 *
	 * Dispose the cached image
	 */
	@Override
	public void dispose() {
		if (cachedImage != null) {
			cachedImage.dispose();
			cachedImage = null;
		}
	}

}

Back to the top