1 package org.simantics.views.swt.client.impl;
\r
3 import java.util.ArrayList;
\r
5 import org.eclipse.jface.dialogs.IInputValidator;
\r
6 import org.eclipse.swt.custom.StyledText;
\r
7 import org.eclipse.swt.graphics.Color;
\r
8 import org.eclipse.swt.widgets.Composite;
\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.TrackedStyledText;
\r
16 import org.simantics.views.swt.client.base.SingleSWTViewNode;
\r
18 public class SWTTrackedStyledText extends SingleSWTViewNode<StyledText> {
\r
20 private static final RGB.Integer WHITE = new RGB.Integer(255, 255, 255);
\r
22 private static final long serialVersionUID = 7932335224632082902L;
\r
24 private TrackedStyledText tt;
\r
26 protected Color invalidBackgroundColor;
\r
27 protected Color inactiveBackgroundColor;
\r
28 protected Color hoverBackgroundColor;
\r
29 protected Color editingBackgroundColor;
\r
31 public Function1<String, String> modifier;
\r
32 public Function1<String, String> validator;
\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
39 protected ArrayList<TrackedModifyListener> listeners = new ArrayList<TrackedModifyListener>();
\r
41 public void addModifyListener(TrackedModifyListener listener) {
\r
42 this.listeners.add(listener);
\r
46 public void createControls(Composite parent) {
\r
48 tt = new TrackedStyledText(parent, style);
\r
49 control = tt.getWidget();
\r
53 tt.setColorProvider(new ITrackedColorProvider() {
\r
56 public Color getInvalidBackground() {
\r
57 return invalidBackgroundColor;
\r
61 public Color getInactiveBackground() {
\r
62 return inactiveBackgroundColor;
\r
66 public Color getHoverBackground() {
\r
67 return hoverBackgroundColor;
\r
71 public Color getEditingBackground() {
\r
72 return editingBackgroundColor;
\r
77 tt.setInputValidator(new IInputValidator() {
\r
80 public String isValid(String newText) {
\r
81 if(validator != null) return validator.apply(newText);
\r
87 tt.addModifyListener(new TrackedModifyListener() {
\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
101 public void synchronizeText(String text) {
\r
102 if(text != null) control.setText(text);
\r
105 final public void synchronizeInvalidBackground(RGB.Integer invalidBackground) {
\r
106 if(invalidBackground != null) invalidBackgroundColor = Colors.swt(control.getDisplay(), invalidBackground);
\r
109 final public void synchronizeInactiveBackground(RGB.Integer inactiveBackground) {
\r
110 if(inactiveBackground != null) inactiveBackgroundColor = Colors.swt(control.getDisplay(), inactiveBackground);
\r
113 final public void synchronizeHoverBackground(RGB.Integer hoverBackground) {
\r
114 if(hoverBackground != null) hoverBackgroundColor = Colors.swt(control.getDisplay(), hoverBackground);
\r
117 final public void synchronizeEditingBackground(RGB.Integer editingBackground) {
\r
118 if(editingBackground != null) editingBackgroundColor = Colors.swt(control.getDisplay(), editingBackground);
\r
121 final public void synchronizeValidator(Function1<String, String> validator) {
\r
125 final public void synchronizeModifier(Function1<String, String> modifier) {
\r