1
package net.launchpad.vide.flowchart.diagram.part;
3
import java.util.ArrayList;
6
import net.launchpad.vide.flowchart.diagram.edit.policies.FlowchartBaseItemSemanticEditPolicy;
7
import net.launchpad.vide.flowchart.diagram.providers.ElementInitializers;
8
import net.launchpad.vide.flowchart.provider.FlowchartItemProviderAdapterFactory;
10
import org.eclipse.core.runtime.IPath;
11
import org.eclipse.core.runtime.IStatus;
12
import org.eclipse.core.runtime.Path;
13
import org.eclipse.core.runtime.Platform;
14
import org.eclipse.core.runtime.Status;
15
import org.eclipse.emf.common.notify.AdapterFactory;
16
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
17
import org.eclipse.emf.edit.provider.IItemLabelProvider;
18
import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
19
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
20
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
21
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
22
import org.eclipse.jface.resource.ImageDescriptor;
23
import org.eclipse.swt.graphics.Image;
24
import org.eclipse.ui.plugin.AbstractUIPlugin;
25
import org.osgi.framework.BundleContext;
30
public class FlowchartDiagramEditorPlugin extends AbstractUIPlugin {
35
public static final String ID = "net.launchpad.vide.diagram"; //$NON-NLS-1$
40
public static final PreferencesHint DIAGRAM_PREFERENCES_HINT = new PreferencesHint(
46
private static FlowchartDiagramEditorPlugin instance;
51
private ComposedAdapterFactory adapterFactory;
56
private FlowchartDocumentProvider documentProvider;
61
private FlowchartBaseItemSemanticEditPolicy.LinkConstraints linkConstraints;
66
private ElementInitializers initializers;
71
public FlowchartDiagramEditorPlugin() {
77
public void start(BundleContext context) throws Exception {
80
PreferencesHint.registerPreferenceStore(DIAGRAM_PREFERENCES_HINT,
81
getPreferenceStore());
82
adapterFactory = createAdapterFactory();
88
public void stop(BundleContext context) throws Exception {
89
adapterFactory.dispose();
90
adapterFactory = null;
91
linkConstraints = null;
100
public static FlowchartDiagramEditorPlugin getInstance() {
107
protected ComposedAdapterFactory createAdapterFactory() {
108
ArrayList<AdapterFactory> factories = new ArrayList<AdapterFactory>();
109
fillItemProviderFactories(factories);
110
return new ComposedAdapterFactory(factories);
116
protected void fillItemProviderFactories(List<AdapterFactory> factories) {
117
factories.add(new FlowchartItemProviderAdapterFactory());
118
factories.add(new ResourceItemProviderAdapterFactory());
119
factories.add(new ReflectiveItemProviderAdapterFactory());
125
public AdapterFactory getItemProvidersAdapterFactory() {
126
return adapterFactory;
132
public ImageDescriptor getItemImageDescriptor(Object item) {
133
IItemLabelProvider labelProvider = (IItemLabelProvider) adapterFactory
134
.adapt(item, IItemLabelProvider.class);
135
if (labelProvider != null) {
136
return ExtendedImageRegistry.getInstance().getImageDescriptor(
137
labelProvider.getImage(item));
143
* Returns an image descriptor for the image file at the given
144
* plug-in relative path.
147
* @param path the path
148
* @return the image descriptor
150
public static ImageDescriptor getBundledImageDescriptor(String path) {
151
return AbstractUIPlugin.imageDescriptorFromPlugin(ID, path);
155
* Respects images residing in any plug-in. If path is relative,
156
* then this bundle is looked up for the image, otherwise, for absolute
157
* path, first segment is taken as id of plug-in with image
160
* @param path the path to image, either absolute (with plug-in id as first segment), or relative for bundled images
161
* @return the image descriptor
163
public static ImageDescriptor findImageDescriptor(String path) {
164
final IPath p = new Path(path);
165
if (p.isAbsolute() && p.segmentCount() > 1) {
166
return AbstractUIPlugin.imageDescriptorFromPlugin(p.segment(0), p
167
.removeFirstSegments(1).makeAbsolute().toString());
169
return getBundledImageDescriptor(p.makeAbsolute().toString());
174
* Returns an image for the image file at the given plug-in relative path.
175
* Client do not need to dispose this image. Images will be disposed automatically.
178
* @param path the path
179
* @return image instance
181
public Image getBundledImage(String path) {
182
Image image = getImageRegistry().get(path);
184
getImageRegistry().put(path, getBundledImageDescriptor(path));
185
image = getImageRegistry().get(path);
191
* Returns string from plug-in's resource bundle
195
public static String getString(String key) {
196
return Platform.getResourceString(getInstance().getBundle(), "%" + key); //$NON-NLS-1$
202
public FlowchartDocumentProvider getDocumentProvider() {
203
if (documentProvider == null) {
204
documentProvider = new FlowchartDocumentProvider();
206
return documentProvider;
212
public FlowchartBaseItemSemanticEditPolicy.LinkConstraints getLinkConstraints() {
213
return linkConstraints;
219
public void setLinkConstraints(
220
FlowchartBaseItemSemanticEditPolicy.LinkConstraints lc) {
221
this.linkConstraints = lc;
227
public ElementInitializers getElementInitializers() {
234
public void setElementInitializers(ElementInitializers i) {
235
this.initializers = i;
241
public void logError(String error) {
242
logError(error, null);
248
public void logError(String error, Throwable throwable) {
249
if (error == null && throwable != null) {
250
error = throwable.getMessage();
253
new Status(IStatus.ERROR, FlowchartDiagramEditorPlugin.ID,
254
IStatus.OK, error, throwable));
255
debug(error, throwable);
261
public void logInfo(String message) {
262
logInfo(message, null);
268
public void logInfo(String message, Throwable throwable) {
269
if (message == null && throwable != null) {
270
message = throwable.getMessage();
273
new Status(IStatus.INFO, FlowchartDiagramEditorPlugin.ID,
274
IStatus.OK, message, throwable));
275
debug(message, throwable);
281
private void debug(String message, Throwable throwable) {
282
if (!isDebugging()) {
285
if (message != null) {
286
System.err.println(message);
288
if (throwable != null) {
289
throwable.printStackTrace();