KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > swt > PageStyleManager


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 package org.eclipse.ui.internal.intro.impl.swt;
12
13 import java.util.Enumeration JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import org.eclipse.core.runtime.FileLocator;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.ui.forms.widgets.FormToolkit;
24 import org.eclipse.ui.internal.intro.impl.model.AbstractBaseIntroElement;
25 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroContainer;
26 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement;
27 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroIdElement;
28 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage;
29 import org.eclipse.ui.internal.intro.impl.model.IntroGroup;
30 import org.eclipse.ui.internal.intro.impl.model.IntroImage;
31 import org.eclipse.ui.internal.intro.impl.model.IntroLink;
32 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
33 import org.eclipse.ui.internal.intro.impl.model.IntroText;
34 import org.eclipse.ui.internal.intro.impl.model.loader.ModelLoaderUtil;
35 import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
36 import org.eclipse.ui.internal.intro.impl.util.Log;
37 import org.osgi.framework.Bundle;
38
39 public class PageStyleManager extends SharedStyleManager {
40
41     private AbstractIntroPage page;
42     private Hashtable JavaDoc altStyleContexts = new Hashtable JavaDoc();
43     private IntroModelRoot root;
44
45
46     /**
47      * Constructor used when page styles need to be loaded. The plugin's bundle
48      * is retrieved from the page model class. The default properties are
49      * assumed to be the presentation shared properties. The inherrited
50      * properties are properties that we got from included and extension styles.
51      *
52      * @param modelRoot
53      */

54     public PageStyleManager(AbstractIntroPage page, Properties JavaDoc sharedProperties) {
55         this.page = page;
56         context = new StyleContext();
57         context.bundle = page.getBundle();
58         
59         // honor shared-style.
60
if (page.injectSharedStyle())
61             properties = new Properties JavaDoc(sharedProperties);
62         else
63             properties = new Properties JavaDoc();
64         String JavaDoc altStyle = page.getAltStyle();
65         if (altStyle != null) {
66             load(properties, altStyle, context);
67         }
68
69         // AltStyles Hashtable has alt-styles as keys, the bundles as
70
// values.
71
Hashtable JavaDoc altStyles = page.getAltStyles();
72         if (altStyles != null) {
73             Enumeration JavaDoc styles = altStyles.keys();
74             while (styles.hasMoreElements()) {
75                 String JavaDoc style = (String JavaDoc) styles.nextElement();
76                 Properties JavaDoc inheritedProperties = new Properties JavaDoc();
77                 Bundle bundle = (Bundle) altStyles.get(style);
78                 StyleContext sc = new StyleContext();
79                 sc.bundle = bundle;
80                 load(inheritedProperties, style, sc);
81                 altStyleContexts.put(inheritedProperties, sc);
82             }
83         }
84
85         // cache root
86
root = (IntroModelRoot) page.getParentPage().getParent();
87     }
88
89
90     // Override parent method to include alt-styles. Use implicit keys as well.
91
public String JavaDoc getProperty(String JavaDoc key) {
92         return getProperty(key, true);
93     }
94
95     // Override parent method to include alt-styles. If useImplicit is true, we
96
// try to resolve a key without its pageId.
97
private String JavaDoc getProperty(String JavaDoc key, boolean useImplicitKey) {
98         Properties JavaDoc aProperties = findPropertyOwner(key);
99         String JavaDoc value = super.doGetProperty(aProperties, key);
100         if (useImplicitKey) {
101             if (value == null && page.getId() != null
102                     && key.startsWith(page.getId())) {
103                 // did not find the key as-is. Trim pageId and try again.
104
String JavaDoc relativeKey = key.substring(page.getId().length());
105                 return getProperty(relativeKey);
106             }
107         }
108         return value;
109     }
110
111
112     /**
113      * Finds a Properties that represents an inherited shared style, or this
114      * current pages style. If the given key is not found, the pageId is trimmed
115      * from the begining of the key, and the key is looked up again. If key does
116      * not start with a pageId, lookup only the key as is.
117      *
118      * @param key
119      * @return
120      */

121     private Properties JavaDoc findPropertyOwner(String JavaDoc key) {
122         // search for the key in this page's properties first.
123
if (properties.containsKey(key))
124             return properties;
125
126         // search inherited properties second.
127
Enumeration JavaDoc inheritedPageProperties = altStyleContexts.keys();
128         while (inheritedPageProperties.hasMoreElements()) {
129             Properties JavaDoc aProperties = (Properties JavaDoc) inheritedPageProperties
130                 .nextElement();
131             if (aProperties.containsKey(key))
132                 return aProperties;
133         }
134         // we did not find the key. Return the local properties anyway.
135
return properties;
136     }
137
138
139
140     /**
141      * Finds the context from which this key was loaded. If the key is not from
142      * an inherited alt style, then use the context corresponding to this page.
143      *
144      * @param key
145      * @return
146      */

147    
148     protected StyleContext getAssociatedContext(String JavaDoc key) {
149         Properties JavaDoc aProperties = findPropertyOwner(key);
150         StyleContext context = (StyleContext) altStyleContexts.get(aProperties);
151         if (context != null)
152             return context;
153         return super.getAssociatedContext(key);
154     }
155
156
157     /*
158      * For number of columns, do not return 1 as the default, to allow for
159      * further processing. At the root page level, getting a 0 as ncolumns means
160      * that the number of columns is the number of children. At the page level,
161      * default is 1.
162      */

163     public int getPageNumberOfColumns() {
164         return getIntProperty(page, ".layout.ncolumns", 0); //$NON-NLS-1$
165
}
166
167
168     public int getNumberOfColumns(IntroGroup group) {
169         return getIntProperty(group, ".layout.ncolumns", 0); //$NON-NLS-1$
170
}
171     
172     public boolean getEqualWidth(IntroGroup group) {
173         return getBooleanProperty(group, ".layout.equalWidth", false); //$NON-NLS-1$
174
}
175
176     public int getPageVerticalSpacing() {
177         return getIntProperty(page, ".layout.vspacing", 5); //$NON-NLS-1$
178
}
179
180     public int getVerticalSpacing(IntroGroup group) {
181         return getIntProperty(group, ".layout.vspacing", 5); //$NON-NLS-1$
182
}
183
184     public int getPageHorizantalSpacing() {
185         return getIntProperty(page, ".layout.hspacing", 5); //$NON-NLS-1$
186
}
187
188     public int getHorizantalSpacing(IntroGroup group) {
189         return getIntProperty(group, ".layout.hspacing", 5); //$NON-NLS-1$
190
}
191
192     public int getColSpan(AbstractBaseIntroElement element) {
193         return getIntProperty(element, ".layout.colspan", 1); //$NON-NLS-1$
194
}
195
196     public int getRowSpan(AbstractBaseIntroElement element) {
197         return getIntProperty(element, ".layout.rowspan", 1); //$NON-NLS-1$
198
}
199
200     private int getIntProperty(AbstractBaseIntroElement element,
201             String JavaDoc qualifier, int defaultValue) {
202         StringBuffer JavaDoc buff = ModelLoaderUtil.createPathToElementKey(element, true);
203         if (buff == null)
204             return defaultValue;
205         String JavaDoc key = buff.append(qualifier).toString();
206         return getIntProperty(key, defaultValue);
207     }
208     
209     private boolean getBooleanProperty(AbstractBaseIntroElement element,
210             String JavaDoc qualifier, boolean defaultValue) {
211         StringBuffer JavaDoc buff = ModelLoaderUtil.createPathToElementKey(element, true);
212         if (buff == null)
213             return defaultValue;
214         String JavaDoc key = buff.append(qualifier).toString();
215         return getBooleanProperty(key, defaultValue);
216     }
217
218     private int getIntProperty(String JavaDoc key, int defaulValue) {
219         int intValue = defaulValue;
220         String JavaDoc value = getProperty(key);
221         if (value == null)
222             return intValue;
223
224         try {
225             intValue = Integer.parseInt(value);
226         } catch (NumberFormatException JavaDoc e) {
227             Log.error("Failed to parse key: " + key + " as an integer.", e); //$NON-NLS-1$ //$NON-NLS-2$
228
}
229         return intValue;
230     }
231     
232     private boolean getBooleanProperty(String JavaDoc key, boolean defaultValue) {
233         boolean booleanValue = defaultValue;
234         String JavaDoc value = getProperty(key);
235         if (value != null)
236             booleanValue = value.equalsIgnoreCase("true"); //$NON-NLS-1$
237
return booleanValue;
238     }
239
240
241     /**
242      * Finds the description text of the given group. Looks for the Text child
243      * element whos id is specified as follows:
244      * <p>
245      * <pageId>. <path_to_group>.description-id= <id of child description Text
246      * element>
247      * </p>
248      * If not found, use the default description style.
249      *
250      * Returns null if no default style found, or any id in path is null.
251      *
252      * @param group
253      * @return
254      */

255     public String JavaDoc getDescription(IntroGroup group) {
256         StringBuffer JavaDoc buff = ModelLoaderUtil.createPathToElementKey(group, true);
257         if (buff == null)
258             return null;
259         String JavaDoc key = buff.append(".description-id").toString(); //$NON-NLS-1$
260
return doGetDescription(group, key);
261     }
262
263
264     /**
265      * Finds the description text of the associated page. Looks for the Text
266      * child element whos id is specified as follows:
267      * <p>
268      * <pageId>.description-id= <id of child description Text element>
269      * </p>
270      * If not found, use the default description style.
271      *
272      * Returns null if no default style found, or any id in path is null.
273      *
274      * @param group
275      * @return
276      */

277     public String JavaDoc getPageDescription() {
278         if (page.getId() == null)
279             return null;
280         String JavaDoc key = page.getId() + ".description-id"; //$NON-NLS-1$
281
return doGetDescription(page, key);
282     }
283
284     private String JavaDoc doGetDescription(AbstractIntroContainer parent, String JavaDoc key) {
285         String JavaDoc path = getProperty(key);
286         String JavaDoc description = null;
287         if (path != null)
288             description = findTextFromPath(parent, path);
289         if (description != null)
290             return description;
291         return findTextFromStyleId(parent, getDescriptionStyleId());
292     }
293
294     private String JavaDoc getDescriptionStyleId() {
295         String JavaDoc key = "description-style-id"; //$NON-NLS-1$
296
return getProperty(key);
297     }
298
299     /**
300      * Finds the subtitle of the associated page. Looks for the Text child
301      * element whose id is specified as follows:
302      * <p>
303      * <pageId>.description-id= <id of child description Text element>
304      * </p>
305      * If not found, use the default description style.
306      *
307      * @param group
308      * @return
309      */

310     public String JavaDoc getPageSubTitle() {
311         String JavaDoc key = page.getId() + ".subtitle-id"; //$NON-NLS-1$
312
String JavaDoc path = getProperty(key);
313         String JavaDoc description = null;
314         if (path != null)
315             description = findTextFromPath(page, path);
316         if (description != null)
317             return description;
318         return findTextFromStyleId(page, getPageSubTitleStyleId());
319     }
320
321     private String JavaDoc getPageSubTitleStyleId() {
322         String JavaDoc key = "subtitle-style-id"; //$NON-NLS-1$
323
return getProperty(key);
324     }
325
326     private String JavaDoc findTextFromPath(AbstractIntroContainer parent, String JavaDoc path) {
327         AbstractIntroElement child = parent.findTarget(root, path);
328         if (child != null && child.isOfType(AbstractIntroElement.TEXT)) {
329             makeFiltered(child);
330             return ((IntroText) child).getText();
331         }
332         return null;
333     }
334
335     /**
336      * Returns the first direct child text element with the given style-id.
337      *
338      * @return
339      */

340     private String JavaDoc findTextFromStyleId(AbstractIntroContainer parent,
341             String JavaDoc styleId) {
342         IntroText[] allText = (IntroText[]) parent
343             .getChildrenOfType(AbstractIntroElement.TEXT);
344         for (int i = 0; i < allText.length; i++) {
345             if (allText[i].getStyleId() == null)
346                 // not all elements have style id.
347
continue;
348             if (allText[i].getStyleId().equals(styleId)) {
349                 makeFiltered(allText[i]);
350                 return allText[i].getText();
351             }
352         }
353         return null;
354     }
355
356     /**
357      * Util method to check model type, and filter model element out if it is of
358      * the correct type.
359      *
360      * @param element
361      */

362     private AbstractIntroElement makeFiltered(AbstractIntroElement element) {
363         if (element.isOfType(AbstractIntroElement.BASE_ELEMENT))
364             ((AbstractBaseIntroElement) element).setFilterState(true);
365         return element;
366     }
367
368
369
370     public boolean getShowLinkDescription() {
371         String JavaDoc key = page.getId() + ".show-link-description"; //$NON-NLS-1$
372
String JavaDoc value = getProperty(key);
373         if (value == null) {
374             key = ".show-link-description"; //$NON-NLS-1$
375
value = getProperty(key);
376         }
377         if (value == null)
378             value = "true"; //$NON-NLS-1$
379
return value.toLowerCase().equals("true"); //$NON-NLS-1$
380
}
381
382     public boolean showHomePageNavigation() {
383         String JavaDoc key = page.getId() + ".show-home-page-navigation"; //$NON-NLS-1$
384
String JavaDoc value = getProperty(key);
385         if (value == null) {
386             key = ".show-home-page-navigation"; //$NON-NLS-1$
387
value = getProperty(key);
388         }
389         if (value == null)
390             value = "true"; //$NON-NLS-1$
391
return value.equalsIgnoreCase("true"); //$NON-NLS-1$
392
}
393
394
395     public Color getColor(FormToolkit toolkit, AbstractBaseIntroElement element) {
396         StringBuffer JavaDoc buff = ModelLoaderUtil.createPathToElementKey(element, true);
397         if (buff == null)
398             return null;
399         String JavaDoc key = buff.append(".font.fg").toString(); //$NON-NLS-1$
400
return getColor(toolkit, key);
401     }
402     
403     public Color getBackgrond(FormToolkit toolkit, AbstractBaseIntroElement element) {
404         StringBuffer JavaDoc buff = ModelLoaderUtil.createPathToElementKey(element, true);
405         if (buff == null)
406             return null;
407         String JavaDoc key = buff.append(".bg").toString(); //$NON-NLS-1$
408
return getColor(toolkit, key);
409     }
410
411     public boolean isBold(IntroText text) {
412         String JavaDoc value = null;
413         /*
414         StringBuffer buff = ModelLoaderUtil.createPathToElementKey(text, true);
415         if (buff != null) {
416             String key = buff.append(".font.bold").toString(); //$NON-NLS-1$
417             value = getProperty(key);
418             if (value != null)
419                 return value.toLowerCase().equals("true"); //$NON-NLS-1$
420             else {
421                 buff = ModelLoaderUtil.createPathToElementKey(text, true);
422             }
423         }
424         */

425         value = getPropertyValue(text, ".font.bold"); //$NON-NLS-1$
426
if (value == null) {
427             // bold is not specified by ID. Check to see if there is a style-id
428
// specified for bold.
429
value = getProperty("bold-style-id"); //$NON-NLS-1$
430
if (value != null && text.getStyleId() != null)
431                 return text.getStyleId().equals(value);
432         }
433         return false;
434     }
435     
436     private String JavaDoc getPropertyValue(AbstractIntroIdElement element, String JavaDoc suffix) {
437         StringBuffer JavaDoc buff = ModelLoaderUtil.createPathToElementKey(element, true);
438         if (buff != null) {
439             String JavaDoc key = buff.append(suffix).toString();
440             String JavaDoc value = getProperty(key);
441             if (value != null)
442                 return value;
443             // try the page.id key
444
buff = ModelLoaderUtil.createPathToElementKey(element, false);
445             if (buff!= null) {
446                 key = buff.append(suffix).toString();
447                 value = getProperty(key);
448                 return value;
449             }
450         }
451         return null;
452     }
453
454     public static Font getBannerFont() {
455         return JFaceResources.getBannerFont();
456     }
457
458     public static Font getHeaderFont() {
459         return JFaceResources.getHeaderFont();
460     }
461
462     /**
463      * Retrieves an image for a link in a page. If not found, uses the page's
464      * default link image. If still not found, uses the passed default.
465      *
466      * @param link
467      * @param qualifier
468      * @return
469      */

470     public Image getImage(IntroLink link, String JavaDoc qualifier, String JavaDoc defaultKey) {
471         // try the Id first
472
String JavaDoc key = createImageByIdKey(page, link, qualifier);
473         String JavaDoc value = getProperty(key, false);
474         if (value==null) {
475             key = createImageKey(page, link, qualifier);
476             // special case where we have to handle this because extended code does
477
// not go through getProperty() in this method.
478
value = getProperty(key, false);
479         }
480         if (value == null && page.getId() != null
481                 && key.startsWith(page.getId()))
482             // did not use the key as-is. Trim pageId and try again.
483
key = key.substring(page.getId().length());
484
485         // pageKey can not become an implicit key.
486
String JavaDoc pageKey = createImageKey(page, null, qualifier);
487
488         return getImage(key, pageKey, defaultKey);
489     }
490
491     private String JavaDoc createImageKey(AbstractIntroPage page, IntroLink link,
492             String JavaDoc qualifier) {
493         StringBuffer JavaDoc buff = null;
494         if (link != null) {
495             buff = ModelLoaderUtil.createPathToElementKey(link, true);
496             if (buff == null)
497                 return ""; //$NON-NLS-1$
498
} else {
499             buff = new StringBuffer JavaDoc();
500             buff.append(page.getId());
501         }
502         buff.append("."); //$NON-NLS-1$
503
buff.append(qualifier);
504         return buff.toString();
505     }
506     
507     private String JavaDoc createImageByIdKey(AbstractIntroPage page, IntroLink link,
508             String JavaDoc qualifier) {
509         if (link==null || link.getId()==null)
510             return ""; //$NON-NLS-1$
511
StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
512         buff.append(page.getId());
513         buff.append("."); //$NON-NLS-1$
514
buff.append(link.getId());
515         buff.append("."); //$NON-NLS-1$
516
buff.append(qualifier);
517         return buff.toString();
518     }
519
520     public Image getImage(IntroImage introImage) {
521         String JavaDoc imageLocation = introImage.getSrcAsIs();
522         StringBuffer JavaDoc buff = ModelLoaderUtil.createPathToElementKey(introImage, true);
523         String JavaDoc key;
524         if (buff == null) {
525             key = "//" + imageLocation; //$NON-NLS-1$
526
} else {
527             key = buff!=null?buff.toString():null;
528         }
529         if (ImageUtil.hasImage(key))
530             return ImageUtil.getImage(key);
531         // key not already registered.
532
if (buff != null) {
533             StyleContext acontext = getAssociatedContext(key);
534             if (acontext.inTheme) {
535                 ImageUtil.registerImage(key, acontext.path, imageLocation);
536                 return ImageUtil.getImage(key);
537             }
538         }
539         Bundle bundle = introImage.getBundle();
540         if (FileLocator.find(bundle, new Path(imageLocation), null) == null) {
541             return null;
542         }
543         ImageUtil.registerImage(key, bundle, imageLocation);
544         return ImageUtil.getImage(key);
545
546     }
547
548
549 }
550
Popular Tags