Revision: 3865
Author: [email protected]
Date: Tue Aug 10 14:52:03 2010
Log: Made the PlayPenLabel able to be dragged and deleted.
http://code.google.com/p/power-architect/source/detail?r=3865

Added:
/trunk/src/main/java/ca/sqlpower/architect/swingui/DraggablePlayPenComponent.java
Modified:
/trunk/regress/ca/sqlpower/architect/swingui/action/TestDeleteSelectedAction.java
 /trunk/src/main/java/ca/sqlpower/architect/swingui/ContainerPane.java
 /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPen.java
 /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenComponent.java
 /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenLabel.java
/trunk/src/main/java/ca/sqlpower/architect/swingui/action/DeleteSelectedAction.java /trunk/src/main/resources/ca/sqlpower/architect/swingui/action/messages.properties

=======================================
--- /dev/null
+++ /trunk/src/main/java/ca/sqlpower/architect/swingui/DraggablePlayPenComponent.java Tue Aug 10 14:52:03 2010
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2010, SQL Power Group Inc.
+ *
+ * This file is part of SQL Power Architect.
+ *
+ * SQL Power Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * SQL Power Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package ca.sqlpower.architect.swingui;
+
+import java.awt.Point;
+import java.util.Iterator;
+
+import org.apache.log4j.Logger;
+
+import ca.sqlpower.architect.swingui.PlayPen.FloatingContainerPaneListener;
+
+public abstract class DraggablePlayPenComponent extends PlayPenComponent {
+
+ private static final Logger logger = Logger.getLogger(DraggablePlayPenComponent.class);
+
+ protected DraggablePlayPenComponent(DraggablePlayPenComponent copyMe, PlayPenContentPane parent) {
+        super(copyMe, parent);
+    }
+
+    public DraggablePlayPenComponent(String name) {
+        super(name);
+    }
+
+ public DraggablePlayPenComponent(String name, PlayPenContentPane parent) {
+        super(name, parent);
+    }
+
+    /**
+     * Sets up the PlayPen's drag listener for all selected items. The drag
+     * listener will keep them at an offset from the mouse position.
+     *
+     * @param p
+ * The relative location of the mouse event, from the top left
+     *            corner of this component.
+     */
+    protected void setupDrag(Point p) {
+        PlayPen pp = getPlayPen();
+ Iterator<DraggablePlayPenComponent> it = pp.getSelectedDraggableComponents().iterator();
+        logger.debug("event point: " + p); //$NON-NLS-1$
+ logger.debug("zoomed event point: " + pp.zoomPoint(new Point(p))); //$NON-NLS-1$
+        pp.setDraggingContainerPanes(true);
+        startedDragging();
+
+        while (it.hasNext()) {
+            // create FloatingContainerPaneListener for each selected item
+            DraggablePlayPenComponent ppc = it.next();
+ logger.debug("(" + ppc.getModel() + ") zoomed selected containerPane's point: " + ppc.getLocationOnScreen()); //$NON-NLS-1$ //$NON-NLS-2$ + logger.debug("(" + ppc.getModel() + ") unzoomed selected containerPane's point: " + pp.unzoomPoint(ppc.getLocationOnScreen())); //$NON-NLS-1$ //$NON-NLS-2$ + /* the floating ContainerPane listener expects zoomed handles which are relative to + the location of the ContainerPane column which was clicked on. */
+            Point clickedItem = getLocationOnScreen();
+            Point otherContainer = ppc.getLocationOnScreen();
+ logger.debug("(" + ppc.getModel() + ") translation x=" //$NON-NLS-1$ //$NON-NLS-2$ + + (otherContainer.getX() - clickedItem.getX()) + ",y=" //$NON-NLS-1$
+                    + (otherContainer.getY() - clickedItem.getY()));
+            Point handle = pp.zoomPoint(new Point(p));
+ handle.translate((int)(clickedItem.getX() - otherContainer.getX()), (int) (clickedItem.getY() - otherContainer.getY()));
+            new FloatingContainerPaneListener(pp, ppc, handle);
+        }
+    }
+
+}
=======================================
--- /trunk/regress/ca/sqlpower/architect/swingui/action/TestDeleteSelectedAction.java Mon Jul 12 08:21:11 2010 +++ /trunk/regress/ca/sqlpower/architect/swingui/action/TestDeleteSelectedAction.java Tue Aug 10 14:52:03 2010
@@ -29,8 +29,8 @@
 import ca.sqlpower.architect.swingui.TablePane;
 import ca.sqlpower.architect.swingui.TestingArchitectSwingSessionContext;
 import ca.sqlpower.architect.swingui.event.SelectionEvent;
