001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.intro.impl.swt;
011:
012: import java.io.InputStream;
013: import java.net.URL;
014: import java.util.Properties;
015:
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.core.runtime.Path;
018: import org.eclipse.swt.graphics.Color;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.swt.graphics.RGB;
021: import org.eclipse.ui.forms.FormColors;
022: import org.eclipse.ui.forms.widgets.FormToolkit;
023: import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
024: import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
025: import org.eclipse.ui.internal.intro.impl.util.Log;
026: import org.osgi.framework.Bundle;
027:
028: public class SharedStyleManager {
029:
030: protected Properties properties;
031: protected StyleContext context;
032:
033: class StyleContext {
034: IPath path;
035: Bundle bundle;
036: boolean inTheme;
037: }
038:
039: SharedStyleManager() {
040: // no-op
041: }
042:
043: /**
044: * Constructor used when shared styles need to be loaded. The bundle is
045: * retrieved from the model root.
046: *
047: * @param modelRoot
048: */
049: public SharedStyleManager(IntroModelRoot modelRoot) {
050: context = new StyleContext();
051: context.bundle = modelRoot.getBundle();
052: properties = new Properties();
053: String[] sharedStyles = modelRoot.getPresentation()
054: .getImplementationStyles();
055: if (sharedStyles != null) {
056: for (int i = 0; i < sharedStyles.length; i++)
057: load(properties, sharedStyles[i], context);
058: }
059: }
060:
061: protected void load(Properties properties, String style,
062: StyleContext context) {
063: if (style == null)
064: return;
065: try {
066: URL styleURL = new URL(style);
067: InputStream is = styleURL.openStream();
068: properties.load(is);
069: is.close();
070: context.path = new Path(style).removeLastSegments(1);
071: String t = (String) properties.get("theme"); //$NON-NLS-1$
072: if (t != null && t.trim().equalsIgnoreCase("true")) //$NON-NLS-1$
073: context.inTheme = true;
074: } catch (Exception e) {
075: Log.error("Could not load SWT style: " + style, e); //$NON-NLS-1$
076: }
077: }
078:
079: /**
080: * Get the property from the shared properties.
081: *
082: * @param key
083: * @return
084: */
085: public String getProperty(String key) {
086: return doGetProperty(properties, key);
087: }
088:
089: /*
090: * Utility method to trim properties retrieval.
091: */
092: protected String doGetProperty(Properties aProperties, String key) {
093: String value = aProperties.getProperty(key);
094: if (value != null)
095: // trim the properties as trailing balnnks cause problems.
096: value = value.trim();
097: return value;
098: }
099:
100: protected RGB getRGB(String key) {
101: String value = getProperty(key);
102: if (value == null)
103: return null;
104: return parseRGB(value);
105: }
106:
107: /**
108: * A utility method that creates RGB object from a value encoded in the
109: * following format: #rrggbb, where r, g and b are hex color values in the
110: * range from 00 to ff.
111: *
112: * @param value
113: * @return
114: */
115:
116: public static RGB parseRGB(String value) {
117: if (value.charAt(0) == '#') {
118: // HEX
119: try {
120: int r = Integer.parseInt(value.substring(1, 3), 16);
121: int g = Integer.parseInt(value.substring(3, 5), 16);
122: int b = Integer.parseInt(value.substring(5, 7), 16);
123: return new RGB(r, g, b);
124: } catch (NumberFormatException e) {
125: Log
126: .error(
127: "Failed to parser: " + value + " as an integer", e); //$NON-NLS-1$ //$NON-NLS-2$
128: }
129: }
130: return null;
131: }
132:
133: /**
134: * Finds the bundle from which this key was loaded. This is the bundle from
135: * which shared styles where loaded.
136: *
137: * @param key
138: * @return
139: */
140: protected Bundle getAssociatedBundle(String key) {
141: return context.bundle;
142: }
143:
144: protected StyleContext getAssociatedContext(String key) {
145: return context;
146: }
147:
148: /**
149: * @return Returns the properties.
150: */
151: public Properties getProperties() {
152: return properties;
153: }
154:
155: /**
156: *
157: *
158: * @param toolkit
159: * @param key
160: * @return color. May return null.
161: */
162: public Color getColor(FormToolkit toolkit, String key) {
163: FormColors colors = toolkit.getColors();
164: Color color = colors.getColor(key);
165: if (color == null) {
166: RGB rgb = getRGB(key);
167: if (rgb != null)
168: color = colors.createColor(key, rgb);
169: }
170: return color;
171: }
172:
173: /**
174: * Retrieve an image from this page's properties, given a key.
175: *
176: * @param key
177: * @param defaultPageKey
178: * @param defaultKey
179: * @return
180: */
181: public Image getImage(String key, String defaultPageKey,
182: String defaultKey) {
183: String currentKey = key;
184: String value = getProperty(currentKey);
185: if (value == null && defaultPageKey != null) {
186: currentKey = defaultPageKey;
187: value = getProperty(defaultPageKey);
188: }
189: if (value != null) {
190: if (ImageUtil.hasImage(currentKey))
191: return ImageUtil.getImage(currentKey);
192: // try to register the image.
193: StyleContext ccontext = getAssociatedContext(currentKey);
194: if (ccontext.inTheme) {
195: // if 'theme' key is set, load image
196: // relative to the file, not relative to the bundle
197: ImageUtil.registerImage(currentKey, ccontext.path,
198: value);
199: } else {
200: Bundle bundle = ccontext.bundle;
201: if (bundle == null)
202: // it means that we are getting a key defined in this page's
203: // styles. (ie: not an inherited style).
204: bundle = this .context.bundle;
205: ImageUtil.registerImage(currentKey, bundle, value);
206: }
207: Image image = ImageUtil.getImage(currentKey);
208: if (image != null)
209: return image;
210: }
211: // try default. We know default is already registered,
212: if (defaultKey != null)
213: return ImageUtil.getImage(defaultKey);
214: return null;
215: }
216:
217: public boolean useCustomHomePagelayout() {
218: String key = "home-page-custom-layout"; //$NON-NLS-1$
219: String value = getProperty(key);
220: if (value == null)
221: value = "true"; //$NON-NLS-1$
222: return value.equalsIgnoreCase("true"); //$NON-NLS-1$
223: }
224:
225: }
|