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