PageRenderTime 272ms CodeModel.GetById 60ms app.highlight 145ms RepoModel.GetById 57ms app.codeStats 1ms

/src/mpv5/ui/misc/DragNDropTreeForGroups.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 221 lines | 168 code | 27 blank | 26 comment | 26 complexity | 313e0b03e6bb3dda9d4a72e1350e894c MD5 | raw file
  1package mpv5.ui.misc;
  2
  3import java.awt.datatransfer.DataFlavor;
  4import java.awt.datatransfer.Transferable;
  5import java.util.Enumeration;
  6import javax.swing.DropMode;
  7import javax.swing.JComponent;
  8import javax.swing.JTree;
  9import javax.swing.TransferHandler;
 10import javax.swing.tree.DefaultMutableTreeNode;
 11import javax.swing.tree.TreePath;
 12import javax.swing.tree.TreeSelectionModel;
 13import mpv5.db.common.DatabaseObject;
 14import mpv5.db.objects.Group;
 15import mpv5.db.objects.Product;
 16import mpv5.db.objects.ProductGroup;
 17import mpv5.logging.Log;
 18import mpv5.ui.panels.ListPanel;
 19
 20////////////////////////////////////////////////////////////////////////////////
 21/**
 22 * Valid for {@link Group} and {@link ProductGroup} trees
 23 * @author andreas.weber
 24 */
 25public final class DragNDropTreeForGroups extends JTree {
 26
 27    /**
 28     * If the tree nodes are ordinary {@link Group}s (any {@link DatabaseObject} can be dropped on).
 29     */
 30    public static final int TYPE_GROUP = 0;
 31    /**
 32     * If the tree nodes are {@link ProductGroup}s (only {@link Product}s can be dropped on).
 33     */
 34    public static final int TYPE_PRODUCT_GROUP = 1;
 35    private final int type;
 36    private ListPanel containerToNotify;
 37
 38    /**
 39     * Creates anew tree for the given group type
 40     * @param type
 41     */
 42    public DragNDropTreeForGroups(int type) {
 43        super();
 44//        setDragEnabled(true);
 45        setDropMode(DropMode.ON_OR_INSERT);
 46//        setDropTarget(new DropTarget());
 47        setTransferHandler(new TreeTransferHandler(type));
 48        getSelectionModel().setSelectionMode(
 49                TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
 50        expandTree();
 51        this.type = type;
 52    }
 53
 54    /**
 55     * Expand the tree
 56     */
 57    public void expandTree() {
 58        DefaultMutableTreeNode root = (DefaultMutableTreeNode) getModel().getRoot();
 59        Enumeration e = root.breadthFirstEnumeration();
 60        while (e.hasMoreElements()) {
 61            DefaultMutableTreeNode node =
 62                    (DefaultMutableTreeNode) e.nextElement();
 63            if (node.isLeaf()) {
 64                continue;
 65            }
 66            int row = getRowForPath(new TreePath(node.getPath()));
 67            expandRow(row);
 68        }
 69    }
 70
 71    /**
 72     * @return the containerToNotify
 73     */
 74    public ListPanel getContainerToNotify() {
 75        return containerToNotify;
 76    }
 77
 78    /**
 79     * @param containerToNotify the containerToNotify to set
 80     */
 81    public void setContainerToNotify(ListPanel containerToNotify) {
 82        this.containerToNotify = containerToNotify;
 83        ((TreeTransferHandler) getTransferHandler()).setContainerToNotify(containerToNotify);
 84    }
 85}
 86
 87class TreeTransferHandler extends TransferHandler {
 88
 89    private DataFlavor nodesFlavor;
 90    private DataFlavor[] flavors = new DataFlavor[2];
 91    private final int type;
 92    private ListPanel containerToNotify;
 93
 94    public TreeTransferHandler(int type) {
 95        try {
 96            String mimeType = DataFlavor.javaJVMLocalObjectMimeType
 97                    + ";class=\""
 98                    + javax.swing.tree.DefaultMutableTreeNode[].class.getName()
 99                    + "\"";
100            nodesFlavor = new DataFlavor(mimeType);
101            flavors[0] = nodesFlavor;
102            flavors[1] = DatabaseObjectTransferable.FLAVOR;
103        } catch (ClassNotFoundException e) {
104        }
105        this.type = type;
106    }
107
108    @Override
109    public boolean canImport(TransferHandler.TransferSupport support) {
110
111        if (!support.isDrop()) {
112            return false;
113        }
114        support.setShowDropLocation(true);
115        if (support.isDataFlavorSupported(flavors[0]) || support.isDataFlavorSupported(flavors[1])) {
116            return true;
117        }
118
119        return false;
120    }
121
122    @Override
123    protected Transferable createTransferable(JComponent c) {
124        return null;
125    }
126
127    @Override
128    protected void exportDone(JComponent source, Transferable data, int action) {
129    }
130
131    @Override
132    public int getSourceActions(JComponent c) {
133        return COPY_OR_MOVE;
134    }
135
136    @Override
137    public boolean importData(TransferHandler.TransferSupport support) {
138        boolean result = false;
139        if (!canImport(support)) {
140            return result;
141        }
142        try {
143            DatabaseObject[] movedObjs = new DatabaseObject[0];
144            try {
145                Object o = support.getTransferable().getTransferData(DatabaseObjectTransferable.FLAVOR);
146                movedObjs = (DatabaseObject[]) o;
147            } catch (Exception ex) {
148                Log.Debug(ex);
149            }
150
151            JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
152            TreePath dest = dl.getPath();
153            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) dest.getLastPathComponent();
154            JTree tree = (JTree) support.getComponent();
155            tree.setSelectionPath(dest);
156
157            for (int i = 0; i < movedObjs.length; i++) {
158
159                DatabaseObject obj = movedObjs[i];
160                DatabaseObject par = (DatabaseObject) parentNode.getUserObject();
161
162                if (type == DragNDropTreeForGroups.TYPE_PRODUCT_GROUP) {
163                    if (dropPGroup(par, obj)) {
164                        result = true;
165                    } else {
166                        result = false;
167                    }
168                } else {
169                    if (dropGroup(par, obj)) {
170                        result = true;
171                    } else {
172                        result = false;
173                    }
174                }
175            }
176        } catch (Exception e) {
177            Log.Debug(e);
178        }
179
180        if (containerToNotify != null) {
181            containerToNotify.refresh();
182        }
183
184        return result;
185    }
186
187    private boolean dropPGroup(DatabaseObject par, DatabaseObject obj) {
188
189        if (par instanceof ProductGroup) {
190            if (obj instanceof ProductGroup) {
191                ((ProductGroup) obj).setProductgroupsids(par.__getIDS());
192                return obj.save(true);
193            } else if (obj instanceof Product) {
194                ((Product) obj).setProductgroupsids(par.__getIDS());
195                return obj.save(true);
196            } else {
197                return false;
198            }
199        } else {
200            return false;
201        }
202    }
203
204    private boolean dropGroup(DatabaseObject par, DatabaseObject obj) {
205        if (par instanceof Group) {
206            if (obj instanceof Group) {
207                ((Group) obj).setGroupsids(par.__getIDS());
208                return obj.save(true);
209            } else {
210                (obj).setGroupsids(par.__getIDS());
211                return obj.save(true);
212            }
213        } else {
214            return false;
215        }
216    }
217
218    protected void setContainerToNotify(ListPanel containerToNotify) {
219        this.containerToNotify = containerToNotify;
220    }
221}