From 69d724c31fcba3ee169cb4472b3cd70ff2eb8efe Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Tue, 9 Sep 2014 08:20:18 +0000
Subject: [PATCH] - GenericAnnotationView: Enable to restrict view to currently
 selected element type (e.g., components, channels, ...) -
 AnnotationViewPartBase: Generalize selectionChanged() to handle all graphical
 model elements (required for the above feature) refs 1841

---
 .../view/AnnotationViewPartBase.java          | 15 +---
 .../view/GenericAnnotationView.java           | 73 ++++++++++++++-----
 2 files changed, 59 insertions(+), 29 deletions(-)

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/AnnotationViewPartBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/AnnotationViewPartBase.java
index ef1bb3c3d..881276aaa 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/AnnotationViewPartBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/AnnotationViewPartBase.java
@@ -27,6 +27,7 @@ import org.eclipse.emf.common.notify.Adapter;
 import org.eclipse.emf.common.notify.Notification;
 import org.eclipse.emf.common.notify.impl.AdapterImpl;
 import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.ISelectionListener;
@@ -36,8 +37,6 @@ import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
 import org.fortiss.tooling.base.model.element.IModelElement;
 import org.fortiss.tooling.base.ui.annotation.AnnotationEntry;
 import org.fortiss.tooling.base.ui.annotation.IAnnotationValueService;
-import org.fortiss.tooling.base.ui.editpart.DiagramEditPartBase;
-import org.fortiss.tooling.base.ui.editpart.ElementEditPartBase;
 import org.fortiss.tooling.kernel.model.IProjectRootElement;
 import org.fortiss.tooling.kernel.service.IPersistencyService;
 import org.fortiss.tooling.kernel.ui.util.SelectionUtils;
@@ -103,18 +102,10 @@ public abstract class AnnotationViewPartBase extends ViewPart implements ISelect
 		currentlySelectedObject = SelectionUtils.checkAndPickFirst(selection, IModelElement.class);
 
 		if(currentlySelectedObject == null) {
-			ElementEditPartBase<?> editPart =
-					SelectionUtils.checkAndPickFirst(selection, ElementEditPartBase.class);
+			AbstractGraphicalEditPart editPart =
+					SelectionUtils.checkAndPickFirst(selection, AbstractGraphicalEditPart.class);
 			if(editPart != null && editPart.getModel() instanceof IModelElement) {
 				currentlySelectedObject = (IModelElement)editPart.getModel();
-			} else {
-				// Not all editors are derived from ElementEditPartBase, most notably
-				// PlatformArchitectureDiagramEditPart
-				DiagramEditPartBase<?> diagramPart =
-						SelectionUtils.checkAndPickFirst(selection, DiagramEditPartBase.class);
-				if(diagramPart != null && diagramPart.getModel() instanceof IModelElement) {
-					currentlySelectedObject = (IModelElement)diagramPart.getModel();
-				}
 			}
 		}
 		if(currentlySelectedObject != null) {
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/GenericAnnotationView.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/GenericAnnotationView.java
index f7ea75ddd..ffee2e86c 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/GenericAnnotationView.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/GenericAnnotationView.java
@@ -41,6 +41,7 @@ import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.swt.widgets.Text;
@@ -50,6 +51,7 @@ import org.fortiss.tooling.base.ui.annotation.AnnotationEntry;
 import org.fortiss.tooling.base.ui.annotation.editingsupport.ElementNameEditingSupport;
 import org.fortiss.tooling.base.ui.annotation.valueprovider.IAnnotationValueProvider;
 import org.fortiss.tooling.kernel.model.INamedElement;
+import org.fortiss.tooling.kernel.model.IProjectRootElement;
 
 /**
  * <p>
@@ -91,6 +93,12 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 	/** Flag whether columns should be filtered. */
 	boolean filterColumns;
 
+	/**
+	 * Flag whether the displayed rows should be restricted to the currently selected model element
+	 * type.
+	 */
+	boolean restrictToSelectedModelElementType;
+
 	/**
 	 * Last set of {@link AnnotationEntry}s used to {@link #update(Collection)} the
 	 * {@link GenericAnnotationView}, i.e. during the construction of the, or because of a model
@@ -321,10 +329,6 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 		/** {@inheritDoc} */
 		@Override
 		public boolean select(Viewer viewer, Object parentElement, Object element) {
-			if(!active) {
-				return true;
-			}
-
 			if(!(element instanceof AnnotationEntry)) {
 				return true;
 			}
@@ -334,13 +338,17 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 				return true;
 			}
 
-			return passesFilter(((INamedElement)annotationEntry.getModelElement()).getName(),
-					filterPattern);
+			return (!active || passesFilter(
+					((INamedElement)annotationEntry.getModelElement()).getName(), filterPattern)) &&
+					(!restrictToSelectedModelElementType || getCurrentlySelectedObject() == null ||
+							getCurrentlySelectedObject() instanceof IProjectRootElement || annotationEntry
+							.getModelElement().getClass()
+							.equals(getCurrentlySelectedObject().getClass()));
 		}
 	}
 
 	/**
-	 * Returns true if a given {@code input} passes an case-insensitive filter specified by
+	 * Returns {@code true} if a given {@code input} passes an case-insensitive filter specified by
 	 * {@code filterString}.
 	 */
 	private static boolean passesFilter(String input, String filterString) {
@@ -356,6 +364,14 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 		return input.toLowerCase().contains(filterString.toLowerCase());
 	}
 
