1 package org.simantics.utils.ui.workbench.dialogs;
3 import java.io.IOException;
4 import java.util.Deque;
5 import java.util.LinkedList;
7 import org.eclipse.core.runtime.preferences.InstanceScope;
8 import org.eclipse.jface.layout.GridLayoutFactory;
9 import org.eclipse.jface.preference.IPersistentPreferenceStore;
10 import org.eclipse.jface.preference.IPreferenceStore;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.graphics.Point;
13 import org.eclipse.swt.widgets.Button;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.swt.widgets.Group;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IMemento;
20 import org.eclipse.ui.preferences.ScopedPreferenceStore;
21 import org.simantics.utils.ui.ExceptionUtils;
22 import org.simantics.utils.ui.color.Color;
23 import org.simantics.utils.ui.workbench.StringMemento;
26 * ColorDialog that keeps track used colors.
28 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
31 public class ColorDialog extends org.simantics.utils.ui.color.ColorDialog{
33 public ColorDialog(Shell parentShell) {
38 protected Control createDialogArea(Composite parent) {
40 Composite composite = (Composite)super.createDialogArea(parent);
41 Group recentGroup = new Group(composite, SWT.NONE);
42 recentGroup.setText("Recent colors");
43 GridLayoutFactory.fillDefaults().numColumns(8).spacing(2, 2).margins(4, 4).applyTo(recentGroup);
45 for (Color c : recentColors) {
47 createColorButton(recentGroup, c);
49 org.eclipse.swt.graphics.Color c = Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
50 Color color = new Color(c.getRGB());
51 for ( ; i < MAX_RECENT_COLORS; i++) {
52 Button button = createColorButton(recentGroup, color);
53 button.setEnabled(false);
59 protected Point getInitialSize() {
60 return new Point(400, 450);
63 private static final int MAX_RECENT_COLORS = 8;
65 public static final String RECENT_COLORS = "RECENT_COLORS";
67 private static final String TAG_COLORS = "color";
68 private static final String R_ATTR_NAME = "r";
69 private static final String G_ATTR_NAME = "g";
70 private static final String B_ATTR_NAME = "b";
72 private static final String PLUGIN_ID = "org.simantics.utils.ui.workbench";
74 protected Deque<Color> recentColors;
76 private void initRecent() {
77 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
78 String recentString = store.getString(RECENT_COLORS);
79 recentColors = decodeColors(recentString);
82 private void storeRecent() {
83 Color color = getColor();
84 recentColors.remove(color);
85 recentColors.addFirst(color);
86 while(recentColors.size() > MAX_RECENT_COLORS)
87 recentColors.removeLast();
89 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
90 store.putValue(RECENT_COLORS, encodeColors(recentColors));
92 if (store.needsSaving())
95 } catch (IOException e) {
96 ExceptionUtils.logError(e);
101 protected void okPressed() {
106 public static Deque<Color> decodeColors(String recentColorsPref) {
107 Deque<Color> result = new LinkedList<Color>();
109 StringMemento sm = new StringMemento(recentColorsPref);
110 for (IMemento m : sm.getChildren(TAG_COLORS)) {
112 int r = m.getInteger(R_ATTR_NAME);
113 int g = m.getInteger(G_ATTR_NAME);
114 int b = m.getInteger(B_ATTR_NAME);
115 Color c = new Color(r, g, b);
117 } catch (Exception e) {
122 } catch (IllegalArgumentException e) {
127 public static String encodeColors(Deque<Color> recentColors) {
128 StringMemento sm = new StringMemento();
129 for (Color c : recentColors) {
130 IMemento m = sm.createChild(TAG_COLORS);
131 m.putInteger(R_ATTR_NAME, c.getR());
132 m.putInteger(G_ATTR_NAME, c.getG());
133 m.putInteger(B_ATTR_NAME, c.getB());
135 return sm.toString();