1 package org.simantics.document.swt.core.widget;
3 import java.util.Collection;
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;
24 public class TrackedTextWidget extends LeafWidgetManager<Text> {
27 protected void doUpdateProperties(final SWTDocument document, Text control, JSONObject object) {
29 if(control.isDisposed()) return;
31 TrackedText tt = (TrackedText)control.getData(TrackedText.TRACKED_TEXT_KEY);
33 String text = object.getJSONField("text");
35 final AbstractEventHandler onModify = object.getJSONField("onModify");
36 final StringInputValidator validator = object.getJSONField("validator");
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));
43 RGB.Integer foreground = object.getBeanJSONFieldDefault("foreground", RGB.Integer.BINDING, new RGB.Integer(0,0,0));
45 Font font = object.getBeanJSONFieldDefault("font", Font.BINDING, new Font("Arial",10,"Normal"));
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);
52 tt.setColorProvider(new ITrackedColorProvider() {
55 public Color getInvalidBackground() {
60 public Color getInactiveBackground() {
65 public Color getHoverBackground() {
70 public Color getEditingBackground() {
77 tt.setTextWithoutNotify(text);
79 System.err.println(this + " text == null");
80 // This applies coloring
83 tt.addModifyListener(new TrackedModifyListener() {
86 public void modifyText(TrackedModifyEvent e) {
87 CommandContextImpl parameters = new CommandContextImpl();
88 parameters.putString("text", e.getText());
89 document.post(onModify, parameters);
94 if(validator != null) {
95 tt.setInputValidator(new IInputValidator() {
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();
109 control.setFont(document.getFont(font));
110 control.setForeground(document.getColor(foreground));
115 protected Text doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
116 int style = SWT.NONE;
118 Boolean multiLine = object.getJSONFieldDefault("multiLine", false);
119 if(Boolean.TRUE.equals(multiLine))
122 Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
123 if(Boolean.TRUE.equals(vscroll))
124 style |= SWT.V_SCROLL;
126 Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
127 if(Boolean.TRUE.equals(hscroll))
128 style |= SWT.H_SCROLL;
130 TrackedText tt = new TrackedText(parent, style);
132 return tt.getWidget();