1 package fi.vtt.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.proconf.ui.ProConfUI;
\r
26 import org.simantics.utils.ErrorLogger;
\r
28 import fi.vtt.simantics.processeditor.ProcessResource;
\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
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
146 boolean add = true;
\r
147 for (Resource r : requiredTypes) {
\r
148 if (!equipment.isInstanceOf(r)) {
\r
154 for (Resource r : filteredTypes) {
\r
155 if (equipment.isInstanceOf(r)) {
\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
170 n = name +"("+i+")";
\r
171 if (typeList.getData(n)== null) {
\r
173 typeList.setData(n, node);
\r
178 typeList.add(name);
\r
179 typeList.setData(name, node);
\r
186 handling.addAll(g.getObjects(node,
\r
187 ProcessResource.builtins.ConsistsOf));
\r