1 package org.simantics.processeditor.dialogs;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\r
5 import java.util.Stack;
\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
30 public class LibraryComponentDialog extends Dialog{
\r
32 private List typeList;
\r
33 private Resource selectedType = null;
\r
34 private Session session;
\r
35 private String title;
\r
37 private Resource primaryType;
\r
38 private Collection<Resource> requiredTypes = new ArrayList<Resource>();
\r
39 private Collection<Resource> filteredTypes = new ArrayList<Resource>();
\r
41 public LibraryComponentDialog(Shell shell, Session session, Resource primaryType, String title) {
\r
43 assert(title != null);
\r
44 this.session = session;
\r
46 this.primaryType = primaryType;
\r
50 protected void setFilter(Collection<Resource> filter) {
\r
51 this.filteredTypes = filter;
\r
54 protected void setRequired(Collection<Resource> required) {
\r
55 this.requiredTypes = required;
\r
60 protected void configureShell(Shell newShell) {
\r
62 super.configureShell(newShell);
\r
63 newShell.setText(title);
\r
66 public Resource getComboValue() {
\r
67 return selectedType;
\r
70 protected Control createDialogArea(Composite parent) {
\r
71 Composite composite = (Composite) super.createDialogArea(parent);
\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
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
90 public void widgetDefaultSelected(SelectionEvent e) {
\r
93 getShell().setText(title + " loading...");
\r
94 session.asyncRead(new GraphRequestAdapter() {
\r
96 public GraphRequestStatus perform(Graph g) throws Exception {
\r
98 return GraphRequestStatus.transactionComplete();
\r
102 public void requestCompleted(GraphRequestStatus status) {
\r
103 getDialogArea().getDisplay().asyncExec(new Runnable() {
\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
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
125 typeList.showSelection();
\r
127 applyDialogFont(composite);
\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
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
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
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
165 boolean add = true;
\r
166 for (Resource r : requiredTypes) {
\r
167 if (!equipment.isInstanceOf(r)) {
\r
173 for (Resource r : filteredTypes) {
\r
174 if (equipment.isInstanceOf(r)) {
\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
189 n = name +"("+i+")";
\r
190 if (typeList.getData(n)== null) {
\r
192 typeList.setData(n, node);
\r
197 typeList.add(name);
\r
198 typeList.setData(name, node);
\r
205 handling.addAll(g.getObjects(node,
\r
206 ProcessResource.builtins.ConsistsOf));
\r