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