From: Tuukka Lehtonen Date: Mon, 26 Sep 2016 13:07:37 +0000 (+0300) Subject: Allow selection of modules to rename through checkboxes and Select All/Clear Selectio... X-Git-Tag: v1.25.0~91^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=d1a82fe1414c77b97bec886d6a3ae3c5d926c334 Allow selection of modules to rename through checkboxes and Select All/Clear Selection buttons. refs #6708 [PRIVATE-11320] --- diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/RenameDiagramComponents.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/RenameDiagramComponents.java index fd824f2bc..5801213e8 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/RenameDiagramComponents.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/RenameDiagramComponents.java @@ -8,7 +8,7 @@ import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.ActionFactory; @@ -30,12 +30,11 @@ public class RenameDiagramComponents implements ActionFactory { return new Runnable() { @Override public void run() { - final ComponentsRenamingModel model = new ComponentsRenamingModel(); try { - Simantics.getSession().syncRequest(new ReadRequest() { + ComponentsRenamingModel model = Simantics.getSession().syncRequest(new UniqueRead() { @Override - public void run(ReadGraph graph) throws DatabaseException { - model.read(graph, composite); + public ComponentsRenamingModel perform(ReadGraph graph) throws DatabaseException { + return new ComponentsRenamingModel().read(graph, composite); } }); ComponentsRenamingDialog dialog = new ComponentsRenamingDialog( diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingDialog.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingDialog.java index 8a73bed10..2af483185 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingDialog.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingDialog.java @@ -1,11 +1,18 @@ package org.simantics.modeling.ui.diagram.renaming; +import java.util.HashSet; +import java.util.Set; + import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ArrayContentProvider; +import org.eclipse.jface.viewers.CheckStateChangedEvent; +import org.eclipse.jface.viewers.CheckboxTableViewer; import org.eclipse.jface.viewers.ColumnLabelProvider; -import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.ICheckStateListener; +import org.eclipse.jface.viewers.ICheckStateProvider; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; @@ -14,6 +21,7 @@ import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -21,6 +29,7 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.simantics.utils.ui.ISelectionUtils; public class ComponentsRenamingDialog extends Dialog { @@ -33,7 +42,7 @@ public class ComponentsRenamingDialog extends Dialog { Text newNamePrefix; Label errorLabel; Text error; - TableViewer tableViewer; + CheckboxTableViewer tableViewer; public ComponentsRenamingDialog(Shell parentShell, ComponentsRenamingModel model) { super(parentShell); @@ -126,11 +135,23 @@ public class ComponentsRenamingDialog extends Dialog { // Name table - tableViewer = new TableViewer(composite); + tableViewer = CheckboxTableViewer.newCheckList(composite, + SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); + tableViewer.getTable().setHeaderVisible(true); GridDataFactory.fillDefaults().grab(true, true).applyTo(tableViewer.getTable()); tableViewer.setContentProvider(ArrayContentProvider.getInstance()); + tableViewer.setCheckStateProvider(new ICheckStateProvider() { + @Override + public boolean isGrayed(Object element) { + return false; + } + @Override + public boolean isChecked(Object element) { + return model.selectedEntries.contains(element); + } + }); TableViewerColumn oldNameColumn = new TableViewerColumn(tableViewer, SWT.NONE); oldNameColumn.getColumn().setText("Old name"); @@ -153,9 +174,59 @@ public class ComponentsRenamingDialog extends Dialog { return entry.newName; } }); + tableViewer.addCheckStateListener(new ICheckStateListener() { + void addOrRemoveSelection(NameEntry entry, boolean add) { + if (add) + model.selectedEntries.add(entry); + else + model.selectedEntries.remove(entry); + } + @Override + public void checkStateChanged(CheckStateChangedEvent event) { + final boolean checked = event.getChecked(); + NameEntry checkedNode = (NameEntry) event.getElement(); + + Set entries = new HashSet(); + Set selection = ISelectionUtils.filterSetSelection(tableViewer.getSelection(), NameEntry.class); + if (selection.contains(checkedNode)) + entries.addAll(selection); + else + tableViewer.setSelection(StructuredSelection.EMPTY); + entries.add(checkedNode); + + for (NameEntry entry : entries) { + addOrRemoveSelection(entry, checked); + } + + tableViewer.refresh(); + } + }); tableViewer.setInput(model.entries.toArray()); newNamePrefix.setFocus(); - + + // Select All/None button bar + Composite bar = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(bar); + bar.setLayout(new RowLayout()); + Button selectAll = new Button(bar, SWT.PUSH); + selectAll.setText("Select &All"); + selectAll.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + model.selectedEntries.addAll(model.entries); + tableViewer.setAllChecked(true); + } + }); + Button clearSelection = new Button(bar, SWT.PUSH); + clearSelection.setText("&Clear Selection"); + clearSelection.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + model.selectedEntries.clear(); + tableViewer.setAllChecked(false); + } + }); + return composite; } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java index 23d2ffa83..4df5d4ec0 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java @@ -2,7 +2,9 @@ package org.simantics.modeling.ui.diagram.renaming; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -31,6 +33,7 @@ import org.simantics.structural.stubs.StructuralResource2; */ public class ComponentsRenamingModel { public ArrayList entries = new ArrayList(); + public Set selectedEntries = new HashSet<>(); public String oldNamePrefix; public String newNamePrefix; public boolean reset; @@ -41,7 +44,7 @@ public class ComponentsRenamingModel { private Resource configuration; private ComponentNamingStrategy namingStrategy; - public void read(ReadGraph g, Resource composite) throws DatabaseException { + public ComponentsRenamingModel read(ReadGraph g, Resource composite) throws DatabaseException { this.session = g.getSession(); this.compositeVariable = Variables.getVariable(g, composite); this.configuration = g.syncRequest(new Configuration(composite)); @@ -68,6 +71,11 @@ public class ComponentsRenamingModel { prefixValidator = displayValue.getPossiblePropertyValue(g, Variables.INPUT_VALIDATOR); this.namingStrategy = ComponentNamingUtil.findNamingStrategy(g, null, composite); + + // By default, select all entries. + this.selectedEntries.addAll(entries); + + return this; } public void computeNewNames() { @@ -125,7 +133,7 @@ public class ComponentsRenamingModel { Layer0 L0 = Layer0.getInstance(g); Layer0X L0X = Layer0X.getInstance(g); for(NameEntry entry : entries) - if(!entry.oldName.equals(entry.newName)) + if(!entry.oldName.equals(entry.newName) && selectedEntries.contains(entry)) g.claimLiteral(entry.resource, L0.HasName, entry.newName, Bindings.STRING); if(!oldNamePrefix.equals(newNamePrefix)) compositeVariable.setPropertyValue(g, L0X.HasGeneratedNamePrefix, newNamePrefix, Bindings.STRING);