[Schmitzm-commits] r2163 - in trunk/schmitzm-core/src/main/java/de/schmitzm/swing: . dnd
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Sun Dec 9 00:02:14 CET 2012
Author: mojays
Date: 2012-12-09 00:02:14 +0100 (Sun, 09 Dec 2012)
New Revision: 2163
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/JListTransferHandler.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/JTableTransferHandler.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/ListingTransferHandler.java
Log:
SwingUtil: new method getParentScrollPane(.)
ListingTransferHandler: handling on drop on JScrollPane (empty table)
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java 2012-12-08 22:50:40 UTC (rev 2162)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java 2012-12-08 23:02:14 UTC (rev 2163)
@@ -398,7 +398,22 @@
return frame;
}
- /**
+ /**
+ * Liefert das {@link JScrollPane}, das eine Kompoenente beinhaltet.
+ *
+ * @param comp
+ * eine GUI-Komponente
+ * @return <code>null</code> falls die Komponente in keinem {@link JScrollPane}
+ * enthalten ist
+ */
+ public static JScrollPane getParentScrollPane(Component comp) {
+ // durch die Parents laufen, bis ScrollPane gefunden
+ while (comp != null && !(comp instanceof JScrollPane))
+ comp = comp.getParent();
+ return (JScrollPane)comp;
+ }
+
+ /**
* Liefert das Fenster, das eine GUI-Komponente beinhaltet. Wenn
* GUI-Komponente selber ein {@link Window} ist, wird diese zurückgelifert.
*
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/JListTransferHandler.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/JListTransferHandler.java 2012-12-08 22:50:40 UTC (rev 2162)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/JListTransferHandler.java 2012-12-08 23:02:14 UTC (rev 2163)
@@ -111,11 +111,13 @@
/**
* Treats the given drop location as {@link JList.DropLocation} and
* returns {@link JList.DropLocation#getIndex()}.
+ * @return -1 if {@code dl} is no {@link JList.DropLocation}
*/
@Override
protected int getDropRow(JList listComp, DropLocation dl) {
- int index = ((JList.DropLocation)dl).getIndex();
- return index;
+ if ( dl instanceof JList.DropLocation )
+ return ((JList.DropLocation)dl).getIndex();
+ return -1;
}
/**
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/JTableTransferHandler.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/JTableTransferHandler.java 2012-12-08 22:50:40 UTC (rev 2162)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/JTableTransferHandler.java 2012-12-08 23:02:14 UTC (rev 2163)
@@ -29,19 +29,29 @@
*/
package de.schmitzm.swing.dnd;
+import java.awt.Component;
+import java.awt.Point;
import java.util.Arrays;
import java.util.Collection;
import javax.swing.DropMode;
+import javax.swing.JScrollPane;
import javax.swing.JTable;
+import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.table.TableModel;
+import de.schmitzm.swing.SwingUtil;
+
/**
* {@link ListingTransferHandler} implementation for {@link JTable} which
* means the drag and drop for <b>complete table rows</b>.
* Implementations only have to implement {@link #reorderListItems(JTable, int[], int, int)} and
- * {@link #moveListItems(JTable, int[], int, JTable, int)}.
+ * {@link #moveListItems(JTable, int[], int, JTable, int)}.<br>
+ * <b>Note:</b> {@link #initializeLists()} also sets this {@link TransferHandler} to the
+ * parent {@link JScrollPane} of the tables. If {@link #initializeLists()} is not used,
+ * the {@link TransferHandler} should be set to {@link JScrollPane} manually to also allow
+ * drop on empty table!
* @author Martin O.J. Schmitz
*
*/
@@ -84,8 +94,31 @@
public JTableTransferHandler(boolean innerListDragAllowed, Collection<JTable> listComp) {
super(innerListDragAllowed,listComp);
}
-
+
/**
+ * For all list components this method
+ * <ul>
+ * <li>set {@code this} as {@link TransferHandler}</li>
+ * <li>set {@code this} as {@link TransferHandler} of parent {@link JScrollPane}, so that
+ * the drop can also performed on the scrollpane (important if table does not yet contain
+ * rows!)</li>
+ * <li>enables drag by {@link #setDragEnabled(Component, boolean)</li>
+ * <li>sets drop mode to {@link DropMode#INSERT} by {@link #setDropMode(Component, DropMode)}
+ * </ul>
+ * Furthermore the method adds {@code this} as {@link TransferHandler} for
+ * the parent {@link JScrollPane} of the
+ */
+ @Override
+ public void initializeLists() {
+ super.initializeLists();
+ for (JTable listComp : dndListComp) {
+ JScrollPane scrollPane = SwingUtil.getParentScrollPane(listComp);
+ if ( scrollPane != null )
+ scrollPane.setTransferHandler(this);
+ }
+ }
+
+ /**
* Simply calls {@link JTable#getSelectedRows()}.
*/
@Override
@@ -113,11 +146,20 @@
/**
* Treats the given drop location as {@link JTable.DropLocation} and
* returns {@link JTable.DropLocation#getRow()}.
+ * @return -1 if {@code dl} is no {@link JTable.DropLocation}
*/
@Override
protected int getDropRow(JTable listComp, DropLocation dl) {
- int index = ((JTable.DropLocation)dl).getRow();
- return index;
+ if ( dl instanceof JTable.DropLocation )
+ return ((JTable.DropLocation)dl).getRow();
+ // If DropLocation is not JTable.DropLocation, we assume that
+ // the drop was performed on JScrollPane
+ JScrollPane scrollPanel = SwingUtil.getParentScrollPane(listComp);
+ Point dropLocOnTable = SwingUtilities.convertPoint(scrollPanel, dl.getDropPoint(), listComp);
+ // If drop was above table, add rows at head, otherwise on tail
+ if ( dropLocOnTable.y <= 0 )
+ return 0;
+ return -1;
}
/**
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/ListingTransferHandler.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/ListingTransferHandler.java 2012-12-08 22:50:40 UTC (rev 2162)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/dnd/ListingTransferHandler.java 2012-12-08 23:02:14 UTC (rev 2163)
@@ -42,6 +42,7 @@
import javax.swing.DropMode;
import javax.swing.JComponent;
import javax.swing.JList;
+import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.TransferHandler;
@@ -60,7 +61,13 @@
* </ul>
* To allow drag and drop between all of the lists without any special restriction
* {@link #initializeLists()} can be called. Otherwise these properties must be set
- * individually by the calling application class!
+ * individually by the calling application class!<br>
+ * <br>
+ * The class also provides a drop which is not performed directly on the list component,
+ * but on a parent {@link JScrollPane}. In this case {@link #convertJScrollPane(Component)}
+ * converts the component to the underlying list component so that the drop also succeed.
+ * {@link #getDropRow(Component, DropLocation)} must handle this situation (that {@link DropLocation}
+ * is not the expected type) by returning -1.
* @author Martin O.J. Schmitz
*
*/
@@ -148,9 +155,20 @@
*/
@Override
protected Transferable createTransferable(JComponent c) {
+ c = (JComponent)convertJScrollPane(c);
assert (dndListComp.contains(c));
return new DataHandler(getSelectedRow((LIST_IMPL)c), localObjectFlavor.getMimeType());
}
+
+ /**t
+ * If a {@link JScrollPane} is passed to this method, its underlying lis
+ * component is returned. Used to handle drop operations on empty {@link JTable}.
+ */
+ protected Component convertJScrollPane(Component comp) {
+ if ( comp instanceof JScrollPane )
+ return ((JScrollPane)comp).getViewport().getComponent(0);
+ return comp;
+ }
/**
* Checks whether drop is allowed to component.
@@ -158,6 +176,8 @@
@Override
public boolean canImport(TransferSupport info) {
Component dropComp = info.getComponent();
+ dropComp = convertJScrollPane(dropComp);
+
logCompIndex("canImport", dropComp);
// Check if drop is allowed to destination component
boolean dndAllowed = ( isInnerListDragAllowed() || dropComp != getDragSourceComponent() ) &&
@@ -202,7 +222,7 @@
*/
@Override
public boolean importData(TransferHandler.TransferSupport info) {
- LIST_IMPL target = (LIST_IMPL)info.getComponent();
+ LIST_IMPL target = (LIST_IMPL) convertJScrollPane( info.getComponent() );
LIST_IMPL source = getDragSourceComponent();
logCompIndex("importData from", source);
More information about the Schmitzm-commits
mailing list