]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java
Ask component rotation angle when adding components
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / dialog / EquipmentSelectionDialog.java
1 package org.simantics.plant3d.dialog;
2
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.eclipse.jface.dialogs.Dialog;
8 import org.eclipse.jface.dialogs.IDialogConstants;
9 import org.eclipse.jface.layout.GridDataFactory;
10 import org.eclipse.jface.resource.JFaceResources;
11 import org.eclipse.jface.resource.LocalResourceManager;
12 import org.eclipse.jface.resource.ResourceManager;
13 import org.eclipse.jface.viewers.ISelectionChangedListener;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.jface.viewers.ListViewer;
16 import org.eclipse.jface.viewers.SelectionChangedEvent;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.ExpandBar;
23 import org.eclipse.swt.widgets.ExpandItem;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Shell;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.plant3d.ontology.Plant3D;
28 import org.simantics.plant3d.scenegraph.Equipment;
29 import org.simantics.plant3d.scenegraph.InlineComponent;
30 import org.simantics.plant3d.scenegraph.Nozzle;
31 import org.simantics.plant3d.scenegraph.P3DRootNode;
32 import org.simantics.plant3d.scenegraph.PipelineComponent;
33 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
34 import org.simantics.plant3d.utils.Item;
35 import org.simantics.plant3d.utils.P3DUtil;
36 import org.simantics.utils.ui.ExceptionUtils;
37
38 public class EquipmentSelectionDialog extends Dialog implements ISelectionChangedListener{
39     
40     private ResourceManager resourceManager;
41     private String libUri;
42     private P3DRootNode root;
43     
44     private Item selected;
45     private Item selectedNozzle;
46     
47     ListViewer equipmentViewer;
48     ListViewer nozzleViewer;
49     
50     public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root) {
51         this(parentShell,root,Plant3D.URIs.Builtin);
52     }
53     
54     public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root, String libUri){
55         super(parentShell);
56         this.root = root;
57         this.libUri = libUri;
58     }
59     
60     protected List<Item> getItems(Class c, String libUri) throws DatabaseException{
61         if (Equipment.class.equals(c)) {
62             return P3DUtil.getEquipments(libUri);
63         } else if (Nozzle.class.equals(c)) {
64             return P3DUtil.getNozzles(libUri);
65         }
66         return null;
67     }
68     
69     @Override
70     protected Control createDialogArea(Composite parent) {
71         resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
72         
73         Composite composite = new Composite(parent, SWT.NONE);
74         GridLayout layout = new GridLayout(2,false);
75         layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
76         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
77         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
78         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
79         composite.setLayout(layout);
80         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
81         applyDialogFont(composite);
82      
83         List<Item> equipment = null;
84         List<Item> nozzles = null;
85         try {
86             equipment = getItems(Equipment.class, libUri);
87             nozzles = getItems(Nozzle.class, libUri);
88         } catch (DatabaseException e) {
89             Label label = new Label(composite, SWT.NONE);
90             label.setText("Cannot load equipment data: " + e.getMessage());
91             ExceptionUtils.logError(e);
92             return composite;
93         }
94         ExpandBar expandBar = new ExpandBar(composite, SWT.NONE);
95         
96         ExpandItem equipmentItem = new ExpandItem(expandBar, SWT.NONE);
97         
98         equipmentItem.setText("Equipment");
99         equipmentViewer = new ListViewer(expandBar);
100         equipmentViewer.setLabelProvider(new ComponentLabelProvider());
101         equipmentViewer.setContentProvider(new ComponentContentProvider());
102         
103         ExpandItem nozzleItem = new ExpandItem(expandBar, SWT.NONE);
104         
105         nozzleItem.setText("Nozzles");
106         nozzleViewer = new ListViewer(expandBar);
107         nozzleViewer.setLabelProvider(new ComponentLabelProvider());
108         nozzleViewer.setContentProvider(new ComponentContentProvider());
109         
110         equipmentItem.setControl(equipmentViewer.getList());
111         nozzleItem.setControl(nozzleViewer.getList());
112         
113         equipmentViewer.setInput(equipment);
114         nozzleViewer.setInput(nozzles);
115         
116         equipmentItem.setHeight(equipmentViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
117         nozzleItem.setHeight(nozzleViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
118         
119         equipmentViewer.addSelectionChangedListener(this);
120         nozzleViewer.addSelectionChangedListener(this);
121         
122         GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).span(2, 1).applyTo(expandBar);
123         GridDataFactory.fillDefaults().minSize(500, 500).hint(500, 500).applyTo(composite);
124         
125         return composite;
126     }
127     
128     @Override
129     public void selectionChanged(SelectionChangedEvent event) {
130         IStructuredSelection sel = (IStructuredSelection)event.getSelection();
131         Item i = (Item)sel.getFirstElement();
132         if (i != null) {
133             if (event.getSource() == equipmentViewer) {
134                 selected = i;
135             } else if (event.getSource() == nozzleViewer) {
136                 selectedNozzle = i;
137             }
138             
139         }  else {
140             if (event.getSource() == equipmentViewer) {
141                 selected = i;
142             } else if (event.getSource() == nozzleViewer) {
143                 selectedNozzle = i;
144             }
145         }
146     }
147     
148     public Item getSelected() {
149         return selected;
150     }
151     
152     public Item getSelectedNozzle() {
153         return selectedNozzle;
154     }
155
156 }