KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Dan Rubel <dan_rubel@instantiations.com>
11  * - Fix for bug 11490 - define hidden view (placeholder for view) in plugin.xml
12  * Chris Gross <schtoo@schtoo.com>
13  * - Fix for 99155 - allow standalone view placeholders
14  *******************************************************************************/

15
16 package org.eclipse.ui.internal.registry;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
24 import org.eclipse.ui.IPageLayout;
25 import org.eclipse.ui.IViewLayout;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.internal.DirtyPerspectiveMarker;
28 import org.eclipse.ui.internal.PageLayout;
29 import org.eclipse.ui.internal.WorkbenchPlugin;
30
31 /**
32  * A strategy to read perspective extension from the registry.
33  * A pespective extension is one of a view, viewAction, perspAction,
34  * newWizardAction, or actionSet.
35  */

36 public class PerspectiveExtensionReader extends RegistryReader {
37     private String JavaDoc targetID;
38
39     private PageLayout pageLayout;
40
41     private Set JavaDoc includeOnlyTags = null;
42
43     private static final String JavaDoc VAL_LEFT = "left";//$NON-NLS-1$
44

45     private static final String JavaDoc VAL_RIGHT = "right";//$NON-NLS-1$
46

47     private static final String JavaDoc VAL_TOP = "top";//$NON-NLS-1$
48

49     private static final String JavaDoc VAL_BOTTOM = "bottom";//$NON-NLS-1$
50

51     private static final String JavaDoc VAL_STACK = "stack";//$NON-NLS-1$
52

53     private static final String JavaDoc VAL_FAST = "fast";//$NON-NLS-1$
54

55     private static final String JavaDoc VAL_TRUE = "true";//$NON-NLS-1$
56

57     // VAL_FALSE added by dan_rubel@instantiations.com
58
// TODO: this logic is backwards... we should be checking for true, but
59
// technically this is API now...
60
private static final String JavaDoc VAL_FALSE = "false";//$NON-NLS-1$
61

62     private IExtensionTracker tracker;
63
64     /**
65      * PerspectiveExtensionReader constructor..
66      */

67     public PerspectiveExtensionReader() {
68         // do nothing
69
}
70
71     /**
72      * Read the view extensions within a registry.
73      *
74      * @param extensionTracker the tracker
75      * @param id the id
76      * @param out the layout
77      */

78     public void extendLayout(IExtensionTracker extensionTracker, String JavaDoc id, PageLayout out) {
79         tracker = extensionTracker;
80         targetID = id;
81         pageLayout = out;
82         readRegistry(Platform.getExtensionRegistry(), PlatformUI.PLUGIN_ID,
83                 IWorkbenchRegistryConstants.PL_PERSPECTIVE_EXTENSIONS);
84     }
85
86     /**
87      * Returns whether the given tag should be included.
88      */

89     private boolean includeTag(String JavaDoc tag) {
90         return includeOnlyTags == null || includeOnlyTags.contains(tag);
91     }
92
93     /**
94      * Process an action set.
95      */

96     private boolean processActionSet(IConfigurationElement element) {
97         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
98         if (id != null) {
99             pageLayout.addActionSet(id);
100         }
101         return true;
102     }
103
104     /**
105      * Process an extension.
106      * Assumption: Extension is for current perspective.
107      */

108     private boolean processExtension(IConfigurationElement element) {
109         IConfigurationElement[] children = element.getChildren();
110         for (int nX = 0; nX < children.length; nX++) {
111             IConfigurationElement child = children[nX];
112             String JavaDoc type = child.getName();
113             if (includeTag(type)) {
114                 boolean result = false;
115                 if (type.equals(IWorkbenchRegistryConstants.TAG_ACTION_SET)) {
116                     result = processActionSet(child);
117                 } else if (type.equals(IWorkbenchRegistryConstants.TAG_VIEW)) {
118                     result = processView(child);
119                 } else if (type.equals(IWorkbenchRegistryConstants.TAG_VIEW_SHORTCUT)) {
120                     result = processViewShortcut(child);
121                 } else if (type.equals(IWorkbenchRegistryConstants.TAG_NEW_WIZARD_SHORTCUT)) {
122                     result = processWizardShortcut(child);
123                 } else if (type.equals(IWorkbenchRegistryConstants.TAG_PERSP_SHORTCUT)) {
124                     result = processPerspectiveShortcut(child);
125                 } else if (type.equals(IWorkbenchRegistryConstants.TAG_SHOW_IN_PART)) {
126                     result = processShowInPart(child);
127                 }
128                 if (!result) {
129                     WorkbenchPlugin.log("Unable to process element: " + //$NON-NLS-1$
130
type
131                             + " in perspective extension: " + //$NON-NLS-1$
132
element.getDeclaringExtension()
133                                     .getUniqueIdentifier());
134                 }
135             }
136         }
137         return true;
138     }
139
140     /**
141      * Process a perspective shortcut
142      */

143     private boolean processPerspectiveShortcut(IConfigurationElement element) {
144         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
145         if (id != null) {
146             pageLayout.addPerspectiveShortcut(id);
147         }
148         return true;
149     }
150
151     /**
152      * Process a show in element.
153      */

154     private boolean processShowInPart(IConfigurationElement element) {
155         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
156         if (id != null) {
157             pageLayout.addShowInPart(id);
158         }
159         return true;
160     }
161
162     // processView(IConfigurationElement) modified by dan_rubel@instantiations.com
163
/**
164      * Process a view
165      */

166     private boolean processView(IConfigurationElement element) {
167         // Get id, relative, and relationship.
168
String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
169         String JavaDoc relative = element.getAttribute(IWorkbenchRegistryConstants.ATT_RELATIVE);
170         String JavaDoc relationship = element.getAttribute(IWorkbenchRegistryConstants.ATT_RELATIONSHIP);
171         String JavaDoc ratioString = element.getAttribute(IWorkbenchRegistryConstants.ATT_RATIO);
172         boolean visible = !VAL_FALSE.equals(element.getAttribute(IWorkbenchRegistryConstants.ATT_VISIBLE));
173         String JavaDoc closeable = element.getAttribute(IWorkbenchRegistryConstants.ATT_CLOSEABLE);
174         String JavaDoc moveable = element.getAttribute(IWorkbenchRegistryConstants.ATT_MOVEABLE);
175         String JavaDoc standalone = element.getAttribute(IWorkbenchRegistryConstants.ATT_STANDALONE);
176         String JavaDoc showTitle = element.getAttribute(IWorkbenchRegistryConstants.ATT_SHOW_TITLE);
177         
178         // Default to 'false'
179
String JavaDoc minVal = element.getAttribute(IWorkbenchRegistryConstants.ATT_MINIMIZED);
180         boolean minimized = minVal != null && VAL_TRUE.equals(minVal);
181
182         float ratio;
183
184         if (id == null) {
185             logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_ID);
186             return false;
187         }
188         if (relationship == null) {
189             logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_RELATIONSHIP);
190             return false;
191         }
192         if (!VAL_FAST.equals(relationship) && relative == null) {
193             logError(
194                     element,
195                     "Attribute '" + IWorkbenchRegistryConstants.ATT_RELATIVE + "' not defined. This attribute is required when " + IWorkbenchRegistryConstants.ATT_RELATIONSHIP + "=\"" + relationship + "\"."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
196
return false;
197         }
198
199         // Get the ratio.
200
if (ratioString == null) {
201             // The ratio has not been specified.
202
ratio = IPageLayout.NULL_RATIO;
203         } else {
204             try {
205                 ratio = new Float JavaDoc(ratioString).floatValue();
206             } catch (NumberFormatException JavaDoc e) {
207                 return false;
208             }
209             // If the ratio is outside the allowable range, mark it as invalid.
210
if (ratio < IPageLayout.RATIO_MIN || ratio > IPageLayout.RATIO_MAX) {
211                 ratio = IPageLayout.INVALID_RATIO;
212             }
213         }
214
215         // Get relationship details.
216
boolean stack = false;
217         boolean fast = false;
218         int intRelation = 0;
219         if (relationship.equals(VAL_LEFT)) {
220             intRelation = IPageLayout.LEFT;
221         } else if (relationship.equals(VAL_RIGHT)) {
222             intRelation = IPageLayout.RIGHT;
223         } else if (relationship.equals(VAL_TOP)) {
224             intRelation = IPageLayout.TOP;
225         } else if (relationship.equals(VAL_BOTTOM)) {
226             intRelation = IPageLayout.BOTTOM;
227         } else if (relationship.equals(VAL_STACK)) {
228             stack = true;
229         } else if (relationship.equals(VAL_FAST)) {
230             fast = true;
231         } else {
232             return false;
233         }
234
235         if (visible) {
236             // If adding a view (not just a placeholder), remove any existing placeholder.
237
// See bug 85948 [Perspectives] Adding register & expressions view by default to debug perspective fails
238
pageLayout.removePlaceholder(id);
239         }
240         
241         // If stack ..
242
if (stack) {
243             if (visible) {
244                 pageLayout.stackView(id, relative);
245             } else {
246                 pageLayout.stackPlaceholder(id, relative);
247             }
248         }
249
250         // If the view is a fast view...
251
else if (fast) {
252             if (ratio == IPageLayout.NULL_RATIO) {
253                 // The ratio has not been specified.
254
pageLayout.addFastView(id);
255             } else {
256                 pageLayout.addFastView(id, ratio);
257             }
258         } else {
259
260             // The view is a regular view.
261
// If the ratio is not specified or is invalid, use the default ratio.
262
if (ratio == IPageLayout.NULL_RATIO
263                     || ratio == IPageLayout.INVALID_RATIO) {
264                 ratio = IPageLayout.DEFAULT_VIEW_RATIO;
265             }
266
267             if (visible) {
268                 if (VAL_TRUE.equals(standalone)) {
269                     pageLayout.addStandaloneView(id, !VAL_FALSE
270                             .equals(showTitle), intRelation, ratio, relative);
271                 } else {
272                     pageLayout.addView(id, intRelation, ratio, relative, minimized);
273                 }
274             } else {
275                 // Fix for 99155, CGross (schtoo@schtoo.com)
276
// Adding standalone placeholder for standalone views
277
if (VAL_TRUE.equals(standalone)) {
278                     pageLayout.addStandaloneViewPlaceholder(id, intRelation,
279                             ratio, relative, !VAL_FALSE.equals(showTitle));
280                 } else {
281                     pageLayout.addPlaceholder(id, intRelation, ratio, relative);
282                 }
283             }
284         }
285         IViewLayout viewLayout = pageLayout.getViewLayout(id);
286         // may be null if it's been filtered by activity
287
if (viewLayout != null) {
288             if (closeable != null) {
289                 viewLayout.setCloseable(!VAL_FALSE.equals(closeable));
290             }
291             if (moveable != null) {
292                 viewLayout.setMoveable(!VAL_FALSE.equals(moveable));
293             }
294         }
295
296         return true;
297     }
298
299     /**
300      * Process a view shortcut
301      */

