1 12 13 16 package com.openedit.webui.tree; 17 18 import java.util.HashSet ; 19 import java.util.Set ; 20 21 import com.openedit.util.PathUtilities; 22 23 28 public abstract class BaseTreeRenderer implements TreeRenderer 29 { 30 protected WebTree fieldWebTree; 31 protected String fieldHome; 32 33 protected String fieldImageDir = "/openedit/images/tree"; 34 protected Set fieldExpandedNodes; 35 protected Object fieldSelectedNode; 36 37 protected String fieldUrlPrefix = ""; 38 protected String fieldUrlPostfix = ""; 39 protected boolean fieldFoldersLinked; 40 41 public BaseTreeRenderer(WebTree inWebTree) 42 { 43 fieldWebTree = inWebTree; 44 } 45 public BaseTreeRenderer() 46 { 47 48 } 49 public String getHome() 50 { 51 if ( fieldHome == null ) 52 { 53 fieldHome = ""; 54 } 55 return fieldHome; 56 } 57 58 public void setHome(String inHome) 59 { 60 fieldHome = inHome; 61 } 62 63 public String getExpandNodeCommand() 64 { 65 return "WebTree.expandTreeNode"; 66 } 67 68 public String getImageDir() 69 { 70 return fieldImageDir; 71 } 72 public void setImageDir(String inImageDir) 73 { 74 fieldImageDir = inImageDir; 75 } 76 public void setWebTree(WebTree tree) 77 { 78 fieldWebTree = tree; 79 } 80 81 public WebTree getWebTree() 82 { 83 return fieldWebTree; 84 } 85 protected Set getExpandedNodes() 86 { 87 if (fieldExpandedNodes == null) 88 { 89 fieldExpandedNodes = new HashSet (); 90 } 91 92 return fieldExpandedNodes; 93 } 94 95 public void expandNode(Object inNode) 96 { 97 String path = toUrl(inNode); 98 if ( !getExpandedNodes().contains(path)) 99 { 100 getExpandedNodes().add(path); 101 Object parent = getWebTree().getModel().getParent( inNode); 103 if ( parent != null) 104 { 105 expandNode(parent); 106 } 107 } 108 } 109 110 113 public void collapseNode(Object inNode) 114 { 115 String path = toUrl(inNode); 116 117 getExpandedNodes().remove(path); 118 } 119 120 public boolean hasBeenExpanded(Object inNode) 121 { 122 String path = toUrl(inNode); 123 124 return getExpandedNodes().contains(path); 125 } 126 127 protected String quotes(String inString) 128 { 129 return "\"" + inString + "\""; 130 } 131 132 public String renderAsString() 133 { 134 StringBuffer js = new StringBuffer ( "var tree = new Tree( \n" ); 135 136 expandNode( getWebTree().getModel().getRoot() ); 138 139 js.append( renderNewFolder( getWebTree().getModel().getRoot())); 140 js.append( ", " ); 141 js.append( quotes(getWebTree().getId()) ); 142 js.append( ", \"" ); 143 js.append( getHome() ); 144 js.append( getImageDir() + "/\" );\n"); 145 if ( getUrlPrefix() != null) 146 { 147 js.append( "tree.setViewerUrl( '" + getHome() + getUrlPrefix() + "' );\n" ); 148 } 149 if ( getUrlPostfix() != null) 150 { 151 js.append( "tree.setViewerUrlEnding( '" + getUrlPostfix() + "' );\n" ); 152 } 153 js.append( "tree.showFolderData= " + isFoldersLinked() + ";\n"); 154 if ( getSelectedNode() != null) 155 { 156 js.append("tree.setSelectedNodeById(" ); 157 js.append(quotes(getWebTree().getModel().getId( getSelectedNode() ) )); 158 js.append(");\n"); 159 } 160 return js.toString(); 161 } 162 public void setSelectedNodeByUrl(String inPath) 163 { 164 if ( inPath == null) 165 { 166 return; 167 } 168 169 Object node = null; 170 171 String root = toUrl( getWebTree().getModel().getRoot() ); 172 if ( PathUtilities.match(root, inPath ) ) 173 { 174 node = getWebTree().getModel().getRoot(); 175 } 176 else 177 { 178 node = findNodeByUrl(getWebTree().getModel().getRoot(), inPath); 179 } 180 setSelectedNode(node); 181 if ( node != null) 182 { 183 node = getWebTree().getModel().getParent(node); 185 if ( node != null) 186 { 187 expandNode(node); 188 } 189 } 190 } 191 192 public Object findNodeByUrl(Object inRoot, String inUrl) 193 { 194 int count = getWebTree().getModel().getChildCount(inRoot); 195 for (int i = 0; i < count; i++) 196 { 197 Object child = getWebTree().getModel().getChild(inRoot,i); 198 199 String test = toUrl( child ); 200 if ( PathUtilities.match(test,inUrl ) ) 201 { 202 return child; 203 } 204 205 if( !inUrl.startsWith(test)) 207 { 208 continue; 209 } 210 child = findNodeByUrl(child,inUrl); 211 if ( child != null) 212 { 213 return child; 214 } 215 } 216 return null; 217 } 218 219 public void setSelectedNodeById(String inId) 220 { 221 if ( inId == null) 222 { 223 return; 224 } 225 Object node = getWebTree().getModel().getChildById(inId); 226 setSelectedNode(node); 227 if ( node != null) 228 { 229 node = getWebTree().getModel().getParent(node); 231 if ( node != null) 232 { 233 expandNode(node); 234 } 235 } 236 237 } 238 239 240 protected String renderNewFolder(Object inNode) 241 { 242 StringBuffer js = new StringBuffer ( 243 "new Node( " + quotes( toName( inNode ) ) + ", " + quotes( toUrl( inNode ) ) + ", "); 244 245 js.append(quotes(getWebTree().getModel().getId(inNode))); 246 247 if ( getWebTree().getModel().isLeaf( inNode ) ) 248 { 249 js.append(",null"); 250 } 251 else 252 { 253 js.append(renderChildren(inNode)); 254 } 255 256 js.append( "," + getWebTree().getModel().isLeaf( inNode ) + " )"); 257 258 return js.toString(); 259 } 260 261 protected String renderChildren(Object inNode) 262 { 263 StringBuffer js = new StringBuffer (); 266 267 if ( hasBeenExpanded( inNode ) ) 268 { 269 273 js.append(", new Array(\n"); 274 275 int c = getWebTree().getModel().getChildCount( inNode ); 276 for (int i = 0; i < c; i++ ) 277 { 278 if ( i > 0 ) 279 { 280 js.append(",\n"); 281 } 282 Object childNode = getWebTree().getModel().getChild( inNode, i ); 283 js.append(renderNewFolder(childNode)); 284 } 285 286 js.append(" ) "); 287 } 288 else 289 { 290 293 js.append(",null " ); 296 297 301 } 302 303 return js.toString(); 304 } 305 306 311 public abstract String toName(Object inNode); 312 313 318 public abstract String toUrl(Object inNode); 319 320 public String getCollapseNodeCommand() 321 { 322 return "WebTree.collapseTreeNode"; 323 } 324 325 public String getUrlPrefix() 326 { 327 return fieldUrlPrefix; 328 } 329 public void setUrlPrefix(String inUrlPrefix) 330 { 331 fieldUrlPrefix = inUrlPrefix; 332 } 333 public boolean isFoldersLinked() 334 { 335 return fieldFoldersLinked; 336 } 337 public void setFoldersLinked(boolean inFoldersLinked) 338 { 339 fieldFoldersLinked = inFoldersLinked; 340 } 341 342 public Object getSelectedNode() 343 { 344 return fieldSelectedNode; 345 } 346 public void setSelectedNode(Object inSelectedNode) 347 { 348 fieldSelectedNode = inSelectedNode; 349 } 350 public String getUrlPostfix() 351 { 352 return fieldUrlPostfix; 353 } 354 public void setUrlPostfix(String inUrlPostfix) 355 { 356 fieldUrlPostfix = inUrlPostfix; 357 } 358 } 359 | Popular Tags |