]> gerrit.simantics Code Review - simantics/3d.git/blob
18263b92ffb5fe001d0229e80fb54f82e4602770
[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                                                 if (typeList.getItemCount() > 0) {\r
109                                                         typeList.select(0);\r
110                                         selectedType = (Resource)typeList.getData(typeList.getItem(0));\r
111                                                 }\r
112                             }\r
113                                 }\r
114                                 \r
115                         });\r
116                 }\r
117          });\r
118 \r
119          GridData data2 = new GridData(GridData.GRAB_HORIZONTAL\r
120                  | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL\r
121                  | GridData.VERTICAL_ALIGN_FILL);\r
122          data2.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
123          data2.heightHint = 200;\r
124          typeList.setLayoutData(data2);\r
125          typeList.setFont(parent.getFont());\r
126          \r
127          typeList.showSelection();\r
128          \r
129          applyDialogFont(composite);\r
130          return composite;\r
131      }\r
132      \r
133      private void loadComponents(Graph g) {\r
134                 Resource projectResource = ProConfUI.getProject().getResource();\r
135                 Stack<Resource> handling = new Stack<Resource>();\r
136                 handling.push(projectResource);\r
137                 \r
138                 // this is just a hack to get equipment defined in ontologies\r
139                 Resource projectsLib = g.getObjects(projectResource, g.getBuiltins().PartOf).iterator().next();\r
140                 Collection<Resource> projects = g.getObjects(projectsLib, g.getBuiltins().ConsistsOf);\r
141                 Resource typeSystemProject = null;\r
142                 for (Resource project : projects) {\r
143                         IEntity ent = EntityFactory.create(g,project);\r
144                         String name = ent.getName();\r
145                         if(name.equals("Type System Project")) {\r
146                                 typeSystemProject = project;\r
147                                 break;\r
148                         }\r
149                 }\r
150                 Collection<Resource> ontologies = g.getObjects(typeSystemProject, g.getBuiltins().ConsistsOf);\r
151                 for (Resource ontology : ontologies) {\r
152                         if(g.isInstanceOf(ontology, g.getBuiltins().Ontology))\r
153                                 handling.add(ontology);\r
154                 }\r
155                 \r
156                 while (!handling.isEmpty()) {\r
157                         final Resource node = handling.pop();\r
158                         if (g.isInstanceOf(node,primaryType)) {\r
159                                 IEntity equipment = EntityFactory.create(g, node);\r
160                                 Collection<IEntity> graphics = equipment\r
161                                                 .getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics);\r
162                                 if (graphics.size() != 1) {\r
163                                         ErrorLogger.defaultLogError("Equipment "\r
164                                                         + equipment.getName() + " has " + graphics.size()\r
165                                                         + " + graphical representation!", null);\r
166                                 } else {\r
167                                         boolean add = true;\r
168                                         for (Resource r : requiredTypes) {\r
169                                                 if (!equipment.isInstanceOf(r)) {\r
170                                                         add = false;\r
171                                                         break;\r
172                                                 }\r
173                                         }\r
174                                         if (add) {\r
175                                                 for (Resource r : filteredTypes) {\r
176                                                         if (equipment.isInstanceOf(r)) {\r
177                                                                 add = false;\r
178                                                                 break;\r
179                                                         }\r
180                                                 }\r
181                                         }\r
182                                         if (add) {\r
183                                                 final String name = equipment.getName();\r
184                                                 getDialogArea().getDisplay().asyncExec(new Runnable() {\r
185                                                         public void run() {\r
186                                                                 // List won't work with two ore more same names.\r
187                                                                 if (typeList.getData(name)!= null) {\r
188                                                                         String n = new String(name);\r
189                                                                         int i = 1;\r
190                                                                         while (true) {\r
191                                                                                 n = name +"("+i+")";\r
192                                                                                 if (typeList.getData(n)== null) {\r
193                                                                                         typeList.add(n);\r
194                                                                                         typeList.setData(n, node);\r
195                                                                                         break;\r
196                                                                                 }\r
197                                                                         }\r
198                                                                 } else {\r
199                                                                         typeList.add(name);\r
200                                                                         typeList.setData(name, node);\r
201                                                                 }\r
202                                                         }\r
203                                                 });\r
204                                         }\r
205                                 }\r
206                         } else {\r
207                                 handling.addAll(g.getObjects(node,\r
208                                                 ProcessResource.builtins.ConsistsOf));\r
209                         }\r
210 \r
211                 }\r
212                 \r
213         }\r
214         \r
215 \r
216 }\r