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