]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Label.java
(refs #7358) Initial 4.7 update commit
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / Label.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.graphics.Color;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17 import org.simantics.Simantics;
18 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;
19 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.common.request.ParametrizedRead;
22 import org.simantics.db.common.request.UniqueRead;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.management.ISessionContext;
25 import org.simantics.db.procedure.Listener;
26 import org.simantics.utils.ui.SWTUtils;
27
28 public class Label extends WidgetImpl {
29
30         private ReadFactory<?, String> textFactory;
31         private ReadFactory<?, Color> colorFactory;
32         
33         final private org.eclipse.swt.widgets.Label label;
34         
35         public Label(Composite parent, WidgetSupport support, int style) {
36                 super(support);
37                 label = new org.eclipse.swt.widgets.Label(parent, style);
38                 support.register(this);
39         }
40         
41         public void setTextFactory(ReadFactory<?, String> textFactory) {
42                 this.textFactory = textFactory;
43         }
44         
45         public void setForegroundFactory(ReadFactory<?, Color> colorFactory) {
46                 this.colorFactory = colorFactory;
47         }
48
49         public org.eclipse.swt.widgets.Label getWidget() {
50                 return label;
51         }
52         
53         @Override
54         public Control getControl() {
55                 return label;
56         }
57
58         @Override
59         public void setInput(ISessionContext context, Object input) {
60
61                 if(textFactory != null) {
62                         textFactory.listen(context, input, new Listener<String>() {
63
64                                 public void exception(final Throwable t) {
65                                         SWTUtils.asyncExec(label, new Runnable() {
66
67                                                 @Override
68                                                 public void run() {
69                                                         if(isDisposed()) return;
70 //                                                      System.out.println("Button received new text: " + text);
71                                                         label.setText(t.toString());
72                                                 }
73
74                                         });
75                                 }
76
77                                 @Override
78                                 public void execute(final String text) {
79                                         SWTUtils.asyncExec(label, new Runnable() {
80
81                                                 @Override
82                                                 public void run() {
83                                                         if(isDisposed()) return;
84                                                         label.setText(text);
85                                                         // TODO: how can we resize without this knife??
86                                                         label.getParent().layout();
87                                                         label.getParent().getParent().layout();
88                                                 }
89
90                                         });
91                                 }
92
93                                 @Override
94                                 public boolean isDisposed() {
95                                         return label.isDisposed();
96                                 }
97
98                         });
99                 }
100                 
101                 if(colorFactory != null) {
102                         colorFactory.listen(context, input, new Listener<Color>() {
103
104                                 @Override
105                                 public void exception(Throwable t) {
106                                         t.printStackTrace();
107                                 }
108
109                                 @Override
110                                 public void execute(final Color color) {
111                                         label.getDisplay().asyncExec(new Runnable() {
112
113                                                 @Override
114                                                 public void run() {
115                                                         if(isDisposed()) return;
116                                                         label.setForeground(color);
117                                                 }
118
119                                         });
120                                 }
121
122                                 @Override
123                                 public boolean isDisposed() {
124                                         return label.isDisposed();
125                                 }
126
127                         });
128                 }
129                 
130         }
131         
132     public <T> void setText(final ParametrizedRead<T, String> read) {
133         
134         Simantics.getSession().async(new UniqueRead<String>() {
135
136                 @Override
137                 public String perform(ReadGraph graph) throws DatabaseException {
138                         T input = support.getInput(graph);
139                         return graph.syncRequest(read.get(input));
140                 }
141
142         }, new Listener<String>() {
143
144                 @Override
145                 public void exception(Throwable t) {
146                         t.printStackTrace();
147                 }
148
149                 @Override
150                 public void execute(final String text) {
151
152                         if(isDisposed()) return;
153
154                         label.getDisplay().asyncExec(new Runnable() {
155
156                                 @Override
157                                 public void run() {
158                                         label.setText(text);
159                                 }
160
161                         });
162                 }
163
164                 @Override
165                 public boolean isDisposed() {
166                         return label.isDisposed();
167                 }
168
169         });
170
171     }
172         
173         public void setText(String text) {
174                 label.setText(text);
175         }
176
177 }