KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > rulers > RulerColumnDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.texteditor.rulers;
12
13 import java.net.URL JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.ILog;
19 import org.eclipse.core.runtime.InvalidRegistryObjectException;
20 import org.eclipse.core.runtime.content.IContentType;
21
22
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IWorkbenchPartSite;
26 import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
27 import org.eclipse.ui.internal.texteditor.rulers.ExtensionPointHelper;
28 import org.eclipse.ui.internal.texteditor.rulers.RulerColumnMessages;
29 import org.eclipse.ui.internal.texteditor.rulers.RulerColumnPlacement;
30 import org.eclipse.ui.internal.texteditor.rulers.RulerColumnTarget;
31 import org.eclipse.ui.texteditor.IDocumentProvider;
32 import org.eclipse.ui.texteditor.IDocumentProviderExtension4;
33 import org.eclipse.ui.texteditor.ITextEditor;
34
35 /**
36  * The description of an extension to the
37  * <code>org.eclipse.ui.workbench.texteditor.rulerColumns</code> extension point. Instances are
38  * immutable. Instances can be obtained from a {@link RulerColumnRegistry}.
39  *
40  * @since 3.3
41  */

42 public final class RulerColumnDescriptor {
43     /** The extension schema name of the class attribute. */
44     private static final String JavaDoc CLASS= "class"; //$NON-NLS-1$
45
/** The extension schema name of the id attribute. */
46     private static final String JavaDoc ID= "id"; //$NON-NLS-1$
47
/** The extension schema name of the optional name attribute. */
48     private static final String JavaDoc NAME= "name"; //$NON-NLS-1$
49
/** The extension schema name of the optional enabled attribute. */
50     private static final String JavaDoc ENABLED= "enabled"; //$NON-NLS-1$
51
/** The extension schema name of the optional icon attribute. */
52     private static final String JavaDoc ICON= "icon"; //$NON-NLS-1$
53
/** The extension schema name of the optional global attribute. */
54     private static final String JavaDoc GLOBAL= "global"; //$NON-NLS-1$
55
/** The extension schema name of the optional menu inclusion attribute. */
56     private static final String JavaDoc INCLUDE_IN_MENU= "includeInMenu"; //$NON-NLS-1$
57
/** The extension schema name of the targetEditor element. */
58     private static final String JavaDoc TARGET_EDITOR= "targetEditor"; //$NON-NLS-1$
59
/** The extension schema name of the targetContentType element. */
60     private static final String JavaDoc TARGET_CONTENT_TYPE= "targetContentType"; //$NON-NLS-1$
61
/** The extension schema name of the targetClass element. */
62     private static final String JavaDoc TARGET_CLASS= "targetClass"; //$NON-NLS-1$
63
/** The extension schema name of the placement element. */
64     private static final String JavaDoc PLACEMENT= "placement"; //$NON-NLS-1$
65

66     /** The identifier of the extension. */
67     private final String JavaDoc fId;
68     /** The name of the extension, equal to the id if no name is given. */
69     private final String JavaDoc fName;
70     /** The icon descriptor. */
71     private final ImageDescriptor fIcon;
72     /** The configuration element of this extension. */
73     private final IConfigurationElement fElement;
74     /** The target specification of the ruler column contribution. */
75     private final RulerColumnTarget fTarget;
76     /** The placement specification of the ruler column contribution. */
77     private final RulerColumnPlacement fRulerColumnPlacement;
78     /** The default enablement setting of the ruler column contribution. */
79     private final boolean fDefaultEnablement;
80     /** The global setting of the ruler column contribution. */
81     private final boolean fIsGlobal;
82     /** The menu inclusion setting of the ruler column contribution. */
83     private final boolean fIncludeInMenu;
84
85     /**
86      * Creates a new descriptor.
87      *
88      * @param element the configuration element to read
89      * @param registry the computer registry creating this descriptor
90      * @throws InvalidRegistryObjectException if the configuration element does not conform to the
91      * extension point spec
92      */

93     RulerColumnDescriptor(IConfigurationElement element, RulerColumnRegistry registry) throws InvalidRegistryObjectException {
94         Assert.isLegal(registry != null);
95         Assert.isLegal(element != null);
96         fElement= element;
97
98         ILog log= TextEditorPlugin.getDefault().getLog();
99         ExtensionPointHelper helper= new ExtensionPointHelper(element, log);
100
101         fId= helper.getNonNullAttribute(ID);
102         fName= helper.getDefaultAttribute(NAME, fId);
103         helper.getNonNullAttribute(CLASS); // just check validity
104
URL JavaDoc iconURL= helper.getDefaultResourceURL(ICON, null);
105         fIcon= iconURL == null ? null : ImageDescriptor.createFromURL(iconURL);
106         fDefaultEnablement= helper.getDefaultAttribute(ENABLED, true);
107         fIsGlobal= helper.getDefaultAttribute(GLOBAL, true);
108         fIncludeInMenu= helper.getDefaultAttribute(INCLUDE_IN_MENU, true);
109
110         IConfigurationElement[] targetEditors= element.getChildren(TARGET_EDITOR);
111         IConfigurationElement[] targetContentTypes= element.getChildren(TARGET_CONTENT_TYPE);
112         IConfigurationElement[] targetClasses= element.getChildren(TARGET_CLASS);
113
114         if (targetContentTypes.length + targetEditors.length + targetClasses.length == 0) {
115             helper.fail(RulerColumnMessages.RulerColumnDescriptor_missing_target_msg);
116             fTarget= null; // dummy
117
} else {
118             RulerColumnTarget combined= null;
119             for (int i= 0; i < targetEditors.length; i++) {
120                 IConfigurationElement targetEditor= targetEditors[i];
121                 RulerColumnTarget target= RulerColumnTarget.createEditorIdTarget(new ExtensionPointHelper(targetEditor, log).getNonNullAttribute(ID));
122                 combined= RulerColumnTarget.createOrTarget(combined, target);
123             }
124             for (int i= 0; i < targetContentTypes.length; i++) {
125                 IConfigurationElement targetContentType= targetContentTypes[i];
126                 RulerColumnTarget target= RulerColumnTarget.createContentTypeTarget(new ExtensionPointHelper(targetContentType, log).getNonNullAttribute(ID));
127                 combined= RulerColumnTarget.createOrTarget(combined, target);
128             }
129             for (int i= 0; i < targetClasses.length; i++) {
130                 IConfigurationElement targetClass= targetClasses[i];
131                 RulerColumnTarget target= RulerColumnTarget.createClassTarget(new ExtensionPointHelper(targetClass, log).getNonNullAttribute(CLASS));
132                 combined= RulerColumnTarget.createOrTarget(combined, target);
133             }
134             fTarget= combined;
135         }
136
137         IConfigurationElement[] placements= element.getChildren(PLACEMENT);
138         switch (placements.length) {
139             case 0:
140                 fRulerColumnPlacement= new RulerColumnPlacement();
141                 break;
142             case 1:
143                 fRulerColumnPlacement= new RulerColumnPlacement(placements[0]);
144                 break;
145             default:
146                 helper.fail(RulerColumnMessages.RulerColumnDescriptor_invalid_placement_msg);
147                 fRulerColumnPlacement= null; // dummy
148
break;
149         }
150
151         Assert.isTrue(fTarget != null);
152         Assert.isTrue(fRulerColumnPlacement != null);
153     }
154
155     /**
156      * Returns the identifier of the described extension.
157      *
158      * @return the identifier of the described extension
159      */

160     public String JavaDoc getId() {
161         return fId;
162     }
163
164     /**
165      * Returns the name of the described extension.
166      *
167      * @return the name of the described extension
168      */

169     public String JavaDoc getName() {
170         return fName;
171     }
172
173     /**
174      * Returns the image descriptor of the described extension, <code>null</code> if it does not
175      * have an image.
176      *
177      * @return the image descriptor of the described extension or <code>null</code> for no image
178      */

179     public ImageDescriptor getIcon() {
180         return fIcon;
181     }
182
183     RulerColumnTarget getTarget() {
184         return fTarget;
185     }
186
187     RulerColumnPlacement getPlacement() {
188         return fRulerColumnPlacement;
189     }
190
191     /**
192      * Returns the default enablement of the described extension. Editors that support this
193      * contribution should typically enable the column by default.
194      *
195      * @return the default enablement of the described extension
196      */

197     public boolean getDefaultEnablement() {
198         return fDefaultEnablement;
199     }
200
201     /**
202      * Returns the global property of the described extension. Changing the visibility of a column
203      * with the global property set to <code>true</code> should typically affect all matching
204      * editors. Changing the visibility of a column with the global property set to
205      * <code>false</code> should only affect the current editor.
206      *
207      * @return the global property of the described extension
208      */

209     public boolean isGlobal() {
210         return fIsGlobal;
211     }
212
213     /**
214      * Returns the menu inclusion property of the described extension. A toggle menu entry should be
215      * inluded in the ruler context menu for columns with this property set to <code>true</code>.
216      *
217      * @return the menu inclusion property of the described extension
218      */

219     public boolean isIncludedInMenu() {
220         return fIncludeInMenu;
221     }
222
223     /**
224      * Returns <code>true</code> if this contribution matches the passed editor, <code>false</code> if not.
225      *
226      * @param editor the editor to check
227      * @return <code>true</code> if this contribution targets the passed editor
228      */

229     public boolean matchesEditor(ITextEditor editor) {
230         Assert.isLegal(editor != null);
231         RulerColumnTarget target= getTarget();
232
233         IWorkbenchPartSite site= editor.getSite();
234         if (site != null && target.matchesEditorId(site.getId()))
235             return true;
236         
237         if (target.matchesClass(editor.getClass()))
238             return true;
239
240         IContentType contentType= getContentType(editor);
241         return contentType != null && target.matchesContentType(contentType);
242
243     }
244
245     /**
246      * Creates a {@link IContributedRulerColumn} instance as described by the receiver. This may load the contributing plug-in.
247      *
248      * @param editor the editor that loads the contributed column
249      * @return the instantiated column
250      * @throws CoreException as thrown by {@link IConfigurationElement#createExecutableExtension(String)}
251      * @throws InvalidRegistryObjectException as thrown by {@link IConfigurationElement#createExecutableExtension(String)}
252      */

253     public IContributedRulerColumn createColumn(ITextEditor editor) throws CoreException, InvalidRegistryObjectException {
254         Assert.isLegal(editor != null);
255         IContributedRulerColumn column= (IContributedRulerColumn)fElement.createExecutableExtension(CLASS);
256         column.setDescriptor(this);
257         column.setEditor(editor);
258         column.columnCreated();
259         return column;
260     }
261
262     /*
263      * @see java.lang.Object#toString()
264      * @since 3.3
265      */

266     public String JavaDoc toString() {
267         return "RulerColumnDescriptor[name=" + getName() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
268
}
269
270     IConfigurationElement getConfigurationElement() {
271         return fElement;
272     }
273
274     /*
275      * @see java.lang.Object#hashCode()
276      */

277     public int hashCode() {
278         final int prime= 31;
279         int result= 1;
280         result= prime * result + ((fId == null) ? 0 : fId.hashCode());
281         return result;
282     }
283
284     /*
285      * @see java.lang.Object#equals(java.lang.Object)
286      */

287     public boolean equals(Object JavaDoc obj) {
288         if (this == obj)
289             return true;
290         if (obj == null)
291             return false;
292         if (getClass() != obj.getClass())
293             return false;
294         final RulerColumnDescriptor other= (RulerColumnDescriptor) obj;
295         if (fId == null) {
296             if (other.fId != null)
297                 return false;
298         } else if (!fId.equals(other.fId))
299             return false;
300         return true;
301     }
302
303     /**
304      * Returns the content type of the editor's input, <code>null</code> if the editor input or
305      * the document provider is <code>null</code> or the content type cannot be determined.
306      *
307      * @param editor the editor to get the content type from
308      * @return the content type of the editor's input, <code>null</code> if it cannot be
309      * determined
310      */

311     private IContentType getContentType(ITextEditor editor) {
312         IEditorInput input= editor.getEditorInput();
313         if (input == null)
314             return null;
315         IDocumentProvider provider= editor.getDocumentProvider();
316         if (provider instanceof IDocumentProviderExtension4) {
317             IDocumentProviderExtension4 ext= (IDocumentProviderExtension4) provider;
318             try {
319                 return ext.getContentType(input);
320             } catch (CoreException x) {
321                 // ignore and return null;
322
}
323         }
324         return null;
325     }
326
327     String JavaDoc getContributor() {
328         try {
329             return fElement.getContributor().getName();
330         } catch (InvalidRegistryObjectException e) {
331             return "unknown"; //$NON-NLS-1$
332
}
333     }
334 }
335
Popular Tags