Author: truesweetman
Date: Wed Aug 27 11:32:58 2008
New Revision: 2627

Modified:
   trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPlayPenFactory.java
trunk/src/ca/sqlpower/architect/swingui/olap/action/OLAPDeleteSelectedAction.java

Log:
Fixed a two bugs with synchronization between OLAPTree and playPen.

Previously when you selected a level or hierarchy that was in a dimension, which was in a cube, it would try to select it on the playpen, but it does not exist on the playpen.

Also edited synchro such that if you select an item on the tree that is not represented in the playpen, then it will deselect all items in the playpen.

Modified: trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPlayPenFactory.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPlayPenFactory.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPlayPenFactory.java Wed Aug 27 11:32:58 2008
@@ -43,12 +43,8 @@
 import ca.sqlpower.architect.olap.OLAPObject;
 import ca.sqlpower.architect.olap.OLAPUtil;
 import ca.sqlpower.architect.olap.MondrianModel.Cube;
-import ca.sqlpower.architect.olap.MondrianModel.CubeDimension;
 import ca.sqlpower.architect.olap.MondrianModel.Hierarchy;
 import ca.sqlpower.architect.olap.MondrianModel.Level;
-import ca.sqlpower.architect.olap.MondrianModel.Measure;
-import ca.sqlpower.architect.olap.MondrianModel.VirtualCube;
-import ca.sqlpower.architect.olap.MondrianModel.VirtualCubeMeasure;
 import ca.sqlpower.architect.swingui.ArchitectSwingSession;
 import ca.sqlpower.architect.swingui.PlayPen;
 import ca.sqlpower.architect.swingui.PlayPenComponent;