302     private boolean processViewShortcut(IConfigurationElement element) {
303         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
304         if (id != null) {
305             pageLayout.addShowViewShortcut(id);
306         }
307         return true;
308     }
309
310     /**
311      * Process a wizard shortcut
312      */

313     private boolean processWizardShortcut(IConfigurationElement element) {
314         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
315         if (id != null) {
316             pageLayout.addNewWizardShortcut(id);
317         }
318         return true;
319     }
320
321     protected boolean readElement(IConfigurationElement element) {
322         String JavaDoc type = element.getName();
323         if (type.equals(IWorkbenchRegistryConstants.TAG_PERSPECTIVE_EXTENSION)) {
324             String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_TARGET_ID);
325             if (targetID.equals(id) || "*".equals(id)) { //$NON-NLS-1$
326
if (tracker != null) {
327                     tracker.registerObject(element.getDeclaringExtension(), new DirtyPerspectiveMarker(id), IExtensionTracker.REF_STRONG);
328                 }
329                 return processExtension(element);
330             }
331             return true;
332         }
333         return false;
334     }
335
336     /**
337      * Sets the tags to include. All others are ignored.
338      *
339      * @param tags the tags to include
340      */

341     public void setIncludeOnlyTags(String JavaDoc[] tags) {
342         includeOnlyTags = new HashSet JavaDoc();
343         for (int i = 0; i < tags.length; i++) {
344             includeOnlyTags.add(tags[i]);
345         }
346     }
347 }
348
Popular Tags