summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchanghyun1.lee <changhyun1.lee@samsung.com>2013-09-12 16:14:24 +0900
committerchanghyun1.lee <changhyun1.lee@samsung.com>2013-09-12 17:18:27 +0900
commit982f979f7d0b69f6a980ca2535a38f3ec48f5ce6 (patch)
treeb711607443f8f651bdf40a1e206593546c586377
parent3d8089e9ec8b43d2bcead080701f27a39f8c8030 (diff)
downloadcommon-eplugin-982f979f7d0b69f6a980ca2535a38f3ec48f5ce6.tar.gz
common-eplugin-982f979f7d0b69f6a980ca2535a38f3ec48f5ce6.tar.bz2
common-eplugin-982f979f7d0b69f6a980ca2535a38f3ec48f5ce6.zip
[Title] Refactoring color picker dialog
[Desc.] [Issue] Redmine-9932 Change-Id: I38b16d15b2a0aadb321cf25c98a1f79b505150b5
-rwxr-xr-xorg.tizen.common.feature/feature.xml4
-rw-r--r--org.tizen.common/src/org/tizen/common/ui/CSSColorNames.java115
-rw-r--r--org.tizen.common/src/org/tizen/common/ui/dialog/ColorPickerDialog.java66
3 files changed, 146 insertions, 39 deletions
diff --git a/org.tizen.common.feature/feature.xml b/org.tizen.common.feature/feature.xml
index 46ea31f93..e923213ec 100755
--- a/org.tizen.common.feature/feature.xml
+++ b/org.tizen.common.feature/feature.xml
@@ -18,8 +18,7 @@
id="org.tizen.common"
download-size="0"
install-size="0"
- version="0.0.0"
- unpack="false"/>
+ version="0.0.0"/>
<plugin
id="org.tizen.common.connection"
@@ -90,4 +89,5 @@
install-size="0"
version="0.0.0"
unpack="false"/>
+
</feature>
diff --git a/org.tizen.common/src/org/tizen/common/ui/CSSColorNames.java b/org.tizen.common/src/org/tizen/common/ui/CSSColorNames.java
new file mode 100644
index 000000000..f8e61d06e
--- /dev/null
+++ b/org.tizen.common/src/org/tizen/common/ui/CSSColorNames.java
@@ -0,0 +1,115 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.tizen.common.ui;
+
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.xml.parsers.SAXParserFactory;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.swt.graphics.RGB;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.tizen.common.CommonPlugin;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * Associates css color names to the appropriate {@link RGB} values.
+ * These associations are defined in org.eclipse.wst.css.ui/csscolors/extended-color-mapping.xml
+ *
+ */
+public class CSSColorNames {
+
+ protected final Logger logger = LoggerFactory.getLogger(CSSColorNames.class);
+
+ private static CSSColorNames instance;
+ private static final Map<String, RGB> colors = new HashMap<String, RGB>();
+ protected static final String COLOR_MAPPING_XML = "resources/csscolors/extended-color-mapping.xml";
+
+ private CSSColorNames() {
+ try {
+ URL url = FileLocator.find(CommonPlugin.getDefault().getBundle(), new Path(COLOR_MAPPING_XML), null);
+ final XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
+ xmlReader.setContentHandler(new ColorMappingHandler());
+ xmlReader.parse(new InputSource(url.openStream()));
+ } catch (Exception e) {
+ logger.error("Failed to read a color mapping file", e);
+ }
+ }
+
+ public static synchronized CSSColorNames getInstance() {
+ if (instance == null) {
+ instance = new CSSColorNames();
+ }
+ return instance;
+ }
+
+ /**
+ * Returns the {@link RGB} value associated with this color name.
+ * @param name the color name
+ * @return {@link RGB} associated with <code>name</code>, null if it is an unknown name or invalid RGB value
+ */
+ public RGB getRGB(String name) {
+ return (RGB) colors.get(name);
+ }
+
+ class ColorMappingHandler extends DefaultHandler {
+ private final String COLOR_ELEM = "color"; //$NON-NLS-1$
+ private final String NAME_ATTR = "name"; //$NON-NLS-1$
+ private final String RGB_ATTR = "rgb"; //$NON-NLS-1$
+
+ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
+ if (COLOR_ELEM.equals(qName)) {
+ final String name = attributes.getValue(NAME_ATTR);
+ final String rgb = attributes.getValue(RGB_ATTR);
+ if (name != null && rgb != null) {
+ final RGB rgbValue = getRGB(rgb);
+ if (rgbValue != null) {
+ colors.put(name, rgbValue);
+ }
+ }
+ }
+ }
+
+ /**
+ * Converts an rgb string into an {@link RGB}
+ * @param rgb the color string
+ * @return an {@link RGB} if one can be created from the string; otherwise, null
+ */
+ private RGB getRGB(String rgb) {
+ final StringTokenizer tokenizer = new StringTokenizer(rgb, ","); //$NON-NLS-1$
+ int[] weights = new int[3];
+ for (int i = 0; tokenizer.hasMoreTokens(); i++) {
+ if (i > 2) {
+ return null;
+ }
+ try {
+ weights[i] = Integer.parseInt(tokenizer.nextToken().trim());
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ if (weights[i] > 255 || weights[i] < 0) {
+ return null;
+ }
+ }
+ return new RGB(weights[0], weights[1], weights[2]);
+ }
+ }
+
+}
diff --git a/org.tizen.common/src/org/tizen/common/ui/dialog/ColorPickerDialog.java b/org.tizen.common/src/org/tizen/common/ui/dialog/ColorPickerDialog.java
index 61dc2a94a..6e916ceb2 100644
--- a/org.tizen.common/src/org/tizen/common/ui/dialog/ColorPickerDialog.java
+++ b/org.tizen.common/src/org/tizen/common/ui/dialog/ColorPickerDialog.java
@@ -37,11 +37,7 @@ import org.eclipse.jface.dialogs.IDialogConstants;
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;
@@ -115,44 +111,15 @@ public class ColorPickerDialog extends Dialog {
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);
@@ -166,6 +133,32 @@ public class ColorPickerDialog extends Dialog {
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}
@@ -194,4 +187,3 @@ public class ColorPickerDialog extends Dialog {
}
}
-