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