--- /dev/null
+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