]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
35e7c35764a2089674cdca9eb0ef1a4a1a8ecca0
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.management.ISessionContext;\r
25 import org.simantics.db.procedure.AsyncListener;\r
26 import org.simantics.db.request.Read;\r
27 import org.simantics.layer0.Layer0;\r
28 import org.simantics.modeling.ModelingResources;\r
29 import org.simantics.selectionview.PropertyTabContributorImpl;\r
30 import org.simantics.ui.SimanticsUI;\r
31 import org.simantics.ui.utils.AdaptionUtils;\r
32 import org.simantics.utils.datastructures.Callback;\r
33 \r
34 public abstract class LabelPropertyTabContributor extends PropertyTabContributorImpl {\r
35 \r
36     private boolean isDisposed = false;\r
37 \r
38 \r
39     public void createControl(Composite parent, final IWorkbenchSite site, final ISessionContext context, final WidgetSupportImpl support) {\r
40         super.createControl(parent, site, context, support);\r
41 \r
42         // Add dispose listener to make sure name listening receives the correct isDisposed -value\r
43         parent.addDisposeListener(new DisposeListener() {\r
44 \r
45             @Override\r
46             public void widgetDisposed(DisposeEvent e) {\r
47                 LabelPropertyTabContributor.this.dispose();\r
48             }\r
49         });\r
50     }\r
51 \r
52     @Override\r
53     public void updatePartName(ISelection forSelection, final Callback<String> updateCallback) {\r
54         final Resource resource = AdaptionUtils.adaptToSingle(forSelection, Resource.class);\r
55         if(resource == null) {\r
56             updateCallback.run("Selection properties");\r
57             return;\r
58         }\r
59 \r
60         SimanticsUI.getSession().asyncRequest(new Read<String>() {\r
61 \r
62             @Override\r
63             public String perform(ReadGraph graph) throws DatabaseException {\r
64                 Layer0 l0 = Layer0.getInstance(graph);\r
65                 ModelingResources mr = ModelingResources.getInstance(graph);\r
66                 Resource r = resource;\r
67                 if(graph.hasStatement(r, mr.ElementToComponent)) {\r
68                     r = graph.getSingleObject(r, mr.ElementToComponent);\r
69                 }\r
70                 String label = graph.getPossibleRelatedValue(r, l0.HasLabel);\r
71                 if(label != null)\r
72                     return label;\r
73                 label = graph.getPossibleRelatedValue(r, l0.HasName);\r
74                 if(label != null)\r
75                     return label;\r
76                 return "No name for selection";\r
77             }\r
78         }, new AsyncListener<String>() {\r
79 \r
80             @Override\r
81             public void execute(AsyncReadGraph graph, String result) {\r
82                 updateCallback.run(result);\r
83             }\r
84 \r
85             @Override\r
86             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
87 \r
88             }\r
89 \r
90             @Override\r
91             public boolean isDisposed() {\r
92                 return isDisposed;\r
93             }\r
94         });\r
95     }\r
96 \r
97     @Override\r
98     protected void dispose() {\r
99         this.isDisposed = true;\r
100     }\r
101 }\r