KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > AbstractIntroElement


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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
12 package org.eclipse.ui.internal.intro.impl.model;
13
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.ui.internal.intro.impl.model.util.BundleUtil;
16 import org.eclipse.ui.internal.intro.impl.util.StringUtil;
17 import org.osgi.framework.Bundle;
18 import org.w3c.dom.Element JavaDoc;
19
20 /**
21  * An intro config component. All config components can get to their defining
22  * config element or bundle depending from where the element was loaded.
23  * <p>
24  * Class Rules:
25  * <ul>
26  * <li>If an element does not appear as a child under any node, then that
27  * element does not need a type to be defined.</li>
28  * <li>Each subclass must ensure that it properly supports cloning. This means
29  * that if a deep copy is needed, the subclass must override the base behavior
30  * here.</li>
31  * <li>if cloning is not needed, override clone method and throw an unsupported
32  * cloning exception. For now, only pages and targets of includes are cloneable.
33  * </li>
34  * </ul>
35  * <p>
36  * Note: This is an abstract base class for all classes in the Intro Model. <br>
37  * Clients are not expected to implement or subclass this class, or any of its
38  * subclasses.
39  */

40 public abstract class AbstractIntroElement implements Cloneable JavaDoc {
41
42     /**
43      * Type constant which identifies an IntroModelRoot element.
44      */

45     public static final int MODEL_ROOT = 1;
46
47     /**
48      * Type constant which identifies an IntroPartPresentation element.
49      */

50     public static final int PRESENTATION = 1 << 1;
51
52     /**
53      * Type constant which identifies an IntroHomePage element.
54      */

55     public static final int HOME_PAGE = 1 << 2;
56
57     /**
58      * Type constant which identifies the IntroPage element.
59      */

60     public static final int PAGE = 1 << 3;
61
62     /**
63      * Type constant which identifies the AbstractIntroPage element.
64      */

65     public static final int ABSTRACT_PAGE = HOME_PAGE | PAGE;
66
67     /**
68      * Type constant which identifies an IntroDiv element.
69      */

70     public static final int GROUP = 1 << 4;
71
72     /**
73      * Type constant which identifies the AbstractIntroContainer element.
74      */

75     public static final int ABSTRACT_CONTAINER = ABSTRACT_PAGE | GROUP
76             | MODEL_ROOT;
77
78     /**
79      * Type constant which identifies the IntroHtml element.
80      */

81     public static final int HTML = 1 << 5;
82
83     /**
84      * Type constant which identifies the IntroLink element.
85      */

86     public static final int LINK = 1 << 6;
87
88     /**
89      * Type constant which identifies the IntroImage element.
90      */

91     public static final int IMAGE = 1 << 7;
92
93     /**
94      * Type constant which identifies the IntroInclude element.
95      */

96     public static final int INCLUDE = 1 << 8;
97
98     /**
99      * Type constant which identifies the IntroText element.
100      */

101     public static final int TEXT = 1 << 9;
102
103     /**
104      * Type constant which identifies the IntroContainerExtension element.
105      */

106     public static final int CONTAINER_EXTENSION = 1 << 10;
107
108     /**
109      * Type constant which identifies the IntroHead element.
110      */

111     public static final int HEAD = 1 << 11;
112
113     /**
114      * Type constant which identifies the IntroHead element.
115      */

116     public static final int PAGE_TITLE = 1 << 12;
117
118     /**
119      * Type constant which identifies the IntroAnchor element.
120      */

121     public static final int ANCHOR = 1 << 13;
122
123     /**
124      * Type constant which identifies the IntroContentProvider element.
125      */

126     public static final int CONTENT_PROVIDER = 1 << 14;
127
128     /**
129      * Type constant which identifies the LaunchBarElement.
130      */

131     public static final int LAUNCH_BAR = 1 << 15;
132
133     /**
134      * Type constant which identifies the launch bar shortcut.
135      */

136     public static final int LAUNCH_BAR_SHORTCUT = 1 << 16;
137
138     /**
139      * Type constant which identifies am injected IFrame model element.
140      */

141     public static final int INJECTED_IFRAME = 1 << 17;
142     
143     /**
144      * Type constant for the theme element.
145      */

146     public static final int THEME = 1 << 18;
147     
148     /**
149      * Type constant for the hr element.
150      */

151     public static final int HR = 1 << 19;
152
153
154     /**
155      * Type constant which identifies the AbstractText element.
156      */

157     public static final int ABSTRACT_TEXT = HTML | LINK | CONTENT_PROVIDER;
158
159     /**
160      * Type constant which identifies the AbstractCommonIntroElement element.
161      */

162     public static final int BASE_ELEMENT = ABSTRACT_CONTAINER | ABSTRACT_TEXT
163             | IMAGE | TEXT | PAGE_TITLE;
164
165     /**
166      * Type constant which identifies any element in the Intro Model which can
167      * have an id. Note: eventhough IntroStandbyContentPart has an id, it does
168      * not appear as a child in the model, and so it does not have a type.
169      */

170     public static final int ID_ELEMENT = BASE_ELEMENT | ANCHOR;
171
172     /**
173      * Type constant which identifies any element in the Intro Model.
174      */

175     public static final int ELEMENT = ID_ELEMENT | CONTAINER_EXTENSION | HEAD
176             | INCLUDE | PRESENTATION | LAUNCH_BAR | LAUNCH_BAR_SHORTCUT;
177
178
179
180     private AbstractIntroElement parent;
181     private Object JavaDoc cfgElement;
182     private Bundle bundle;
183     private String JavaDoc mixinStyle;
184
185
186     /**
187      * Constructor used when model elements are being loaded from plugin.xml.
188      */

189     AbstractIntroElement(IConfigurationElement element) {
190         cfgElement = element;
191         bundle = BundleUtil.getBundleFromConfigurationElement(element);
192     }
193
194
195     /**
196      * Constructor used when model elements are being loaded from an xml content
197      * file. Bundle is propagated down the model to enable resolving resources
198      * relative to the base of the bundle.
199      *
200      * @param element
201      * @param pd
202      */

203     AbstractIntroElement(Element element, Bundle bundle) {
204         this.cfgElement = element;
205         this.bundle = bundle;
206     }
207
208
209     /**
210      * Constructor used when model elements are being loaded from an xml content
211      * file. Bundle AND base is propagated down the model to enable resolving
212      * resources relative to the xml content file. The base is set to point to
213      * the relative location of the parent folder that holds the content file.
214      * In the case of a configExtension, it is set to point to the relative
215      * position of the parent folder that holds the extension. Only when needed,
216      * the base field is stored in a model element. This saves memory.
217      *
218      * @param element
219      * @param pd
220      */

221     AbstractIntroElement(Element element, Bundle bundle, String JavaDoc base) {
222         this(element, bundle);
223     }
224
225
226
227
228     /**
229      * Returns the configuration element from which this intro element was
230      * loaded. In the case of extension, returns the configuration element of
231      * the defining extension.
232      *
233      * @return
234      */

235     public IConfigurationElement getCfgElement() {
236         return cfgElement instanceof IConfigurationElement?(IConfigurationElement)cfgElement:null;
237     }
238     
239     public Element getElement() {
240         return cfgElement instanceof Element?(Element)cfgElement:null;
241     }
242
243     /**
244      * DOM getAttribute retruns an empty string (not null) if attribute is not
245      * defined. Override this behavior to be consistent with Intro Model, and
246      * IConfiguration element.
247      *
248      * @param element
249      * @param att
250      * @return
251      */

252     protected String JavaDoc getAttribute(Element element, String JavaDoc att) {
253         if (element.hasAttribute(att)) {
254             String JavaDoc value = element.getAttribute(att);
255             if (value!=null) {
256                 IntroModelRoot root = getModelRoot();
257                 if (root!=null)
258                     return root.resolveVariables(value);
259                 return value;
260             }
261         }
262         return null;
263     }
264
265     /**
266      * Util method to parse a comma separated list of values
267      *
268      * @param element
269      * @param att
270      * @return
271      */

272     protected String JavaDoc[] getAttributeList(Element element, String JavaDoc att) {
273         if (element.hasAttribute(att)) {
274             String JavaDoc value = element.getAttribute(att);
275             if (value!=null) {
276                 IntroModelRoot root = getModelRoot();
277                 if (root!=null)
278                     value = root.resolveVariables(value);
279                 return StringUtil.split(value, ","); //$NON-NLS-1$
280
}
281         }
282         /*
283         if (element.hasAttribute(att))
284             return element.getAttribute(att).split(","); //$NON-NLS-1$
285             */

286         return null;
287     }
288     
289     protected void loadFromParent() {
290     }
291
292
293     /**
294      * Returns the plugin descriptor of the plugin from which this intro element
295      * was loaded. In the case of extension, returns the plugin descriptor of
296      * the plugin defining the extension.
297      *
298      * @return
299      */

300     public Bundle getBundle() {
301         return bundle;
302     }
303
304
305
306     /**
307      * Returns the specific model type of this intro element. To be implemented
308      * by all subclasses.
309      *
310      * @return returns one of the model class types defined in this class.
311      */

312     public abstract int getType();
313
314
315     /**
316      * Returns the parent of this intro element.
317      * <p>
318      * Rules:
319      * <ul>
320      * <li>For the model root, it retruns null.</li>
321      * <li>For the introPart presentation it returns a model root.</li>
322      * <li>For Pages, it returns an intro model root.</li>
323      * <li>For all other elements, it retruns a subclass of abstract container.
324      * </li>
325      * <li>for divs that are children of configs (shared divs), it returns the
326      * holding model root.</li>
327      * <li>for Head elements that are children of Implementation elements
328      * (shared Heads), it returns the holding presentation element.</li>
329      * </ul>
330      *
331      * @return returns the parent of this intro element. Null only for model
332      * root.
333      */

334     public AbstractIntroElement getParent() {
335         return parent;
336     }
337
338     /**
339      * @param parent
340      * The parent to set.
341      */

342     public void setParent(AbstractIntroElement parent) {
343         this.parent = parent;
344         if (parent!=null)
345             loadFromParent();
346     }
347
348     public void setBundle(Bundle bundle) {
349         this.bundle = bundle;
350     }
351
352     /**
353      * Returns the parent page holding this intro element. For the model root
354      * and the introPart presentation it returns null. For Pages, it returns the
355      * page itself. For all other element, returns the holding page.
356      * <p>
357      * Exceptions:
358      * <ul>
359      * <li>for divs that are children of configs (shared divs), it returns
360      * null.</li>
361      * <li>for Head elements that are children of Implementation elements
362      * (shared Heads), it returns null.</li>
363      * </ul>
364      */

365     public AbstractIntroPage getParentPage() {
366         // return yourself if you are a page.
367
if (isOfType(AbstractIntroElement.ABSTRACT_PAGE))
368             return (AbstractIntroPage) this;
369
370         AbstractIntroElement parent = getParent();
371         if (parent == null)
372             return null;
373
374         while (parent != null && parent.getParent() != null
375                 && !parent.isOfType(AbstractIntroElement.ABSTRACT_PAGE))
376             parent = parent.getParent();
377         if (parent.isOfType(ABSTRACT_PAGE))
378             return (AbstractIntroPage) parent;
379         return null;
380     }
381     
382     public IntroModelRoot getModelRoot() {
383         // return yourself if you are a model root.
384
if (isOfType(AbstractIntroElement.MODEL_ROOT))
385             return (IntroModelRoot) this;
386
387         AbstractIntroElement parent = getParent();
388         if (parent == null)
389             return null;
390
391         while (parent != null && parent.getParent() != null
392                 && !parent.isOfType(AbstractIntroElement.MODEL_ROOT))
393             parent = parent.getParent();
394         if (parent.isOfType(MODEL_ROOT))
395             return (IntroModelRoot) parent;
396         return null;
397     }
398
399
400     /**
401      * Returns whether the element is among the specified element types. An
402      * example of an element mask is as follows:
403      * <p>
404      * <code>
405      * int elementMask = IntroElement.ABSTRACT_CONTAINER;
406      * int elementMask = IntroElement.DIV | IntroElement.DEFAULT_LINK;
407      * </code>
408      *
409      * @param elementMask
410      * element mask formed by bitwise OR of element type constants
411      * defined in this class.
412      * @return <code>true</code> if this element has a matching type, and
413      * <code>false</code> otherwise.
414      */

415     public boolean isOfType(int elementMask) {
416         return (getType() & elementMask) != 0;
417     }
418
419     /**
420      * Returns whether the types of all the elements in the given array are
421      * among the specified element types. <br>
422      * An example of an element mask is as follows:
423      * <p>
424      * <code>
425      * int elementMask = IntroElement.DIV | IntroElement.DEFAULT_LINK;
426      * </code>
427      *
428      * @return <code>true</code> if all elements are of the right type, and
429      * <code>false</code> if the list is empty, or at least one
430      * element is not of the specified types.
431      */

432     public static final boolean allElementsAreOfType(
433             AbstractIntroElement[] elements, int elementMask) {
434         // if we have an empty list, no point going on.
435
if (elements.length == 0)
436             return false;
437
438         for (int i = 0; i < elements.length; i++) {
439             AbstractIntroElement element = elements[i];
440             if (!element.isOfType(elementMask))
441                 return false;
442         }
443         return true;
444     }
445
446     /**
447      * Shallow copy. The design of cloning this model assumes that when a
448      * container is cloned, all its children must be cloned and reparented to
449      * it, hence one clone of this container object. This is why we have a
450      * shallow copy here.
451      */

452     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
453         return super.clone();
454     }
455
456
457     
458     public String JavaDoc getMixinStyle() {
459         return mixinStyle;
460     }
461
462
463     
464     public void setMixinStyle(String JavaDoc mixinStyle) {
465         this.mixinStyle = mixinStyle;
466     }
467
468
469
470 }
Popular Tags