Skip to main content
summaryrefslogtreecommitdiffstats
blob: e3a2cdf6835e4b06b72acaa7847432a0f39ee5cb (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*******************************************************************************
 * 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 org.eclipse.wst.ws.internal.explorer.platform.constants.*;
import org.eclipse.wst.ws.internal.explorer.platform.datamodel.*;
import org.eclipse.wst.ws.internal.explorer.platform.util.*;

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

public abstract class Node
{
  protected TreeElement element_;
  protected NodeManager nodeManager_;
  protected int nodeId_;
  protected int nodeDepth_;
  protected String imagePath_;
  protected boolean isOpen_;
  protected Vector childNodes_;
  protected ToolManager toolManager_;
  protected Node parent_;
  private String anchorName_;
  private int viewId_;

  public Node(TreeElement element,NodeManager nodeManager,int nodeDepth,String imagePath)
  {
    element_ = element;
    nodeManager_ = nodeManager;
    nodeId_ = nodeManager.addToNodeTable(this);
    nodeDepth_ = nodeDepth;
    Controller controller = nodeManager_.getController();
    if (imagePath == null)
      imagePath_ = controller.getPathWithContext(NodeManager.PATH_SPACE);
    else
      imagePath_ = imagePath;
    isOpen_ = true;
    childNodes_ = new Vector();
    toolManager_ = new ToolManager(this);
    initTools();
    anchorName_ = (new StringBuffer("action")).append(nodeId_).toString();
    parent_ = null;
    viewId_ = ActionInputs.VIEWID_DEFAULT;
  }

  /**
  * Returns the TreeElement associated with this node.
  * @return TreeElement The element associated with this node.
  */
  public TreeElement getTreeElement()
  {
    return element_;
  }

  /**
  * Returns the name of the node.
  * @return String
  */
  public String getNodeName()
  {
    return element_.getName();
  }

  /**
  * Returns the NodeManager managing this node.
  * @return NodeManager
  */
  public NodeManager getNodeManager()
  {
    return nodeManager_;
  }

  /**
  * Returns the id of this node.
  * @return int
  */
  public int getNodeId()
  {
    return nodeId_;
  }

  /**
  * Returns the depth in the tree of this node relative to the root node
  * @return int
  */
  public int getNodeDepth()
  {
    return nodeDepth_;
  }

  /**
  * Set the path of the image representing this class of Node.
  * @param String The path.
  */
  public void setImagePath(String path)
  {
    imagePath_ = path;
  }

  /**
  * Returns true if the children of this node are visible. Returns false
  * if they are not.
  * @return boolean
  */
  public boolean isOpen()
  {
    return isOpen_;
  }

  public boolean isVisible()
  {
    Node parentNode = parent_;
    while (parentNode != null)
    {
      if (!parentNode.isOpen())
        return false;
      parentNode = parentNode.getParent();
    }
    return true;
  }

  /**
  * Sets the visibility of this node's children to the specified value.
  * Updates the NodeManager's record of the maximum depth visible in the tree.
  * @param boolean
  */
  public final void setVisibilityOfChildren(boolean open)
  {
    isOpen_ = open;
    nodeManager_.updateMaxDepthVisible();
  }

  /**
  * Gets the maximum depth displayed in this node's branch of the tree
  * Used by NodeManager for html display purposes.
  * @return int
  */
  public int getMaxDepthVisible()
  {
    if (!isOpen_)
      return nodeDepth_;
    else
    {
      int currentDepth = nodeDepth_;
      Enumeration e = childNodes_.elements();
      while (e.hasMoreElements())
      {
        Node thisNode = (Node)e.nextElement();
        int depth = thisNode.getMaxDepthVisible();
        if (depth>currentDepth)
          currentDepth=depth;
      }
      return currentDepth;
    }
  }

  public final void setParent(Node node)
  {
    parent_ = node;
  }

  public final Node getParent()
  {
    return parent_;
  }

  public final void setViewId(int viewId)
  {
    viewId_ = viewId;
  }

  public final int getViewId()
  {
    return viewId_;
  }

  public final int getViewToolId()
  {
    ToolManager viewToolManager = getViewToolManager();
    if (viewToolManager != null)
      return viewToolManager.getSelectedToolId();
    return ActionInputs.VIEWTOOLID_DEFAULT;
  }

  /**
  * Adds the specified node as a child of this node.
  * @param Node The child being added.
  * @return boolean Always true.
  */
  public final boolean addChild(Node childNode)
  {
    childNode.setParent(this);
    childNodes_.addElement(childNode);
    nodeManager_.updateMaxDepthVisible();
    return true;
  }

  /**
  * Returns the child nodes of this node. If this node has no children, an
  * empty vector is returned.
  * @return Vector
  */
  public Vector getChildNodes()
  {
    return childNodes_;
  }

  public final Node getChildNode(TreeElement element)
  {
    Enumeration e = childNodes_.elements();
    while (e.hasMoreElements())
    {
      Node presentNode = (Node)e.nextElement();
      if (presentNode.getTreeElement() == element)
        return presentNode;
    }
    return null;
  }

  public final void removeChildNodes(boolean isEntry)
  {
    int numberOfChildNodes = childNodes_.size();
    for (int i=numberOfChildNodes-1;i>=0;i--)
    {
      Node childNode = (Node)childNodes_.elementAt(i);
      childNode.removeChildNodes(false);
      nodeManager_.removeFromNodeTable(childNode.getNodeId());
    }
    if (numberOfChildNodes > 0)
    {
      childNodes_.removeAllElements();
      if (isEntry)
        nodeManager_.updateMaxDepthVisible();
    }
  }

  public final boolean removeChildNode(TreeElement element)
  {
    Node childNode = getChildNode(element);
    if (childNode != null)
    {
      int childNodeId = childNode.getNodeId();
      if (nodeManager_.getSelectedNodeId() == childNodeId)
        nodeManager_.setSelectedNodeId(nodeId_);
      childNodes_.removeElement(childNode);
      nodeManager_.removeFromNodeTable(childNodeId);
      nodeManager_.updateMaxDepthVisible();
      return true;
    }
    return false;
  }

  /**
  * Get the name of the HTML anchor associated with this node.
  * @return String The name of the anchor.
  */
  public String getAnchorName()
  {
    return anchorName_;
  }

  public ToolManager getToolManager()
  {
    return toolManager_;
  }

  public ToolManager getViewToolManager()
  {
    if (viewId_ != ActionInputs.VIEWID_DEFAULT)
    {
      ViewTool viewTool = (ViewTool)toolManager_.getSelectedTool();
      return viewTool.getToolManager(viewId_);
    }
    return null;
  }
  
  public ToolManager getCurrentToolManager()
  {
    ToolManager toolManager = getViewToolManager();
    if (toolManager == null)
      toolManager = toolManager_;
    return toolManager;
  }

  protected abstract void initTools();

  protected abstract String getToggleNodeActionHref();

  protected String getToggleNodeActionTarget()
  {
    return FrameNames.PERSPECTIVE_WORKAREA;
  }

  protected void addToggle(HttpServletResponse response,StringBuffer treeView,boolean isLastChild)
  {
    if (nodeDepth_ != 1)
    {
      treeView.append("    <td align=\"left\" width=16 height=16 nowrap>");
      String toggleImageTag;
      if (childNodes_.size() > 0)
      {
        // <a href="..." target="...">toggleImageTag</a>
        String href = nodeManager_.getController().getPathWithContext(getToggleNodeActionHref());
        if (isOpen_)
        {
          String toggleAlt = nodeManager_.getController().getMessage("ALT_CLOSE");
          if (isLastChild)
            toggleImageTag = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_MINUS_LAST),toggleAlt,"16","16",null);
          else
            toggleImageTag = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_MINUS_NOTLAST),toggleAlt,"16","16",null);
        }
        else
        {
          String toggleAlt = nodeManager_.getController().getMessage("ALT_OPEN");
          if (isLastChild)
            toggleImageTag = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_PLUS_LAST),toggleAlt,"16","16",null);
          else
            toggleImageTag = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_PLUS_NOTLAST),toggleAlt,"16","16",null);
        }
        String linkTag = HTMLUtils.getHTMLLinkTag(response,href,getToggleNodeActionTarget(),String.valueOf(nodeId_),toggleImageTag,null);
        treeView.append(linkTag);
      }
      else
      {
        // <img...>
        if (isLastChild)
          toggleImageTag = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_LINE_LAST),"","16","16",null);
        else
          toggleImageTag = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_LINE_NOTLAST),"","16","16",null);
        treeView.append(toggleImageTag);
      }
      treeView.append("</td>").append(HTMLUtils.LINE_SEPARATOR);
    }
  }

  protected abstract String getLinkActionHref();

  protected String getLinkActionTarget()
  {
    return FrameNames.PERSPECTIVE_WORKAREA;
  }

  public String getOpenImagePath()
  {
    return imagePath_;
  }

  public String getClosedImagePath()
  {
    return imagePath_;
  }

  protected void addHTMLLabel(HttpServletResponse response,StringBuffer treeView,boolean isLastChild)
  {
    // <a href="..." target="..." onClick="selectNode('...')"><img...></a>
    Hashtable additionalAttributes = new Hashtable();
    additionalAttributes.put("name",anchorName_);
    String imagePath = (nodeManager_.getSelectedNodeId() == nodeId_)?getOpenImagePath():getClosedImagePath();

    String imageTag = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(imagePath),"","16","16",additionalAttributes);
    String baseHref = getLinkActionHref();
    String href = ((baseHref == null)?baseHref:nodeManager_.getController().getPathWithContext(baseHref));
    String target = getLinkActionTarget();
    additionalAttributes.clear();

    treeView.append("    <td align=\"left\" width=16 height=16 nowrap>");
    if (href != null)
      treeView.append(HTMLUtils.getHTMLLinkTag(response,href,target,null,imageTag,additionalAttributes));
    else
      treeView.append(imageTag);
    treeView.append("</td>").append(HTMLUtils.LINE_SEPARATOR);
    treeView.append("    <td align=\"left\" width=3 height=10 nowrap>").append(HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_SPACE),"","3","10",null)).append("</td>").append(HTMLUtils.LINE_SEPARATOR);

    // <a name="..." href="..." target="...">label</a>
    treeView.append("    <td align=\"left\" nowrap>").append(HTMLUtils.LINE_SEPARATOR);
    treeView.append("      ");
    if (href != null)
    {
      String textAnchorClass;
      if (nodeManager_.getSelectedNodeId() == nodeId_)
        textAnchorClass="selectedTextAnchor";
      else
        textAnchorClass="unselectedTextAnchor";
      additionalAttributes.put("class",textAnchorClass);
      treeView.append(HTMLUtils.getHTMLLinkTag(response,href,target,anchorName_,getNodeName(),additionalAttributes));
    }
    else
      treeView.append("<strong>").append(getNodeName()).append("</strong>");
    treeView.append(HTMLUtils.LINE_SEPARATOR).append("    </td>").append(HTMLUtils.LINE_SEPARATOR);
  }

  public void renderNode(HttpServletResponse response,StringBuffer treeView,String prefixColumns,boolean isLastChild)
  {
    String space_16x16 = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_SPACE),"","16","16",null);
    String line_16x16 = HTMLUtils.getHTMLImageTag(response,nodeManager_.getController().getPathWithContext(NodeManager.PATH_LINE),"","16","16",null);

    // Use border=1 to help with debugging.
    treeView.append("<table cellspacing=0 cellpadding=0 border=0>").append(HTMLUtils.LINE_SEPARATOR);
    treeView.append("  <tr>").append(HTMLUtils.LINE_SEPARATOR);
    treeView.append("    <td align=\"left\" width=16 height=16 nowrap>").append(space_16x16).append("</td>").append(HTMLUtils.LINE_SEPARATOR);
    StringBuffer newPrefixColumns = new StringBuffer(prefixColumns);
    if (nodeDepth_ >= 2)
    {
      if (prefixColumns.length() > 0)
        treeView.append(prefixColumns).append(HTMLUtils.LINE_SEPARATOR);
      // generate a new prefix columns.
      newPrefixColumns.append("    <td align=\"left\" width=16 height=16 nowrap>");
      if (isLastChild)
        newPrefixColumns.append(space_16x16);
      else
        newPrefixColumns.append(line_16x16);
      newPrefixColumns.append("</td>").append(HTMLUtils.LINE_SEPARATOR);
    }
    addToggle(response,treeView,isLastChild);
    addHTMLLabel(response,treeView,isLastChild);
    treeView.append("  </tr>").append(HTMLUtils.LINE_SEPARATOR);
    treeView.append("</table>").append(HTMLUtils.LINE_SEPARATOR);
    int numberOfChildNodes = childNodes_.size();
    if (numberOfChildNodes > 0)
    {
      if (isOpen_)
      {
        for (int i=0;i<numberOfChildNodes-1;i++)
        {
          Node childNode = (Node)childNodes_.elementAt(i);
          childNode.renderNode(response,treeView,newPrefixColumns.toString(),false);
        }
        Node lastChildNode = (Node)childNodes_.elementAt(numberOfChildNodes-1);
        lastChildNode.renderNode(response,treeView,newPrefixColumns.toString(),true);
      }
    }
  }

  // For sorting purposes.  
  public final String toString()
  {
    return getNodeName();
  }
}

Back to the top