Refactoring of Enum selector to be usable independent from AgileGrid.
diff --git a/org.eclipse.rmf.reqif10.pror.editor/src/org/eclipse/rmf/reqif10/pror/editor/agilegrid/ProrEnumerationMultiValueCellEditor.java b/org.eclipse.rmf.reqif10.pror.editor/src/org/eclipse/rmf/reqif10/pror/editor/agilegrid/ProrEnumerationMultiValueCellEditor.java
index 9f29b7d..e04c311 100644
--- a/org.eclipse.rmf.reqif10.pror.editor/src/org/eclipse/rmf/reqif10/pror/editor/agilegrid/ProrEnumerationMultiValueCellEditor.java
+++ b/org.eclipse.rmf.reqif10.pror.editor/src/org/eclipse/rmf/reqif10/pror/editor/agilegrid/ProrEnumerationMultiValueCellEditor.java
@@ -21,27 +21,14 @@
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.rmf.reqif10.AttributeValueEnumeration;
import org.eclipse.rmf.reqif10.DatatypeDefinitionEnumeration;
-import org.eclipse.rmf.reqif10.EnumValue;
import org.eclipse.rmf.reqif10.ReqIF10Package;
+import org.eclipse.rmf.reqif10.pror.editor.presentation.EnumSelector;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.layout.FillLayout;
-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;
import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Table;
-import org.eclipse.swt.widgets.TableItem;
-import org.eclipse.ui.ISharedImages;
-import org.eclipse.ui.PlatformUI;
public class ProrEnumerationMultiValueCellEditor extends PopupCellEditor {
-
- private ArrayList<EnumValue> itemList = new ArrayList<EnumValue>();
private final DatatypeDefinitionEnumeration dde;
/**
@@ -51,6 +38,7 @@
private final EditingDomain editingDomain;
private Object parent;
private Object affectedObject;
+ private EnumSelector selector;
public ProrEnumerationMultiValueCellEditor(AgileGrid agileGrid,
DatatypeDefinitionEnumeration dde,
@@ -65,70 +53,12 @@
@Override
protected Control createContents(Composite parent) {
- Composite composite = new Composite(parent, SWT.BORDER_SOLID);
- GridLayout layout = new GridLayout();
- layout.marginHeight = 0;
- layout.marginWidth = 0;
- layout.horizontalSpacing = 0;
- layout.verticalSpacing = 5;
- composite.setLayout(layout);
- Table table = new Table(composite, SWT.CHECK);
- GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
- gd.heightHint = 150;
- table.setLayoutData(gd);
- table.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- if (e.detail == SWT.CHECK && e.item instanceof TableItem) {
- TableItem item = (TableItem) e.item;
- if (item.getChecked()) {
- itemList.add((EnumValue) item.getData());
- } else {
- itemList.remove((EnumValue) item.getData());
- }
- }
- }
- });
-
- for (EnumValue enumValue : dde.getSpecifiedValues()) {
- TableItem tableItem = new TableItem(table, SWT.NONE);
- String enumId = enumValue.getLongName() == null ? enumValue.getIdentifier() : enumValue.getLongName();
- tableItem.setText(enumId);
- tableItem.setData(enumValue);
- if (attributeValue.getValues().contains(enumValue)) {
- tableItem.setChecked(true);
- itemList.add(enumValue);
- }
- }
-
- Composite buttonPanel = new Composite(composite, SWT.NONE);
- buttonPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- buttonPanel.setLayout(new FillLayout(SWT.HORIZONTAL));
- Button cancel = new Button(buttonPanel, SWT.PUSH);
- cancel.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE));
- cancel.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- fireCancelEditor();
- }
- });
-
- Button ok = new Button(buttonPanel, SWT.PUSH);
- ok.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ETOOL_SAVE_EDIT));
- ok.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- fireApplyEditorValue();
- }
- });
- return composite;
+ selector = new EnumSelector(dde.getSpecifiedValues(), attributeValue.getValues(), parent, SWT.BORDER_SOLID);
+ return selector;
}
@Override
protected Object doGetValue() {
- if (itemList == null) {
- return super.doGetValue();
- }
CompoundCommand cmd = new CompoundCommand("Set Enumeration") {
public java.util.Collection<?> getAffectedObjects() {
@@ -150,7 +80,7 @@
.create(editingDomain,
attributeValue,
ReqIF10Package.Literals.ATTRIBUTE_VALUE_ENUMERATION__VALUES,
- itemList));
+ selector.getItems()));
editingDomain.getCommandStack().execute(cmd);
return attributeValue;
@@ -166,7 +96,7 @@
@Override
protected void fireCancelEditor() {
- itemList = null;
+// itemList = null;
super.fireCancelEditor();
}
/**
@@ -176,17 +106,11 @@
@Override
protected void openPopupBox(Control parent) {
super.openPopupBox(parent);
-
- // Workaround to prevent shell from closing right after opening on Linux.
- Display display = Display.getCurrent();
- Shell shell = display.getActiveShell();
- shell.pack();
-
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch()) {
- display.sleep();
- }
+ int result = selector.showEnumSelector(Display.getCurrent().getActiveShell());
+ if (result == SWT.CANCEL) {
+ fireCancelEditor();
+ } else {
+ fireApplyEditorValue();
}
}
-
}
diff --git a/org.eclipse.rmf.reqif10.pror.editor/src/org/eclipse/rmf/reqif10/pror/editor/presentation/EnumSelector.java b/org.eclipse.rmf.reqif10.pror.editor/src/org/eclipse/rmf/reqif10/pror/editor/presentation/EnumSelector.java
new file mode 100644
index 0000000..87dc3fe
--- /dev/null
+++ b/org.eclipse.rmf.reqif10.pror.editor/src/org/eclipse/rmf/reqif10/pror/editor/presentation/EnumSelector.java
@@ -0,0 +1,131 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Formal Mind GmbH.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Michael Jastram - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.rmf.reqif10.pror.editor.presentation;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.rmf.reqif10.EnumValue;
+import org.eclipse.rmf.reqif10.pror.editor.agilegrid.ProrEnumerationMultiValueCellEditor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.FillLayout;
+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.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableItem;
+import org.eclipse.ui.ISharedImages;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * This -Control represents a GUI element that allows the selection of {@link EnumValue}s.
+ * It offers a save and cancel button, and is designed to be shown in a {@link Shell}.
+ *
+ * It is written so that it can be used by AgileGrid (see {@link ProrEnumerationMultiValueCellEditor}
+ * or stand-alone (e.g. in a search interface).
+ *
+ * @author jastram
+ */
+public class EnumSelector extends Composite {
+
+ private ArrayList<EnumValue> itemList = new ArrayList<EnumValue>();
+ private int status = SWT.OK;
+ private Collection<EnumValue> originalSelection;
+
+
+ public EnumSelector(List<EnumValue> items,
+ Collection<EnumValue> selection, Composite parent, int style) {
+ super(parent, style);
+ originalSelection = selection;
+
+ GridLayout layout = new GridLayout();
+ layout.marginHeight = 0;
+ layout.marginWidth = 0;
+ layout.horizontalSpacing = 0;
+ layout.verticalSpacing = 5;
+ setLayout(layout);
+ Table table = new Table(this, SWT.CHECK);
+ GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
+ gd.heightHint = 150;
+ table.setLayoutData(gd);
+ table.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ if (e.detail == SWT.CHECK && e.item instanceof TableItem) {
+ TableItem item = (TableItem) e.item;
+ if (item.getChecked()) {
+ itemList.add((EnumValue) item.getData());
+ } else {
+ itemList.remove((EnumValue) item.getData());
+ }
+ }
+ }
+ });
+
+ for (EnumValue enumValue : items) {
+ TableItem tableItem = new TableItem(table, SWT.NONE);
+ String enumId = enumValue.getLongName() == null ? enumValue.getIdentifier() : enumValue.getLongName();
+ tableItem.setText(enumId);
+ tableItem.setData(enumValue);
+ if (selection.contains(enumValue)) {
+ tableItem.setChecked(true);
+ itemList.add(enumValue);
+ }
+ }
+
+ Composite buttonPanel = new Composite(this, SWT.NONE);
+ buttonPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+ buttonPanel.setLayout(new FillLayout(SWT.HORIZONTAL));
+ Button cancel = new Button(buttonPanel, SWT.PUSH);
+ cancel.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE));
+ cancel.addSelectionListener(new SelectionAdapter() {
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ status = SWT.CANCEL;
+ Display.getCurrent().getActiveShell().close();
+ }
+ });
+
+ Button ok = new Button(buttonPanel, SWT.PUSH);
+ ok.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ETOOL_SAVE_EDIT));
+ ok.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ status = SWT.OK;
+ Display.getCurrent().getActiveShell().close();
+ }
+ });
+ }
+
+ public int showEnumSelector(Shell shell) {
+ shell.pack();
+
+ while (!shell.isDisposed()) {
+ if (!Display.getCurrent().readAndDispatch()) {
+ Display.getCurrent().sleep();
+ }
+ }
+
+ return status;
+ }
+
+ public Collection<EnumValue> getItems() {
+ return status == SWT.OK ? itemList : originalSelection;
+ }
+
+}
\ No newline at end of file