+import ca.sqlpower.object.SPObject;
 import ca.sqlpower.sqlobject.SQLColumn;
-import ca.sqlpower.sqlobject.SQLObject;
 import ca.sqlpower.sqlobject.SQLObjectException;
 import ca.sqlpower.sqlobject.SQLRelationship;
 import ca.sqlpower.sqlobject.SQLTable;
@@ -146,7 +146,7 @@
            tp.setSelected(true, SelectionEvent.SINGLE_SELECT);
            tp2.setSelected(true, SelectionEvent.SINGLE_SELECT);
            assertEquals(2, session.getDBTree().getSelectionPaths().length);
-           List<SQLObject> list = deleteAction.retrieveDeletableItems();
+           List<SPObject> list = deleteAction.retrieveDeletableItems();
            assertEquals(2, list.size());
            System.out.println(list);
            assertTrue(list.contains(tp.getModel()));
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/ContainerPane.java Thu May 6 15:50:49 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/ContainerPane.java Tue Aug 10 14:52:03 2010
@@ -42,12 +42,10 @@
 import java.util.Set;

 import javax.swing.JComponent;
-import javax.swing.SwingUtilities;

 import org.apache.log4j.Logger;

 import ca.sqlpower.architect.layout.LayoutNode;
-import ca.sqlpower.architect.swingui.PlayPen.FloatingContainerPaneListener;
 import ca.sqlpower.architect.swingui.PlayPen.MouseModeType;
 import ca.sqlpower.architect.swingui.event.ItemSelectionEvent;
 import ca.sqlpower.architect.swingui.event.ItemSelectionListener;
@@ -65,7 +63,7 @@
  * @param <C> Class of the an item.
  */
 public abstract class ContainerPane<T, C>
-extends PlayPenComponent
+extends DraggablePlayPenComponent
 implements DragSourceListener, LayoutNode {

     /**
@@ -231,28 +229,7 @@
             }

if (clickItem == ITEM_INDEX_TITLE && !pp.getSession().getArchitectFrame().createRelationshipIsActive()) { - Iterator<ContainerPane<?, ?> > it = pp.getSelectedContainers().iterator();
-                logger.debug("event point: " + p); //$NON-NLS-1$
- logger.debug("zoomed event point: " + pp.zoomPoint(new Point(p))); //$NON-NLS-1$
-                pp.setDraggingContainerPanes(true);
-                startedDragging();
-
-                while (it.hasNext()) {
- // create FloatingContainerPaneListener for each selected item - ContainerPane<?, ?> cp = (ContainerPane<?, ?> )it.next(); - logger.debug("(" + cp.getModel() + ") zoomed selected containerPane's point: " + cp.getLocationOnScreen()); //$NON-NLS-1$ //$NON-NLS-2$ - logger.debug("(" + cp.getModel() + ") unzoomed selected containerPane's point: " + pp.unzoomPoint(cp.getLocationOnScreen())); //$NON-NLS-1$ //$NON-NLS-2$ - /* the floating ContainerPane listener expects zoomed handles which are relative to - the location of the ContainerPane column which was clicked on. */
-                    Point clickedItem = getLocationOnScreen();
-                    Point otherContainer = cp.getLocationOnScreen();
- logger.debug("(" + cp.getModel() + ") translation x=" //$NON-NLS-1$ //$NON-NLS-2$ - + (otherContainer.getX() - clickedItem.getX()) + ",y=" //$NON-NLS-1$ - + (otherContainer.getY() - clickedItem.getY()));
-                    Point handle = pp.zoomPoint(new Point(p));
- handle.translate((int)(clickedItem.getX() - otherContainer.getX()), (int) (clickedItem.getY() - otherContainer.getY()));
-                    new FloatingContainerPaneListener(pp, cp, handle);
-                }
+                setupDrag(p);
             }
} else if (evt.getID() == MouseEvent.MOUSE_MOVED || evt.getID() == MouseEvent.MOUSE_DRAGGED) { setSelected(pp.rubberBand.intersects(getBounds(new Rectangle())),SelectionEvent.SINGLE_SELECT);
@@ -288,16 +265,6 @@
             r.setSelected(false,SelectionEvent.SINGLE_SELECT);
         }
     }
