KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InputStream JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.RGB;
22 import org.eclipse.ui.forms.FormColors;
23 import org.eclipse.ui.forms.widgets.FormToolkit;
24 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
25 import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
26 import org.eclipse.ui.internal.intro.impl.util.Log;
27 import org.osgi.framework.Bundle;
28
29 public class SharedStyleManager {
30
31     protected Properties JavaDoc properties;
32     protected StyleContext context;
33     
34     class StyleContext {
35         IPath path;
36         Bundle bundle;
37         boolean inTheme;
38     }
39
40     SharedStyleManager() {
41         // no-op
42
}
43
44     /**
45      * Constructor used when shared styles need to be loaded. The bundle is
46      * retrieved from the model root.
47      *
48      * @param modelRoot
49      */

50     public SharedStyleManager(IntroModelRoot modelRoot) {
51         context = new StyleContext();
52         context.bundle = modelRoot.getBundle();
53         properties = new Properties JavaDoc();
54         String JavaDoc [] sharedStyles = modelRoot.getPresentation()
55             .getImplementationStyles();
56         if (sharedStyles != null) {
57             for (int i=0; i<sharedStyles.length; i++)
58             load(properties, sharedStyles[i], context);
59         }
60     }
61
62     protected void load(Properties JavaDoc properties, String JavaDoc style, StyleContext context) {
63         if (style == null)
64             return;
65         try {
66             URL JavaDoc styleURL = new URL JavaDoc(style);
67             InputStream JavaDoc is = styleURL.openStream();
68             properties.load(is);
69             is.close();
70             context.path = new Path(style).removeLastSegments(1);
71             String JavaDoc t = (String JavaDoc)properties.get("theme"); //$NON-NLS-1$
72
if (t!=null && t.trim().equalsIgnoreCase("true")) //$NON-NLS-1$
73
context.inTheme = true;
74         } catch (Exception JavaDoc e) {
75             Log.error("Could not load SWT style: " + style, e); //$NON-NLS-1$
76
}
77     }
78
79
80     /**
81      * Get the property from the shared properties.
82      *
83      * @param key
84      * @return
85      */

86     public String JavaDoc getProperty(String JavaDoc key) {
87         return doGetProperty(properties, key);
88     }
89
90     /*
91      * Utility method to trim properties retrieval.
92      */

93     protected String JavaDoc doGetProperty(Properties JavaDoc aProperties, String JavaDoc key) {
94         String JavaDoc value = aProperties.getProperty(key);
95         if (value != null)
96             // trim the properties as trailing balnnks cause problems.
97
value = value.trim();
98         return value;
99     }
100
101
102     protected RGB getRGB(String JavaDoc key) {
103         String JavaDoc value = getProperty(key);
104         if (value == null)
105             return null;
106         return parseRGB(value);
107     }
108
109     /**
110      * A utility method that creates RGB object from a value encoded in the
111      * following format: #rrggbb, where r, g and b are hex color values in the
112      * range from 00 to ff.
113      *
114      * @param value
115      * @return
116      */

117
118     public static RGB parseRGB(String JavaDoc value) {
119         if (value.charAt(0) == '#') {
120             // HEX
121
try {
122                 int r = Integer.parseInt(value.substring(1, 3), 16);
123                 int g = Integer.parseInt(value.substring(3, 5), 16);
124                 int b = Integer.parseInt(value.substring(5, 7), 16);
125                 return new RGB(r, g, b);
126             } catch (NumberFormatException JavaDoc e) {
127                 Log.error("Failed to parser: " + value + " as an integer", e); //$NON-NLS-1$ //$NON-NLS-2$
128
}
129         }
130         return null;
131     }
132
133
134
135     /**
136      * Finds the bundle from which this key was loaded. This is the bundle from
137      * which shared styles where loaded.
138      *
139      * @param key
140      * @return
141      */

142     protected Bundle getAssociatedBundle(String JavaDoc key) {
143         return context.bundle;
144     }
145     
146     protected StyleContext getAssociatedContext(String JavaDoc key) {
147         return context;
148     }
149
150
151
152     /**
153      * @return Returns the properties.
154      */

155     public Properties JavaDoc getProperties() {
156         return properties;
157     }
158
159
160     /**
161      *
162      *
163      * @param toolkit
164      * @param key
165      * @return color. May return null.
166      */

167     public Color getColor(FormToolkit toolkit, String JavaDoc key) {
168         FormColors colors = toolkit.getColors();
169         Color color = colors.getColor(key);
170         if (color == null) {
171             RGB rgb = getRGB(key);
172             if (rgb != null)
173                 color = colors.createColor(key, rgb);
174         }
175         return color;
176     }
177
178
179
180     /**
181      * Retrieve an image from this page's properties, given a key.
182      *
183      * @param key
184      * @param defaultPageKey
185      * @param defaultKey
186      * @return
187      */

188     public Image getImage(String JavaDoc key, String JavaDoc defaultPageKey, String JavaDoc defaultKey) {
189         String JavaDoc currentKey = key;
190         String JavaDoc value = getProperty(currentKey);
191         if (value == null && defaultPageKey != null) {
192             currentKey = defaultPageKey;
193             value = getProperty(defaultPageKey);
194         }
195         if (value != null) {
196             if (ImageUtil.hasImage(currentKey))
197                 return ImageUtil.getImage(currentKey);
198             // try to register the image.
199
StyleContext ccontext = getAssociatedContext(currentKey);
200             if (ccontext.inTheme) {
201                 // if 'theme' key is set, load image
202
// relative to the file, not relative to the bundle
203
ImageUtil.registerImage(currentKey, ccontext.path, value);
204             }
205             else {
206                 Bundle bundle = ccontext.bundle;
207                 if (bundle == null)
208                     // it means that we are getting a key defined in this page's
209
// styles. (ie: not an inherited style).
210
bundle = this.context.bundle;
211                 ImageUtil.registerImage(currentKey, bundle, value);
212             }
213             Image image = ImageUtil.getImage(currentKey);
214             if (image != null)
215                 return image;
216         }
217         // try default. We know default is already registered,
218
if (defaultKey != null)
219             return ImageUtil.getImage(defaultKey);
220         return null;
221     }
222
223
224     public boolean useCustomHomePagelayout() {
225         String JavaDoc key = "home-page-custom-layout"; //$NON-NLS-1$
226
String JavaDoc value = getProperty(key);
227         if (value == null)
228             value = "true"; //$NON-NLS-1$
229
return value.equalsIgnoreCase("true"); //$NON-NLS-1$
230
}
231
232 }
233
Popular Tags