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