Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jac/aspects/gui/web/Tree.java


1   /*
2     Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3     
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU Lesser General Public License as
6     published by the Free Software Foundation; either version 2 of the
7     License, or (at your option) any later version.
8   
9     This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13  
14    You should have received a copy of the GNU Lesser General Public
15    License along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17    USA */
18  
19  package jac.aspects.gui.web;
20  
21  
22  import jac.aspects.gui.*;
23  import jac.core.Wrappee;
24  import jac.core.rtti.CollectionItem;
25  import jac.util.Log;
26  import java.io.PrintWriter;
27  import java.util.HashSet;
28  
29  /**
30   * This class defines a Swing component tree view for objects that are
31   * related to a root object through relations or collections.
32   *
33   * @see GuiAC */
34  
35  public class Tree extends AbstractView
36     implements View, HTMLViewer, TreeListener
37  {
38     TreeModel model;
39     boolean showRelations = true;
40     RootNode rootNode = null;
41     HashSet expandedNodes = new HashSet();
42  
43     /**
44      * Constructs a new tree view.
45      *
46      * @param parent the Swing dialog parent
47      * @param parentView a view that aggregates this view if any
48      * @param substance the object the viewed collection belongs to
49      * @param collection the collection that is showed by this view
50      * @param showRelations if false, the relations are not showed as
51      * tree elements */
52  
53     public Tree(ViewFactory factory, DisplayContext context,
54                 String pathDef, boolean showRelations ) {
55        super(factory,context);
56        Log.trace("web","building Tree...");
57        this.showRelations = showRelations;
58  
59        rootNode = new RootNode();
60        model = new TreeModel( rootNode, pathDef, showRelations );
61        Log.trace("web","building Tree DONE");
62     }
63     // interface TreeView
64  
65     public void close() {
66        model.unregisterEvents();
67     }
68  
69     public void genHTML(PrintWriter out) {
70        genNode(out,(AbstractNode)model.getRoot(),"");
71     }
72  
73     protected boolean isExpanded(AbstractNode node) {
74        return expandedNodes.contains(node);
75     }
76  
77     protected void genNode(PrintWriter out, AbstractNode node, String curPath) {
78        String nodePath = node instanceof RootNode ? "/" : curPath;
79        String nodeClass = node.isLeaf() ? "leafNode" : "treeNode";
80        boolean isRoot = node == model.getRoot();
81  
82        if (!isRoot) {
83           out.println("<div class=\""+nodeClass+"\">");
84           if (!node.isLeaf()) {
85              if (isExpanded(node)) 
86                 out.print("  <a href=\""+eventURL("onCollapseNode")+
87                           "&amp;nodePath="+nodePath+"\" class=\"fixed\">[-]</a>");
88              else 
89                 out.print("  <a href=\""+eventURL("onExpandNode")+
90                        "&amp;nodePath="+nodePath+"\" class=\"fixed\">[+]</a>");
91           }
92           out.println(iconElement(node.getIcon(),"")+
93                       " <a href=\""+eventURL("onSelectNode")+"&amp;nodePath="+
94                       nodePath+"\">"+node.getText()+"</a>");
95        }
96  
97        if (isExpanded(node) || isRoot) {
98           out.println("  <div class=\""+(isRoot?"rootNodes":"nodes")+"\">");
99           for (int i=0; i<node.getChildCount();i++) {
100             genNode(out,(AbstractNode)node.getChildAt(i),curPath+"/"+i);
101          }
102          out.println("  </div>");
103       }
104       if (!isRoot) {
105          out.println("</div>");
106       }
107    }
108 
109    // TreeListener interface
110 
111    public void onSelectNode(String nodePath) {
112       try {
113          AbstractNode node = pathToNode(nodePath);
114          if (node == null) return;
115          Object selected = node.getUserObject();
116          if (selected instanceof CollectionItem) {
117             Log.trace("gui.events","selected the collection "+
118                       ((CollectionItem)selected).getName());
119          } else if ( selected instanceof Wrappee ) {
120             Log.trace("gui.events","selected a wrappee "+selected);
121             EventHandler.get().onNodeSelection(context,node,true);
122          }
123       } catch (Exception e) {
124          context.getDisplay().showError("Error",e.toString());
125       }
126    }
127 
128    public void onExpandNode(String nodePath) {
129       try {
130          AbstractNode node = pathToNode(nodePath);
131          Log.trace("gui.events","expanding node "+node);
132          // ensure that the children of this node are uptodate
133          if (node instanceof ObjectNode)
134             ((ObjectNode)node).updateChildren();
135          expandedNodes.add(node);
136       } finally {
137          context.getDisplay().refresh();
138       }
139    }
140 
141    public void onCollapseNode(String nodePath) {
142       try {
143          AbstractNode node = pathToNode(nodePath);
144          Log.trace("gui.events","collapsing node "+node);
145          expandedNodes.remove(node);
146       } finally {
147          context.getDisplay().refresh();
148       }
149    }
150 
151    /*
152     * Get a Node from its path. 
153     */
154    protected AbstractNode pathToNode(String nodePath) {
155       AbstractNode node = (AbstractNode)model.getRoot();
156       if (nodePath.startsWith("/")) {
157          nodePath = nodePath.substring(1);
158          int index = nodePath.indexOf("/");
159          while (index!=-1) {
160             node = (AbstractNode)node.getChildAt(
161                Integer.parseInt(nodePath.substring(0,index)));
162             nodePath = nodePath.substring(index+1);
163             index = nodePath.indexOf("/");
164          } 
165          if (nodePath.length()>0)
166             node = (AbstractNode)node.getChildAt(Integer.parseInt(nodePath));
167       }
168       return node;
169    }
170 }