PageRenderTime 43ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/models/MPTreeModel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 275 lines | 194 code | 30 blank | 51 comment | 19 complexity | aeef80339a0b4917a65dc43adb4368eb 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. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.models;
  18. import java.awt.Component;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.MouseEvent;
  22. import java.awt.event.MouseListener;
  23. import java.util.ArrayList;
  24. import java.util.Enumeration;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.Set;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import javax.swing.JTree;
  32. import javax.swing.tree.DefaultMutableTreeNode;
  33. import javax.swing.tree.DefaultTreeCellRenderer;
  34. import javax.swing.tree.DefaultTreeModel;
  35. import javax.swing.tree.MutableTreeNode;
  36. import mpv5.db.common.Context;
  37. import mpv5.db.common.DatabaseObject;
  38. import mpv5.db.common.NodataFoundException;
  39. import mpv5.db.common.QueryCriteria2;
  40. import mpv5.db.common.QueryHandler;
  41. import mpv5.db.common.QueryParameter;
  42. import mpv5.db.objects.Contact;
  43. import mpv5.db.objects.FileToContact;
  44. import mpv5.db.objects.FileToItem;
  45. import mpv5.db.objects.Group;
  46. import mpv5.db.objects.Item;
  47. import mpv5.globals.Messages;
  48. import mpv5.logging.Log;
  49. import mpv5.ui.frames.MPView;
  50. import mpv5.utils.images.MPIcon;
  51. /**
  52. *
  53. */
  54. public class MPTreeModel extends DefaultTreeModel {
  55. private static final long serialVersionUID = 1L;
  56. private synchronized static DefaultMutableTreeNode getGroupHierarchy(
  57. Group childGroup, DefaultMutableTreeNode childNode,
  58. HashMap<Integer, DefaultMutableTreeNode> groups,
  59. DefaultMutableTreeNode rootNode) throws NodataFoundException {
  60. groups.put(childGroup.__getIDS(), childNode);
  61. // System.err.println(childNode);
  62. if (childGroup.__getGroupsids() > 0) {
  63. Group parent = (Group) DatabaseObject.getObject(Context.getGroup(), childGroup.__getGroupsids());
  64. DefaultMutableTreeNode gnode = new DefaultMutableTreeNode(parent);
  65. gnode.add(childNode);
  66. return getGroupHierarchy(parent, gnode, groups, rootNode);
  67. } else {
  68. rootNode.add(childNode);
  69. return childNode;
  70. }
  71. }
  72. /**
  73. * Generates a tree view of the contact including related items and files
  74. *
  75. * @param rootNode
  76. */
  77. public MPTreeModel(Contact rootNode, QueryCriteria2 itemfilter) {
  78. super(buildTreeFor(rootNode, itemfilter));
  79. }
  80. private static MutableTreeNode buildTreeFor(Contact obj, QueryCriteria2 itemfilter) {
  81. HashMap<Integer, DefaultMutableTreeNode> groups = new HashMap<Integer, DefaultMutableTreeNode>();
  82. DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(obj);
  83. //build group hierarchy
  84. // Add related items
  85. List<Item> items = null;
  86. try {
  87. if (itemfilter == null) {
  88. items = DatabaseObject.toObjectList(DatabaseObject.getReferencedObjects(obj, Context.getItem(), DatabaseObject.getObject(Context.getItem())), new Item());
  89. } else {
  90. List<QueryParameter> p = new ArrayList<QueryParameter>();
  91. p.add(new QueryParameter(Context.getItem(), "invisible", 0, QueryParameter.EQUALS));
  92. p.add(new QueryParameter(Context.getItem(), "contactsids", obj.__getIDS(), QueryParameter.EQUALS));
  93. itemfilter.setOrder("groupsids", true);
  94. itemfilter.and(p);
  95. items = DatabaseObject.getObjects(new Item(), itemfilter);
  96. }
  97. for (int i = 0; i < items.size(); i++) {
  98. Item item = items.get(i);
  99. DefaultMutableTreeNode itemnode = new DefaultMutableTreeNode(item);
  100. if (!groups.containsKey(new Integer(item.__getGroupsids()))) {
  101. DatabaseObject g = DatabaseObject.getObject(Context.getGroup(), item.__getGroupsids());
  102. Log.Debug(MPTreeModel.class, g);
  103. getGroupHierarchy((Group) g, new DefaultMutableTreeNode(g), groups, rootNode);
  104. }
  105. // Add files to the items
  106. List<DatabaseObject> itemfiles = null;
  107. try {
  108. itemfiles = DatabaseObject.getReferencedObjects(item, Context.getFilesToItems(), DatabaseObject.getObject(Context.getFilesToItems()));
  109. for (int j = 0; j < itemfiles.size(); j++) {
  110. FileToItem ifile = (FileToItem) itemfiles.get(j);
  111. itemnode.add(new DefaultMutableTreeNode(ifile));
  112. }
  113. } catch (NodataFoundException ex) {
  114. Log.Debug(MPTreeModel.class, ex.getMessage());
  115. }
  116. (groups.get(item.__getGroupsids())).add(itemnode);
  117. }
  118. if (groups.get(new Integer(1)) != null) {
  119. rootNode.add(groups.get(new Integer(1)));
  120. } else {
  121. Log.Debug(MPTreeModel.class, "No root group node found.");
  122. }
  123. // Add files to the contact
  124. List<DatabaseObject> contactFiles = null;
  125. try {
  126. contactFiles = DatabaseObject.getReferencedObjects(obj, Context.getFilesToContacts(), DatabaseObject.getObject(Context.getFilesToContacts()));
  127. DefaultMutableTreeNode ifil = new DefaultMutableTreeNode(Messages.FILES);
  128. for (int j = 0; j < contactFiles.size(); j++) {
  129. FileToContact ifile = (FileToContact) contactFiles.get(j);
  130. ifil.add(new DefaultMutableTreeNode(ifile));
  131. }
  132. rootNode.add(ifil);
  133. } catch (NodataFoundException ex) {
  134. Log.Debug(MPTreeModel.class, ex.getMessage());
  135. }
  136. } catch (NodataFoundException ex) {
  137. Log.Debug(MPTreeModel.class, ex.getMessage());
  138. }
  139. return rootNode;
  140. }
  141. /**
  142. * The default renderer for trees containing {@link DatabaseObject}s
  143. *
  144. * @return
  145. */
  146. public static DefaultTreeCellRenderer getRenderer() {
  147. return new DefaultTreeCellRenderer() {
  148. @Override
  149. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
  150. super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
  151. DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
  152. try {
  153. DatabaseObject obj = (DatabaseObject) node.getUserObject();
  154. this.setIcon(obj.getIcon());
  155. } catch (Exception e) {
  156. try {
  157. if (node.isLeaf()) {
  158. setIcon(new MPIcon("/mpv5/resources/images/22/folder_grey.png"));
  159. } else {
  160. setIcon(new MPIcon("/mpv5/resources/images/22/folder_green.png"));
  161. }
  162. } catch (Exception ef) {
  163. setIcon(leafIcon);
  164. Log.Debug(ef);
  165. }
  166. }
  167. return this;
  168. }
  169. };
  170. }
  171. /**
  172. * The default {@link MouseListener} for trees containing
  173. * {@link DatabaseObject}s
  174. *
  175. * @param tree
  176. * @return
  177. */
  178. public static MouseListener getDefaultTreeListener(final JTree tree) {
  179. return new MouseListener() {
  180. @Override
  181. public void mouseClicked(MouseEvent evt) {
  182. evt.consume();
  183. if (evt.getClickCount() > 1) {
  184. DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
  185. if (node != null) {
  186. if (node.getUserObject() instanceof DatabaseObject) {
  187. mpv5.YabsViewProxy.instance().getIdentifierView().addTab((DatabaseObject) node.getUserObject());
  188. }
  189. }
  190. }
  191. }
  192. @Override
  193. public void mousePressed(MouseEvent e) {
  194. }
  195. @Override
  196. public void mouseReleased(MouseEvent e) {
  197. }
  198. @Override
  199. public void mouseEntered(MouseEvent e) {
  200. }
  201. @Override
  202. public void mouseExited(MouseEvent e) {
  203. }
  204. };
  205. }
  206. /**
  207. * Reflects the given data in a tree model, builds grouping based on
  208. * {@link DatabaseObject#__getGroupsids()}
  209. *
  210. * @param <T>
  211. * @param data
  212. * @param rootNode
  213. * @return
  214. */
  215. public static <T extends DatabaseObject> DefaultTreeModel toTreeModel(T rootNode) {
  216. DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(rootNode);
  217. try {
  218. mpv5.YabsViewProxy.instance().setWaiting(true);
  219. node1 = addChildren(node1);
  220. } catch (Exception e) {
  221. Log.Debug(e);
  222. } finally {
  223. mpv5.YabsViewProxy.instance().setWaiting(false);
  224. }
  225. DefaultTreeModel model = new DefaultTreeModel(node1);
  226. return model;
  227. }
  228. @SuppressWarnings("unchecked")
  229. private static <T extends DatabaseObject> DefaultMutableTreeNode addChildren(DefaultMutableTreeNode firstnode) {
  230. DatabaseObject dbo = (DatabaseObject) firstnode.getUserObject();
  231. QueryCriteria2 c = new QueryCriteria2();
  232. c.and(new QueryParameter(dbo.getContext(), "groupsids", dbo.__getIDS(), QueryParameter.EQUALS));
  233. try {
  234. ArrayList<DatabaseObject> l = DatabaseObject.getObjects(dbo.getContext(), c);
  235. for (DatabaseObject a : l) {
  236. DefaultMutableTreeNode child = new DefaultMutableTreeNode(a);
  237. firstnode.add(child);
  238. addChildren(child);
  239. }
  240. } catch (NodataFoundException ex) {
  241. //Logger.getLogger(MPTreeModel.class.getName()).log(Level.SEVERE, null, ex);
  242. }
  243. return firstnode;
  244. }
  245. }