From 515d7d358947acda751add591c8f43ebb8d52ef4 Mon Sep 17 00:00:00 2001 From: Jussi Koskela Date: Mon, 17 Aug 2020 13:35:01 +0300 Subject: [PATCH] Layer removal with delete key instead of on-screen button gitlab simantics/platform#585 Change-Id: I9b33c995cec4fc4f3fc342e09e1980d885a9ea4b --- .../ui/diagramEditor/DiagramLayersPage.java | 145 ++++++++---------- 1 file changed, 66 insertions(+), 79 deletions(-) diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java index 3bce16ccb..bfbfacbc7 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramLayersPage.java @@ -26,7 +26,6 @@ import org.eclipse.jface.viewers.ICheckStateProvider; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.ITreeContentProvider; -import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerCell; @@ -40,6 +39,8 @@ import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -382,73 +383,12 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { private void initialize2(ILayersEditor layers, IDiagram diagram) { layers.addLayersListener(layersListener); - Button addButton = new Button(composite, SWT.NONE); - addButton.setText(Messages.DiagramLayersPage_New); - addButton.setToolTipText(Messages.DiagramLayersPage_NewTT); - addButton.addSelectionListener(new SelectionListener() { - - @Override - public void widgetSelected(SelectionEvent e) { - String name = findFreshName(layers, Messages.DiagramLayersPage_NewRole); - SimpleLayer layer = new SimpleLayer(name); - layers.addLayer(layer); - layers.activate(layer); - } - - @Override - public void widgetDefaultSelected(SelectionEvent e) { - widgetSelected(e); - } - - }); - - final Button removeButton = new Button(composite, SWT.NONE); - removeButton.setText(Messages.DiagramLayersPage_Remove); - removeButton.setToolTipText(Messages.DiagramLayersPage_RemoveTT); - removeButton.addSelectionListener(new SelectionListener() { - @Override - public void widgetSelected(SelectionEvent e) { - TreeItem[] items = viewer.getTree().getSelection(); - if (items.length == 0) - return; - - TreeItem[] all = viewer.getTree().getItems(); - int firstIndex = Arrays.indexOf(all, items[0]); - for (TreeItem item : items) { - int index = Arrays.indexOf(all, item); - all[index] = null; - ILayer layer = (ILayer)item.getData(); - layers.removeLayer(layer); - } - int selectIndex = firstIndex - 1; - if (firstIndex == 0) { - for (int i = firstIndex; i < all.length; ++i) - if (all[i] != null) { - selectIndex = i; - break; - } - } - if (selectIndex >= 0) { - viewer.getTree().select(all[selectIndex]); - } - context.getThreadAccess().asyncExec(new Runnable() { - - @Override - public void run() { - if(context.isDisposed()) return; - context.getContentContext().setDirty(); - } - - }); - } - - @Override - public void widgetDefaultSelected(SelectionEvent e) { - widgetSelected(e); - } - }); - + GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, false); + composite.setLayoutData(gridData); + composite.setLayout(new GridLayout(3, false)); + ignoreVisibilityButton = new Button(composite, SWT.CHECK); + ignoreVisibilityButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1)); ignoreVisibilityButton.setText(TEXT_IGNORE_VISIBILITY_SETTINGS); ignoreVisibilityButton.setToolTipText(TOOLTIP_IGNORE_VISIBILITY_SETTINGS); ignoreVisibilityButton.setSelection(layers.getIgnoreVisibilitySettings()); @@ -476,6 +416,7 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { }); ignoreFocusButton = new Button(composite, SWT.CHECK); + ignoreFocusButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1)); ignoreFocusButton.setText(TEXT_IGNORE_FOCUS_SETTINGS); ignoreFocusButton.setToolTipText(TOOLTIP_IGNORE_FOCUS_SETTINGS); ignoreFocusButton.setSelection(layers.getIgnoreFocusSettings()); @@ -502,6 +443,27 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { }); + Button addButton = new Button(composite, SWT.NONE); + addButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); + addButton.setText(Messages.DiagramLayersPage_New); + addButton.setToolTipText(Messages.DiagramLayersPage_NewTT); + addButton.addSelectionListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + String name = findFreshName(layers, Messages.DiagramLayersPage_NewRole); + SimpleLayer layer = new SimpleLayer(name); + layers.addLayer(layer); + layers.activate(layer); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + widgetSelected(e); + } + + }); + viewer = new CheckboxTreeViewer(composite, SWT.BORDER | SWT.FULL_SELECTION ); GridDataFactory.fillDefaults().grab(true, true).span(4, 1).applyTo(viewer.getControl()); @@ -596,6 +558,8 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { return; startEditing(layer, item); + } else if (event.keyCode == SWT.DEL) { + removeSelectedLayers(); } } }); @@ -672,18 +636,6 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { } }); - viewer.addSelectionChangedListener(new ISelectionChangedListener() { - @Override - public void selectionChanged(SelectionChangedEvent event) { - ISelection s = event.getSelection(); - if (s.isEmpty()) { - removeButton.setEnabled(false); - } else { - removeButton.setEnabled(true); - } - } - }); - viewer.setContentProvider(new ITreeContentProvider(){ @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { @@ -767,6 +719,41 @@ public class DiagramLayersPage extends Page implements ILayersViewPage { public void setSelection(ISelection selection) { } + private void removeSelectedLayers() { + TreeItem[] items = viewer.getTree().getSelection(); + if (items.length == 0) + return; + + TreeItem[] all = viewer.getTree().getItems(); + int firstIndex = Arrays.indexOf(all, items[0]); + for (TreeItem item : items) { + int index = Arrays.indexOf(all, item); + all[index] = null; + ILayer layer = (ILayer)item.getData(); + layers.removeLayer(layer); + } + int selectIndex = firstIndex - 1; + if (firstIndex == 0) { + for (int i = firstIndex; i < all.length; ++i) + if (all[i] != null) { + selectIndex = i; + break; + } + } + if (selectIndex >= 0) { + viewer.getTree().select(all[selectIndex]); + } + context.getThreadAccess().asyncExec(new Runnable() { + + @Override + public void run() { + if(context.isDisposed()) return; + context.getContentContext().setDirty(); + } + + }); + } + private boolean startEditing(final ILayer layer, final TreeItem item/*, final int columnIndex*/) { // Column column = columns[columnIndex]; -- 2.47.1