package org.simantics.processeditor.dialogs; import java.util.ArrayList; import java.util.Collection; import java.util.Stack; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; import org.simantics.db.Graph; import org.simantics.db.GraphRequestAdapter; import org.simantics.db.GraphRequestStatus; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.layer0.utils.EntityFactory; import org.simantics.layer0.utils.IEntity; import org.simantics.processeditor.ProcessResource; import org.simantics.proconf.ui.ProConfUI; import org.simantics.utils.ErrorLogger; public class LibraryComponentDialog extends Dialog{ private List typeList; private Resource selectedType = null; private Session session; private String title; private Resource primaryType; private Collection requiredTypes = new ArrayList(); private Collection filteredTypes = new ArrayList(); public LibraryComponentDialog(Shell shell, Session session, Resource primaryType, String title) { super(shell); assert(title != null); this.session = session; this.title = title; this.primaryType = primaryType; } protected void setFilter(Collection filter) { this.filteredTypes = filter; } protected void setRequired(Collection required) { this.requiredTypes = required; } @Override protected void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText(title); } public Resource getComboValue() { return selectedType; } protected Control createDialogArea(Composite parent) { Composite composite = (Composite) super.createDialogArea(parent); Label label = new Label(composite, SWT.WRAP); label.setText("Select Component"); GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER); data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); label.setLayoutData(data); label.setFont(parent.getFont()); // TODO : list populating is done in random order; change to alphabetic typeList = new List(composite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY | SWT.V_SCROLL); typeList.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL)); typeList.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { String key = typeList.getItem(typeList.getSelectionIndex()); selectedType = (Resource) typeList.getData(key); } public void widgetDefaultSelected(SelectionEvent e) { } }); getShell().setText(title + " loading..."); session.asyncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { loadComponents(g); return GraphRequestStatus.transactionComplete(); } @Override public void requestCompleted(GraphRequestStatus status) { getDialogArea().getDisplay().asyncExec(new Runnable() { @Override public void run() { getShell().setText(title); if (selectedType == null) { typeList.select(0); selectedType = (Resource)typeList.getData(typeList.getItem(0)); } } }); } }); GridData data2 = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL); data2.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); data2.heightHint = 200; typeList.setLayoutData(data2); typeList.setFont(parent.getFont()); typeList.showSelection(); applyDialogFont(composite); return composite; } private void loadComponents(Graph g) { Resource projectResource = ProConfUI.getProject().getResource(); Stack handling = new Stack(); handling.push(projectResource); while (!handling.isEmpty()) { final Resource node = handling.pop(); if (g.isInstanceOf(node,primaryType)) { IEntity equipment = EntityFactory.create(g, node); Collection graphics = equipment .getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics); if (graphics.size() != 1) { ErrorLogger.defaultLogError("Equipment " + equipment.getName() + " has " + graphics.size() + " + graphical representation!", null); } else { boolean add = true; for (Resource r : requiredTypes) { if (!equipment.isInstanceOf(r)) { add = false; break; } } if (add) { for (Resource r : filteredTypes) { if (equipment.isInstanceOf(r)) { add = false; break; } } } if (add) { final String name = equipment.getName(); getDialogArea().getDisplay().asyncExec(new Runnable() { public void run() { // List won't work with two ore more same names. if (typeList.getData(name)!= null) { String n = new String(name); int i = 1; while (true) { n = name +"("+i+")"; if (typeList.getData(n)== null) { typeList.add(n); typeList.setData(n, node); break; } } } else { typeList.add(name); typeList.setData(name, node); } } }); } } } else { handling.addAll(g.getObjects(node, ProcessResource.builtins.ConsistsOf)); } } } }