KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > EditorDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.registry;
12
13 import java.io.File JavaDoc;
14 import java.io.Serializable JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.program.Program;
22 import org.eclipse.ui.IEditorActionBarContributor;
23 import org.eclipse.ui.IEditorDescriptor;
24 import org.eclipse.ui.IEditorMatchingStrategy;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IMemento;
27 import org.eclipse.ui.IPluginContribution;
28 import org.eclipse.ui.ISharedImages;
29 import org.eclipse.ui.internal.IWorkbenchConstants;
30 import org.eclipse.ui.internal.WorkbenchImages;
31 import org.eclipse.ui.internal.WorkbenchPlugin;
32 import org.eclipse.ui.internal.misc.ProgramImageDescriptor;
33 import org.eclipse.ui.internal.util.Util;
34 import org.eclipse.ui.plugin.AbstractUIPlugin;
35
36 /**
37  * @see IEditorDescriptor
38  */

39 public final class EditorDescriptor implements IEditorDescriptor, Serializable JavaDoc,
40         IPluginContribution {
41
42     /**
43      * Generated serial version UID for this class.
44      * @since 3.1
45      */

46     private static final long serialVersionUID = 3905241225668998961L;
47
48     // @issue the following constants need not be public; see bug 47600
49
/**
50      * Open internal constant. Value <code>0x01</code>.
51      */

52     public static final int OPEN_INTERNAL = 0x01;
53
54     /**
55      * Open in place constant. Value <code>0x02</code>.
56      */

57     public static final int OPEN_INPLACE = 0x02;
58
59     /**
60      * Open external constant. Value <code>0x04</code>.
61      */

62     public static final int OPEN_EXTERNAL = 0x04;
63
64     private String JavaDoc editorName;
65
66     private String JavaDoc imageFilename;
67
68     private transient ImageDescriptor imageDesc;
69
70     private boolean testImage = true;
71
72     private String JavaDoc className;
73
74     private String JavaDoc launcherName;
75
76     private String JavaDoc fileName;
77
78     private String JavaDoc id = Util.ZERO_LENGTH_STRING;
79
80     private boolean matchingStrategyChecked = false;
81     private IEditorMatchingStrategy matchingStrategy;
82
83     private Program program;
84
85     //The id of the plugin which contributed this editor, null for external editors
86
private String JavaDoc pluginIdentifier;
87
88     private int openMode = 0;
89
90     private transient IConfigurationElement configurationElement;
91
92     /**
93      * Create a new instance of an editor descriptor. Limited
94      * to internal framework calls.
95      * @param element
96      * @param id2
97      */

98     /* package */EditorDescriptor(String JavaDoc id2, IConfigurationElement element) {
99         setID(id2);
100         setConfigurationElement(element);
101     }
102
103     
104
105     /**
106      * Create a new instance of an editor descriptor. Limited
107      * to internal framework calls.
108      */

109     /* package */ EditorDescriptor() {
110         super();
111     }
112
113
114
115     /**
116      * Creates a descriptor for an external program.
117      *
118      * @param filename the external editor full path and filename
119      * @return the editor descriptor
120      */

121     public static EditorDescriptor createForProgram(String JavaDoc filename) {
122         if (filename == null) {
123             throw new IllegalArgumentException JavaDoc();
124         }
125         EditorDescriptor editor = new EditorDescriptor();
126
127         editor.setFileName(filename);
128         editor.setID(filename);
129         editor.setOpenMode(OPEN_EXTERNAL);
130
131         //Isolate the program name (no directory or extension)
132
int start = filename.lastIndexOf(File.separator);
133         String JavaDoc name;
134         if (start != -1) {
135             name = filename.substring(start + 1);
136         } else {
137             name = filename;
138         }
139         int end = name.lastIndexOf('.');
140         if (end != -1) {
141             name = name.substring(0, end);
142         }
143         editor.setName(name);
144
145         // get the program icon without storing it in the registry
146
ImageDescriptor imageDescriptor = new ProgramImageDescriptor(filename,
147                 0);
148         editor.setImageDescriptor(imageDescriptor);
149
150         return editor;
151     }
152
153     /**
154      * Return the program called programName. Return null if it is not found.
155      * @return org.eclipse.swt.program.Program
156      */

157     private static Program findProgram(String JavaDoc programName) {
158
159         Program[] programs = Program.getPrograms();
160         for (int i = 0; i < programs.length; i++) {
161             if (programs[i].getName().equals(programName)) {
162                 return programs[i];
163             }
164         }
165
166         return null;
167     }
168
169     /**
170      * Create the editor action bar contributor for editors of this type.
171      *
172      * @return the action bar contributor, or <code>null</code>
173      */

174     public IEditorActionBarContributor createActionBarContributor() {
175         // Handle case for predefined editor descriptors, like the
176
// one for IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID, which
177
// don't have a configuration element.
178
if (configurationElement == null) {
179             return null;
180         }
181
182         // Get the contributor class name.
183
String JavaDoc className = configurationElement
184                 .getAttribute(IWorkbenchRegistryConstants.ATT_CONTRIBUTOR_CLASS);
185         if (className == null) {
186             return null;
187         }
188
189         // Create the contributor object.
190
IEditorActionBarContributor contributor = null;
191         try {
192             contributor = (IEditorActionBarContributor) WorkbenchPlugin
193                     .createExtension(configurationElement,
194                             IWorkbenchRegistryConstants.ATT_CONTRIBUTOR_CLASS);
195         } catch (CoreException e) {
196             WorkbenchPlugin.log("Unable to create editor contributor: " + //$NON-NLS-1$
197
id, e.getStatus());
198         }
199         return contributor;
200     }
201
202     /**
203      * Return the editor class name.
204      *
205      * @return the class name
206      */

207     public String JavaDoc getClassName() {
208         if (configurationElement == null) {
209             return className;
210         }
211         return RegistryReader.getClassValue(configurationElement,
212                 IWorkbenchRegistryConstants.ATT_CLASS);
213     }
214
215     /**
216      * Return the configuration element used to define this editor, or <code>null</code>.
217      *
218      * @return the element or null
219      */

220     public IConfigurationElement getConfigurationElement() {
221         return configurationElement;
222     }
223     
224     /**
225      * Create an editor part based on this descriptor.
226      *
227      * @return the editor part
228      * @throws CoreException thrown if there is an issue creating the editor
229      */

230     public IEditorPart createEditor() throws CoreException {
231         Object JavaDoc extension = WorkbenchPlugin.createExtension(getConfigurationElement(), IWorkbenchRegistryConstants.ATT_CLASS);
232         return (IEditorPart)extension;
233     }
234
235     /**
236      * Return the file name of the command to execute for this editor.
237      *
238      * @return the file name to execute
239      */

240     public String JavaDoc getFileName() {
241         if (program == null) {
242             if (configurationElement == null) {
243                 return fileName;
244             }
245             return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_COMMAND);
246         }
247         return program.getName();
248     }
249
250     /**
251      * Return the id for this editor.
252      *
253      * @return the id
254      */

