]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/TrackedTextWidget.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / TrackedTextWidget.java
1 package org.simantics.document.swt.core.widget;
2
3 import java.util.Collection;
4
5 import org.eclipse.jface.dialogs.IInputValidator;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.graphics.Color;
8 import org.eclipse.swt.widgets.Composite;
9 import org.eclipse.swt.widgets.Text;
10 import org.simantics.datatypes.literal.Font;
11 import org.simantics.datatypes.literal.RGB;
12 import org.simantics.document.server.JSONObject;
13 import org.simantics.document.server.handler.AbstractEventHandler;
14 import org.simantics.document.server.io.CommandContextImpl;
15 import org.simantics.document.swt.core.SWTDocument;
16 import org.simantics.document.swt.core.base.LeafWidgetManager;
17 import org.simantics.utils.strings.StringInputProblem;
18 import org.simantics.utils.strings.StringInputValidator;
19 import org.simantics.utils.ui.widgets.ITrackedColorProvider;
20 import org.simantics.utils.ui.widgets.TrackedModifyEvent;
21 import org.simantics.utils.ui.widgets.TrackedModifyListener;
22 import org.simantics.utils.ui.widgets.TrackedText;
23
24 public class TrackedTextWidget extends LeafWidgetManager<Text> {
25
26         @Override
27         protected void doUpdateProperties(final SWTDocument document, Text control, JSONObject object) {
28
29                 if(control.isDisposed()) return;
30                 
31                 TrackedText tt = (TrackedText)control.getData(TrackedText.TRACKED_TEXT_KEY);
32                 
33                 String text = object.getJSONField("text");
34
35                 final AbstractEventHandler onModify = object.getJSONField("onModify");
36                 final StringInputValidator validator = object.getJSONField("validator");
37                 
38                 RGB.Integer invalidBackground = object.getBeanJSONFieldDefault("invalidBackground", RGB.Integer.BINDING, new RGB.Integer(255,255,255));
39                 RGB.Integer inactiveBackground = object.getBeanJSONFieldDefault("inactiveBackground", RGB.Integer.BINDING, new RGB.Integer(255,255,255));
40                 RGB.Integer hoverBackground = object.getBeanJSONFieldDefault("hoverBackground", RGB.Integer.BINDING, new RGB.Integer(255,255,255));
41                 RGB.Integer editingBackground = object.getBeanJSONFieldDefault("editingBackground", RGB.Integer.BINDING, new RGB.Integer(255,255,255));
42
43                 RGB.Integer foreground = object.getBeanJSONFieldDefault("foreground", RGB.Integer.BINDING, new RGB.Integer(0,0,0));
44                 
45                 Font font = object.getBeanJSONFieldDefault("font", Font.BINDING, new Font("Arial",10,"Normal"));
46                 
47                 final Color invalid = document.getColor(invalidBackground);
48                 final Color inactive = document.getColor(inactiveBackground);
49                 final Color hover = document.getColor(hoverBackground);
50                 final Color editing = document.getColor(editingBackground);
51                 
52                 tt.setColorProvider(new ITrackedColorProvider() {
53                         
54                         @Override
55                         public Color getInvalidBackground() {
56                                 return invalid;
57                         }
58                         
59                         @Override
60                         public Color getInactiveBackground() {
61                                 return inactive;
62                         }
63                         
64                         @Override
65                         public Color getHoverBackground() {
66                                 return hover;
67                         }
68                         
69                         @Override
70                         public Color getEditingBackground() {
71                                 return editing;
72                         }
73                         
74                 });
75                 
76                 if(text != null)
77                     tt.setTextWithoutNotify(text);
78                 else
79                     System.err.println(this + " text == null");
80                 // This applies coloring
81                 tt.setEditable(true);
82                 
83                 tt.addModifyListener(new TrackedModifyListener() {
84                         
85                         @Override
86                         public void modifyText(TrackedModifyEvent e) {
87                 CommandContextImpl parameters = new CommandContextImpl();
88                 parameters.putString("text", e.getText());
89                                 document.post(onModify, parameters);
90                         }
91                         
92                 });
93                 
94                 if(validator != null) {
95                         tt.setInputValidator(new IInputValidator() {
96
97                                 @Override
98                                 public String isValid(String newText) {
99                                         Collection<StringInputProblem> problems = validator.validate(newText);
100                                         if(problems.isEmpty()) return null;
101                                         else if (problems.size() == 1) {
102                                                 return problems.iterator().next().getDescription();
103                                         } else return problems.toString();
104                                 }
105
106                         });
107                 }
108                 
109                 control.setFont(document.getFont(font));
110                 control.setForeground(document.getColor(foreground));
111                 
112         }
113
114         @Override
115         protected Text doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
116         int style = SWT.NONE;
117
118         Boolean multiLine = object.getJSONFieldDefault("multiLine", false);
119         if(Boolean.TRUE.equals(multiLine))
120             style |= SWT.MULTI;
121         
122         Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
123         if(Boolean.TRUE.equals(vscroll))
124             style |= SWT.V_SCROLL;
125         
126         Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
127         if(Boolean.TRUE.equals(hscroll))
128             style |= SWT.H_SCROLL;
129         
130                 TrackedText tt = new TrackedText(parent, style);
131
132                 return tt.getWidget();
133         }
134         
135 }