Author: truesweetman
Date: Fri Aug 29 14:55:51 2008
New Revision: 2673

Modified:
   trunk/src/ca/sqlpower/architect/swingui/PlayPen.java
   trunk/src/ca/sqlpower/architect/swingui/olap/DimensionPane.java
   trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPane.java
   trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPlayPenFactory.java
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateLevelAction.java

Log:
Fixed Hierarchy and Level drag and drop!
It now drags and drops properly. In order for this to work, some DnD methods were overwritten in DimensionPane.

Modified: trunk/src/ca/sqlpower/architect/swingui/PlayPen.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/PlayPen.java        (original)
+++ trunk/src/ca/sqlpower/architect/swingui/PlayPen.java Fri Aug 29 14:55:51 2008
@@ -2814,7 +2814,7 @@
                     }

                     // Get the hierarchies from the sections and add them.
- for (PaneSection<? extends Level> hs : dp.getSelectedSections()) { + for (PaneSection<? extends OLAPObject> hs : dp.getSelectedSections()) {
                         if (hs instanceof HierarchySection) {
if (!selections.contains(((HierarchySection) hs).getHierarchy()) && !extraSelections.contains(((HierarchySection) hs).getHierarchy())) {

Modified: trunk/src/ca/sqlpower/architect/swingui/olap/DimensionPane.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/olap/DimensionPane.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/olap/DimensionPane.java Fri Aug 29 14:55:51 2008
@@ -22,6 +22,8 @@
 import java.util.ArrayList;
 import java.util.List;

+import org.apache.log4j.Logger;
+
 import ca.sqlpower.architect.ArchitectException;
 import ca.sqlpower.architect.olap.OLAPChildEvent;
 import ca.sqlpower.architect.olap.OLAPChildListener;
@@ -39,8 +41,9 @@
* hierarchies of the Dimension object, and the items in each section are the
  * levels of the corresponding hierarchy.
  */
-public class DimensionPane extends OLAPPane<Dimension, Level> {
+public class DimensionPane extends OLAPPane<Dimension, OLAPObject> {

+ private static final Logger logger = Logger.getLogger(DimensionPane.class);

     private class HierarchyWatcher implements OLAPChildListener {

@@ -106,12 +109,13 @@
     }

     @Override
-    protected List<Level> getItems() {
-        List<Level> levels = new ArrayList<Level>();
+    protected List<OLAPObject> getItems() {
+        List<OLAPObject> oos = new ArrayList<OLAPObject>();
         for (Hierarchy h : model.getHierarchies()) {
-            levels.addAll(h.getLevels());
+            oos.add(h);
+            oos.addAll(h.getLevels());
         }
-        return levels;
+        return oos;
     }

// ---------------------- PlayPenComponent Overrides ----------------------
@@ -129,7 +133,7 @@
     }

     @Override
- public DataEntryPanel createEditDialog(PlayPenCoordinate<Dimension, Level> coord) throws ArchitectException { + public DataEntryPanel createEditDialog(PlayPenCoordinate<Dimension, OLAPObject> coord) throws ArchitectException {
         DataEntryPanel panel;
         // TODO add getName() method to DataEntryPanel.
         if (coord.getIndex() == PlayPenCoordinate.ITEM_INDEX_TITLE) {
@@ -154,11 +158,13 @@
     }

     @Override
-    protected List<Level> filterDroppableItems(List<OLAPObject> items) {
-        List<Level> filtered = new ArrayList<Level>();
+ protected List<OLAPObject> filterDroppableItems(List<OLAPObject> items) {
+        List<OLAPObject> filtered = new ArrayList<OLAPObject>();
         for (OLAPObject item : items) {
             if (item instanceof Level) {
                 filtered.add((Level) item);
+            } else if (item instanceof Hierarchy) {
+                filtered.add((Hierarchy) item);
             }
         }
         return filtered;
@@ -169,7 +175,7 @@
      * Returns null if it was not found.
      */
     public HierarchySection findSection(Hierarchy hierarchy) {
-        for (PaneSection<? extends Level> hs : sections) {
+        for (PaneSection<? extends OLAPObject> hs : sections) {
            if (hs instanceof HierarchySection) {
                if (((HierarchySection) hs).getHierarchy() == hierarchy){
                    return ((HierarchySection) hs);
@@ -177,5 +183,60 @@
            }
         }
         return null;
+    }
+
+    /**
+     * Returns a list of levels which are selected.
+     */
+    public List<Level> getSelectedLevels() {
+        List<Level> selectedItems = new ArrayList<Level>();
+        for (int i=0; i < getItems().size(); i++) {
+            if (isItemSelected(i) && getItems().get(i) instanceof Level) {
+                selectedItems.add((Level) getItems().get(i));
+            }
+        }
+        return selectedItems;
+    }
+
+    @Override
+    protected List<OLAPObject> getItemsFromCoordinates(
+ List<PlayPenCoordinate<? extends OLAPObject, ? extends OLAPObject>> coords) {
+        List<OLAPObject> items = new ArrayList<OLAPObject>();
+ for (PlayPenCoordinate<? extends OLAPObject, ? extends OLAPObject> coord : coords) { + if (coord.getIndex() == PlayPenCoordinate.ITEM_INDEX_SECTION_TITLE) { + // Only add sections which are HierarchySections because they are + // also OLAPObjects. If it is a Hierarchy, then we do not want to + // move its levels as well because they come with the Hierarchy
+                // anyways.
+ items.add(((HierarchySection) coord.getSection()).getHierarchy());
+            } else if (coord.getIndex() >= 0) {
+                if (coord.getItem() == null) {
+                    throw new NullPointerException(
+                            "Found a coordinate with nonnegative " +
+                            "item index but null item: " + coord);
+                }
+                items.add(coord.getItem());
+            }
+        }
+        return items;
+    }
+
+    @Override
+ protected void transferInvalidIndexItem(OLAPObject item, PaneSection<OLAPObject> insertSection) {
+        item.getParent().removeChild(item);
+        if (item instanceof Level) {
+            if (insertSection == null || sections.isEmpty()) {
+ // If a pane has no sections, then we must add one to put the + // item in. If it had no sections, must be a DimensionPane and
+                // therefore has only HierarchySections.
+                Hierarchy hier = new Hierarchy();
+                hier.setHasAll(true);
+                getModel().addChild(hier);
+                insertSection = (PaneSection<OLAPObject>) sections.get(0);
+            }
+            insertSection.addItem(item);
+        } else {
+            getModel().addChild(item);
+        }
     }
 }

Modified: trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPane.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPane.java  (original)
+++ trunk/src/ca/sqlpower/architect/swingui/olap/OLAPPane.java Fri Aug 29 14:55:51 2008
@@ -474,22 +474,7 @@
List<PlayPenCoordinate<? extends OLAPObject, ? extends OLAPObject>> coords =
                 DnDOLAPTransferable.resolve(getPlayPen(), paths);
             logger.debug("Resolved Paths = " + coords);
-            List<OLAPObject> items = new ArrayList<OLAPObject>();
- for (PlayPenCoordinate<? extends OLAPObject, ? extends OLAPObject> coord : coords) { - if (coord.getIndex() == PlayPenCoordinate.ITEM_INDEX_SECTION_TITLE) {
-                    for (OLAPObject item : coord.getSection().getItems()) {
-                        items.add(item);
-                    }
-                } else if (coord.getIndex() >= 0) {
-                    if (coord.getItem() == null) {
-                        throw new NullPointerException(
-                                "Found a coordinate with nonnegative " +
-                                "item index but null item: " + coord);
-                    }
-                    items.add(coord.getItem());
-                }
-            }
-
+            List<OLAPObject> items = getItemsFromCoordinates(coords);
             List<C> acceptedItems = filterDroppableItems(items);

// XXX we don't want to weaken the type here (PaneSection<C> would be better)
@@ -519,22 +504,24 @@
* has to be adjusted to account for the subsequent items
                      * shifting up to take the place of the removed item.
                      */
- int removedItemIndex = insertSection.getItems().indexOf(item);
+                    int removedItemIndex = 0;
+                    if (insertSection != null) {
+ removedItemIndex = insertSection.getItems().indexOf(item);
+                    }
logger.debug("Removed item index in target section: " + removedItemIndex);

-                    item.getParent().removeChild(item);
-
-                    if (removedItemIndex >= 0 &&
+                    if (insertSection != null && removedItemIndex >= 0 &&
                             insertSection.getItemType().isInstance(item) &&
                             insertIndex > removedItemIndex) {
                         insertIndex--;
                     }
                 }

- if (insertIndex >= 0 && insertSection.getItemType().isInstance(item)) { + if (insertSection != null && insertIndex >= 0 && insertSection.getItemType().isInstance(item)) {
+                    item.getParent().removeChild(item);
                     insertSection.addItem(insertIndex++, item);
                 } else {
-                    getModel().addChild(item);
+                    transferInvalidIndexItem(item, insertSection);
                 }
             }

@@ -554,6 +541,45 @@
             setInsertionPoint(null);
             schema.endCompoundEdit();
         }
+    }
+
+    /**
+     * Returns a list of OLAPObjects that correspond to the given
+     * PlayPenCoordinates.
+     *
+     * @param coords
+     *            The list of PlayPenCoordinates to convert.
+     * @return A list of OLAPObjects that correspond to the given
+     *         PlayPenCoordinates.
+     */
+ protected List<OLAPObject> getItemsFromCoordinates(List<PlayPenCoordinate<? extends OLAPObject, ? extends OLAPObject>> coords) {
+        List<OLAPObject> items = new ArrayList<OLAPObject>();
+ for (PlayPenCoordinate<? extends OLAPObject, ? extends OLAPObject> coord : coords) { + if (coord.getIndex() == PlayPenCoordinate.ITEM_INDEX_SECTION_TITLE) {
+                for (OLAPObject item : coord.getSection().getItems()) {
+                    items.add(item);
+                }
+            } else if (coord.getIndex() >= 0) {
+                if (coord.getItem() == null) {
+                    throw new NullPointerException(
+                            "Found a coordinate with nonnegative " +
+                            "item index but null item: " + coord);
+                }
+                items.add(coord.getItem());
+            }
+        }
+        return items;
+    }
+
+    /**
+     * Handle Drag and Drop transfer for items with invalid index.
+     *
+     * @param item The item to be transferred.
+     * @param insertSection The section to be inserted into.
+     */
+ protected void transferInvalidIndexItem(OLAPObject item, PaneSection<OLAPObject> insertSection) {
+        item.getParent().removeChild(item);
+        getModel().addChild(item);
     }

     /**

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 Fri Aug 29 14:55:51 2008
@@ -45,7 +45,6 @@
 import ca.sqlpower.architect.olap.OLAPUtil;
 import ca.sqlpower.architect.olap.MondrianModel.Cube;
 import ca.sqlpower.architect.olap.MondrianModel.Hierarchy;
-import ca.sqlpower.architect.olap.MondrianModel.Level;
 import ca.sqlpower.architect.swingui.ArchitectSwingSession;
 import ca.sqlpower.architect.swingui.PlayPen;
 import ca.sqlpower.architect.swingui.PlayPenComponent;
@@ -298,7 +297,7 @@
                             lastPath = tp;
                         }
                     }
- for (PaneSection<? extends Level> sect :((DimensionPane) comp).getSelectedSections()) { + for (PaneSection<? extends OLAPObject> sect :((DimensionPane) comp).getSelectedSections()) {
                         Hierarchy hierarchy;
                         if (sect instanceof HierarchySection) {
hierarchy = ((HierarchySection) sect).getHierarchy();

Modified: trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateLevelAction.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateLevelAction.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateLevelAction.java Fri Aug 29 14:55:51 2008
@@ -54,7 +54,7 @@
         int newIndex;

         // If there are levels selected, we'll add after the last one
-        List<Level> levels = pane.getSelectedItems();
+        List<Level> levels = pane.getSelectedLevels();
         if (!levels.isEmpty()) {
             Level addAfter = levels.get(levels.size() - 1);
             newParent = (Hierarchy) addAfter.getParent();

Reply via email to