+	/** Returns {@code true} if the column represented by the given column is visible in the view. */
+	private boolean passesFilter(ColumnHandle columnHandle, String filterPattern) {
+		if(!filterColumns) {
+			return true;
+		}
+		return passesFilter(getColumnName(columnHandle), filterPattern);
+	}
+
 	/** Updates the table after a filter change */
 	private void update() {
 		// Full update, i.e. including reconstructions of columns is always required due to the
@@ -406,8 +422,7 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 					for(String instanceKey : entry.getInstanceKeys(spec.getClass())) {
 						ColumnHandle columnHandle =
 								new ColumnHandle(entry, spec, instanceKey, false);
-						if(!filterColumns ||
-								passesFilter(getColumnName(columnHandle), filterPattern)) {
+						if(passesFilter(columnHandle, filterPattern)) {
 							sortedColumnHandles.add(columnHandle);
 						}
 					}
@@ -423,7 +438,7 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 							new ColumnHandle(entry, spec, null,
 									entry.allowsDynamicAnnotationInstances(spec.getClass()));
 
-					if(!filterColumns || passesFilter(getColumnName(columnHandle), filterPattern)) {
+					if(passesFilter(columnHandle, filterPattern)) {
 						sortedColumnHandles.add(columnHandle);
 					}
 
@@ -507,13 +522,18 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 		filterText.addListener(SWT.MouseDown, textSelectionOnFocusListener);
 		filterText.addListener(SWT.MouseUp, textSelectionOnFocusListener);
 
+		// Filter options
+		Composite filterOptionsComposite = new Composite(filterComposite, SWT.NULL);
+		filterOptionsComposite
+				.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+		filterOptionsComposite.setLayout(new RowLayout());
+
 		// Radio buttons to select between row and column filtering
-		Composite radioButtonComposite = new Composite(filterComposite, SWT.NULL);
-		radioButtonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
-		radioButtonComposite.setLayout(new RowLayout());
+		final Label filterLabel = new Label(filterOptionsComposite, SWT.NULL);
+		filterLabel.setText("Filter ");
 
-		final Button filterComponentNamesButton = new Button(radioButtonComposite, SWT.RADIO);
-		filterComponentNamesButton.setText("Component names");
+		final Button filterComponentNamesButton = new Button(filterOptionsComposite, SWT.RADIO);
+		filterComponentNamesButton.setText("component");
 		filterComponentNamesButton.setSelection(true);
 		filterComponentNamesButton.addSelectionListener(new SelectionAdapter() {
 
@@ -530,8 +550,8 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 			}
 		});
 
-		final Button filterAnnotationNamesButton = new Button(radioButtonComposite, SWT.RADIO);
-		filterAnnotationNamesButton.setText("Annotation names");
+		final Button filterAnnotationNamesButton = new Button(filterOptionsComposite, SWT.RADIO);
+		filterAnnotationNamesButton.setText("annotation names.");
 		filterAnnotationNamesButton.setSelection(false);
 		filterAnnotationNamesButton.addSelectionListener(new SelectionAdapter() {
 
@@ -547,6 +567,25 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
 				update();
 			}
 		});
+
+		final Label spacerLabel = new Label(filterOptionsComposite, SWT.NULL);
+		spacerLabel.setText("  ");
+
+		// Check box to restrict view to currently selected model element type
+		final Button filterModelElementType = new Button(filterOptionsComposite, SWT.CHECK);
+		filterModelElementType.setText("Show only selected model element type.");
+		filterModelElementType.setSelection(false);
+		filterModelElementType.addSelectionListener(new SelectionAdapter() {
+
+			@Override
+			public void widgetSelected(SelectionEvent e) {
+				// widgetSelected() is also fired when the selection is removed from the radio
+				// button, so check, if this is the button that just obtained the selection
+				restrictToSelectedModelElementType = ((Button)e.getSource()).getSelection();
+				update();
+			}
+		});
+
 	}
 
 	/** {@inheritDoc} */
-- 
GitLab