TA779169 Keep child elements expanded on stereotype search

Patch provided by Daniil Efremov (Xored Software Inc.)

Change-Id: I0be671612fd0069b709611ad4046a095a4ec1ee0
Signed-off-by: Daniel Johnson <danijoh2@cisco.com>
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/profile/stereotype/Stereotype.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/profile/stereotype/Stereotype.java
index 0f24f97..ec1dce9 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/profile/stereotype/Stereotype.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/profile/stereotype/Stereotype.java
@@ -14,6 +14,8 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.HashCodeBuilder;
 import org.dom4j.DocumentFactory;
 import org.dom4j.Element;
 import org.eclipse.tigerstripe.workbench.TigerstripeException;
@@ -398,14 +400,21 @@
 
     @Override
     public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
         if (obj instanceof Stereotype) {
             Stereotype st = (Stereotype) obj;
-            if (st.getName() != null)
-                return st.getName().equals(getName());
+            return StringUtils.equals(st.getName(), getName());
         }
 
         return false;
     }
+    
+    @Override
+    public int hashCode() {
+        return new HashCodeBuilder().append(getName()).toHashCode();
+    }
 
     public IStereotypeInstance makeInstance() {
         StereotypeInstance result = new StereotypeInstance(this);
diff --git a/plugins/org.eclipse.tigerstripe.workbench.ui.base/src/java/org/eclipse/tigerstripe/workbench/ui/internal/dialogs/BrowseForStereotypeDialog.java b/plugins/org.eclipse.tigerstripe.workbench.ui.base/src/java/org/eclipse/tigerstripe/workbench/ui/internal/dialogs/BrowseForStereotypeDialog.java
index aacf26d..12719ee 100644
--- a/plugins/org.eclipse.tigerstripe.workbench.ui.base/src/java/org/eclipse/tigerstripe/workbench/ui/internal/dialogs/BrowseForStereotypeDialog.java
+++ b/plugins/org.eclipse.tigerstripe.workbench.ui.base/src/java/org/eclipse/tigerstripe/workbench/ui/internal/dialogs/BrowseForStereotypeDialog.java
@@ -24,6 +24,8 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.preferences.ConfigurationScope;
+import org.eclipse.jdt.internal.corext.util.Strings;
+import org.eclipse.jdt.internal.ui.util.SWTUtil;
 import org.eclipse.jdt.internal.ui.viewsupport.FilteredElementTreeSelectionDialog;
 import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.preference.IPreferenceStore;
@@ -66,6 +68,8 @@
 import org.eclipse.tigerstripe.workbench.ui.internal.utils.ExpandableTreeListener;
 import org.eclipse.tigerstripe.workbench.ui.internal.utils.TreeExpandableActionsProvider;
 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
+import org.eclipse.ui.dialogs.FilteredTree;
+import org.eclipse.ui.dialogs.PatternFilter;
 import org.eclipse.ui.preferences.ScopedPreferenceStore;
 
 /**
@@ -80,11 +84,13 @@
     private static final String DEFAULT_CATEGORY = "Other";
     private static final String DLG_TITLE = "Stereotype Selection";
     private static final String DLG_MSG = "Please select a Stereotype";
+    private static final int DEFAULT_TREE_EXPAND_LEVEL = 2;
 
     private final Set<IStereotype> existingInstances;
     private final IStereotypeCapable component;
 
     private boolean showExistingStereotypes;
+    private TreeExpandableActionsProvider expanderProvider;
 
     /**
      * 
@@ -100,8 +106,8 @@
      * Opens up a dialog box with a list of available entities and returns the
      * EntityOptions that have been selected.
      * 
-     * @return EntityOption[] - Returns an array of EntityOption as selected
-     *         from the dialog
+     * @return EntityOption[] - Returns an array of EntityOption as selected from
+     *         the dialog
      */
     public IStereotype[] browseAvailableStereotypes(Shell parentShell) {
 
@@ -258,7 +264,7 @@
         @Override
         protected TreeViewer createTreeViewer(Composite parent) {
             actionBar = new ToolBar(parent, SWT.FLAT);
-            TreeExpandableActionsProvider expanderProvider = new TreeExpandableActionsProvider(actionBar);
+            expanderProvider = new TreeExpandableActionsProvider(actionBar);
 
             TreeViewer viewer = super.createTreeViewer(parent);
             viewer.addSelectionChangedListener(this);
@@ -274,11 +280,11 @@
             GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(showUsedCheckBox);
             showUsedCheckBox.setText("Show stereotypes in use");
             showUsedCheckBox.addSelectionListener(new SelectionAdapter() {
-               @Override
+                @Override
                 public void widgetSelected(SelectionEvent e) {
-                       showExistingStereotypes = showUsedCheckBox.getSelection();
-                       viewer.refresh();
-                       expanderProvider.updateExpandCollapseAllActions();
+                    showExistingStereotypes = showUsedCheckBox.getSelection();
+                    viewer.refresh();
+                    expanderProvider.updateExpandCollapseAllActions();
                 }
             });
 
@@ -288,6 +294,25 @@
         }
 
         @Override
+        protected TreeViewer doCreateTreeViewer(Composite parent, int style) {
+            PatternFilter filter = new PatternFilter();
+            FilteredTree tree = new FilteredTree(parent, style, filter, true) {
+                @Override
+                protected void updateToolbar(boolean visible) {
+                    super.updateToolbar(visible);
+                    treeViewer.expandToLevel(DEFAULT_TREE_EXPAND_LEVEL);
+                    expanderProvider.updateExpandCollapseAllActions();
+                }
+            };
+            tree.setLayoutData(new GridData(GridData.FILL_BOTH));
+            applyDialogFont(tree);
+
+            TreeViewer viewer = tree.getViewer();
+            SWTUtil.setAccessibilityText(viewer.getControl(), Strings.removeMnemonicIndicator(super.getMessage()));
+            return viewer;
+        }
+
+        @Override
         protected Control createDialogArea(Composite parent) {
             parentComposite = parent;
             parentComposite.addHelpListener(helpListener);