]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java
No insertion of straight pipe in straight pipe
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / dialog / ComponentSelectionDialog.java
1 package org.simantics.plant3d.dialog;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.ListIterator;
7 import java.util.Set;
8
9 import org.eclipse.jface.dialogs.Dialog;
10 import org.eclipse.jface.dialogs.IDialogConstants;
11 import org.eclipse.jface.dialogs.IDialogSettings;
12 import org.eclipse.jface.layout.GridDataFactory;
13 import org.eclipse.jface.layout.GridLayoutFactory;
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.jface.resource.LocalResourceManager;
16 import org.eclipse.jface.resource.ResourceLocator;
17 import org.eclipse.jface.resource.ResourceManager;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.ListViewer;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.KeyAdapter;
25 import org.eclipse.swt.events.KeyEvent;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.ExpandBar;
34 import org.eclipse.swt.widgets.ExpandItem;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Text;
38 import org.simantics.Simantics;
39 import org.simantics.db.Session;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.plant3d.Activator;
42 import org.simantics.plant3d.ontology.Plant3D;
43 import org.simantics.plant3d.scenegraph.EndComponent;
44 import org.simantics.plant3d.scenegraph.InlineComponent;
45 import org.simantics.plant3d.scenegraph.Nozzle;
46 import org.simantics.plant3d.scenegraph.PipeRun;
47 import org.simantics.plant3d.scenegraph.PipelineComponent;
48 import org.simantics.plant3d.scenegraph.TurnComponent;
49 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
50 import org.simantics.plant3d.utils.Item;
51 import org.simantics.plant3d.utils.Item.Type;
52 import org.simantics.plant3d.utils.P3DUtil;
53 import org.simantics.utils.ui.ExceptionUtils;
54
55 public class ComponentSelectionDialog extends Dialog implements ISelectionChangedListener {
56
57         private static final String DIALOG = "ComponentSelectionDialog"; //$NON-NLS-1$
58
59         private IDialogSettings dialogSettings;
60         
61         private double lengthFactor = 1.0;
62
63         private ResourceManager resourceManager;
64
65         private String libUri;
66
67         private Item selected;
68         private Set<PositionType> allowed;
69         private Set<PositionType> filterAllowed;
70         // private boolean positionAdjustment;
71         private PipelineComponent component;
72         private boolean insertAdjustable;
73         private boolean lenghtAdjustable;
74         private PositionType insertPosition = PositionType.NEXT;
75
76         private Double angle;
77         private Double length;
78         private Double rotationAngle;
79
80         private String name;
81         
82         private Text nameText;
83
84         // In-line component
85         private Text lengthText;
86         // Turn component
87         private Text angleText;
88         // Rotated In-line, or turn component.
89         private Text rotationAngleText;
90
91         // Input for new PipeRun
92         private Double diameter;
93         private Double thickness;
94         private Double turnRadius;
95
96         private Text diameterText;
97         private Text thicknessText;
98         private Text turnRadiusText;
99         
100         // Validation message label
101         private Label validationLabel;
102
103         // Position selection
104         private Button startButton;
105         private Button middleButton;
106         private Button endButton;
107
108         private boolean inlineSplit = false;
109
110         ListViewer inlineViewer;
111         ListViewer turnViewer;
112         ListViewer endViewer;
113
114         private Set<String> usedNames;
115
116         public ComponentSelectionDialog(Shell parentShell, Set<PositionType> allowed, PipelineComponent component) {
117                 this(parentShell, allowed, component, Plant3D.URIs.Builtin);
118         }
119
120         public ComponentSelectionDialog(Shell parentShell, Set<PositionType> allowed, PipelineComponent component,
121                         String libUri) {
122                 super(parentShell);
123                 this.allowed = allowed;
124                 this.component = component;
125                 filterAllowed = new HashSet<PositionType>();
126                 insertAdjustable = component instanceof InlineComponent ? ((InlineComponent) component).isVariableLength()
127                                 : false;
128                 lenghtAdjustable = false;
129                 this.libUri = libUri;
130
131                 usedNames = new HashSet<>();
132
133                 setShellStyle(getShellStyle() | SWT.RESIZE);
134
135                 IDialogSettings settings = Activator.getDefault().getDialogSettings();
136                 dialogSettings = settings.getSection(DIALOG);
137                 if (dialogSettings == null)
138                         dialogSettings = settings.addNewSection(DIALOG);
139                 
140                 if (component.getNext() != null && component.getPrevious() != null)
141                         insertPosition = PositionType.PREVIOUS;
142         }
143         
144         private void copyPipeRunParameters() {
145                 PipeRun run = component.getPipeRun();
146                 if (component.getNext() == null && component.getAlternativePipeRun() != null)
147                         run = component.getAlternativePipeRun();
148                 
149                 if (run == null)
150                         return;
151                 
152                 diameter = run.getPipeDiameter();
153                 thickness = run.getPipeThickness();
154                 turnRadius = run.getTurnRadius();
155                 
156                 diameterText.setText(Double.toString(diameter * lengthFactor));
157                 thicknessText.setText(Double.toString(thickness * lengthFactor));
158                 turnRadiusText.setText(Double.toString(turnRadius * lengthFactor));
159         }
160
161         public void setLengthFactor(double lengthFactor) {
162                 this.lengthFactor = lengthFactor;
163         }
164
165         @Override
166         protected IDialogSettings getDialogBoundsSettings() {
167                 return dialogSettings;
168         }
169
170         @Override
171         protected void configureShell(Shell newShell) {
172                 super.configureShell(newShell);
173                 newShell.setText("Create pipeline component");
174         }
175
176         public void addUsedNames(Collection<String> names) {
177                 usedNames.addAll(names);
178         }
179
180         protected List<Item> getItems(Class<?> c, String libUri) throws DatabaseException {
181                 Session session = Simantics.getSession();
182                 if (InlineComponent.class.equals(c)) {
183                         return P3DUtil.getInlines(session, libUri);
184                 } else if (TurnComponent.class.equals(c)) {
185                         return P3DUtil.getTurns(session, libUri);
186                 } else if (EndComponent.class.equals(c)) {
187                         return P3DUtil.getEnds(session, libUri);
188                 } else {
189                         return null;
190                 }
191         }
192
193         @Override
194         protected Control createDialogArea(Composite parent) {
195                 resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
196
197                 Composite composite = new Composite(parent, SWT.NONE);
198                 GridLayout layout = new GridLayout(2, false);
199                 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
200                 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
201                 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
202                 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
203                 composite.setLayout(layout);
204                 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
205                 applyDialogFont(composite);
206
207                 // Grid layout data for fields that grab horizontal space
208                 final GridDataFactory horizFillData = GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP);
209                 
210                 // TODO : we need better classification than inline,turn, and end:
211                 // * fixed length inlines
212                 // * fixed angle turns
213                 // * size changes (requires input for pipe run specs)
214                 // * variable length inlines (input for length)
215                 // * variable angle turns (input for angle)
216                 // * ends
217
218                 List<Item> ends = null;
219                 List<Item> turns = null;
220                 List<Item> inlines = null;
221                 try {
222                         ends = getItems(EndComponent.class, libUri);
223                         turns = getItems(TurnComponent.class, libUri);
224                         inlines = getItems(InlineComponent.class, libUri);
225                 } catch (DatabaseException e) {
226                         Label label = new Label(composite, SWT.NONE);
227                         label.setText("Cannot load pipeline components: " + e.getMessage());
228                         ExceptionUtils.logError(e);
229                         return composite;
230                 }
231                 ends = P3DUtil.filterUserComponents(ends);
232                 turns = P3DUtil.filterUserComponents(turns);
233                 inlines = P3DUtil.filterUserComponents(inlines);
234
235                 ExpandBar expandBar = new ExpandBar(composite, SWT.V_SCROLL);
236
237                 boolean isOpen = component.getNext() == null || component.getPrevious() == null;
238                 if (!inlines.isEmpty()) {
239                         if (!isOpen) {
240                                 // Remove variable length pipe from options
241                                 ListIterator<Item> it = inlines.listIterator();
242                                 while (it.hasNext()) {
243                                         Item item = it.next();
244                                         if (item.isVariable())
245                                                 it.remove();
246                                 }
247                         }
248                         
249                         inlineViewer = createItemList(expandBar, inlines, "Inline");
250                 }
251
252                 // Only allow elbows and ends to be added to open ends of the pipelines
253                 if (isOpen) {
254                         if (!turns.isEmpty()) {
255                                 turnViewer = createItemList(expandBar, turns, "Elbow");
256                         }
257                         
258                         if (!ends.isEmpty()) {
259                                 turnViewer = createItemList(expandBar, ends, "End");
260                         }
261                 }
262
263                 GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).span(2, 1).applyTo(expandBar);
264                 GridDataFactory.fillDefaults().grab(true, true).minSize(500, 500).hint(500, 500).applyTo(composite);
265
266                 Label label = new Label(composite, SWT.NONE);
267                 label.setText("Position");
268                 Composite buttonComposite = new Composite(composite, SWT.NONE);
269                 startButton = new Button(buttonComposite, SWT.TOGGLE);
270                 middleButton = new Button(buttonComposite, SWT.TOGGLE);
271                 endButton = new Button(buttonComposite, SWT.TOGGLE);
272                 startButton.setImage(resourceManager.createImage(
273                                 ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/insert_start.png").get()));
274                 middleButton.setImage(resourceManager.createImage(
275                                 ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/insert_middle.png").get()));
276                 endButton.setImage(resourceManager.createImage(
277                                 ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/insert_end.png").get()));
278                 startButton.setToolTipText("Overlapping insert");
279                 middleButton.setToolTipText("Cutting insert");
280                 endButton.setToolTipText("Adding insert");
281                 horizFillData.applyTo(buttonComposite);
282                 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite);
283
284                 startButton.setSelection(insertPosition == PositionType.PREVIOUS);
285                 middleButton.setSelection(insertPosition == PositionType.SPLIT);
286                 endButton.setSelection(insertPosition == PositionType.NEXT);
287                 
288                 startButton.setEnabled(false);
289                 middleButton.setEnabled(false);
290                 endButton.setEnabled(false);
291
292                 startButton.addSelectionListener(new SelectionAdapter() {
293                         @Override
294                         public void widgetSelected(SelectionEvent e) {
295                                 updateInsertPosition(PositionType.PREVIOUS);
296                         }
297                 });
298
299                 middleButton.addSelectionListener(new SelectionAdapter() {
300                         @Override
301                         public void widgetSelected(SelectionEvent e) {
302                                 updateInsertPosition(PositionType.SPLIT);
303                         }
304                 });
305                 endButton.addSelectionListener(new SelectionAdapter() {
306                         @Override
307                         public void widgetSelected(SelectionEvent e) {
308                                 updateInsertPosition(PositionType.NEXT);
309                         }
310                 });
311                 
312                 if (!hasInsertPosition()) {
313                         label.setVisible(false);
314                         buttonComposite.setVisible(false);
315                 }
316
317                 label = new Label(composite, SWT.NONE);
318                 label.setText("Name");
319                 nameText = new Text(composite, SWT.SINGLE | SWT.BORDER);
320
321                 label = new Label(composite, SWT.NONE);
322                 label.setText("Length");
323                 lengthText = new Text(composite, SWT.SINGLE | SWT.BORDER);
324                 label = new Label(composite, SWT.NONE);
325                 label.setText("Angle");
326                 angleText = new Text(composite, SWT.SINGLE | SWT.BORDER);
327                 label = new Label(composite, SWT.NONE);
328                 label.setText("Rotation angle");
329                 rotationAngleText = new Text(composite, SWT.SINGLE | SWT.BORDER);
330
331                 label = new Label(composite, SWT.NONE);
332                 label.setText("Diameter");
333                 diameterText = new Text(composite, SWT.SINGLE | SWT.BORDER);
334                 label = new Label(composite, SWT.NONE);
335                 label.setText("Wall thickness");
336                 thicknessText = new Text(composite, SWT.SINGLE | SWT.BORDER);
337                 label = new Label(composite, SWT.NONE);
338                 label.setText("Elbow radius");
339                 turnRadiusText = new Text(composite, SWT.SINGLE | SWT.BORDER);
340                 
341                 validationLabel = new Label(composite, SWT.NONE);
342                 validationLabel.setText("");
343
344                 lengthText.setEnabled(false);
345                 angleText.setEnabled(false);
346                 rotationAngleText.setEnabled(false);
347                 turnRadiusText.setEnabled(false);
348                 diameterText.setEnabled(false);
349                 thicknessText.setEnabled(false);
350
351                 copyPipeRunParameters();
352
353                 nameText.addKeyListener(new KeyAdapter() {
354                         @Override
355                         public void keyReleased(KeyEvent e) {
356                                 name = nameText.getText();
357                                 validate();
358                         }
359                 });
360                 
361                 lengthText.addKeyListener(new KeyAdapter() {
362                         @Override
363                         public void keyReleased(KeyEvent e) {
364                                 try {
365                                         length = Double.parseDouble(lengthText.getText()) / lengthFactor;
366                                 } catch (NumberFormatException err) {
367                                         length = null;
368                                 }
369                                 validate();
370                         }
371                 });
372
373                 angleText.addKeyListener(new KeyAdapter() {
374                         @Override
375                         public void keyReleased(KeyEvent e) {
376                                 try {
377                                         angle = Double.parseDouble(angleText.getText());
378                                 } catch (NumberFormatException err) {
379                                         angle = null;
380                                 }
381                                 validate();
382                         }
383                 });
384
385                 rotationAngleText.addKeyListener(new KeyAdapter() {
386                         @Override
387                         public void keyReleased(KeyEvent e) {
388                                 try {
389                                         rotationAngle = Double.parseDouble(rotationAngleText.getText());
390                                 } catch (NumberFormatException err) {
391                                         rotationAngle = null;
392                                 }
393                                 validate();
394                         }
395                 });
396
397                 diameterText.addKeyListener(new KeyAdapter() {
398                         @Override
399                         public void keyReleased(KeyEvent e) {
400                                 try {
401                                         diameter = Double.parseDouble(diameterText.getText()) / lengthFactor;
402                                 } catch (NumberFormatException err) {
403                                         diameter = null;
404                                 }
405                                 validate();
406                         }
407                 });
408                 
409                 thicknessText.addKeyListener(new KeyAdapter() {
410                         @Override
411                         public void keyReleased(KeyEvent e) {
412                                 try {
413                                         thickness = Double.parseDouble(thicknessText.getText()) / lengthFactor;
414                                 } catch (NumberFormatException err) {
415                                         thickness = null;
416                                 }
417                                 validate();
418                         }
419                 });
420
421                 turnRadiusText.addKeyListener(new KeyAdapter() {
422                         @Override
423                         public void keyReleased(KeyEvent e) {
424                                 try {
425                                         turnRadius = Double.parseDouble(turnRadiusText.getText()) / lengthFactor;
426                                 } catch (NumberFormatException err) {
427                                         turnRadius = null;
428                                 }
429                                 validate();
430                         }
431                 });
432
433                 horizFillData.applyTo(nameText);
434                 horizFillData.applyTo(lengthText);
435                 horizFillData.applyTo(angleText);
436                 horizFillData.applyTo(rotationAngleText);
437                 horizFillData.applyTo(diameterText);
438                 horizFillData.applyTo(thicknessText);
439                 horizFillData.applyTo(turnRadiusText);
440                 
441                 GridDataFactory.fillDefaults().span(2, 1).align(SWT.END, SWT.END).grab(true, false).applyTo(validationLabel);
442
443                 if (!allowed.contains(PositionType.NEXT) && !allowed.contains(PositionType.PREVIOUS)) {
444                         turnViewer.getList().setEnabled(false);
445                         endViewer.getList().setEnabled(false);
446                         inlineSplit = true;
447                 }
448
449                 return composite;
450         }
451
452         private ListViewer createItemList(ExpandBar expandBar, List<Item> items, String name) {
453                 ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
454                 expandItem.setText(name);
455                 ListViewer viewer = new ListViewer(expandBar);
456                 viewer.setLabelProvider(new ComponentLabelProvider());
457                 viewer.setContentProvider(new ComponentContentProvider());
458                 
459                 expandItem.setControl(viewer.getList());
460                 viewer.setInput(items);
461                 expandItem.setHeight(viewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
462                 viewer.addSelectionChangedListener(this);
463                 return viewer;
464         }
465
466         private boolean hasInsertPosition() {
467                 return component.getNext() == null || component.getPrevious() == null;
468         }
469
470         private void updateInsertPosition(PositionType type) {
471                 if (insertPosition == type)
472                         return;
473                 startButton.setSelection(type == PositionType.PREVIOUS);
474                 middleButton.setSelection(type == PositionType.SPLIT);
475                 endButton.setSelection(type == PositionType.NEXT);
476                 insertPosition = type;
477         }
478
479         @Override
480         public void selectionChanged(SelectionChangedEvent event) {
481                 IStructuredSelection sel = (IStructuredSelection) event.getSelection();
482                 Item i = (Item) sel.getFirstElement();
483                 if (i != null) {
484                         selected = i;
485                         if (inlineViewer != null && event.getSource() == inlineViewer) {
486                                 clearSelection(turnViewer);
487                                 clearSelection(endViewer);
488                         } else if (turnViewer != null && event.getSource() == turnViewer) {
489                                 clearSelection(inlineViewer);
490                                 clearSelection(endViewer);
491                         } else if (endViewer != null && event.getSource() == endViewer) {
492                                 clearSelection(inlineViewer);
493                                 clearSelection(turnViewer);
494                         }
495                         
496                         name = generateUniqueName(selected.getName());
497                         nameText.setText(name);
498                         
499                         validate();
500                 }
501         }
502
503         private void clearSelection(ListViewer possibleListViewer) {
504                 if (possibleListViewer != null)
505                         possibleListViewer.setSelection(new StructuredSelection());
506         }
507
508         private void validate() {
509                 filterAllowed.clear();
510                 Set<PositionType> filterAllowed = new HashSet<PositionType>();
511                 boolean ok = true;
512                 String msg = null;
513                 
514                 if (name.isEmpty() || usedNames.contains(name)) {
515                         ok = false;
516                         if (msg == null) {
517                                 if (name.isEmpty())
518                                         msg = "Please provide a valid name";
519                                 else
520                                         msg = "Name \"" + name + "\" is already in use";
521                         }
522                 }
523                 
524                 if (selected == null) {
525                         ok = false;
526                 } else if (selected.isCode()) {// TODO : instead of disabling the button, we should filter the content.
527                         ok = false;
528                 } else {
529                         lenghtAdjustable = ((selected.getType() == Type.INLINE)
530                                         && (selected.isVariable() || selected.isModifiable()));
531                         
532                         if (!hasInsertPosition()) {
533                                 // We are inserting to a fully connected variable length component
534                                 // only allow insertion within the component
535                                 startButton.setEnabled(false);
536                                 middleButton.setEnabled(false);
537                                 endButton.setEnabled(false);
538                                 updateInsertPosition(PositionType.PREVIOUS);
539                         } else if (insertAdjustable) {
540                                 switch (selected.getType()) {
541                                 case END:
542                                         startButton.setEnabled(false);
543                                         middleButton.setEnabled(false);
544                                         endButton.setEnabled(true);
545                                         updateInsertPosition(PositionType.NEXT);
546                                         break;
547                                 case INLINE:
548                                         if (!selected.isVariable()) {
549                                                 startButton.setEnabled(true);
550                                                 middleButton.setEnabled(true);
551                                                 endButton.setEnabled(true);
552                                         } else {
553                                                 startButton.setEnabled(false);
554                                                 middleButton.setEnabled(false);
555                                                 endButton.setEnabled(true);
556                                                 updateInsertPosition(PositionType.NEXT);
557                                         }
558                                         break;
559                                 case NOZZLE:
560                                         startButton.setEnabled(false);
561                                         middleButton.setEnabled(false);
562                                         endButton.setEnabled(true);
563                                         updateInsertPosition(PositionType.NEXT);
564                                         break;
565                                 case TURN:
566                                         startButton.setEnabled(false);
567                                         middleButton.setEnabled(true);
568                                         endButton.setEnabled(true);
569                                         if (insertPosition == PositionType.PREVIOUS)
570                                                 updateInsertPosition(PositionType.NEXT);
571                                         break;
572                                 case EQUIPMENT:
573                                         throw new RuntimeException("Expected component, got equipment " + selected);
574                                 }
575                         } else if (lenghtAdjustable) {
576                                 if (component instanceof InlineComponent) {
577                                         startButton.setEnabled(true);
578                                         middleButton.setEnabled(true);
579                                         endButton.setEnabled(true);
580                                 } else if (component instanceof TurnComponent) {
581                                         startButton.setEnabled(false);
582                                         middleButton.setEnabled(true);
583                                         endButton.setEnabled(true);
584                                         if (insertPosition == PositionType.PREVIOUS)
585                                                 updateInsertPosition(PositionType.NEXT);
586                                 } else if (component instanceof EndComponent || component instanceof Nozzle) {
587                                         startButton.setEnabled(false);
588                                         middleButton.setEnabled(false);
589                                         endButton.setEnabled(true);
590                                         updateInsertPosition(PositionType.NEXT);
591                                 }
592                         } else {
593                                 startButton.setEnabled(false);
594                                 middleButton.setEnabled(false);
595                                 endButton.setEnabled(true);
596                         }
597                         if (selected.isVariable() || selected.isModifiable()) {
598                                 if (selected.getType() == Type.INLINE) {
599                                         filterAllowed.add(PositionType.NEXT);
600                                         filterAllowed.add(PositionType.PREVIOUS);
601                                         if (inlineSplit && selected.isVariable()) {
602                                                 lengthText.setEnabled(false);
603                                                 angleText.setEnabled(false);
604                                                 rotationAngleText.setEnabled(selected.isRotated());
605                                                 ok = false;
606                                                 if (msg == null) msg = "Cannot split a straight pipe with a straight pipe";
607                                         } else {
608                                                 lengthText.setEnabled(true);
609                                                 angleText.setEnabled(false);
610                                                 rotationAngleText.setEnabled(selected.isRotated());
611                                                 if (length == null || length <= 0.0) {
612                                                         ok = false;
613                                                         if (msg == null) msg = "Please provide a valid length";
614                                                 }
615                                         }
616                                 } else if (selected.getType() == Type.TURN) {
617                                         filterAllowed.add(PositionType.NEXT);
618                                         filterAllowed.add(PositionType.PREVIOUS);
619                                         lengthText.setEnabled(false);
620                                         angleText.setEnabled(true);
621                                         rotationAngleText.setEnabled(true);
622                                         if (angle == null) {
623                                                 ok = false;
624                                                 if (msg == null) msg = "Please provide a turn angle";
625                                         }
626                                 } else {
627                                         // this should not happen, since end components should not have variable, or
628                                         // modifiable flag.
629                                         lengthText.setEnabled(false);
630                                         angleText.setEnabled(false);
631                                         rotationAngleText.setEnabled(false);
632
633                                 }
634                         } else {
635                                 lengthText.setEnabled(false);
636                                 angleText.setEnabled(false);
637                                 rotationAngleText.setEnabled(selected.getType() == Type.TURN || selected.isRotated());
638                         }
639
640                         if (selected.isSizeChange()) {
641                                 turnRadiusText.setEnabled(true);
642                                 diameterText.setEnabled(true);
643                                 thicknessText.setEnabled(true);
644                                 if (diameter == null || diameter <= 0.0) {
645                                         ok = false;
646                                         if (msg == null) msg = "Please provide a valid diameter";
647                                 }
648                                 if (turnRadius == null || diameter != null && turnRadius < diameter / 2) {
649                                         ok = false;
650                                         if (msg == null) msg = "Please provide a valid turn radius";
651                                 }
652                                 if (thickness == null || thickness < 0.0 || diameter != null && thickness >= diameter / 2) {
653                                         ok = false;
654                                         if (msg == null) msg = "Please provide a valid wall thickness";
655                                 }
656                         } else {
657                                 turnRadiusText.setEnabled(false);
658                                 diameterText.setEnabled(false);
659                                 thicknessText.setEnabled(false);
660                         }
661
662                         if (!selected.isVariable()) {
663                                 switch (selected.getType()) {
664                                 case END:
665                                         filterAllowed.add(PositionType.NEXT);
666                                         filterAllowed.add(PositionType.PREVIOUS);
667                                         break;
668                                 case NOZZLE:
669                                 case EQUIPMENT:
670                                         break;
671                                 case INLINE:
672                                         filterAllowed.add(PositionType.NEXT);
673                                         filterAllowed.add(PositionType.PREVIOUS);
674                                         filterAllowed.add(PositionType.SPLIT);
675                                 case TURN:
676                                         filterAllowed.add(PositionType.NEXT);
677                                         filterAllowed.add(PositionType.PREVIOUS);
678                                 }
679                         }
680                 }
681                 for (PositionType t : filterAllowed) {
682                         if (allowed.contains(t))
683                                 this.filterAllowed.add(t);
684                 }
685
686                 validationLabel.setText(msg != null ? msg : "");
687                 validationLabel.requestLayout();
688                 
689                 getButton(OK).setEnabled(ok);
690         }
691
692         private String generateUniqueName(String name) {
693                 int i = 1;
694                 String newName;
695                 while (usedNames.contains((newName = name + "_" + i)))
696                         i++;
697                 return newName;
698         }
699
700         public Item getSelected() {
701                 return selected;
702         }
703         
704         public String getName() {
705                 return name;
706         }
707
708         public Double getAngle() {
709                 return angle;
710         }
711
712         public Double getLength() {
713                 return length;
714         }
715
716         public Double getRotationAngle() {
717                 return rotationAngle;
718         }
719
720         public Double getDiameter() {
721                 return diameter;
722         }
723
724         public Double getTurnRadius() {
725                 return turnRadius;
726         }
727
728         public Set<PositionType> filterAllowed() {
729                 return filterAllowed;
730         }
731
732         public PositionType getInsertPosition() {
733                 return insertPosition;
734         }
735
736         public boolean isInsertAdjustable() {
737                 return insertAdjustable;
738         }
739
740         public boolean isLenghtAdjustable() {
741                 return lenghtAdjustable;
742         }
743
744         public Double getThickness() {
745                 return thickness;
746         }
747
748 }