]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/impl/SWTTrackedText.java
Several Wiki documentation view improvements.
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / impl / SWTTrackedText.java
1 package org.simantics.views.swt.client.impl;\r
2 \r
3 import java.util.ArrayList;\r
4 \r
5 import org.eclipse.jface.dialogs.IInputValidator;\r
6 import org.eclipse.swt.graphics.Color;\r
7 import org.eclipse.swt.widgets.Composite;\r
8 import org.eclipse.swt.widgets.Text;\r
9 import org.simantics.datatypes.literal.RGB;\r
10 import org.simantics.scl.runtime.function.Function1;\r
11 import org.simantics.ui.colors.Colors;\r
12 import org.simantics.utils.ui.widgets.ITrackedColorProvider;\r
13 import org.simantics.utils.ui.widgets.TrackedModifyEvent;\r
14 import org.simantics.utils.ui.widgets.TrackedModifyListener;\r
15 import org.simantics.utils.ui.widgets.TrackedText;\r
16 import org.simantics.views.swt.client.base.SingleSWTViewNode;\r
17 \r
18 public class SWTTrackedText extends SingleSWTViewNode<Text> {\r
19         \r
20         private static final RGB.Integer WHITE = new RGB.Integer(255, 255, 255);\r
21         \r
22         private static final long serialVersionUID = 7932335224632082902L;\r
23         \r
24         private TrackedText tt;\r
25         \r
26         protected Color invalidBackgroundColor;\r
27         protected Color inactiveBackgroundColor;\r
28         protected Color hoverBackgroundColor;\r
29         protected Color editingBackgroundColor;\r
30 \r
31         public Function1<String, String> modifier;\r
32         public Function1<String, String> validator;\r
33         \r
34         public RGB.Integer invalidBackground = WHITE;\r
35         public RGB.Integer inactiveBackground = WHITE;\r
36         public RGB.Integer hoverBackground = WHITE;\r
37         public RGB.Integer editingBackground = WHITE;\r
38 \r
39         protected ArrayList<TrackedModifyListener> listeners = new ArrayList<TrackedModifyListener>();\r
40         \r
41         public void addModifyListener(TrackedModifyListener listener) {\r
42                 this.listeners.add(listener);\r
43         }\r
44         \r
45         @Override\r
46         public void createControls(Composite parent) {\r
47                 \r
48                 tt = new TrackedText(parent, style);\r
49                 control = tt.getWidget();\r
50 \r
51                 setProperties();\r
52                 \r
53                 tt.setColorProvider(new ITrackedColorProvider() {\r
54                         \r
55                         @Override\r
56                         public Color getInvalidBackground() {\r
57                                 return invalidBackgroundColor;\r
58                         }\r
59                         \r
60                         @Override\r
61                         public Color getInactiveBackground() {\r
62                                 return inactiveBackgroundColor;\r
63                         }\r
64                         \r
65                         @Override\r
66                         public Color getHoverBackground() {\r
67                                 return hoverBackgroundColor;\r
68                         }\r
69                         \r
70                         @Override\r
71                         public Color getEditingBackground() {\r
72                                 return editingBackgroundColor;\r
73                         }\r
74                         \r
75                 });\r
76                 \r
77                 tt.setInputValidator(new IInputValidator() {\r
78                         \r
79                         @Override\r
80                         public String isValid(String newText) {\r
81                                 if(validator != null) return validator.apply(newText);\r
82                                 else return null;\r
83                         }\r
84                         \r
85                 });\r
86                 \r
87                 tt.addModifyListener(new TrackedModifyListener() {\r
88                         \r
89                         @Override\r
90                         public void modifyText(TrackedModifyEvent e) {\r
91                                 if(modifier != null)\r
92                                         modifier.apply(e.getText());\r
93                                 for(TrackedModifyListener listener : listeners) listener.modifyText(e);\r
94                         }\r
95                         \r
96                 });\r
97                 \r
98         }\r
99 \r
100         @Override\r
101         public void synchronizeText(String text) {\r
102                 if(text != null) control.setText(text);\r
103         }\r
104         \r
105         final public void synchronizeInvalidBackground(RGB.Integer invalidBackground) {\r
106                 if(invalidBackground != null) invalidBackgroundColor = Colors.swt(control.getDisplay(), invalidBackground);\r
107         }\r
108 \r
109         final public void synchronizeInactiveBackground(RGB.Integer inactiveBackground) {\r
110                 if(inactiveBackground != null) inactiveBackgroundColor = Colors.swt(control.getDisplay(), inactiveBackground);\r
111         }\r
112 \r
113         final public void synchronizeHoverBackground(RGB.Integer hoverBackground) {\r
114                 if(hoverBackground != null) hoverBackgroundColor = Colors.swt(control.getDisplay(), hoverBackground);\r
115         }\r
116 \r
117         final public void synchronizeEditingBackground(RGB.Integer editingBackground) {\r
118                 if(editingBackground != null) editingBackgroundColor = Colors.swt(control.getDisplay(), editingBackground);\r
119         }\r
120         \r
121         final public void synchronizeValidator(Function1<String, String> validator) {\r
122                 \r
123         }\r
124 \r
125         final public void synchronizeModifier(Function1<String, String> modifier) {\r
126                 \r
127         }\r
128         \r
129 }\r