1 /*/*
   2  * $Id$
   3  *
   4  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.exec;
  28 
  29 import com.sun.javatest.TestResult;
  30 
  31 import java.util.Enumeration;
  32 import javax.swing.tree.TreeNode;
  33 
  34 /**
  35  *
  36  * @author work
  37  */
  38 public class TT_TestNode extends TT_TreeNode {
  39 
  40     TT_TestNode(TT_BasicNode parent, TestResult tr) {
  41         this.tr = tr;
  42         this.parent = parent;
  43     }
  44     // ------- interface methods --------
  45     public Enumeration<?> children() {
  46         throw new UnsupportedOperationException("Not supported.");
  47     }
  48 
  49     public boolean getAllowsChildren() {
  50         return false;
  51     }
  52 
  53     public TreeNode getChildAt(int arg0) {
  54         throw new UnsupportedOperationException("Not supported.");
  55     }
  56 
  57     public int getChildCount() {
  58         return 0;
  59     }
  60 
  61     public int getIndex(TreeNode arg0) {
  62         return -1;
  63     }
  64 
  65     public TreeNode getParent() {
  66         return parent;
  67     }
  68 
  69     public boolean isLeaf() {
  70         return true;
  71     }
  72 
  73     boolean isRoot() {
  74         // not possible, right?
  75         return false;
  76     }
  77 
  78     // -------- custom methods -----------
  79     TestResult getTestResult() {
  80         return tr;
  81     }
  82 
  83     String getDisplayName() {
  84         return getShortName();
  85     }
  86 
  87     String getLongDescription() {
  88         // should be the long path to the folder, or custom
  89         return null;
  90     }
  91 
  92     /**
  93      * String for use whenever you need a basic name for this node.  You can
  94      * assume that this name is unique within any node.
  95      * @return Short name for this node, containing no forward slashes or
  96      *    spaces.
  97      */
  98     String getShortName() {
  99         if (shortN == null) {
 100             String fullname = tr.getTestName();
 101             int lastSlash = fullname.lastIndexOf("/");
 102             shortN = (lastSlash != -1 ? fullname.substring(lastSlash + 1) : fullname);
 103         }
 104 
 105         return shortN;
 106     }
 107 
 108     /**
 109      * Get the long internal representation of this location.
 110      * @return Null if the node is the root, else a forward slash separated
 111      *      path.
 112      */
 113     String getLongPath() {
 114         if (parent == null) // root
 115         {
 116             return null;
 117         }
 118         StringBuffer sb = new StringBuffer(getShortName());
 119         TT_BasicNode spot = parent;
 120         while (spot != null && !spot.isRoot()) {
 121             sb.insert(0, "/");
 122             sb.insert(0, spot.getShortName());
 123             spot = spot.parent;
 124         }
 125         return sb.toString();
 126     }
 127     private TestResult tr;
 128     private String shortN;      // stored, expensive to recalculate
 129 }