]> gerrit.simantics Code Review - simantics/3d.git/blob
6909fb639efdb4567782049da501f8c0ac794bce
[simantics/3d.git] /
1 package fi.vtt.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.proconf.ui.ProConfUI;\r
26 import org.simantics.utils.ErrorLogger;\r
27 \r
28 import fi.vtt.simantics.processeditor.ProcessResource;\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                 while (!handling.isEmpty()) {\r
136                         final Resource node = handling.pop();\r
137                         if (g.isInstanceOf(node,primaryType)) {\r
138                                 IEntity equipment = EntityFactory.create(g, node);\r
139                                 Collection<IEntity> graphics = equipment\r
140                                                 .getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics);\r
141                                 if (graphics.size() != 1) {\r
142                                         ErrorLogger.defaultLogError("Equipment "\r
143                                                         + equipment.getName() + " has " + graphics.size()\r
144                                                         + " + graphical representation!", null);\r
145                                 } else {\r
146                                         boolean add = true;\r
147                                         for (Resource r : requiredTypes) {\r
148                                                 if (!equipment.isInstanceOf(r)) {\r
149                                                         add = false;\r
150                                                         break;\r
151                                                 }\r
152                                         }\r
153                                         if (add) {\r
154                                                 for (Resource r : filteredTypes) {\r
155                                                         if (equipment.isInstanceOf(r)) {\r
156                                                                 add = false;\r
157                                                                 break;\r
158                                                         }\r
159                                                 }\r
160                                         }\r
161                                         if (add) {\r
162                                                 final String name = equipment.getName();\r
163                                                 getDialogArea().getDisplay().asyncExec(new Runnable() {\r
164                                                         public void run() {\r
165                                                                 // List won't work with two ore more same names.\r
166                                                                 if (typeList.getData(name)!= null) {\r
167                                                                         String n = new String(name);\r
168                                                                         int i = 1;\r
169                                                                         while (true) {\r
170                                                                                 n = name +"("+i+")";\r
171                                                                                 if (typeList.getData(n)== null) {\r
172                                                                                         typeList.add(n);\r
173                                                                                         typeList.setData(n, node);\r
174                                                                                         break;\r
175                                                                                 }\r
176                                                                         }\r
177                                                                 } else {\r
178                                                                         typeList.add(name);\r
179                                                                         typeList.setData(name, node);\r
180                                                                 }\r
181                                                         }\r
182                                                 });\r
183                                         }\r
184                                 }\r
185                         } else {\r
186                                 handling.addAll(g.getObjects(node,\r
187                                                 ProcessResource.builtins.ConsistsOf));\r
188                         }\r
189 \r
190                 }\r
191                 \r
192         }\r
193         \r
194 \r
195 }\r