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