]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui.workbench/src/org/simantics/utils/ui/workbench/dialogs/ColorDialog.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.utils.ui.workbench / src / org / simantics / utils / ui / workbench / dialogs / ColorDialog.java
1 package org.simantics.utils.ui.workbench.dialogs;
2
3 import java.io.IOException;
4 import java.util.Deque;
5 import java.util.LinkedList;
6
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;
24
25 /**
26  * ColorDialog that keeps track used colors.
27  * 
28  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
29  *
30  */
31 public class ColorDialog extends org.simantics.utils.ui.color.ColorDialog{
32         
33         public ColorDialog(Shell parentShell) {
34                 super(parentShell);
35         }
36
37     
38     protected Control createDialogArea(Composite parent) {
39         initRecent();
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);
44                 int i = 0;
45                 for (Color c : recentColors) {
46                         i++;
47                         createColorButton(recentGroup, c);
48                 }
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);
54                 }
55                 return composite;
56     }
57     
58     @Override
59     protected Point getInitialSize() {
60         return new Point(400, 450);
61     }
62     
63         private static final int MAX_RECENT_COLORS = 8;
64         
65         public static final String  RECENT_COLORS = "RECENT_COLORS";
66
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";
71     
72     private static final String PLUGIN_ID = "org.simantics.utils.ui.workbench";
73     
74     protected Deque<Color> recentColors;
75     
76     private void initRecent() {
77          IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
78          String recentString = store.getString(RECENT_COLORS);
79          recentColors = decodeColors(recentString);
80     }
81     
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();
88         
89         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PLUGIN_ID);
90         store.putValue(RECENT_COLORS, encodeColors(recentColors));
91         
92         if (store.needsSaving())
93                 try { 
94                         store.save();
95                 } catch (IOException e) {
96                         ExceptionUtils.logError(e);
97                 }
98     }
99     
100     @Override
101         protected void okPressed() {
102                 storeRecent();
103                 super.okPressed();
104         }
105     
106     public static Deque<Color> decodeColors(String recentColorsPref) {
107         Deque<Color> result = new LinkedList<Color>();
108         try {
109             StringMemento sm = new StringMemento(recentColorsPref);
110             for (IMemento m : sm.getChildren(TAG_COLORS)) {
111                 try {
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);
116                         result.add(c);
117                 } catch (Exception e) {
118                         
119                 }
120                 
121             }
122         } catch (IllegalArgumentException e) {
123         }
124         return result;
125     }
126     
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());
134         }
135         return sm.toString();
136     }
137 }