1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.simantics.Simantics;
17 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;
18 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.common.request.ParametrizedRead;
21 import org.simantics.db.common.request.UniqueRead;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.management.ISessionContext;
24 import org.simantics.db.procedure.Listener;
25 import org.simantics.utils.ui.SWTUtils;
27 public class Text extends WidgetImpl {
29 private ReadFactory<?, String> textFactory;
31 final private org.eclipse.swt.widgets.Text text;
33 public Text(Composite parent, WidgetSupport support, int style) {
35 text = new org.eclipse.swt.widgets.Text(parent, style);
36 support.register(this);
39 public void setTextFactory(ReadFactory<?, String> textFactory) {
40 this.textFactory = textFactory;
43 public org.eclipse.swt.widgets.Text getWidget() {
48 public Control getControl() {
53 public void setInput(ISessionContext context, Object input) {
55 if(textFactory != null) {
56 textFactory.listen(context, input, new Listener<String>() {
58 public void exception(final Throwable t) {
59 SWTUtils.asyncExec(text, new Runnable() {
63 if(isDisposed()) return;
64 // System.out.println("Button received new text: " + text);
65 text.setText(t.toString());
72 public void execute(final String s) {
73 SWTUtils.asyncExec(text, new Runnable() {
77 if(isDisposed()) return;
85 public boolean isDisposed() {
86 return text.isDisposed();
95 public void setText(String s) {
99 public <T> void setText(final ParametrizedRead<T, String> read) {
101 Simantics.getSession().async(new UniqueRead<String>() {
104 public String perform(ReadGraph graph) throws DatabaseException {
105 T input = support.getInput(graph);
106 return graph.syncRequest(read.get(input));
109 }, new Listener<String>() {
112 public void exception(Throwable t) {
117 public void execute(final String _text) {
119 if(isDisposed()) return;
121 text.getDisplay().asyncExec(new Runnable() {
125 if(_text == null) text.setText("");
126 else text.setText(_text);
133 public boolean isDisposed() {
134 return text.isDisposed();