-
-    @Transient @Accessor
-    public Point getLocationOnScreen() {
-        Point p = new Point();
-        PlayPen pp = getPlayPen();
-        getLocation(p);
-        pp.zoomPoint(p);
-        SwingUtilities.convertPointToScreen(p, pp);
-        return p;
-    }

     /**
      * Sets the value of margin
@@ -654,5 +621,5 @@
     public String getNodeName() {
         return getName();
     }
-
-}
+
+}
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPen.java Mon Aug 9 15:23:27 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPen.java Tue Aug 10 14:52:03 2010
@@ -1951,6 +1951,10 @@
                }
                return Collections.unmodifiableList(selected);
        }
+
+       public List<DraggablePlayPenComponent> getSelectedDraggableComponents() 
{
+ return Collections.unmodifiableList(contentPane.getChildren(DraggablePlayPenComponent.class));
+       }

        /**
* Returns a read-only view of the set of selected relationships in the PlayPen.
@@ -2587,7 +2591,7 @@
private Insets scrollUnits = new Insets(AUTO_SCROLL_INSET, AUTO_SCROLL_INSET, AUTO_SCROLL_INSET, AUTO_SCROLL_INSET);

                private PlayPen pp;
-               private ContainerPane<?, ?> cp;
+               private PlayPenComponent ppc;
                private Point handle;
                private Point p;

@@ -2595,7 +2599,7 @@
* Creates a new mouse event handler that tracks mouse motion and moves
          * a container pane around on the play pen accordingly.
          *
-         * @param cp
+         * @param ppc
          *            The container pane that's going to be moved
          * @param handle
* The position relative to the container pane's top left
@@ -2613,21 +2617,21 @@
          *            table" type actions, and should be set to false for
          *            dragging existing objects.
          */
- public FloatingContainerPaneListener(PlayPen pp, ContainerPane<?, ?> cp, Point handle) { + public FloatingContainerPaneListener(PlayPen pp, PlayPenComponent ppc, Point handle) {
                        this.pp = pp;
                        Point pointerLocation = 
MouseInfo.getPointerInfo().getLocation();
                        
SwingUtilities.convertPointFromScreen(pointerLocation,pp);
logger.debug("Adding floating container pane at:"+ pointerLocation); //$NON-NLS-1$ p = new Point(pointerLocation.x - handle.x, pointerLocation.y - handle.y);

-                       this.cp = cp;
+                       this.ppc = ppc;
                        this.handle = handle;

                        pp.addMouseMotionListener(this);
                        pp.addMouseListener(this); // the click that ends this 
operation

                        pp.cursorManager.tableDragStarted();
-                       pp.startCompoundEdit("Move " + cp.getName()); 
//$NON-NLS-1$
+                       pp.startCompoundEdit("Move " + ppc.getName()); 
//$NON-NLS-1$
                }

                public void mouseMoved(MouseEvent e) {
@@ -2637,7 +2641,7 @@
                public void mouseDragged(MouseEvent e) {
                        pp.zoomPoint(e.getPoint());
                        p.setLocation(e.getPoint().x - handle.x, e.getPoint().y 
- handle.y);
-                       pp.setChildPosition(cp, p);
+                       pp.setChildPosition(ppc, p);
JViewport viewport = (JViewport)SwingUtilities.getAncestorOfClass(JViewport.class, pp);
                if(viewport==null || pp.getSelectedItems().size() < 1)
                    return;
@@ -2651,7 +2655,7 @@
                int viewWidth = viewport.getWidth();

                // performs scrolling
-               Rectangle bounds = pp.zoomRect(cp.getBounds());
+               Rectangle bounds = pp.zoomRect(ppc.getBounds());
if ((p.y - viewPos.y) < scrollUnits.top && viewPos.y > 0) { // scroll up
                 view.y = bounds.y - scrollUnits.top;
} if ((viewPos.y + viewHeight - p.y - bounds.height) < scrollUnits.bottom) { // scroll down
@@ -2664,8 +2668,8 @@
                logger.debug(viewport.getViewPosition());
                pp.scrollRectToVisible(view);
                // Necessary to stop tables from flashing.
-               if (cp != null) {
-                   cp.repaint();
+               if (ppc != null) {
+                   ppc.repaint();
                }
                }

@@ -2688,7 +2692,7 @@
                                pp.normalize();
                                pp.revalidate();
                        } finally {
- pp.endCompoundEdit("Ending move for table "+cp.getName()); //$NON-NLS-1$ + pp.endCompoundEdit("Ending move for table "+ppc.getName()); //$NON-NLS-1$
                        }
                }
        }
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenComponent.java Tue Aug 3 09:52:46 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenComponent.java Tue Aug 10 14:52:03 2010
@@ -38,6 +38,7 @@
 import javax.annotation.Nonnull;
 import javax.swing.JOptionPane;
 import javax.swing.JPopupMenu;
+import javax.swing.SwingUtilities;

 import org.apache.log4j.Logger;

@@ -758,5 +759,16 @@
     public Point getPointForModelObject(@Nonnull Object modelObject) {
         return getUI().getPointForModelObject(modelObject);
     }
+
+    @Transient
+    @Accessor
+    public Point getLocationOnScreen() {
+        Point p = new Point();
+        PlayPen pp = getPlayPen();
+        getLocation(p);
+        pp.zoomPoint(p);
+        SwingUtilities.convertPointToScreen(p, pp);
+        return p;
+    }

 }
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenLabel.java Mon Aug 9 15:23:27 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenLabel.java Tue Aug 10 14:52:03 2010
@@ -26,6 +26,7 @@
 import java.awt.Graphics2D;
 import java.awt.Insets;
 import java.awt.Point;
