Skip to main content
summaryrefslogtreecommitdiffstats
blob: edbe5ae8572d326956253efd8f4dab4005d3e324 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.wst.ws.internal.explorer.platform.perspective;

import javax.servlet.http.*;
import java.util.*;

public class NodeManager
{
  // paths to icon images
  public static final String PATH_PLUS_NOTLAST = "images/plus_notlast.gif";
  public static final String PATH_PLUS_LAST = "images/plus_last.gif";
  public static final String PATH_MINUS_NOTLAST = "images/minus_notlast.gif";
  public static final String PATH_MINUS_LAST = "images/minus_last.gif";
  public static final String PATH_LINE = "images/line.gif";
  public static final String PATH_LINE_NOTLAST = "images/line_notlast.gif";
  public static final String PATH_LINE_LAST = "images/line_last.gif";
  public static final String PATH_SPACE = "images/space.gif";

  private Controller controller_;
  private Node rootNode_;
  private Hashtable nodeTable_;
  private int focusedNodeId_;
  private int selectedNodeId_;
  private int previousSelectedNodeId_;
  private int maxDepthVisible_;
  private int nextId_;

  public NodeManager(Controller controller)
  {
    controller_ = controller;
    rootNode_ = null;
    nodeTable_ = new Hashtable();
    focusedNodeId_ = -1;
    selectedNodeId_ = -1;
    previousSelectedNodeId_ = -1;
    maxDepthVisible_ = 1;
    nextId_ = 0;
  }

  /**
  * Hook for the controller.
  * @return Controller The perspective controller.
  */
  public Controller getController()
  {
    return controller_;
  }

  /**
  * Sets the root node of the tree.
  * @param rootNode The node being designated as the root node.
  */
  public void setRootNode(Node rootNode)
  {
    rootNode_ = rootNode;
  }

  /**
  * Returns the root node of the tree managed by this NodeManager.
  * @return Node The root node.
  */
  public Node getRootNode()
  {
    return rootNode_;
  }

  /**
  * @param Integer The id of the node being added.
  * @param Node The node being added.
  * @return The nodeId assigned to this node.
  */
  public int addToNodeTable(Node node)
  {
    int assignedNodeId = nextId_;
    nodeTable_.put(String.valueOf(assignedNodeId),node);
    nextId_++;
    return assignedNodeId;
  }

  public void removeFromNodeTable(int nodeId)
  {
    Vector nodesToRemove = new Vector();
    Node node = getNode(nodeId);
    gatherNodesToRemove(nodesToRemove, node);
    for (Iterator it = nodesToRemove.iterator(); it.hasNext();) {
      nodeTable_.remove(String.valueOf(((Node)it.next()).getNodeId()));
    }
  }

  private void gatherNodesToRemove(Vector nodesToRemove, Node node) {
    if (node != null) {
      nodesToRemove.add(node);
      Vector childNodes = node.getChildNodes();
      for (Iterator it = childNodes.iterator(); it.hasNext();) {
        gatherNodesToRemove(nodesToRemove, (Node)it.next());
      }
    }
  }

  /**
  * Returns the depth of the deepest visible node in the tree.
  * @return int
  */
  public int getMaxDepthVisible()
  {
    return maxDepthVisible_;
  }

  /**
  * Updates the depth of the deepest visible node in the tree. Nodes
  * must call this when the visibility of their children changes.
  * (e.g. expand/collapse of a parent node).
  */
  public void updateMaxDepthVisible()
  {
    if (rootNode_ != null)
      maxDepthVisible_ = rootNode_.getMaxDepthVisible();
  }

  /**
  * Returns the next available id for a newly created node.
  * @return int
  */
  public int getNextNodeId()
  {
    return nextId_;
  }

  /**
  * Returns the id of the currently focused node in the tree. A node
  * becomes focused when any of the following events occur:
  * 1) The node's icon or image are selected by a user.
  * 2) The node's expand/collapse icon is clicked.
  * 3) Programmatically set by the programmer.
  * The treeview will always scroll to the current focused node when loaded.
  * @return int The id.
  */
  public int getFocusedNodeId()
  {
    return focusedNodeId_;
  }

  /**
  * Allows modification of the focused node.
  * @param int The id of the node that is in focus.
  */
  public void setFocusedNodeId(int nodeId)
  {
    focusedNodeId_ = nodeId;
  }

  public Node getFocusedNode()
  {
    return getNode(focusedNodeId_);
  }

  /**
  * Returns the id of the currently selected node in the tree.
  * @return int The id.
  */
  public int getSelectedNodeId()
  {
    return selectedNodeId_;
  }

  public Node getSelectedNode()
  {
    return getNode(selectedNodeId_);
  }

  /**
  * Retrieve a reference to a Node in this manager's node table.
  * @return Node The node.
  */
  public Node getNode(int nodeId)
  {
    return (Node)nodeTable_.get(String.valueOf(nodeId));
  }

  /**
  * Allows modification of the selected Node. Note that setting a node to become
  * selected automatically makes it the focused node. However, this is only a one
  * way relationship.
  * @param int The id of the node that is being selected.
  */
  public void setSelectedNodeId(int nodeId)
  {
    previousSelectedNodeId_ = selectedNodeId_;
    selectedNodeId_ = nodeId;
    setFocusedNodeId(selectedNodeId_);
  }

  public int getPreviousSelectedNodeId()
  {
    return previousSelectedNodeId_;
  }

  public Node getPreviousSelectedNode()
  {
    return (Node)nodeTable_.get(String.valueOf(previousSelectedNodeId_));
  }

  /**
  * Render the HTML tree view for this node.
  * @param HttpServletResponse For encoding URLs.
  * @return String The HTML representing the tree view.
  */
  public String renderTreeView(HttpServletResponse response)
  {
    StringBuffer treeView = new StringBuffer();
    rootNode_.renderNode(response,treeView,"",false);
    return treeView.toString();
  }
  
  /**
  * Make the provided node is visible by ensuring that its ancestors are visible.
  * @param Node The node to be made visible.
  * @return boolean Indicator for whether or not the treeview needs refreshing (i.e. an ancestor was not visible).
  */
  public final boolean makeNodeVisible(Node node)
  {
    boolean requiresTreeViewRefresh = false;
    while ((node = node.getParent()) != null)
    {
      if (!node.isOpen())
      {
        requiresTreeViewRefresh = true;
        node.setVisibilityOfChildren(true);
      }
    }
    return requiresTreeViewRefresh;
  }
  
  /**
  * Make the currently selected node visible by ensuring that its ancestors are visible.
  */
  public final boolean makeSelectedNodeVisible()
  {
    return makeNodeVisible(getSelectedNode());
  }
}

Back to the top