]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
14b57a98f0f5028b13a3e3c232e1b6f8f250a283
[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     private final Object id;\r
28     \r
29     public LabelPropertyTabContributor(Object id) {\r
30         assert (id != null);\r
31         this.id = id;\r
32     }\r
33     \r
34     @Override\r
35     public Read<String> getPartNameReadRequest(final ISelection forSelection) {\r
36 \r
37         return new Read<String>() {\r
38 \r
39             @Override\r
40             public String perform(ReadGraph graph) throws DatabaseException {\r
41                 Layer0 l0 = Layer0.getInstance(graph);\r
42                 ModelingResources mr = ModelingResources.getInstance(graph);\r
43 \r
44                         final Variable variable = AdaptionUtils.adaptToSingle(forSelection, Variable.class);\r
45                 final Resource resource = AdaptionUtils.adaptToSingle(forSelection, Resource.class);\r
46                 if(resource == null && variable == null) {\r
47                         return "Selection";\r
48                 }\r
49                 \r
50                 Resource r;\r
51                 if(variable != null) {\r
52                         r = (Resource)variable.getRepresents(graph);\r
53                 } else {\r
54                         r = resource;\r
55                 }\r
56                 \r
57                 if(graph.hasStatement(r, mr.ElementToComponent)) {\r
58                     r = graph.getSingleObject(r, mr.ElementToComponent);\r
59                 }\r
60                 String label = graph.getPossibleRelatedValue(r, l0.HasLabel);\r
61                 if(label != null)\r
62                     return label;\r
63                 label = graph.getPossibleRelatedValue(r, l0.HasName);\r
64                 if(label != null)\r
65                     return label;\r
66                 return "No name for selection";\r
67             }\r
68             \r
69         };\r
70         \r
71     }\r
72 \r
73     @Override\r
74     public int hashCode() {\r
75         return id.hashCode();\r
76     }\r
77 \r
78     @Override\r
79     public boolean equals(Object obj) {\r
80         if (this == obj)\r
81             return true;\r
82         if (obj == null)\r
83             return false;\r
84         if (getClass() != obj.getClass())\r
85             return false;\r
86         LabelPropertyTabContributor other = (LabelPropertyTabContributor) obj;\r
87         return id.equals(other.id);\r
88     }\r
89 \r
90 }\r