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.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
28 public class Label extends WidgetImpl {
\r
30 private ReadFactory<?, String> textFactory;
\r
31 private ReadFactory<?, Color> colorFactory;
\r
33 final private org.eclipse.swt.widgets.Label label;
\r
35 public Label(Composite parent, WidgetSupport support, int style) {
\r
37 label = new org.eclipse.swt.widgets.Label(parent, style);
\r
38 support.register(this);
\r
41 public void setTextFactory(ReadFactory<?, String> textFactory) {
\r
42 this.textFactory = textFactory;
\r
45 public void setForegroundFactory(ReadFactory<?, Color> colorFactory) {
\r
46 this.colorFactory = colorFactory;
\r
49 public org.eclipse.swt.widgets.Label getWidget() {
\r
54 public Control getControl() {
\r
59 public void setInput(ISessionContext context, Object input) {
\r
61 if(textFactory != null) {
\r
62 textFactory.listen(context, input, new Listener<String>() {
\r
64 public void exception(final Throwable t) {
\r
65 SWTUtils.asyncExec(label, new Runnable() {
\r
69 if(isDisposed()) return;
\r
70 // System.out.println("Button received new text: " + text);
\r
71 label.setText(t.toString());
\r
78 public void execute(final String text) {
\r
79 SWTUtils.asyncExec(label, new Runnable() {
\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
94 public boolean isDisposed() {
\r
95 return label.isDisposed();
\r
101 if(colorFactory != null) {
\r
102 colorFactory.listen(context, input, new Listener<Color>() {
\r
105 public void exception(Throwable t) {
\r
106 t.printStackTrace();
\r
110 public void execute(final Color color) {
\r
111 label.getDisplay().asyncExec(new Runnable() {
\r
114 public void run() {
\r
115 if(isDisposed()) return;
\r
116 label.setForeground(color);
\r
123 public boolean isDisposed() {
\r
124 return label.isDisposed();
\r
132 public <T> void setText(final ParametrizedRead<T, String> read) {
\r
134 Simantics.getSession().async(new UniqueRead<String>() {
\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
142 }, new Listener<String>() {
\r
145 public void exception(Throwable t) {
\r
146 t.printStackTrace();
\r
150 public void execute(final String text) {
\r
152 if(isDisposed()) return;
\r
154 label.getDisplay().asyncExec(new Runnable() {
\r
157 public void run() {
\r
158 label.setText(text);
\r
165 public boolean isDisposed() {
\r
166 return label.isDisposed();
\r
173 public void setText(String text) {
\r
174 label.setText(text);
\r