]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
b1f8351aa109a6d4b7929fa125a77939340fabe7
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.properties;\r
13 \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
34 \r
35 public abstract class LabelPropertyTabContributor extends PropertyTabContributorImpl {\r
36 \r
37     private boolean isDisposed = false;\r
38 \r
39 \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
42 \r
43         // Add dispose listener to make sure name listening receives the correct isDisposed -value\r
44         parent.addDisposeListener(new DisposeListener() {\r
45 \r
46             @Override\r
47             public void widgetDisposed(DisposeEvent e) {\r
48                 LabelPropertyTabContributor.this.dispose();\r
49             }\r
50         });\r
51     }\r
52 \r
53     @Override\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
59             return;\r
60         }\r
61 \r
62         SimanticsUI.getSession().asyncRequest(new Read<String>() {\r
63 \r
64             @Override\r
65             public String perform(ReadGraph graph) throws DatabaseException {\r
66                 Layer0 l0 = Layer0.getInstance(graph);\r
67                 ModelingResources mr = ModelingResources.getInstance(graph);\r
68                 \r
69                 Resource r;\r
70                 if(variable != null) {\r
71                         r = (Resource)variable.getRepresents(graph);\r
72                 } else {\r
73                         r = resource;\r
74                 }\r
75                 \r
76                 if(graph.hasStatement(r, mr.ElementToComponent)) {\r
77                     r = graph.getSingleObject(r, mr.ElementToComponent);\r
78                 }\r
79                 String label = graph.getPossibleRelatedValue(r, l0.HasLabel);\r
80                 if(label != null)\r
81                     return label;\r
82                 label = graph.getPossibleRelatedValue(r, l0.HasName);\r
83                 if(label != null)\r
84                     return label;\r
85                 return "No name for selection";\r
86             }\r
87         }, new AsyncListener<String>() {\r
88 \r
89             @Override\r
90             public void execute(AsyncReadGraph graph, String result) {\r
91                 updateCallback.run(result);\r
92             }\r
93 \r
94             @Override\r
95             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
96 \r
97             }\r
98 \r
99             @Override\r
100             public boolean isDisposed() {\r
101                 return isDisposed;\r
102             }\r
103         });\r
104     }\r
105 \r
106     @Override\r
107     protected void dispose() {\r
108         this.isDisposed = true;\r
109     }\r
110 }\r