1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\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.sysdyn.ui.properties;
\r
14 import org.eclipse.jface.viewers.ISelection;
\r
15 import org.eclipse.swt.events.DisposeEvent;
\r
16 import org.eclipse.swt.events.DisposeListener;
\r
17 import org.eclipse.swt.widgets.Composite;
\r
18 import org.eclipse.ui.IWorkbenchSite;
\r
19 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
\r
20 import org.simantics.db.AsyncReadGraph;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.db.layer0.variable.Variable;
\r
25 import org.simantics.db.management.ISessionContext;
\r
26 import org.simantics.db.procedure.AsyncListener;
\r
27 import org.simantics.db.request.Read;
\r
28 import org.simantics.layer0.Layer0;
\r
29 import org.simantics.modeling.ModelingResources;
\r
30 import org.simantics.selectionview.PropertyTabContributorImpl;
\r
31 import org.simantics.ui.SimanticsUI;
\r
32 import org.simantics.ui.utils.AdaptionUtils;
\r
33 import org.simantics.utils.datastructures.Callback;
\r
35 public abstract class LabelPropertyTabContributor extends PropertyTabContributorImpl {
\r
37 private boolean isDisposed = false;
\r
40 public void createControl(Composite parent, final IWorkbenchSite site, final ISessionContext context, final WidgetSupportImpl support) {
\r
41 super.createControl(parent, site, context, support);
\r
43 // Add dispose listener to make sure name listening receives the correct isDisposed -value
\r
44 parent.addDisposeListener(new DisposeListener() {
\r
47 public void widgetDisposed(DisposeEvent e) {
\r
48 LabelPropertyTabContributor.this.dispose();
\r
54 public void updatePartName(ISelection forSelection, final Callback<String> updateCallback) {
\r
55 final Variable variable = AdaptionUtils.adaptToSingle(forSelection, Variable.class);
\r
56 final Resource resource = AdaptionUtils.adaptToSingle(forSelection, Resource.class);
\r
57 if(resource == null && variable == null) {
\r
58 updateCallback.run("Selection properties");
\r
62 SimanticsUI.getSession().asyncRequest(new Read<String>() {
\r
65 public String perform(ReadGraph graph) throws DatabaseException {
\r
66 Layer0 l0 = Layer0.getInstance(graph);
\r
67 ModelingResources mr = ModelingResources.getInstance(graph);
\r
70 if(variable != null) {
\r
71 r = (Resource)variable.getRepresents(graph);
\r
76 if(graph.hasStatement(r, mr.ElementToComponent)) {
\r
77 r = graph.getSingleObject(r, mr.ElementToComponent);
\r
79 String label = graph.getPossibleRelatedValue(r, l0.HasLabel);
\r
82 label = graph.getPossibleRelatedValue(r, l0.HasName);
\r
85 return "No name for selection";
\r
87 }, new AsyncListener<String>() {
\r
90 public void execute(AsyncReadGraph graph, String result) {
\r
91 updateCallback.run(result);
\r
95 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
100 public boolean isDisposed() {
\r
107 protected void dispose() {
\r
108 this.isDisposed = true;
\r