--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2011 IBM Corporation and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * IBM Corporation - initial API and implementation\r
+ *******************************************************************************/\r
+package org.tizen.common.ui;\r
+\r
+import java.net.URL;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.StringTokenizer;\r
+\r
+import javax.xml.parsers.SAXParserFactory;\r
+\r
+import org.eclipse.core.runtime.FileLocator;\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.swt.graphics.RGB;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.tizen.common.CommonPlugin;\r
+import org.xml.sax.Attributes;\r
+import org.xml.sax.InputSource;\r
+import org.xml.sax.SAXException;\r
+import org.xml.sax.XMLReader;\r
+import org.xml.sax.helpers.DefaultHandler;\r
+\r
+/**\r
+ * Associates css color names to the appropriate {@link RGB} values.\r
+ * These associations are defined in org.eclipse.wst.css.ui/csscolors/extended-color-mapping.xml\r
+ *\r
+ */\r
+public class CSSColorNames {\r
+\r
+ protected final Logger logger = LoggerFactory.getLogger(CSSColorNames.class);\r
+\r
+ private static CSSColorNames instance;\r
+ private static final Map<String, RGB> colors = new HashMap<String, RGB>();\r
+ protected static final String COLOR_MAPPING_XML = "resources/csscolors/extended-color-mapping.xml";\r
+\r
+ private CSSColorNames() {\r
+ try {\r
+ URL url = FileLocator.find(CommonPlugin.getDefault().getBundle(), new Path(COLOR_MAPPING_XML), null);\r
+ final XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();\r
+ xmlReader.setContentHandler(new ColorMappingHandler());\r
+ xmlReader.parse(new InputSource(url.openStream()));\r
+ } catch (Exception e) {\r
+ logger.error("Failed to read a color mapping file", e);\r
+ }\r
+ }\r
+\r
+ public static synchronized CSSColorNames getInstance() {\r
+ if (instance == null) {\r
+ instance = new CSSColorNames();\r
+ }\r
+ return instance;\r
+ }\r
+\r
+ /**\r
+ * Returns the {@link RGB} value associated with this color name.\r
+ * @param name the color name\r
+ * @return {@link RGB} associated with <code>name</code>, null if it is an unknown name or invalid RGB value\r
+ */\r
+ public RGB getRGB(String name) {\r
+ return (RGB) colors.get(name);\r
+ }\r
+\r
+ class ColorMappingHandler extends DefaultHandler {\r
+ private final String COLOR_ELEM = "color"; //$NON-NLS-1$\r
+ private final String NAME_ATTR = "name"; //$NON-NLS-1$\r
+ private final String RGB_ATTR = "rgb"; //$NON-NLS-1$\r
+\r
+ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {\r
+ if (COLOR_ELEM.equals(qName)) {\r
+ final String name = attributes.getValue(NAME_ATTR);\r
+ final String rgb = attributes.getValue(RGB_ATTR);\r
+ if (name != null && rgb != null) {\r
+ final RGB rgbValue = getRGB(rgb);\r
+ if (rgbValue != null) {\r
+ colors.put(name, rgbValue);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Converts an rgb string into an {@link RGB}\r
+ * @param rgb the color string\r
+ * @return an {@link RGB} if one can be created from the string; otherwise, null\r
+ */\r
+ private RGB getRGB(String rgb) {\r
+ final StringTokenizer tokenizer = new StringTokenizer(rgb, ","); //$NON-NLS-1$\r
+ int[] weights = new int[3];\r
+ for (int i = 0; tokenizer.hasMoreTokens(); i++) {\r
+ if (i > 2) {\r
+ return null;\r
+ }\r
+ try {\r
+ weights[i] = Integer.parseInt(tokenizer.nextToken().trim());\r
+ } catch (NumberFormatException e) {\r
+ return null;\r
+ }\r
+ if (weights[i] > 255 || weights[i] < 0) {\r
+ return null;\r
+ }\r
+ }\r
+ return new RGB(weights[0], weights[1], weights[2]);\r
+ }\r
+ }\r
+\r
+}\r
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.BrowserFunction;
-import org.eclipse.swt.browser.LocationAdapter;
-import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.OpenWindowListener;
-import org.eclipse.swt.browser.ProgressAdapter;
-import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
root.setLayout(new GridLayout( 1, false ));
root.setLayoutData(new GridData(380, 250));
- final Browser browser = new Browser(root, SWT.NONE);
+ final Browser browser = createBrowser(root);
browser.setLayoutData(new GridData(GridData.FILL_BOTH));
- browser.setJavascriptEnabled(true);
- browser.addOpenWindowListener(new OpenWindowListener() {
- public void open(WindowEvent event) {
- event.required = true; // Cancel opening of new windows
- }
- });
- // Replace browser's built-in context menu with none
- browser.setMenu(new Menu(root.getShell(), SWT.NONE));
-
- Bundle bundle = Platform.getBundle(CommonPlugin.PLUGIN_ID);
- Path path = new Path(COLORPICKER_HOME);
- URL fColorPickerUrl = null;;
- try {
- fColorPickerUrl = FileLocator.toFileURL(FileLocator.find(bundle, path, null));
- } catch (Exception e) {
- logger.error("Failed to find a colorpicker", e);
- }
-
- String url = fColorPickerUrl.toString();
String color = SWTUtil.convertRGBToHexadecimal(getRGB());
- url += "?color=" + color;
+ String url = getColorPickerURL() + "?color=" + color;
browser.setUrl(url);
browser.refresh();
- final BrowserFunction function = new SetColorFunction(browser, GET_COLOR_FUNCTION);
- browser.addProgressListener(new ProgressAdapter() {
- public void completed(ProgressEvent event) {
- browser.addLocationListener(new LocationAdapter() {
- public void changed(LocationEvent event) {
- browser.removeLocationListener(this);
- function.dispose();
- }
- });
- }
- });
+ new SetColorFunction(browser, GET_COLOR_FUNCTION);
applyDialogFont(root);
initializeDialogUnits(root);
return root;
}
+ public static Browser createBrowser(Composite parent) {
+ Browser browser = new Browser(parent, SWT.NONE);
+ browser.setJavascriptEnabled(true);
+ browser.addOpenWindowListener(new OpenWindowListener() {
+ public void open(WindowEvent event) {
+ event.required = true; // Cancel opening of new windows
+ }
+ });
+ // Replace browser's built-in context menu with none
+ browser.setMenu(new Menu(parent.getShell(), SWT.NONE));
+
+ return browser;
+ }
+
+ public static String getColorPickerURL() {
+ Bundle bundle = Platform.getBundle(CommonPlugin.PLUGIN_ID);
+ Path path = new Path(COLORPICKER_HOME);
+ URL fColorPickerUrl = null;
+ try {
+ fColorPickerUrl = FileLocator.toFileURL(FileLocator.find(bundle, path, null));
+ } catch (Exception e) {
+ logger.error("Failed to find a colorpicker", e);
+ }
+ return fColorPickerUrl.toString();
+ }
+
/**
* JavaScript Function Adaptor Class
* {@link http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet307.java}
}
}
-