X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2Fwidgets%2FLabel.java;fp=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2Fwidgets%2FLabel.java;h=c9651a1fa2b564abf71fd1928fcfa7472c694174;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Label.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Label.java new file mode 100644 index 000000000..c9651a1fa --- /dev/null +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Label.java @@ -0,0 +1,177 @@ +/******************************************************************************* + * Copyright (c) 2007, 2012 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.swt.widgets; + +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.simantics.Simantics; +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.ReadGraph; +import org.simantics.db.common.request.ParametrizedRead; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.db.procedure.Listener; +import org.simantics.utils.ui.SWTUtils; + +public class Label extends WidgetImpl { + + private ReadFactory textFactory; + private ReadFactory colorFactory; + + final private org.eclipse.swt.widgets.Label label; + + public Label(Composite parent, WidgetSupport support, int style) { + super(support); + label = new org.eclipse.swt.widgets.Label(parent, style); + support.register(this); + } + + public void setTextFactory(ReadFactory textFactory) { + this.textFactory = textFactory; + } + + public void setForegroundFactory(ReadFactory colorFactory) { + this.colorFactory = colorFactory; + } + + public org.eclipse.swt.widgets.Label getWidget() { + return label; + } + + @Override + public Control getControl() { + return label; + } + + @Override + public void setInput(ISessionContext context, Object input) { + + if(textFactory != null) { + textFactory.listen(context, input, new Listener() { + + public void exception(final Throwable t) { + SWTUtils.asyncExec(label, new Runnable() { + + @Override + public void run() { + if(isDisposed()) return; +// System.out.println("Button received new text: " + text); + label.setText(t.toString()); + } + + }); + } + + @Override + public void execute(final String text) { + SWTUtils.asyncExec(label, new Runnable() { + + @Override + public void run() { + if(isDisposed()) return; + label.setText(text); + // TODO: how can we resize without this knife?? + label.getParent().layout(); + label.getParent().getParent().layout(); + } + + }); + } + + @Override + public boolean isDisposed() { + return label.isDisposed(); + } + + }); + } + + if(colorFactory != null) { + colorFactory.listen(context, input, new Listener() { + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public void execute(final Color color) { + label.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + if(isDisposed()) return; + label.setForeground(color); + } + + }); + } + + @Override + public boolean isDisposed() { + return label.isDisposed(); + } + + }); + } + + } + + public void setText(final ParametrizedRead read) { + + Simantics.getSession().async(new UniqueRead() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + T input = support.getInput(graph); + return graph.syncRequest(read.get(input)); + } + + }, new Listener() { + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public void execute(final String text) { + + if(isDisposed()) return; + + label.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + label.setText(text); + } + + }); + } + + @Override + public boolean isDisposed() { + return label.isDisposed(); + } + + }); + + } + + public void setText(String text) { + label.setText(text); + } + +}