+import java.awt.Rectangle;
 import java.awt.event.ActionEvent;
 import java.awt.event.MouseEvent;
 import java.beans.PropertyChangeEvent;
@@ -45,7 +46,7 @@
 import ca.sqlpower.object.annotation.ConstructorParameter.ParameterType;
 import ca.sqlpower.swingui.ColourScheme;

-public class PlayPenLabel extends PlayPenComponent {
+public class PlayPenLabel extends DraggablePlayPenComponent {

     public static final List<Class<? extends SPObject>> allowedChildTypes =
Collections.unmodifiableList(new ArrayList<Class<? extends SPObject>>(
@@ -180,6 +181,10 @@
                     setSelected(true, SelectionEvent.SINGLE_SELECT);
                 }
             }
+ } else if (evt.getID() == MouseEvent.MOUSE_PRESSED && !pp.getSession().getArchitectFrame().createRelationshipIsActive()) {
+            setupDrag(p);
+ } else if (evt.getID() == MouseEvent.MOUSE_MOVED || evt.getID() == MouseEvent.MOUSE_DRAGGED) { + setSelected(pp.rubberBand.intersects(getBounds(new Rectangle())),SelectionEvent.SINGLE_SELECT);
         }
     }

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/action/DeleteSelectedAction.java Mon Jul 12 08:21:11 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/action/DeleteSelectedAction.java Tue Aug 10 14:52:03 2010
@@ -36,12 +36,14 @@
 import ca.sqlpower.architect.swingui.ContainerPane;
 import ca.sqlpower.architect.swingui.PlayPen;
 import ca.sqlpower.architect.swingui.PlayPenComponent;
+import ca.sqlpower.architect.swingui.PlayPenLabel;
 import ca.sqlpower.architect.swingui.TablePane;
 import ca.sqlpower.architect.swingui.event.ItemSelectionEvent;
 import ca.sqlpower.architect.swingui.event.ItemSelectionListener;
 import ca.sqlpower.architect.swingui.event.SelectionEvent;
 import ca.sqlpower.architect.swingui.event.SelectionListener;
 import ca.sqlpower.object.ObjectDependentException;
+import ca.sqlpower.object.SPObject;
 import ca.sqlpower.sqlobject.LockedColumnException;
 import ca.sqlpower.sqlobject.SQLColumn;
 import ca.sqlpower.sqlobject.SQLIndex;
@@ -89,14 +91,15 @@
             return;
         }

-        List<SQLObject> deleteItems = retrieveDeletableItems();
+        List<SPObject> deleteItems = retrieveDeletableItems();

         int tCount = 0;
         int rCount = 0;
         int cCount = 0;
         int iCount = 0;
