1 11 package org.eclipse.ui.internal.intro.impl.swt; 12 13 import java.io.InputStream ; 14 import java.net.URL ; 15 import java.util.Properties ; 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 properties; 32 protected StyleContext context; 33 34 class StyleContext { 35 IPath path; 36 Bundle bundle; 37 boolean inTheme; 38 } 39 40 SharedStyleManager() { 41 } 43 44 50 public SharedStyleManager(IntroModelRoot modelRoot) { 51 context = new StyleContext(); 52 context.bundle = modelRoot.getBundle(); 53 properties = new Properties (); 54 String [] 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 properties, String style, StyleContext context) { 63 if (style == null) 64 return; 65 try { 66 URL styleURL = new URL (style); 67 InputStream is = styleURL.openStream(); 68 properties.load(is); 69 is.close(); 70 context.path = new Path(style).removeLastSegments(1); 71 String t = (String )properties.get("theme"); if (t!=null && t.trim().equalsIgnoreCase("true")) context.inTheme = true; 74 } catch (Exception e) { 75 Log.error("Could not load SWT style: " + style, e); } 77 } 78 79 80 86 public String getProperty(String key) { 87 return doGetProperty(properties, key); 88 } 89 90 93 protected String doGetProperty(Properties aProperties, String key) { 94 String value = aProperties.getProperty(key); 95 if (value != null) 96 value = value.trim(); 98 return value; 99 } 100 101 102 protected RGB getRGB(String key) { 103 String value = getProperty(key); 104 if (value == null) 105 return null; 106 return parseRGB(value); 107 } 108 109 117 118 public static RGB parseRGB(String value) { 119 if (value.charAt(0) == '#') { 120 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 e) { 127 Log.error("Failed to parser: " + value + " as an integer", e); } 129 } 130 return null; 131 } 132 133 134 135 142 protected Bundle getAssociatedBundle(String key) { 143 return context.bundle; 144 } 145 146 protected StyleContext getAssociatedContext(String key) { 147 return context; 148 } 149 150 151 152 155 public Properties getProperties() { 156 return properties; 157 } 158 159 160 167 public Color getColor(FormToolkit toolkit, String 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 188 public Image getImage(String key, String defaultPageKey, String defaultKey) { 189 String currentKey = key; 190 String 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 StyleContext ccontext = getAssociatedContext(currentKey); 200 if (ccontext.inTheme) { 201 ImageUtil.registerImage(currentKey, ccontext.path, value); 204 } 205 else { 206 Bundle bundle = ccontext.bundle; 207 if (bundle == null) 208 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 if (defaultKey != null) 219 return ImageUtil.getImage(defaultKey); 220 return null; 221 } 222 223 224 public boolean useCustomHomePagelayout() { 225 String key = "home-page-custom-layout"; String value = getProperty(key); 227 if (value == null) 228 value = "true"; return value.equalsIgnoreCase("true"); } 231 232 } 233 | Popular Tags |