]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / selection / WorkbenchSelectionUtils.java
1 package org.simantics.ui.selection;\r
2 \r
3 import java.util.Collection;\r
4 import java.util.List;\r
5 \r
6 import org.eclipse.core.commands.ExecutionEvent;\r
7 import org.eclipse.jface.viewers.ISelection;\r
8 import org.eclipse.ui.handlers.HandlerUtil;\r
9 import org.simantics.Simantics;\r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.RequestProcessor;\r
12 import org.simantics.db.Resource;\r
13 import org.simantics.db.common.primitiverequest.IsInstanceOf;\r
14 import org.simantics.db.exception.DatabaseException;\r
15 import org.simantics.db.layer0.request.VariableURI;\r
16 import org.simantics.db.layer0.variable.Variable;\r
17 import org.simantics.utils.ui.ISelectionUtils;\r
18 \r
19 public class WorkbenchSelectionUtils {\r
20 \r
21         public static Resource getPossibleResource(ExecutionEvent event) throws DatabaseException {\r
22                 ISelection selection = HandlerUtil.getCurrentSelection(event);\r
23                 return getPossibleResource(selection);\r
24         }\r
25         \r
26         public static Variable getPossibleVariable(ExecutionEvent event) throws DatabaseException {\r
27                 ISelection selection = HandlerUtil.getCurrentSelection(event);\r
28                 return getPossibleVariable(selection);\r
29         }\r
30         \r
31         public static List<WorkbenchSelectionElement> getWorkbenchSelectionElements(ISelection selection) {\r
32             return ISelectionUtils.filterSelection(selection, WorkbenchSelectionElement.class);\r
33         }\r
34 \r
35         public static String getPossibleJSON(Object selection) throws DatabaseException {\r
36                 if(selection instanceof WorkbenchSelectionElement) return getPossibleJSON((WorkbenchSelectionElement)selection);\r
37                 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
38                 if(element == null) return null;\r
39                 return getPossibleJSON(element);\r
40         }\r
41 \r
42         public static Resource getPossibleResource(Object selection) throws DatabaseException {\r
43                 if(selection instanceof WorkbenchSelectionElement) return getPossibleResource((WorkbenchSelectionElement)selection);\r
44                 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
45                 if(element == null) return null;\r
46                 return getPossibleResource(element);\r
47         }\r
48 \r
49         public static Variable getPossibleVariable(Object selection) throws DatabaseException {\r
50                 if(selection instanceof WorkbenchSelectionElement) return getPossibleVariable((WorkbenchSelectionElement)selection);\r
51                 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
52                 if(element == null) return null;\r
53                 return getPossibleVariable(element);\r
54         }\r
55 \r
56         public static Variable getPossibleVariable(Object[] selection) throws DatabaseException {\r
57                 if(selection.length != 1) return null;\r
58                 if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;\r
59                 return getPossibleVariable((WorkbenchSelectionElement)selection[0]);\r
60         }\r
61 \r
62         public static <T> T getPossibleElement(Object[] selection, WorkbenchSelectionContentType<T> contentType) throws DatabaseException {\r
63                 if(selection.length != 1) return null;\r
64                 if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;\r
65                 WorkbenchSelectionElement wse = (WorkbenchSelectionElement)selection[0];\r
66                 return wse.getContent(contentType);\r
67         }\r
68 \r
69 //      public static <T> T getPossibleExplorerInput(Object selection) throws DatabaseException {\r
70 //              if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerInput((WorkbenchSelectionElement)selection);\r
71 //              WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
72 //              if(element == null) return null;\r
73 //              return getPossibleExplorerInput(element);\r
74 //      }\r
75 \r
76 //      public static <T> T getPossibleExplorerColumn(Object selection) throws DatabaseException {\r
77 //              if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerColumn((WorkbenchSelectionElement)selection);\r
78 //              WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
79 //              if(element == null) return null;\r
80 //              return getPossibleExplorerColumn(element);\r
81 //      }\r
82         \r
83         public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {\r
84                 return getPossibleJSON(Simantics.getSession(), wse);\r
85         }\r
86 \r
87         public static Resource getPossibleResource(WorkbenchSelectionElement wse) throws DatabaseException {\r
88                 return getPossibleResource(Simantics.getSession(), wse);\r
89         }\r
90 \r
91         public static Variable getPossibleVariable(WorkbenchSelectionElement wse) throws DatabaseException {\r
92                 return getPossibleVariable(Simantics.getSession(), wse);\r
93         }\r
94 \r
95         public static Variable getPossibleVariableSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {\r
96                 return getPossibleVariable(graph, wse);\r
97         }\r
98 \r
99         public static Resource getPossibleResourceSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {\r
100                 return getPossibleResource(graph, wse);\r
101         }\r
102 \r
103         public static String getPossibleJSON(RequestProcessor processor, Object input) throws DatabaseException {\r
104                 Variable var = getPossibleVariable(processor, input);\r
105                 if(var != null) {\r
106                         String uri = processor.syncRequest(new VariableURI(var));\r
107                         return "{ \"type\":\"Variable\", \"uri\" : \"" + uri + "\" }";\r
108                 }\r
109                 Resource res = getPossibleResource(processor, input);\r
110                 if(res != null) {\r
111                         return "{ type:\"Resource\" }";\r
112                 }\r
113                 return "{ type:\"Unknown\" }";\r
114                 \r
115         }\r
116 \r
117         public static Resource getPossibleResource(RequestProcessor processor, Object input) throws DatabaseException {\r
118                 return getPossibleResource(processor, input, null);\r
119         }\r
120 \r
121         public static Resource getPossibleResource(RequestProcessor processor, Object input, Resource type) throws DatabaseException {\r
122             if(input instanceof Collection && !((Collection)input).isEmpty()) {\r
123                 Object element = ((Collection)input).iterator().next();\r
124                 if(element instanceof Resource)\r
125                     return (Resource)element;\r
126             }\r
127                 Resource resource = getPossibleElement(input, new AnyResource(processor));\r
128                 if(resource == null) return resource;\r
129                 if(type != null) {\r
130                         if(processor.sync(new IsInstanceOf(resource, type))) return resource;\r
131                         else return null;\r
132                 }\r
133                 return resource;\r
134         }\r
135 \r
136         public static Variable getPossibleVariable(RequestProcessor processor, Object input) throws DatabaseException {\r
137                 return getPossibleElement(input, new AnyVariable(processor));\r
138         }\r
139 \r
140 //      @SuppressWarnings("unchecked")\r
141 //      public static <T> T getPossibleExplorerInput(WorkbenchSelectionElement input) throws DatabaseException {\r
142 //              return ((T)getPossibleElement(input, new ExplorerInputContentType()));\r
143 //      }\r
144 \r
145         public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> contentType) {\r
146                 Object single = getPossibleSingleElement(input);\r
147                 if(single == null) return null;\r
148                 if(single instanceof WorkbenchSelectionElement) {\r
149                         WorkbenchSelectionElement element = (WorkbenchSelectionElement)single;\r
150                         return (T)element.getContent(contentType);\r
151                 }\r
152                 return null;\r
153         }\r
154         \r
155         public static WorkbenchSelectionElement getPossibleSelectionElement(Object input) {\r
156         if(input instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)input;\r
157         if(input instanceof Collection) {\r
158                 Collection<?> c = (Collection<?>)input;\r
159                 if(c.size() == 1) {\r
160                         Object o = c.iterator().next();\r
161                         if(o instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)o;\r
162                 }\r
163         }\r
164         return null;\r
165         }\r
166 \r
167         // Internal helpers\r
168         \r
169     private static Object getPossibleSingleElement(Object input) {\r
170         if(input instanceof WorkbenchSelectionElement) return input;\r
171         if(input instanceof Collection) {\r
172                 Collection<?> c = (Collection<?>)input;\r
173                 if(c.size() == 1) return c.iterator().next();\r
174         } \r
175         return null;\r
176     }   \r
177     \r
178         \r
179 }\r