255     public String JavaDoc getId() {
256         if (program == null) {
257             if (configurationElement == null) {
258                 return Util.safeString(id);
259             }
260             return Util.safeString(configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID));
261             
262         }
263         return Util.safeString(program.getName());
264     }
265
266     /**
267      * Return the image descriptor describing this editor.
268      *
269      * @return the image descriptor
270      */

271     public ImageDescriptor getImageDescriptor() {
272         if (testImage) {
273             testImage = false;
274             if (imageDesc == null) {
275                 String JavaDoc imageFileName = getImageFilename();
276                 String JavaDoc command = getFileName();
277                 if (imageFileName != null && configurationElement != null) {
278                     imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(
279                             configurationElement.getNamespace(), imageFileName);
280                 } else if (command != null) {
281                     imageDesc = WorkbenchImages.getImageDescriptorFromProgram(
282                             command, 0);
283                 }
284             }
285             verifyImage();
286         }
287         
288         return imageDesc;
289     }
290
291     /**
292      * Verifies that the image descriptor generates an image. If not, the
293      * descriptor is replaced with the default image.
294      *
295      * @since 3.1
296      */

297     private void verifyImage() {
298         if (imageDesc == null) {
299             imageDesc = WorkbenchImages
300                 .getImageDescriptor(ISharedImages.IMG_OBJ_FILE);
301         }
302         else {
303             Image img = imageDesc.createImage(false);
304             if (img == null) {
305                 // @issue what should be the default image?
306
imageDesc = WorkbenchImages
307                         .getImageDescriptor(ISharedImages.IMG_OBJ_FILE);
308             } else {
309                 img.dispose();
310             }
311         }
312     }
313
314     /**
315      * The name of the image describing this editor.
316      *
317      * @return the image file name
318      */

319     public String JavaDoc getImageFilename() {
320         if (configurationElement == null) {
321             return imageFilename;
322         }
323         return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
324     }
325
326     /**
327      * Return the user printable label for this editor.
328      *
329      * @return the label
330      */

331     public String JavaDoc getLabel() {
332         if (program == null) {
333             if (configurationElement == null) {
334                 return editorName;
335             }
336             return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
337         }
338         return program.getName();
339     }
340
341     /**
342      * Returns the class name of the launcher.
343      *
344      * @return the launcher class name
345      */

