]> gerrit.simantics Code Review - simantics/3d.git/blob
63961a9881902f3bc68c0d0067a7c9ace263ff1c
[simantics/3d.git] /
1 package org.simantics.processeditor.dialogs;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Stack;\r
6 \r
7 import org.eclipse.jface.dialogs.Dialog;\r
8 import org.eclipse.jface.dialogs.IDialogConstants;\r
9 import org.eclipse.swt.SWT;\r
10 import org.eclipse.swt.events.SelectionEvent;\r
11 import org.eclipse.swt.events.SelectionListener;\r
12 import org.eclipse.swt.layout.GridData;\r
13 import org.eclipse.swt.widgets.Composite;\r
14 import org.eclipse.swt.widgets.Control;\r
15 import org.eclipse.swt.widgets.Label;\r
16 import org.eclipse.swt.widgets.List;\r
17 import org.eclipse.swt.widgets.Shell;\r
18 import org.simantics.db.Graph;\r
19 import org.simantics.db.GraphRequestAdapter;\r
20 import org.simantics.db.GraphRequestStatus;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.Session;\r
23 import org.simantics.layer0.utils.EntityFactory;\r
24 import org.simantics.layer0.utils.IEntity;\r
25 import org.simantics.processeditor.ProcessResource;\r
26 import org.simantics.proconf.ui.ProConfUI;\r
27 import org.simantics.utils.ui.ErrorLogger;\r
28 \r
29 \r
30 public class LibraryComponentDialog extends Dialog{ \r
31         \r
32          private List typeList;\r
33      private Resource selectedType = null;\r
34      private Session session;\r
35      private String title;\r
36      \r
37      private Resource primaryType;\r
38      private Collection<Resource> requiredTypes = new ArrayList<Resource>();\r
39      private Collection<Resource> filteredTypes = new ArrayList<Resource>();\r
40      \r
41      public LibraryComponentDialog(Shell shell, Session session, Resource primaryType, String title) {\r
42          super(shell);\r
43          assert(title != null);\r
44          this.session = session;\r
45          this.title = title;\r
46          this.primaryType = primaryType;\r
47      }\r
48      \r
49      \r
50      protected void setFilter(Collection<Resource> filter) {\r
51          this.filteredTypes = filter;\r
52      }\r
53      \r
54      protected void setRequired(Collection<Resource> required) {\r
55          this.requiredTypes = required;\r
56      }\r
57      \r
58      \r
59      @Override\r
60      protected void configureShell(Shell newShell) {\r
61          \r
62          super.configureShell(newShell);\r
63          newShell.setText(title);\r
64      }\r
65 \r
66      public Resource getComboValue() {\r
67          return selectedType;\r
68      }\r
69      \r
70      protected Control createDialogArea(Composite parent) {\r
71          Composite composite = (Composite) super.createDialogArea(parent);\r
72          \r
73          Label label = new Label(composite, SWT.WRAP);\r
74          label.setText("Select Component");\r
75          GridData data = new GridData(GridData.GRAB_HORIZONTAL\r
76                  | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL\r
77                  | GridData.VERTICAL_ALIGN_CENTER);\r
78          data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
79          label.setLayoutData(data);\r
80          label.setFont(parent.getFont());\r
81          // TODO : list populating is done in random order; change to alphabetic\r
82          typeList = new List(composite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY | SWT.V_SCROLL);\r
83          typeList.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
84          \r
85          typeList.addSelectionListener(new SelectionListener() {\r
86              public void widgetSelected(SelectionEvent e) {\r
87                  String key = typeList.getItem(typeList.getSelectionIndex());\r
88                  selectedType = (Resource) typeList.getData(key);\r
89              }\r
90              public void widgetDefaultSelected(SelectionEvent e) {\r
91              }\r
92          });\r
93          getShell().setText(title + " loading...");\r
94          session.asyncRead(new GraphRequestAdapter() {\r
95                 @Override\r
96                 public GraphRequestStatus perform(Graph g) throws Exception {\r
97                         loadComponents(g);\r
98                         return GraphRequestStatus.transactionComplete();\r
99                 }\r
100                 \r
101                 @Override\r
102                 public void requestCompleted(GraphRequestStatus status) {\r
103                         getDialogArea().getDisplay().asyncExec(new Runnable() {\r
104                                 @Override\r
105                                 public void run() {\r
106                                         getShell().setText(title);\r
107                                         if (selectedType == null) {\r
108                                 typeList.select(0);\r
109                                 selectedType = (Resource)typeList.getData(typeList.getItem(0));\r
110                             }\r
111                                 }\r
112                                 \r
113                         });\r
114                 }\r
115          });\r
116 \r
117          GridData data2 = new GridData(GridData.GRAB_HORIZONTAL\r
118                  | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL\r
119                  | GridData.VERTICAL_ALIGN_FILL);\r
120          data2.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
121          data2.heightHint = 200;\r
122          typeList.setLayoutData(data2);\r
123          typeList.setFont(parent.getFont());\r
124          \r
125          typeList.showSelection();\r
126          \r
127          applyDialogFont(composite);\r
128          return composite;\r
129      }\r
130      \r
131      private void loadComponents(Graph g) {\r
132                 Resource projectResource = ProConfUI.getProject().getResource();\r
133                 Stack<Resource> handling = new Stack<Resource>();\r
134                 handling.push(projectResource);\r
135                 \r
136                 // this is just a hack to get equipment defined in ontologies\r
137                 Resource projectsLib = g.getObjects(projectResource, g.getBuiltins().PartOf).iterator().next();\r
138                 Collection<Resource> projects = g.getObjects(projectsLib, g.getBuiltins().ConsistsOf);\r
139                 Resource typeSystemProject = null;\r
140                 for (Resource project : projects) {\r
141                         IEntity ent = EntityFactory.create(g,project);\r
142                         String name = ent.getName();\r
143                         if(name.equals("Type System Project")) {\r
144                                 typeSystemProject = project;\r
145                                 break;\r
146                         }\r
147                 }\r
148                 Collection<Resource> ontologies = g.getObjects(typeSystemProject, g.getBuiltins().ConsistsOf);\r
149                 for (Resource ontology : ontologies) {\r
150                         if(g.isInstanceOf(ontology, g.getBuiltins().Ontology))\r
151                                 handling.add(ontology);\r
152                 }\r
153                 \r
154                 while (!handling.isEmpty()) {\r
155                         final Resource node = handling.pop();\r
156                         if (g.isInstanceOf(node,primaryType)) {\r
157                                 IEntity equipment = EntityFactory.create(g, node);\r
158                                 Collection<IEntity> graphics = equipment\r
159                                                 .getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics);\r
160                                 if (graphics.size() != 1) {\r
161                                         ErrorLogger.defaultLogError("Equipment "\r
162                                                         + equipment.getName() + " has " + graphics.size()\r
163                                                         + " + graphical representation!", null);\r
164                                 } else {\r
165                                         boolean add = true;\r
166                                         for (Resource r : requiredTypes) {\r
167                                                 if (!equipment.isInstanceOf(r)) {\r
168                                                         add = false;\r
169                                                         break;\r
170                                                 }\r
171                                         }\r
172                                         if (add) {\r
173                                                 for (Resource r : filteredTypes) {\r
174                                                         if (equipment.isInstanceOf(r)) {\r
175                                                                 add = false;\r
176                                                                 break;\r
177                                                         }\r
178                                                 }\r
179                                         }\r
180                                         if (add) {\r
181                                                 final String name = equipment.getName();\r
182                                                 getDialogArea().getDisplay().asyncExec(new Runnable() {\r
183                                                         public void run() {\r
184                                                                 // List won't work with two ore more same names.\r
185                                                                 if (typeList.getData(name)!= null) {\r
186                                                                         String n = new String(name);\r
187                                                                         int i = 1;\r
188                                                                         while (true) {\r
189                                                                                 n = name +"("+i+")";\r
190                                                                                 if (typeList.getData(n)== null) {\r
191                                                                                         typeList.add(n);\r
192                                                                                         typeList.setData(n, node);\r
193                                                                                         break;\r
194                                                                                 }\r
195                                                                         }\r
196                                                                 } else {\r
197                                                                         typeList.add(name);\r
198                                                                         typeList.setData(name, node);\r
199                                                                 }\r
200                                                         }\r
201                                                 });\r
202                                         }\r
203                                 }\r
204                         } else {\r
205                                 handling.addAll(g.getObjects(node,\r
206                                                 ProcessResource.builtins.ConsistsOf));\r
207                         }\r
208 \r
209                 }\r
210                 \r
211         }\r
212         \r
213 \r
214 }\r