]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Only show elbows and ends when adding to an open pipe end. 25/4125/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 20 Apr 2020 08:20:45 +0000 (11:20 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 20 Apr 2020 10:49:16 +0000 (10:49 +0000)
gitlab #120

Change-Id: I947fdfa5939ce110cfa82310e9437f6e281e4599
(cherry picked from commit f5438b7d35bfb66014369bae22dc3daddbff6498)

org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java

index cce00075faf8b1940ab2da5988f4dbbad5d92df3..fc3f0cefc587f16e785a8973bfd2bd0fbc84e931 100644 (file)
@@ -215,39 +215,21 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange
 
                ExpandBar expandBar = new ExpandBar(composite, SWT.V_SCROLL);
 
-               ExpandItem inlineItem = new ExpandItem(expandBar, SWT.NONE);
-               inlineItem.setText("Inline");
-               inlineViewer = new ListViewer(expandBar);
-               inlineViewer.setLabelProvider(new ComponentLabelProvider());
-               inlineViewer.setContentProvider(new ComponentContentProvider());
-
-               ExpandItem turnItem = new ExpandItem(expandBar, SWT.NONE);
-               turnItem.setText("Elbow");
-               turnViewer = new ListViewer(expandBar);
-               turnViewer.setLabelProvider(new ComponentLabelProvider());
-               turnViewer.setContentProvider(new ComponentContentProvider());
-
-               ExpandItem endItem = new ExpandItem(expandBar, SWT.NONE);
-               endItem.setText("End");
-               endViewer = new ListViewer(expandBar);
-               endViewer.setLabelProvider(new ComponentLabelProvider());
-               endViewer.setContentProvider(new ComponentContentProvider());
-
-               inlineItem.setControl(inlineViewer.getList());
-               turnItem.setControl(turnViewer.getList());
-               endItem.setControl(endViewer.getList());
-
-               inlineViewer.setInput(inlines);
-               turnViewer.setInput(turns);
-               endViewer.setInput(ends);
-
-               inlineItem.setHeight(inlineViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
-               turnItem.setHeight(turnViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
-               endItem.setHeight(endViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
-
-               inlineViewer.addSelectionChangedListener(this);
-               turnViewer.addSelectionChangedListener(this);
-               endViewer.addSelectionChangedListener(this);
+               if (!inlines.isEmpty()) {
+                       inlineViewer = createItemList(expandBar, inlines, "Inline");
+               }
+
+               // Only allow elbows and ends to be added to open ends of the pipelines
+               boolean isOpen = component.getNext() == null || component.getPrevious() == null;
+               if (isOpen) {
+                       if (!turns.isEmpty()) {
+                               turnViewer = createItemList(expandBar, turns, "Elbow");
+                       }
+                       
+                       if (!ends.isEmpty()) {
+                               turnViewer = createItemList(expandBar, ends, "End");
+                       }
+               }
 
                GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).span(2, 1).applyTo(expandBar);
                GridDataFactory.fillDefaults().grab(true, true).minSize(500, 500).hint(500, 500).applyTo(composite);
@@ -436,6 +418,20 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange
                return composite;
        }
 
+       private ListViewer createItemList(ExpandBar expandBar, List<Item> items, String name) {
+               ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
+               expandItem.setText(name);
+               ListViewer viewer = new ListViewer(expandBar);
+               viewer.setLabelProvider(new ComponentLabelProvider());
+               viewer.setContentProvider(new ComponentContentProvider());
+               
+               expandItem.setControl(viewer.getList());
+               viewer.setInput(items);
+               expandItem.setHeight(viewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
+               viewer.addSelectionChangedListener(this);
+               return viewer;
+       }
+
        private boolean hasInsertPosition() {
                return component.getNext() == null || component.getPrevious() == null;
        }
@@ -455,15 +451,15 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange
                Item i = (Item) sel.getFirstElement();
                if (i != null) {
                        selected = i;
-                       if (event.getSource() == inlineViewer) {
-                               turnViewer.setSelection(new StructuredSelection());
-                               endViewer.setSelection(new StructuredSelection());
-                       } else if (event.getSource() == turnViewer) {
-                               inlineViewer.setSelection(new StructuredSelection());
-                               endViewer.setSelection(new StructuredSelection());
-                       } else if (event.getSource() == endViewer) {
-                               inlineViewer.setSelection(new StructuredSelection());
-                               turnViewer.setSelection(new StructuredSelection());
+                       if (inlineViewer != null && event.getSource() == inlineViewer) {
+                               clearSelection(turnViewer);
+                               clearSelection(endViewer);
+                       } else if (turnViewer != null && event.getSource() == turnViewer) {
+                               clearSelection(inlineViewer);
+                               clearSelection(endViewer);
+                       } else if (endViewer != null && event.getSource() == endViewer) {
+                               clearSelection(inlineViewer);
+                               clearSelection(turnViewer);
                        }
                        
                        name = generateUniqueName(selected.getName());
@@ -473,6 +469,11 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange
                }
        }
 
+       private void clearSelection(ListViewer possibleListViewer) {
+               if (possibleListViewer != null)
+                       possibleListViewer.setSelection(new StructuredSelection());
+       }
+
        private void validate() {
                filterAllowed.clear();
                Set<PositionType> filterAllowed = new HashSet<PositionType>();