346     public String JavaDoc getLauncher() {
347         if (configurationElement == null) {
348             return launcherName;
349         }
350         return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_LAUNCHER);
351     }
352
353     /**
354      * Return the contributing plugin id.
355      *
356      * @return the contributing plugin id
357      */

358     public String JavaDoc getPluginID() {
359         if (configurationElement != null) {
360             return configurationElement.getNamespace();
361         }
362         return pluginIdentifier;
363     }
364
365     /**
366      * Get the program for the receiver if there is one.
367      * @return Program
368      */

369     public Program getProgram() {
370         return this.program;
371     }
372
373     /* (non-Javadoc)
374      * @see org.eclipse.ui.IEditorDescriptor#isInternal
375      */

376     public boolean isInternal() {
377         return getOpenMode() == OPEN_INTERNAL;
378     }
379
380     /* (non-Javadoc)
381      * @see org.eclipse.ui.IEditorDescriptor#isOpenInPlace
382      */

383     public boolean isOpenInPlace() {
384         return getOpenMode() == OPEN_INPLACE;
385     }
386
387     /* (non-Javadoc)
388      * @see org.eclipse.ui.IEditorDescriptor#isOpenExternal
389      */

390     public boolean isOpenExternal() {
391         return getOpenMode() == OPEN_EXTERNAL;
392     }
393
394     /**
395      * Load the object properties from a memento.
396      *
397      * @return <code>true</code> if the values are valid, <code>false</code> otherwise
398      */

399     protected boolean loadValues(IMemento memento) {
400         editorName = memento.getString(IWorkbenchConstants.TAG_LABEL);
401         imageFilename = memento.getString(IWorkbenchConstants.TAG_IMAGE);
402         className = memento.getString(IWorkbenchConstants.TAG_CLASS);
403         launcherName = memento.getString(IWorkbenchConstants.TAG_LAUNCHER);
404         fileName = memento.getString(IWorkbenchConstants.TAG_FILE);
405         id = Util.safeString(memento.getString(IWorkbenchConstants.TAG_ID));
406         pluginIdentifier = memento.getString(IWorkbenchConstants.TAG_PLUGIN);
407
408         Integer JavaDoc openModeInt = memento
409                 .getInteger(IWorkbenchConstants.TAG_OPEN_MODE);
410         if (openModeInt != null) {
411             openMode = openModeInt.intValue();
412         } else {
413             // legacy: handle the older attribute names, needed to allow reading of pre-3.0-RCP workspaces
414
boolean internal = new Boolean JavaDoc(memento
415                     .getString(IWorkbenchConstants.TAG_INTERNAL))
416                     .booleanValue();
417             boolean openInPlace = new Boolean JavaDoc(memento
418                     .getString(IWorkbenchConstants.TAG_OPEN_IN_PLACE))
419                     .booleanValue();
420             if (internal) {
421                 openMode = OPEN_INTERNAL;
422             } else {
423                 if (openInPlace) {
424                     openMode = OPEN_INPLACE;
425                 } else {
426                     openMode = OPEN_EXTERNAL;
427                 }
428             }
429         }
430         if (openMode != OPEN_EXTERNAL && openMode != OPEN_INTERNAL
431                 && openMode != OPEN_INPLACE) {
432             WorkbenchPlugin
433                     .log("Ignoring editor descriptor with invalid openMode: " + this); //$NON-NLS-1$
434
return false;
435         }
436
437         String JavaDoc programName = memento
438                 .getString(IWorkbenchConstants.TAG_PROGRAM_NAME);
439         if (programName != null) {
440             this.program = findProgram(programName);
441         }
442         return true;
443     }
444
445     /**
446      * Save the object values in a IMemento
447      */

448     protected void saveValues(IMemento memento) {
449         memento.putString(IWorkbenchConstants.TAG_LABEL, getLabel());
450         memento.putString(IWorkbenchConstants.TAG_IMAGE, getImageFilename());
451         memento.putString(IWorkbenchConstants.TAG_CLASS, getClassName());
452         memento.putString(IWorkbenchConstants.TAG_LAUNCHER, getLauncher());
453         memento.putString(IWorkbenchConstants.TAG_FILE, getFileName());
454         memento.putString(IWorkbenchConstants.TAG_ID, getId());
455         memento.putString(IWorkbenchConstants.TAG_PLUGIN, getPluginId());
456
457         memento.putInteger(IWorkbenchConstants.TAG_OPEN_MODE, getOpenMode());
458         // legacy: handle the older attribute names, needed to allow reading of workspace by pre-3.0-RCP eclipses
459
memento.putString(IWorkbenchConstants.TAG_INTERNAL, String
460                 .valueOf(isInternal()));
461         memento.putString(IWorkbenchConstants.TAG_OPEN_IN_PLACE, String
462                 .valueOf(isOpenInPlace()));
463
464         if (this.program != null) {
465             memento.putString(IWorkbenchConstants.TAG_PROGRAM_NAME,
466                     this.program.getName());
467         }
468     }
469
470     /**
471      * Return the open mode of this editor.
472      *
473      * @return the open mode of this editor
474      * @since 3.1
475      */

