]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java
No insertion of straight pipe in straight pipe
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / dialog / EquipmentSelectionDialog.java
1 package org.simantics.plant3d.dialog;
2
3 import java.util.List;
4
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;
33
34 public class EquipmentSelectionDialog extends Dialog implements ISelectionChangedListener{
35     
36     private String libUri;
37     
38     private Item selected;
39     private Item selectedNozzle;
40     
41     ListViewer equipmentViewer;
42     ListViewer nozzleViewer;
43     
44     public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root) {
45         this(parentShell,root,Plant3D.URIs.Builtin);
46     }
47     
48     public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root, String libUri){
49         super(parentShell);
50         this.libUri = libUri;
51     }
52     
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);
59         }
60         return null;
61     }
62     
63     @Override
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);
74      
75         List<Item> equipment = null;
76         List<Item> nozzles = null;
77         try {
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);
84             return composite;
85         }
86         ExpandBar expandBar = new ExpandBar(composite, SWT.NONE);
87         
88         ExpandItem equipmentItem = new ExpandItem(expandBar, SWT.NONE);
89         
90         equipmentItem.setText("Equipment");
91         equipmentViewer = new ListViewer(expandBar);
92         equipmentViewer.setLabelProvider(new ComponentLabelProvider());
93         equipmentViewer.setContentProvider(new ComponentContentProvider());
94         
95         ExpandItem nozzleItem = new ExpandItem(expandBar, SWT.NONE);
96         
97         nozzleItem.setText("Nozzles");
98         nozzleViewer = new ListViewer(expandBar);
99         nozzleViewer.setLabelProvider(new ComponentLabelProvider());
100         nozzleViewer.setContentProvider(new ComponentContentProvider());
101         
102         equipmentItem.setControl(equipmentViewer.getList());
103         nozzleItem.setControl(nozzleViewer.getList());
104         
105         equipmentViewer.setInput(equipment);
106         nozzleViewer.setInput(nozzles);
107         
108         equipmentItem.setHeight(equipmentViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
109         nozzleItem.setHeight(nozzleViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
110         
111         equipmentViewer.addSelectionChangedListener(this);
112         nozzleViewer.addSelectionChangedListener(this);
113         
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);
116         
117         return composite;
118     }
119     
120     @Override
121     public void selectionChanged(SelectionChangedEvent event) {
122         IStructuredSelection sel = (IStructuredSelection)event.getSelection();
123         Item i = (Item)sel.getFirstElement();
124         if (i != null) {
125             if (event.getSource() == equipmentViewer) {
126                 selected = i;
127             } else if (event.getSource() == nozzleViewer) {
128                 selectedNozzle = i;
129             }
130             
131         }  else {
132             if (event.getSource() == equipmentViewer) {
133                 selected = i;
134             } else if (event.getSource() == nozzleViewer) {
135                 selectedNozzle = i;
136             }
137         }
138     }
139     
140     public Item getSelected() {
141         return selected;
142     }
143     
144     public Item getSelectedNozzle() {
145         return selectedNozzle;
146     }
147
148 }