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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.processeditor.dialogs;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Collection;
\r
15 import java.util.Stack;
\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
40 public class LibraryComponentDialog extends Dialog{
\r
42 private List typeList;
\r
43 private Resource selectedType = null;
\r
44 private Session session;
\r
45 private String title;
\r
47 private Resource primaryType;
\r
48 private Collection<Resource> requiredTypes = new ArrayList<Resource>();
\r
49 private Collection<Resource> filteredTypes = new ArrayList<Resource>();
\r
51 public LibraryComponentDialog(Shell shell, Session session, Resource primaryType, String title) {
\r
53 assert(title != null);
\r
54 this.session = session;
\r
56 this.primaryType = primaryType;
\r
60 protected void setFilter(Collection<Resource> filter) {
\r
61 this.filteredTypes = filter;
\r
64 protected void setRequired(Collection<Resource> required) {
\r
65 this.requiredTypes = required;
\r
70 protected void configureShell(Shell newShell) {
\r
72 super.configureShell(newShell);
\r
73 newShell.setText(title);
\r
76 public Resource getComboValue() {
\r
77 return selectedType;
\r
80 protected Control createDialogArea(Composite parent) {
\r
81 Composite composite = (Composite) super.createDialogArea(parent);
\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
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
100 public void widgetDefaultSelected(SelectionEvent e) {
\r
103 getShell().setText(title + " loading...");
\r
104 session.asyncRead(new GraphRequestAdapter() {
\r
106 public GraphRequestStatus perform(Graph g) throws Exception {
\r
108 return GraphRequestStatus.transactionComplete();
\r
112 public void requestCompleted(GraphRequestStatus status) {
\r
113 getDialogArea().getDisplay().asyncExec(new Runnable() {
\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
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
137 typeList.showSelection();
\r
139 applyDialogFont(composite);
\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
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
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
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
177 boolean add = true;
\r
178 for (Resource r : requiredTypes) {
\r
179 if (!equipment.isInstanceOf(r)) {
\r
185 for (Resource r : filteredTypes) {
\r
186 if (equipment.isInstanceOf(r)) {
\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
201 n = name +"("+i+")";
\r
202 if (typeList.getData(n)== null) {
\r
204 typeList.setData(n, node);
\r
209 typeList.add(name);
\r
210 typeList.setData(name, node);
\r
217 handling.addAll(g.getObjects(node,
\r
218 ProcessResource.builtins.ConsistsOf));
\r