476     private int getOpenMode() {
477         if (configurationElement == null) { // if we've been serialized, return our serialized value
478
return openMode;
479         }
480         else if (getLauncher() != null) {
481             // open using a launcer
482
return EditorDescriptor.OPEN_EXTERNAL;
483         } else if (getFileName() != null) {
484             // open using an external editor
485
return EditorDescriptor.OPEN_EXTERNAL;
486         } else if (getPluginId() != null) {
487             // open using an internal editor
488
return EditorDescriptor.OPEN_INTERNAL;
489         }
490         else {
491             return 0; // default for system editor
492
}
493     }
494
495     /**
496      * Set the class name of an internal editor.
497      */

498     /* package */void setClassName(String JavaDoc newClassName) {
499         className = newClassName;
500     }
501
502     /**
503      * Set the configuration element which contributed this editor.
504      */

505     /* package */void setConfigurationElement(
506             IConfigurationElement newConfigurationElement) {
507         configurationElement = newConfigurationElement;
508     }
509
510     /**
511      * Set the filename of an external editor.
512      */

513     /* package */void setFileName(String JavaDoc aFileName) {
514         fileName = aFileName;
515     }
516
517     /**
518      * Set the id of the editor.
519      * For internal editors this is the id as provided in the extension point
520      * For external editors it is path and filename of the editor
521      */

522     /* package */void setID(String JavaDoc anID) {
523         Assert.isNotNull(anID);
524         id = anID;
525     }
526
527     /**
528      * The Image to use to repesent this editor
529      */

530     /* package */void setImageDescriptor(ImageDescriptor desc) {
531         imageDesc = desc;
532         testImage = true;
533     }
534
535     /**
536      * The name of the image to use for this editor.
537      */

538     /* package */void setImageFilename(String JavaDoc aFileName) {
539         imageFilename = aFileName;
540     }
541
542     /**
543      * Sets the new launcher class name
544      *
545      * @param newLauncher the new launcher
546      */

547     /* package */void setLauncher(String JavaDoc newLauncher) {
548         launcherName = newLauncher;
549     }
550
551     /**
552      * The label to show for this editor.
553      */

554     /* package */void setName(String JavaDoc newName) {
555         editorName = newName;
556     }
557
558     /**
559      * Sets the open mode of this editor descriptor.
560      *
561      * @param mode the open mode
562      *
563      * @issue this method is public as a temporary fix for bug 47600
564      */

565     public void setOpenMode(int mode) {
566         openMode = mode;
567     }
568
569     /**
570      * The id of the plugin which contributed this editor, null for external editors.
571      */

572     /* package */void setPluginIdentifier(String JavaDoc anID) {
573         pluginIdentifier = anID;
574     }
575
576     /**
577      * Set the receivers program.
578      * @param newProgram
579      */

580     /* package */void setProgram(Program newProgram) {
581
582         this.program = newProgram;
583         if (editorName == null) {
584             setName(newProgram.getName());
585         }
586     }
587
588     /**
589      * For debugging purposes only.
590      */

591     public String JavaDoc toString() {
592         return "EditorDescriptor(id=" + getId() + ", label=" + getLabel() + ")"; //$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-1$
593
}
594
595     /* (non-Javadoc)
596      * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
597      */

598     public String JavaDoc getLocalId() {
599         return getId();
600     }
601
602     /* (non-Javadoc)
603      * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
604      */

605     public String JavaDoc getPluginId() {
606         return getPluginID();
607     }
608
609     /* (non-Javadoc)
610      * @see org.eclipse.ui.IEditorDescriptor#getEditorManagementPolicy()
611      */

612     public IEditorMatchingStrategy getEditorMatchingStrategy() {
613         if (matchingStrategy == null && !matchingStrategyChecked) {
614             matchingStrategyChecked = true;
615             if (program == null && configurationElement != null) {
616                 if (configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_MATCHING_STRATEGY) != null) {
617                     try {
618                         matchingStrategy = (IEditorMatchingStrategy) WorkbenchPlugin.createExtension(configurationElement, IWorkbenchRegistryConstants.ATT_MATCHING_STRATEGY);
619                     } catch (CoreException e) {
620                         WorkbenchPlugin.log("Error creating editor management policy for editor id " + getId(), e); //$NON-NLS-1$
621
}
622                 }
623             }
624         }
625         return matchingStrategy;
626     }
627     
628     
629 }
630
Popular Tags