1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets;
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;
28 public class Label extends WidgetImpl {
30 private ReadFactory<?, String> textFactory;
31 private ReadFactory<?, Color> colorFactory;
33 final private org.eclipse.swt.widgets.Label label;
35 public Label(Composite parent, WidgetSupport support, int style) {
37 label = new org.eclipse.swt.widgets.Label(parent, style);
38 support.register(this);
41 public void setTextFactory(ReadFactory<?, String> textFactory) {
42 this.textFactory = textFactory;
45 public void setForegroundFactory(ReadFactory<?, Color> colorFactory) {
46 this.colorFactory = colorFactory;
49 public org.eclipse.swt.widgets.Label getWidget() {
54 public Control getControl() {
59 public void setInput(ISessionContext context, Object input) {
61 if(textFactory != null) {
62 textFactory.listen(context, input, new Listener<String>() {
64 public void exception(final Throwable t) {
65 SWTUtils.asyncExec(label, new Runnable() {
69 if(isDisposed()) return;
70 // System.out.println("Button received new text: " + text);
71 label.setText(t.toString());
78 public void execute(final String text) {
79 SWTUtils.asyncExec(label, new Runnable() {
83 if(isDisposed()) return;
85 // TODO: how can we resize without this knife??
86 label.getParent().layout();
87 label.getParent().getParent().layout();
94 public boolean isDisposed() {
95 return label.isDisposed();
101 if(colorFactory != null) {
102 colorFactory.listen(context, input, new Listener<Color>() {
105 public void exception(Throwable t) {
110 public void execute(final Color color) {
111 label.getDisplay().asyncExec(new Runnable() {
115 if(isDisposed()) return;
116 label.setForeground(color);
123 public boolean isDisposed() {
124 return label.isDisposed();
132 public <T> void setText(final ParametrizedRead<T, String> read) {
134 Simantics.getSession().async(new UniqueRead<String>() {
137 public String perform(ReadGraph graph) throws DatabaseException {
138 T input = support.getInput(graph);
139 return graph.syncRequest(read.get(input));
142 }, new Listener<String>() {
145 public void exception(Throwable t) {
150 public void execute(final String text) {
152 if(isDisposed()) return;
154 label.getDisplay().asyncExec(new Runnable() {
165 public boolean isDisposed() {
166 return label.isDisposed();
173 public void setText(String text) {