1 19 20 package org.netbeans.modules.java.ui.editors; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyVetoException ; 24 25 import java.util.Enumeration ; 26 import java.util.StringTokenizer ; 27 28 import javax.swing.event.DocumentListener ; 29 import javax.swing.event.DocumentEvent ; 30 31 import javax.swing.SwingUtilities ; 32 import javax.swing.text.BadLocationException ; 33 import javax.swing.text.Caret ; 34 import javax.swing.text.Document ; 35 import javax.swing.text.JTextComponent ; 36 37 import org.openide.nodes.Node; 38 import org.openide.nodes.NodeOp; 39 import org.openide.explorer.ExplorerManager; 40 41 42 54 public class TreePathWalker implements DocumentListener , PropertyChangeListener { 55 58 ExplorerManager manager; 59 60 63 String delimiter; 64 65 68 JTextComponent textComponent; 69 70 73 boolean disableExplorer; 74 75 78 public static final String DEFAULT_DELIMITER = "."; 80 85 public TreePathWalker(ExplorerManager em) { 86 this(em, DEFAULT_DELIMITER); 87 } 88 89 95 public TreePathWalker(ExplorerManager man, String delimiter) { 96 this.delimiter = delimiter; 97 this.manager = man; 98 99 man.addPropertyChangeListener(this); 100 } 101 102 106 public void setTextComponent(JTextComponent text) { 107 if (this.textComponent != null) 108 textComponent.getDocument().removeDocumentListener(this); 109 textComponent = text; 110 if (text != null) 111 text.getDocument().addDocumentListener(this); 112 } 113 114 118 public JTextComponent getTextComponent() { 119 return textComponent; 120 } 121 122 private void selectString(Document doc) { 123 try { 124 String s = doc.getText(0, doc.getLength()); 125 selectString(s); 126 } catch (BadLocationException ex) { 127 } 128 } 129 130 133 public final void removeUpdate(javax.swing.event.DocumentEvent documentEvent) { 134 } 136 137 140 public final void insertUpdate(javax.swing.event.DocumentEvent documentEvent) { 141 selectString(documentEvent.getDocument()); 142 } 143 144 147 public final void changedUpdate(javax.swing.event.DocumentEvent documentEvent) { 148 selectString(documentEvent.getDocument()); 149 } 150 151 private boolean nameMatches(Node child, String component) { 152 return child.getName().startsWith(component); 153 } 154 155 private boolean namesEqual(Node child, String component) { 156 return child.getName().equals(component); 157 } 158 159 private Enumeration enumerateComponents(String s) { 160 return new StringTokenizer (s, getDelimiter()); 161 } 162 163 public String getDelimiter() { 164 return this.delimiter; 165 } 166 167 public ExplorerManager getExplorerManager() { 168 return this.manager; 169 } 170 171 174 private void selectString(String sel) { 175 Enumeration components = enumerateComponents(sel); 176 177 Node context = getExplorerManager().getRootContext(); 178 String component; 179 Node found = null; 180 boolean exact = false; 181 String suffix = ""; boolean ambiguous = false; 184 185 System.err.println("begin search"); 187 while (components.hasMoreElements()) { 188 String comp = (String )components.nextElement(); 189 Enumeration children = context.getChildren().nodes(); 190 int compLen = comp.length(); 191 found = null; 192 exact = false; 193 ambiguous = false; 195 196 System.err.println("got component: " + comp); suffix = null; 198 199 while (children.hasMoreElements()) { 200 Node ch = (Node)children.nextElement(); 201 String childName = ch.getName(); 202 203 if (!nameMatches(ch, comp)) { 205 continue; 206 } 207 if (found != null) 208 ambiguous = true; 209 if (!exact) { 210 found = ch; 211 } 212 if (namesEqual(ch, comp)) { 213 exact = true; suffix = ""; } else { 217 String childSuffix = childName.substring(compLen); 218 if (suffix == null) 219 suffix = childSuffix; 220 else 221 suffix = commonPrefix(suffix, childSuffix); 222 } 223 224 } 227 228 if (found == null || 229 (ambiguous && !exact)) { 230 break; 233 } 234 235 context = found; 236 } 237 if (found == null) 239 return; 240 241 if (exact || !ambiguous) { 242 selectNode(found); 244 if (sel.endsWith(getDelimiter())) { 246 expandNode(found); 247 } 248 } 249 250 JTextComponent textComp = getTextComponent(); 251 if ("".equals(suffix) || textComp == null) 252 return; 253 appendSelectedString(textComp, suffix); 254 } 255 256 private void expandNode(Node n) { 257 getExplorerManager().setExploredContext(n); 258 } 259 260 261 private void selectNode(Node n) { 262 try { 263 disableExplorer = true; 264 getExplorerManager().setSelectedNodes(new Node[] { 265 n 266 }); 267 } catch (PropertyVetoException ex) { 268 } finally { 272 disableExplorer = false; 273 } 274 } 275 276 private void appendSelectedString(final JTextComponent comp, final String suffix) { 277 if ("".equals(suffix)) 278 return; 279 280 final Document doc = comp.getDocument(); 281 SwingUtilities.invokeLater(new Runnable () { 282 public void run() { 283 try { 284 Caret c = comp.getCaret(); 285 int docLength = doc.getLength(); 286 doc.insertString(docLength, suffix, null); 287 288 int newLength = doc.getLength(); 289 c.setDot(newLength); 290 c.moveDot(docLength); 291 } catch (BadLocationException ex) { 292 ex.printStackTrace(); 293 } 294 } 295 }); 296 } 297 298 301 private String commonPrefix(String first, String second) { 302 int l1 = first.length(); 303 int l2 = second.length(); 304 int maxl = l1 > l2 ? l2 : l1; 305 306 for (int i = 0; i < maxl; i++) { 307 if (first.charAt(i) != second.charAt(i)) { 308 return first.substring(0, i); 309 } 310 } 311 return maxl == l1 ? first : second; 312 } 313 314 private void displayNodePath(Node target) { 315 String [] path = NodeOp.createPath(target, getExplorerManager().getRootContext()); 316 StringBuffer sb = new StringBuffer (); 317 for (int i = 0; i < path.length; i++) { 318 if (i > 0) 319 sb.append(getDelimiter()); 320 sb.append(path[i]); 321 } 322 JTextComponent txt = getTextComponent(); 323 if (txt != null) { 324 Document d = txt.getDocument(); 325 try { 326 d.remove(0, d.getLength()); 327 d.insertString(0, sb.toString(), null); 328 } catch (BadLocationException ex) { 329 } 330 } 331 } 332 333 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent) { 334 if (disableExplorer) 335 return; 336 337 String evName = propertyChangeEvent.getPropertyName(); 338 if (ExplorerManager.PROP_SELECTED_NODES.equals(evName)) { 339 Node[] selNodes = getExplorerManager().getSelectedNodes(); 342 if (selNodes.length == 0) 343 return; 344 displayNodePath(selNodes[0]); 345 } 346 } 347 } 348 | Popular Tags |