X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.swt.core%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fswt%2Fcore%2Fwidget%2FTrackedTextWidget.java;fp=bundles%2Forg.simantics.document.swt.core%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fswt%2Fcore%2Fwidget%2FTrackedTextWidget.java;h=4aa9d7c44f5f40baec4764d796bd9320118d07ed;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/TrackedTextWidget.java b/bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/TrackedTextWidget.java new file mode 100644 index 000000000..4aa9d7c44 --- /dev/null +++ b/bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/TrackedTextWidget.java @@ -0,0 +1,135 @@ +package org.simantics.document.swt.core.widget; + +import java.util.Collection; + +import org.eclipse.jface.dialogs.IInputValidator; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; +import org.simantics.datatypes.literal.Font; +import org.simantics.datatypes.literal.RGB; +import org.simantics.document.server.JSONObject; +import org.simantics.document.server.handler.AbstractEventHandler; +import org.simantics.document.server.io.CommandContextImpl; +import org.simantics.document.swt.core.SWTDocument; +import org.simantics.document.swt.core.base.LeafWidgetManager; +import org.simantics.utils.strings.StringInputProblem; +import org.simantics.utils.strings.StringInputValidator; +import org.simantics.utils.ui.widgets.ITrackedColorProvider; +import org.simantics.utils.ui.widgets.TrackedModifyEvent; +import org.simantics.utils.ui.widgets.TrackedModifyListener; +import org.simantics.utils.ui.widgets.TrackedText; + +public class TrackedTextWidget extends LeafWidgetManager { + + @Override + protected void doUpdateProperties(final SWTDocument document, Text control, JSONObject object) { + + if(control.isDisposed()) return; + + TrackedText tt = (TrackedText)control.getData(TrackedText.TRACKED_TEXT_KEY); + + String text = object.getJSONField("text"); + + final AbstractEventHandler onModify = object.getJSONField("onModify"); + final StringInputValidator validator = object.getJSONField("validator"); + + RGB.Integer invalidBackground = object.getBeanJSONFieldDefault("invalidBackground", RGB.Integer.BINDING, new RGB.Integer(255,255,255)); + RGB.Integer inactiveBackground = object.getBeanJSONFieldDefault("inactiveBackground", RGB.Integer.BINDING, new RGB.Integer(255,255,255)); + RGB.Integer hoverBackground = object.getBeanJSONFieldDefault("hoverBackground", RGB.Integer.BINDING, new RGB.Integer(255,255,255)); + RGB.Integer editingBackground = object.getBeanJSONFieldDefault("editingBackground", RGB.Integer.BINDING, new RGB.Integer(255,255,255)); + + RGB.Integer foreground = object.getBeanJSONFieldDefault("foreground", RGB.Integer.BINDING, new RGB.Integer(0,0,0)); + + Font font = object.getBeanJSONFieldDefault("font", Font.BINDING, new Font("Arial",10,"Normal")); + + final Color invalid = document.getColor(invalidBackground); + final Color inactive = document.getColor(inactiveBackground); + final Color hover = document.getColor(hoverBackground); + final Color editing = document.getColor(editingBackground); + + tt.setColorProvider(new ITrackedColorProvider() { + + @Override + public Color getInvalidBackground() { + return invalid; + } + + @Override + public Color getInactiveBackground() { + return inactive; + } + + @Override + public Color getHoverBackground() { + return hover; + } + + @Override + public Color getEditingBackground() { + return editing; + } + + }); + + if(text != null) + tt.setTextWithoutNotify(text); + else + System.err.println(this + " text == null"); + // This applies coloring + tt.setEditable(true); + + tt.addModifyListener(new TrackedModifyListener() { + + @Override + public void modifyText(TrackedModifyEvent e) { + CommandContextImpl parameters = new CommandContextImpl(); + parameters.putString("text", e.getText()); + document.post(onModify, parameters); + } + + }); + + if(validator != null) { + tt.setInputValidator(new IInputValidator() { + + @Override + public String isValid(String newText) { + Collection problems = validator.validate(newText); + if(problems.isEmpty()) return null; + else if (problems.size() == 1) { + return problems.iterator().next().getDescription(); + } else return problems.toString(); + } + + }); + } + + control.setFont(document.getFont(font)); + control.setForeground(document.getColor(foreground)); + + } + + @Override + protected Text doCreateControl(SWTDocument document, Composite parent, JSONObject object) { + int style = SWT.NONE; + + Boolean multiLine = object.getJSONFieldDefault("multiLine", false); + if(Boolean.TRUE.equals(multiLine)) + style |= SWT.MULTI; + + Boolean vscroll = object.getJSONFieldDefault("VScroll", false); + if(Boolean.TRUE.equals(vscroll)) + style |= SWT.V_SCROLL; + + Boolean hscroll = object.getJSONFieldDefault("HScroll", false); + if(Boolean.TRUE.equals(hscroll)) + style |= SWT.H_SCROLL; + + TrackedText tt = new TrackedText(parent, style); + + return tt.getWidget(); + } + +}