1 package org.simantics.plant3d.dialog;
5 import org.eclipse.jface.dialogs.Dialog;
6 import org.eclipse.jface.dialogs.IDialogConstants;
7 import org.eclipse.jface.layout.GridDataFactory;
8 import org.eclipse.jface.resource.JFaceResources;
9 import org.eclipse.jface.resource.LocalResourceManager;
10 import org.eclipse.jface.viewers.ISelectionChangedListener;
11 import org.eclipse.jface.viewers.IStructuredSelection;
12 import org.eclipse.jface.viewers.ListViewer;
13 import org.eclipse.jface.viewers.SelectionChangedEvent;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.ExpandBar;
20 import org.eclipse.swt.widgets.ExpandItem;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Shell;
23 import org.simantics.Simantics;
24 import org.simantics.db.Session;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.plant3d.ontology.Plant3D;
27 import org.simantics.plant3d.scenegraph.Equipment;
28 import org.simantics.plant3d.scenegraph.Nozzle;
29 import org.simantics.plant3d.scenegraph.P3DRootNode;
30 import org.simantics.plant3d.utils.Item;
31 import org.simantics.plant3d.utils.P3DUtil;
32 import org.simantics.utils.ui.ExceptionUtils;
34 public class EquipmentSelectionDialog extends Dialog implements ISelectionChangedListener{
36 private String libUri;
38 private Item selected;
39 private Item selectedNozzle;
41 ListViewer equipmentViewer;
42 ListViewer nozzleViewer;
44 public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root) {
45 this(parentShell,root,Plant3D.URIs.Builtin);
48 public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root, String libUri){
53 protected List<Item> getItems(Class<?> c, String libUri) throws DatabaseException{
54 Session session = Simantics.getSession();
55 if (Equipment.class.equals(c)) {
56 return P3DUtil.getEquipments(session, libUri);
57 } else if (Nozzle.class.equals(c)) {
58 return P3DUtil.getNozzles(session, libUri);
64 protected Control createDialogArea(Composite parent) {
65 Composite composite = new Composite(parent, SWT.NONE);
66 GridLayout layout = new GridLayout(2,false);
67 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
68 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
69 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
70 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
71 composite.setLayout(layout);
72 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
73 applyDialogFont(composite);
75 List<Item> equipment = null;
76 List<Item> nozzles = null;
78 equipment = getItems(Equipment.class, libUri);
79 nozzles = getItems(Nozzle.class, libUri);
80 } catch (DatabaseException e) {
81 Label label = new Label(composite, SWT.NONE);
82 label.setText("Cannot load equipment data: " + e.getMessage());
83 ExceptionUtils.logError(e);
86 ExpandBar expandBar = new ExpandBar(composite, SWT.NONE);
88 ExpandItem equipmentItem = new ExpandItem(expandBar, SWT.NONE);
90 equipmentItem.setText("Equipment");
91 equipmentViewer = new ListViewer(expandBar);
92 equipmentViewer.setLabelProvider(new ComponentLabelProvider());
93 equipmentViewer.setContentProvider(new ComponentContentProvider());
95 ExpandItem nozzleItem = new ExpandItem(expandBar, SWT.NONE);
97 nozzleItem.setText("Nozzles");
98 nozzleViewer = new ListViewer(expandBar);
99 nozzleViewer.setLabelProvider(new ComponentLabelProvider());
100 nozzleViewer.setContentProvider(new ComponentContentProvider());
102 equipmentItem.setControl(equipmentViewer.getList());
103 nozzleItem.setControl(nozzleViewer.getList());
105 equipmentViewer.setInput(equipment);
106 nozzleViewer.setInput(nozzles);
108 equipmentItem.setHeight(equipmentViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
109 nozzleItem.setHeight(nozzleViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
111 equipmentViewer.addSelectionChangedListener(this);
112 nozzleViewer.addSelectionChangedListener(this);
114 GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).span(2, 1).applyTo(expandBar);
115 GridDataFactory.fillDefaults().minSize(500, 500).hint(500, 500).applyTo(composite);
121 public void selectionChanged(SelectionChangedEvent event) {
122 IStructuredSelection sel = (IStructuredSelection)event.getSelection();
123 Item i = (Item)sel.getFirstElement();
125 if (event.getSource() == equipmentViewer) {
127 } else if (event.getSource() == nozzleViewer) {
132 if (event.getSource() == equipmentViewer) {
134 } else if (event.getSource() == nozzleViewer) {
140 public Item getSelected() {
144 public Item getSelectedNozzle() {
145 return selectedNozzle;