PageRenderTime 64ms CodeModel.GetById 20ms app.highlight 25ms RepoModel.GetById 16ms 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
  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 */
 17package mpv5.utils.models;
 18
 19import java.awt.Component;
 20import java.awt.event.ActionEvent;
 21import java.awt.event.ActionListener;
 22import java.awt.event.MouseEvent;
 23import java.awt.event.MouseListener;
 24import java.util.ArrayList;
 25import java.util.Enumeration;
 26import java.util.HashMap;
 27import java.util.Iterator;
 28import java.util.List;
 29import java.util.Set;
 30import java.util.logging.Level;
 31import java.util.logging.Logger;
 32import javax.swing.JTree;
 33import javax.swing.tree.DefaultMutableTreeNode;
 34import javax.swing.tree.DefaultTreeCellRenderer;
 35import javax.swing.tree.DefaultTreeModel;
 36import javax.swing.tree.MutableTreeNode;
 37import mpv5.db.common.Context;
 38import mpv5.db.common.DatabaseObject;
 39import mpv5.db.common.NodataFoundException;
 40import mpv5.db.common.QueryCriteria2;
 41import mpv5.db.common.QueryHandler;
 42import mpv5.db.common.QueryParameter;
 43import mpv5.db.objects.Contact;
 44import mpv5.db.objects.FileToContact;
 45import mpv5.db.objects.FileToItem;
 46import mpv5.db.objects.Group;
 47import mpv5.db.objects.Item;
 48
 49import mpv5.globals.Messages;
 50import mpv5.logging.Log;
 51import mpv5.ui.frames.MPView;
 52import mpv5.utils.images.MPIcon;
 53
 54/**
 55 *
 56 */
 57public class MPTreeModel extends DefaultTreeModel {
 58
 59    private static final long serialVersionUID = 1L;
 60
 61    private synchronized static DefaultMutableTreeNode getGroupHierarchy(
 62            Group childGroup, DefaultMutableTreeNode childNode,
 63            HashMap<Integer, DefaultMutableTreeNode> groups,
 64            DefaultMutableTreeNode rootNode) throws NodataFoundException {
 65        groups.put(childGroup.__getIDS(), childNode);
 66//        System.err.println(childNode);
 67        if (childGroup.__getGroupsids() > 0) {
 68            Group parent = (Group) DatabaseObject.getObject(Context.getGroup(), childGroup.__getGroupsids());
 69            DefaultMutableTreeNode gnode = new DefaultMutableTreeNode(parent);
 70            gnode.add(childNode);
 71            return getGroupHierarchy(parent, gnode, groups, rootNode);
 72        } else {
 73
 74            rootNode.add(childNode);
 75            return childNode;
 76        }
 77    }
 78
 79    /**
 80     * Generates a tree view of the contact including related items and files
 81     *
 82     * @param rootNode
 83     */
 84    public MPTreeModel(Contact rootNode, QueryCriteria2 itemfilter) {
 85        super(buildTreeFor(rootNode, itemfilter));
 86    }
 87
 88    private static MutableTreeNode buildTreeFor(Contact obj, QueryCriteria2 itemfilter) {
 89        HashMap<Integer, DefaultMutableTreeNode> groups = new HashMap<Integer, DefaultMutableTreeNode>();
 90        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(obj);
 91
 92        //build group hierarchy
 93
 94        // Add related items
 95        List<Item> items = null;
 96
 97        try {
 98            if (itemfilter == null) {
 99                items = DatabaseObject.toObjectList(DatabaseObject.getReferencedObjects(obj, Context.getItem(), DatabaseObject.getObject(Context.getItem())), new Item());
100            } else {
101                List<QueryParameter> p = new ArrayList<QueryParameter>();
102                p.add(new QueryParameter(Context.getItem(), "invisible", 0, QueryParameter.EQUALS));
103                p.add(new QueryParameter(Context.getItem(), "contactsids", obj.__getIDS(), QueryParameter.EQUALS));
104                itemfilter.setOrder("groupsids", true);
105                itemfilter.and(p);
106                items = DatabaseObject.getObjects(new Item(), itemfilter);
107            }
108
109            for (int i = 0; i < items.size(); i++) {
110                Item item = items.get(i);
111                DefaultMutableTreeNode itemnode = new DefaultMutableTreeNode(item);
112
113                if (!groups.containsKey(new Integer(item.__getGroupsids()))) {
114                    DatabaseObject g = DatabaseObject.getObject(Context.getGroup(), item.__getGroupsids());
115                    Log.Debug(MPTreeModel.class, g);
116                    getGroupHierarchy((Group) g, new DefaultMutableTreeNode(g), groups, rootNode);
117                }
118
119                // Add files to the items
120                List<DatabaseObject> itemfiles = null;
121                try {
122                    itemfiles = DatabaseObject.getReferencedObjects(item, Context.getFilesToItems(), DatabaseObject.getObject(Context.getFilesToItems()));
123                    for (int j = 0; j < itemfiles.size(); j++) {
124                        FileToItem ifile = (FileToItem) itemfiles.get(j);
125                        itemnode.add(new DefaultMutableTreeNode(ifile));
126                    }
127                } catch (NodataFoundException ex) {
128                    Log.Debug(MPTreeModel.class, ex.getMessage());
129                }
130
131                (groups.get(item.__getGroupsids())).add(itemnode);
132            }
133            if (groups.get(new Integer(1)) != null) {
134                rootNode.add(groups.get(new Integer(1)));
135            } else {
136                Log.Debug(MPTreeModel.class, "No root group node found.");
137            }
138            // Add files to the contact
139            List<DatabaseObject> contactFiles = null;
140            try {
141                contactFiles = DatabaseObject.getReferencedObjects(obj, Context.getFilesToContacts(), DatabaseObject.getObject(Context.getFilesToContacts()));
142
143                DefaultMutableTreeNode ifil = new DefaultMutableTreeNode(Messages.FILES);
144                for (int j = 0; j < contactFiles.size(); j++) {
145                    FileToContact ifile = (FileToContact) contactFiles.get(j);
146                    ifil.add(new DefaultMutableTreeNode(ifile));
147                }
148                rootNode.add(ifil);
149            } catch (NodataFoundException ex) {
150                Log.Debug(MPTreeModel.class, ex.getMessage());
151            }
152        } catch (NodataFoundException ex) {
153            Log.Debug(MPTreeModel.class, ex.getMessage());
154        }
155        return rootNode;
156    }
157
158    /**
159     * The default renderer for trees containing {@link DatabaseObject}s
160     *
161     * @return
162     */
163    public static DefaultTreeCellRenderer getRenderer() {
164        return new DefaultTreeCellRenderer() {
165            @Override
166            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
167                super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
168                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
169                try {
170                    DatabaseObject obj = (DatabaseObject) node.getUserObject();
171                    this.setIcon(obj.getIcon());
172                } catch (Exception e) {
173                    try {
174                        if (node.isLeaf()) {
175                            setIcon(new MPIcon("/mpv5/resources/images/22/folder_grey.png"));
176                        } else {
177                            setIcon(new MPIcon("/mpv5/resources/images/22/folder_green.png"));
178                        }
179                    } catch (Exception ef) {
180                        setIcon(leafIcon);
181                        Log.Debug(ef);
182                    }
183                }
184                return this;
185            }
186        };
187    }
188
189    /**
190     * The default {@link MouseListener} for trees containing
191     * {@link DatabaseObject}s
192     *
193     * @param tree
194     * @return
195     */
196    public static MouseListener getDefaultTreeListener(final JTree tree) {
197        return new MouseListener() {
198            @Override
199            public void mouseClicked(MouseEvent evt) {
200                evt.consume();
201                if (evt.getClickCount() > 1) {
202                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
203                    if (node != null) {
204                        if (node.getUserObject() instanceof DatabaseObject) {
205                            mpv5.YabsViewProxy.instance().getIdentifierView().addTab((DatabaseObject) node.getUserObject());
206                        }
207                    }
208                }
209            }
210
211            @Override
212            public void mousePressed(MouseEvent e) {
213            }
214
215            @Override
216            public void mouseReleased(MouseEvent e) {
217            }
218
219            @Override
220            public void mouseEntered(MouseEvent e) {
221            }
222
223            @Override
224            public void mouseExited(MouseEvent e) {
225            }
226        };
227    }
228
229    /**
230     * Reflects the given data in a tree model, builds grouping based on
231     * {@link DatabaseObject#__getGroupsids()}
232     *
233     * @param <T>
234     * @param data
235     * @param rootNode
236     * @return
237     */
238    public static <T extends DatabaseObject> DefaultTreeModel toTreeModel(T rootNode) {
239
240        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(rootNode);
241        try {
242            mpv5.YabsViewProxy.instance().setWaiting(true);
243
244            node1 = addChildren(node1);
245
246        } catch (Exception e) {
247            Log.Debug(e);
248        } finally {
249            mpv5.YabsViewProxy.instance().setWaiting(false);
250        }
251
252        DefaultTreeModel model = new DefaultTreeModel(node1);
253        return model;
254    }
255
256    @SuppressWarnings("unchecked")
257    private static <T extends DatabaseObject> DefaultMutableTreeNode addChildren(DefaultMutableTreeNode firstnode) {
258        DatabaseObject dbo = (DatabaseObject) firstnode.getUserObject();
259        QueryCriteria2 c = new QueryCriteria2();
260        c.and(new QueryParameter(dbo.getContext(), "groupsids", dbo.__getIDS(), QueryParameter.EQUALS));
261        try {
262            ArrayList<DatabaseObject> l = DatabaseObject.getObjects(dbo.getContext(), c);
263            for (DatabaseObject a : l) {
264                DefaultMutableTreeNode child = new DefaultMutableTreeNode(a);
265                firstnode.add(child);
266                addChildren(child);
267            }
268        } catch (NodataFoundException ex) {
269            //Logger.getLogger(MPTreeModel.class.getName()).log(Level.SEVERE, null, ex);
270        }
271
272
273        return firstnode;
274    }
275}