]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Text.java
(refs #7358) Initial 4.7 update commit
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / Text.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets;
13
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;
26
27 public class Text extends WidgetImpl {
28
29         private ReadFactory<?, String> textFactory;
30         
31         final private org.eclipse.swt.widgets.Text text;
32         
33         public Text(Composite parent, WidgetSupport support, int style) {
34                 super(support);
35                 text = new org.eclipse.swt.widgets.Text(parent, style);
36                 support.register(this);
37         }
38         
39         public void setTextFactory(ReadFactory<?, String> textFactory) {
40                 this.textFactory = textFactory;
41         }
42         
43         public org.eclipse.swt.widgets.Text getWidget() {
44                 return text;
45         }
46         
47         @Override
48         public Control getControl() {
49                 return text;
50         }
51
52         @Override
53         public void setInput(ISessionContext context, Object input) {
54
55                 if(textFactory != null) {
56                         textFactory.listen(context, input, new Listener<String>() {
57
58                                 public void exception(final Throwable t) {
59                                         SWTUtils.asyncExec(text, new Runnable() {
60
61                                                 @Override
62                                                 public void run() {
63                                                         if(isDisposed()) return;
64 //                                                      System.out.println("Button received new text: " + text);
65                                                         text.setText(t.toString());
66                                                 }
67
68                                         });
69                                 }
70
71                                 @Override
72                                 public void execute(final String s) {
73                                         SWTUtils.asyncExec(text, new Runnable() {
74
75                                                 @Override
76                                                 public void run() {
77                                                         if(isDisposed()) return;
78                                                         text.setText(s);
79                                                 }
80
81                                         });
82                                 }
83
84                                 @Override
85                                 public boolean isDisposed() {
86                                         return text.isDisposed();
87                                 }
88
89                         });
90                 
91                 }
92                 
93         }
94         
95         public void setText(String s) {
96                 text.setText(s);
97         }
98
99     public <T> void setText(final ParametrizedRead<T, String> read) {
100         
101         Simantics.getSession().async(new UniqueRead<String>() {
102
103                 @Override
104                 public String perform(ReadGraph graph) throws DatabaseException {
105                         T input = support.getInput(graph);
106                         return graph.syncRequest(read.get(input));
107                 }
108
109         }, new Listener<String>() {
110
111                 @Override
112                 public void exception(Throwable t) {
113                         t.printStackTrace();
114                 }
115
116                 @Override
117                 public void execute(final String _text) {
118
119                         if(isDisposed()) return;
120
121                         text.getDisplay().asyncExec(new Runnable() {
122
123                                 @Override
124                                 public void run() {
125                                         if(_text == null) text.setText("");
126                                         else text.setText(_text);
127                                 }
128
129                         });
130                 }
131
132                 @Override
133                 public boolean isDisposed() {
134                         return text.isDisposed();
135                 }
136
137         });
138
139     }
140         
141 }