-
-        for (SQLObject item : deleteItems) {
+        int lCount = 0;
+
+        for (SPObject item : deleteItems) {
             if (item instanceof SQLColumn) {
                 cCount++;
             } else if (item instanceof SQLTable) {
@@ -105,6 +108,8 @@
                 rCount++;
             } else if (item instanceof SQLIndex) {
                 iCount++;
+            } else if (item instanceof PlayPenLabel) {
+                lCount++;
             } else {
logger.warn("Unexpected type for deletable item: " + item.getClass());
             }
@@ -114,7 +119,8 @@
             int decision = JOptionPane.showConfirmDialog(frame,
Messages.getString("DeleteSelectedAction.multipleDeleteConfirmation", //$NON-NLS-1$
                             String.valueOf(tCount), String.valueOf(cCount),
- String.valueOf(rCount), String.valueOf(iCount)),
+                            String.valueOf(rCount), String.valueOf(iCount),
+                            String.valueOf(lCount)),
Messages.getString("DeleteSelectedAction.multipleDeleteDialogTitle"), //$NON-NLS-1$
                             JOptionPane.YES_NO_OPTION);
             if (decision != JOptionPane.YES_OPTION ) {
@@ -138,11 +144,13 @@
 //                ppc.setSelected(false,SelectionEvent.SINGLE_SELECT);
 //            }

-            for (SQLObject o : deleteItems) {
+            for (SPObject o : deleteItems) {
                 try {
                     if (o instanceof SQLIndex) {
                         SQLIndex index = (SQLIndex) o;
                         o.getParent().removeChild(o);
+                    } else if (o instanceof PlayPenLabel) {
+                        getPlaypen().getContentPane().removeChild(o);
                     } else {
//Side effect of removing a relationship's parent table is to remove the relationship //causing this to fail if the relationship is removed immediately after.
@@ -179,17 +187,18 @@
     }

     /**
-     * Extracts the list of items we should try to delete from the DBTree's
-     * selection list.
-     * Package private for testing.
+ * Extracts the list of items we should try to delete from the PlayPen's
+     * selection list. Package private for testing.
      */
-    List<SQLObject> retrieveDeletableItems() {
+    List<SPObject> retrieveDeletableItems() {
         List<PlayPenComponent> selection = getPlaypen().getSelectedItems();
- List<SQLObject> deleteItems = new ArrayList<SQLObject>(selection.size()); + List<SPObject> deleteItems = new ArrayList<SPObject>(selection.size());
         for (PlayPenComponent ppc : selection) {
             if (ppc instanceof TablePane) {
                 TablePane tp = (TablePane) ppc;
                 deleteItems.addAll(tp.getSelectedItems());
+            } else if (ppc instanceof PlayPenLabel) {
+                deleteItems.add(ppc);
             }
             Object model = ppc.getModel();
             if (model instanceof SQLObject) {
@@ -199,8 +208,8 @@

         Set<SQLTable> tablesWithSelectedColumns = new HashSet<SQLTable>();

- for (ListIterator<SQLObject> it = deleteItems.listIterator(); it.hasNext(); ) {
-            SQLObject item = it.next();
+ for (ListIterator<SPObject> it = deleteItems.listIterator(); it.hasNext(); ) {
+            SPObject item = it.next();
             if (item instanceof SQLColumn) {
tablesWithSelectedColumns.add(((SQLColumn) item).getParent());
             } else if (item instanceof SQLTable) {
@@ -209,6 +218,8 @@
                 // ok
             } else if (item instanceof SQLIndex) {
                 // ok
+            } else if (item instanceof PlayPenLabel) {
+                // ok
             } else {
// The business model is not fancy enough to properly cope with // deletion of other types using the removeChild() method yet.
@@ -257,13 +268,13 @@
      */
     private void setupAction() {
         String description;
-        List<SQLObject> deletableItems = retrieveDeletableItems();
+        List<SPObject> deletableItems = retrieveDeletableItems();
         if (deletableItems.size() == 0) {
             setEnabled(false);
description = Messages.getString("DeleteSelectedAction.deleteSelected"); //$NON-NLS-1$
         } else if (deletableItems.size() == 1) {
             setEnabled(true);
-            SQLObject item = deletableItems.get(0);
+            SPObject item = deletableItems.get(0);
             String name = item.getName();
description = Messages.getString("DeleteSelectedAction.deleteItem", name); //$NON-NLS-1$
         } else {
=======================================
--- /trunk/src/main/resources/ca/sqlpower/architect/swingui/action/messages.properties Mon Aug 9 15:23:27 2010 +++ /trunk/src/main/resources/ca/sqlpower/architect/swingui/action/messages.properties Tue Aug 10 14:52:03 2010
@@ -42,7 +42,7 @@
 DeleteSelectedAction.deleteNumberOfItems=Delete {0} items
 DeleteSelectedAction.deleteSelected=Delete Selected
 DeleteSelectedAction.description=Delete Selected
-DeleteSelectedAction.multipleDeleteConfirmation=Are you sure you want to delete these {0} tables, {1} columns, {2} relationships, and {3} indexes? +DeleteSelectedAction.multipleDeleteConfirmation=Are you sure you want to delete these {0} tables, {1} columns, {2} relationships, {3} indexes, and {4} labels?
 DeleteSelectedAction.multipleDeleteDialogTitle=Multiple Delete
 DeleteSelectedAction.name=Delete Selected
 DeleteSelectedAction.noItemsToDelete=No items to delete\!

Reply via email to