PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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