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