@@ -323,13 +319,7 @@
                 List<OLAPObject> objects = new ArrayList<OLAPObject>();
                 for (TreePath tp : treePaths) {
OLAPObject obj = (OLAPObject) tp.getLastPathComponent();
-                    // only select playpen represented objects.
- if ((obj instanceof Cube || obj instanceof VirtualCube || obj instanceof Measure - || obj instanceof CubeDimension || obj instanceof VirtualCubeMeasure - || obj instanceof Level || obj instanceof Hierarchy) &&
-                            !objects.contains(obj)) {
-                        objects.add(obj);
-                    }
+                    objects.add(obj);
                 }
                 try {
                     pp.selectObjects(objects, tree);

Modified: trunk/src/ca/sqlpower/architect/swingui/olap/action/OLAPDeleteSelectedAction.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/olap/action/OLAPDeleteSelectedAction.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/olap/action/OLAPDeleteSelectedAction.java Wed Aug 27 11:32:58 2008
@@ -20,48 +20,126 @@
 package ca.sqlpower.architect.swingui.olap.action;

 import java.awt.event.ActionEvent;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Set;

 import javax.swing.JOptionPane;
 import javax.swing.tree.TreePath;

+import org.apache.log4j.Logger;
+
 import ca.sqlpower.architect.olap.OLAPObject;
+import ca.sqlpower.architect.olap.MondrianModel.Cube;
+import ca.sqlpower.architect.olap.MondrianModel.Dimension;
+import ca.sqlpower.architect.olap.MondrianModel.DimensionUsage;
+import ca.sqlpower.architect.olap.MondrianModel.Hierarchy;
+import ca.sqlpower.architect.olap.MondrianModel.Level;
+import ca.sqlpower.architect.olap.MondrianModel.Measure;
+import ca.sqlpower.architect.olap.MondrianModel.VirtualCubeDimension;
+import ca.sqlpower.architect.olap.MondrianModel.VirtualCubeMeasure;
 import ca.sqlpower.architect.swingui.ArchitectSwingSession;
 import ca.sqlpower.architect.swingui.action.AbstractArchitectAction;
 import ca.sqlpower.architect.swingui.olap.OLAPEditSession;
+import ca.sqlpower.architect.swingui.olap.OLAPTree;

 /**
* This action deletes OLAPObjects that are selected on the OLAPTree using removeChild methods.
  */
 public class OLAPDeleteSelectedAction extends AbstractArchitectAction {

+ private static final Logger logger = Logger.getLogger(OLAPDeleteSelectedAction.class);
+
     private final OLAPEditSession editSession;

+    private OLAPTree tree;
+
public OLAPDeleteSelectedAction(ArchitectSwingSession session, OLAPEditSession editSession) { super(session, editSession.getOlapPlayPen(), "Delete Selected", "Delete selected item.", (String) null);
         this.editSession = editSession;
+        this.tree = editSession.getOlapTree();
     }

     public void actionPerformed(ActionEvent arg0) {
-        TreePath[] tps = editSession.getOlapTree().getSelectionPaths();
-        if (tps.length > 1 ) {
- int decision = JOptionPane.showConfirmDialog(editSession.getDialog(), "Are you sure you want to delete the following " + tps.length + " items?",
+        List<OLAPObject> itemsToDelete = getDeletableItems();
+        if (itemsToDelete.size() > 1 ) {
+ int decision = JOptionPane.showConfirmDialog(editSession.getDialog(), "Are you sure you want to delete the following " + itemsToDelete.size() + " items?",
                             "Multiple Delete", JOptionPane.YES_NO_OPTION);
             if (decision != JOptionPane.YES_OPTION ) {
                 return;
             }
-        } else if (tps.length < 1) {
+        } else if (itemsToDelete.size() < 1) {
             JOptionPane.showMessageDialog(playpen, "No items to delete!");
             return;
         }
         try {
             playpen.startCompoundEdit("OLAP Delete");
-            for (TreePath tp : tps) {
-                OLAPObject obj = (OLAPObject) tp.getLastPathComponent();
-                obj.getParent().removeChild(obj);
+            for (OLAPObject oo : itemsToDelete) {
+                try {
+                    oo.getParent().removeChild(oo);
+                } catch (NullPointerException ex) {
+                    System.out.println("who dares be null!");
+                    System.out.println(oo);
+                    System.out.println(oo.getParent());
+
+                }
             }
         } finally {
             playpen.endCompoundEdit("OLAP Delete End");
         }

     }
+
+    /**
+ * Extracts the list of items we should try to delete from the OLAPTree's
+     * selection list.
+     */
+    private List<OLAPObject> getDeletableItems() {
+        TreePath[] selectionPaths = tree.getSelectionPaths();
+        if (selectionPaths == null) return Collections.emptyList();
+ List <OLAPObject> deleteItems = new ArrayList<OLAPObject>(selectionPaths.length);
+        for (int i = 0; i < selectionPaths.length; i++) {
+            if (   selectionPaths[i].getPathCount() > 1 &&
+ selectionPaths[i].getPathComponent(0) == editSession.getOlapSession().getSchema()) { + deleteItems.add((OLAPObject) selectionPaths[i].getLastPathComponent());
+            } else {
+                logger.debug("Skipping non-deletable object: " +
+                        selectionPaths[i].getLastPathComponent());
+            }
+        }
+
+ Set<OLAPObject> objectsWithSelectedItems = new HashSet<OLAPObject>();
+
+ for (ListIterator<OLAPObject> it = deleteItems.listIterator(); it.hasNext(); ) {
+            OLAPObject item = it.next();
+            if (item instanceof Dimension) {
+                if (item.getParent() instanceof Cube) {
+                    objectsWithSelectedItems.add(item.getParent());
+                }
+ } else if (item instanceof Measure || item instanceof DimensionUsage) {
+                objectsWithSelectedItems.add(item.getParent());
+ } else if (item instanceof VirtualCubeDimension || item instanceof VirtualCubeMeasure) {
+                objectsWithSelectedItems.add(item.getParent());
+            } else if (item instanceof Hierarchy) {
+                objectsWithSelectedItems.add(item.getParent());
+            } else if (item instanceof Level){
+                // Level is inside Hierarchy which is inside Dimension
+ // Hierarchy is not selected when Level is, but Dimension is.
+                objectsWithSelectedItems.add(item.getParent().getParent());
+            } else {
+                // for now, allow any other item to be deleted.
+            }
+        }
+
+ // When a item is selected in the playpen that is contained in a cubePane, + // virtualCubePane or DimensionPane, then the matching pane is also selected. + // In this case, we want to delete the selected item(s) but NOT the pane!
+        deleteItems.removeAll(objectsWithSelectedItems);
+
+        return deleteItems;
+    }
+
 }

Reply via email to