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