]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
8c0441c48b42ee73c745a9d6f30665d51d51aa9d
[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.jfreechart.chart.properties;\r
13 \r
14 import org.eclipse.jface.viewers.ISelection;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.db.layer0.variable.Variable;\r
19 import org.simantics.db.request.Read;\r
20 import org.simantics.layer0.Layer0;\r
21 import org.simantics.modeling.ModelingResources;\r
22 import org.simantics.selectionview.PropertyTabContributorImpl;\r
23 import org.simantics.utils.ui.AdaptionUtils;\r
24 \r
25 public abstract class LabelPropertyTabContributor extends PropertyTabContributorImpl {\r
26 \r
27     @Override\r
28     public Read<String> getPartNameReadRequest(final ISelection forSelection) {\r
29 \r
30         return new Read<String>() {\r
31 \r
32             @Override\r
33             public String perform(ReadGraph graph) throws DatabaseException {\r
34                 Layer0 l0 = Layer0.getInstance(graph);\r
35                 ModelingResources mr = ModelingResources.getInstance(graph);\r
36 \r
37                         final Variable variable = AdaptionUtils.adaptToSingle(forSelection, Variable.class);\r
38                 final Resource resource = AdaptionUtils.adaptToSingle(forSelection, Resource.class);\r
39                 if(resource == null && variable == null) {\r
40                         return "Selection";\r
41                 }\r
42                 \r
43                 Resource r;\r
44                 if(variable != null) {\r
45                         r = (Resource)variable.getRepresents(graph);\r
46                 } else {\r
47                         r = resource;\r
48                 }\r
49                 \r
50                 if(graph.hasStatement(r, mr.ElementToComponent)) {\r
51                     r = graph.getSingleObject(r, mr.ElementToComponent);\r
52                 }\r
53                 String label = graph.getPossibleRelatedValue(r, l0.HasLabel);\r
54                 if(label != null)\r
55                     return label;\r
56                 label = graph.getPossibleRelatedValue(r, l0.HasName);\r
57                 if(label != null)\r
58                     return label;\r
59                 return "No name for selection";\r
60             }\r
61             \r
62         };\r
63         \r
64     }\r
65 \r
66 }\r