-package org.simantics.utils.ui.workbench.dialogs;\r
-\r
-import java.io.IOException;\r
-import java.util.Deque;\r
-import java.util.LinkedList;\r
-\r
-import org.eclipse.core.runtime.preferences.InstanceScope;\r
-import org.eclipse.jface.layout.GridLayoutFactory;\r
-import org.eclipse.jface.preference.IPersistentPreferenceStore;\r
-import org.eclipse.jface.preference.IPreferenceStore;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.graphics.Point;\r
-import org.eclipse.swt.widgets.Button;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.Group;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.ui.IMemento;\r
-import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
-import org.simantics.utils.ui.color.Color;\r
-import org.simantics.utils.ui.workbench.StringMemento;\r
-\r
-/**\r
- * ColorDialog that keeps track used colors.\r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- *\r
- */\r
-public class ColorDialog extends org.simantics.utils.ui.color.ColorDialog{\r
- \r
- public ColorDialog(Shell parentShell) {\r
- super(parentShell);\r
- }\r
-\r
- \r
- protected Control createDialogArea(Composite parent) {\r
- initRecent();\r
- Composite composite = (Composite)super.createDialogArea(parent);\r
- Group recentGroup = new Group(composite, SWT.NONE);\r
- recentGroup.setText("Recent colors");\r
- GridLayoutFactory.fillDefaults().numColumns(8).spacing(2, 2).margins(4, 4).applyTo(recentGroup);\r
- int i = 0;\r
- for (Color c : recentColors) {\r
- i++;\r
- createColorButton(recentGroup, c);\r
- }\r
- org.eclipse.swt.graphics.Color c = Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);\r
- Color color = new Color(c.getRGB());\r
- for ( ; i < MAX_RECENT_COLORS; i++) {\r
- Button button = createColorButton(recentGroup, color);\r
- button.setEnabled(false);\r
- }\r
- return composite;\r
- }\r
- \r
- @Override\r
- protected Point getInitialSize() {\r
- return new Point(400, 450);\r
- }\r
- \r
- private static final int MAX_RECENT_COLORS = 8;\r
- \r
- public static final String RECENT_COLORS = "RECENT_COLORS";\r
-\r
- private static final String TAG_COLORS = "color";\r
- private static final String R_ATTR_NAME = "r";\r
- private static final String G_ATTR_NAME = "g";\r
- private static final String B_ATTR_NAME = "b";\r
- \r
- private static final String PLUGIN_ID = "org.simantics.utils.ui.workbench";\r
- \r
- protected Deque<Color> recentColors;\r
- \r
- private void initRecent() {\r
- IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);\r
- String recentString = store.getString(RECENT_COLORS);\r
- recentColors = decodeColors(recentString);\r
- }\r
- \r
- private void storeRecent() {\r
- Color color = getColor();\r
- recentColors.remove(color);\r
- recentColors.addFirst(color);\r
- while(recentColors.size() > MAX_RECENT_COLORS)\r
- recentColors.removeLast();\r
- \r
- IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);\r
- store.putValue(RECENT_COLORS, encodeColors(recentColors));\r
- \r
- if (store.needsSaving())\r
- try { \r
- store.save();\r
- } catch (IOException e) {\r
- ExceptionUtils.logError(e);\r
- }\r
- }\r
- \r
- @Override\r
- protected void okPressed() {\r
- storeRecent();\r
- super.okPressed();\r
- }\r
- \r
- public static Deque<Color> decodeColors(String recentColorsPref) {\r
- Deque<Color> result = new LinkedList<Color>();\r
- try {\r
- StringMemento sm = new StringMemento(recentColorsPref);\r
- for (IMemento m : sm.getChildren(TAG_COLORS)) {\r
- try {\r
- int r = m.getInteger(R_ATTR_NAME);\r
- int g = m.getInteger(G_ATTR_NAME);\r
- int b = m.getInteger(B_ATTR_NAME);\r
- Color c = new Color(r, g, b);\r
- result.add(c);\r
- } catch (Exception e) {\r
- \r
- }\r
- \r
- }\r
- } catch (IllegalArgumentException e) {\r
- }\r
- return result;\r
- }\r
- \r
- public static String encodeColors(Deque<Color> recentColors) {\r
- StringMemento sm = new StringMemento();\r
- for (Color c : recentColors) {\r
- IMemento m = sm.createChild(TAG_COLORS);\r
- m.putInteger(R_ATTR_NAME, c.getR());\r
- m.putInteger(G_ATTR_NAME, c.getG());\r
- m.putInteger(B_ATTR_NAME, c.getB());\r
- }\r
- return sm.toString();\r
- }\r
-}\r
+package org.simantics.utils.ui.workbench.dialogs;
+
+import java.io.IOException;
+import java.util.Deque;
+import java.util.LinkedList;
+
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.preference.IPersistentPreferenceStore;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IMemento;
+import org.eclipse.ui.preferences.ScopedPreferenceStore;
+import org.simantics.utils.ui.ExceptionUtils;
+import org.simantics.utils.ui.color.Color;
+import org.simantics.utils.ui.workbench.StringMemento;
+
+/**
+ * ColorDialog that keeps track used colors.
+ *
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+public class ColorDialog extends org.simantics.utils.ui.color.ColorDialog{
+
+ public ColorDialog(Shell parentShell) {
+ super(parentShell);
+ }
+
+
+ protected Control createDialogArea(Composite parent) {
+ initRecent();
+ Composite composite = (Composite)super.createDialogArea(parent);
+ Group recentGroup = new Group(composite, SWT.NONE);
+ recentGroup.setText("Recent colors");
+ GridLayoutFactory.fillDefaults().numColumns(8).spacing(2, 2).margins(4, 4).applyTo(recentGroup);
+ int i = 0;
+ for (Color c : recentColors) {
+ i++;
+ createColorButton(recentGroup, c);
+ }
+ org.eclipse.swt.graphics.Color c = Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
+ Color color = new Color(c.getRGB());
+ for ( ; i < MAX_RECENT_COLORS; i++) {
+ Button button = createColorButton(recentGroup, color);
+ button.setEnabled(false);
+ }
+ return composite;
+ }
+
+ @Override
+ protected Point getInitialSize() {
+ return new Point(400, 450);
+ }
+
+ private static final int MAX_RECENT_COLORS = 8;
+
+ public static final String RECENT_COLORS = "RECENT_COLORS";
+
+ private static final String TAG_COLORS = "color";
+ private static final String R_ATTR_NAME = "r";
+ private static final String G_ATTR_NAME = "g";
+ private static final String B_ATTR_NAME = "b";
+
+ private static final String PLUGIN_ID = "org.simantics.utils.ui.workbench";
+
+ protected Deque<Color> recentColors;
+
+ private void initRecent() {
+ IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
+ String recentString = store.getString(RECENT_COLORS);
+ recentColors = decodeColors(recentString);
+ }
+
+ private void storeRecent() {
+ Color color = getColor();
+ recentColors.remove(color);
+ recentColors.addFirst(color);
+ while(recentColors.size() > MAX_RECENT_COLORS)
+ recentColors.removeLast();
+
+ IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
+ store.putValue(RECENT_COLORS, encodeColors(recentColors));
+
+ if (store.needsSaving())
+ try {
+ store.save();
+ } catch (IOException e) {
+ ExceptionUtils.logError(e);
+ }
+ }
+
+ @Override
+ protected void okPressed() {
+ storeRecent();
+ super.okPressed();
+ }
+
+ public static Deque<Color> decodeColors(String recentColorsPref) {
+ Deque<Color> result = new LinkedList<Color>();
+ try {
+ StringMemento sm = new StringMemento(recentColorsPref);
+ for (IMemento m : sm.getChildren(TAG_COLORS)) {
+ try {
+ int r = m.getInteger(R_ATTR_NAME);
+ int g = m.getInteger(G_ATTR_NAME);
+ int b = m.getInteger(B_ATTR_NAME);
+ Color c = new Color(r, g, b);
+ result.add(c);
+ } catch (Exception e) {
+
+ }
+
+ }
+ } catch (IllegalArgumentException e) {
+ }
+ return result;
+ }
+
+ public static String encodeColors(Deque<Color> recentColors) {
+ StringMemento sm = new StringMemento();
+ for (Color c : recentColors) {
+ IMemento m = sm.createChild(TAG_COLORS);
+ m.putInteger(R_ATTR_NAME, c.getR());
+ m.putInteger(G_ATTR_NAME, c.getG());
+ m.putInteger(B_ATTR_NAME, c.getB());
+ }
+ return sm.toString();
+ }
+}