1 19 20 package org.netbeans.modules.html.palette; 21 import java.awt.Component ; 22 import java.awt.Container ; 23 import java.util.StringTokenizer ; 24 import javax.swing.JTree ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.Caret ; 27 import javax.swing.text.Document ; 28 import javax.swing.text.JTextComponent ; 29 import org.netbeans.api.project.FileOwnerQuery; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectUtils; 32 import org.netbeans.api.project.SourceGroup; 33 import org.netbeans.api.project.Sources; 34 import org.netbeans.editor.BaseDocument; 35 import org.netbeans.editor.Formatter; 36 import org.netbeans.editor.TokenItem; 37 import org.netbeans.editor.ext.html.HTMLSyntaxSupport; 38 import org.netbeans.editor.ext.html.HTMLTokenContext; 39 import org.openide.filesystems.FileObject; 40 41 42 46 public final class HTMLPaletteUtilities { 47 48 public static int wrapTags(HTMLSyntaxSupport sup, int start, int end, BaseDocument doc) { 49 50 try { 51 TokenItem token = sup.getTokenChain(start, start + 1); 52 53 if (token == null) 54 return end; 55 56 while (token.getOffset() < end) { token = token.getNext(); 58 if (token.getTokenID() == HTMLTokenContext.TAG_OPEN_SYMBOL) { int offset = token.getOffset(); 60 doc.insertString(offset, "\n", null); end++; token = sup.getTokenChain(offset + 1, offset + 2); } 64 } 65 66 } catch (IllegalStateException ise) { 67 } catch (BadLocationException ble) { 68 } 69 70 return end; 71 } 72 73 public static SourceGroup[] getSourceGroups(FileObject fObj) { 74 75 Project proj = FileOwnerQuery.getOwner(fObj); 76 SourceGroup[] sg = new SourceGroup[] {}; 77 if (proj != null) { 78 Sources sources = ProjectUtils.getSources(proj); 79 sg = sources.getSourceGroups("doc_root"); 80 } 83 84 return sg; 85 } 86 87 public static JTree findTreeComponent(Component component) { 88 if (component instanceof JTree ) { 89 return (JTree ) component; 90 } 91 if (component instanceof Container ) { 92 Component [] components = ((Container ) component).getComponents(); 93 for (int i = 0; i < components.length; i++) { 94 JTree tree = findTreeComponent(components[i]); 95 if (tree != null) { 96 return tree; 97 } 98 } 99 } 100 return null; 101 } 102 103 public static String getRelativePath(FileObject base, FileObject target) { 104 105 final String DELIM = "/"; 106 final String PARENT = ".." + DELIM; 107 108 String targetPath = target.getPath(); 109 String basePath = base.getPath(); 110 111 String baseDisc = basePath.substring(0, basePath.indexOf(DELIM)); 113 String targetDisc = targetPath.substring(0, targetPath.indexOf(DELIM)); 114 if (!baseDisc.equals(targetDisc)) 115 return ""; 117 basePath = basePath.substring(0, basePath.lastIndexOf(base.getNameExt())); 119 targetPath = targetPath.substring(0, targetPath.lastIndexOf(target.getNameExt())); 120 121 StringTokenizer baseST = new StringTokenizer (basePath, DELIM); 123 StringTokenizer targetST = new StringTokenizer (targetPath, DELIM); 124 String baseDir = ""; 125 String targetDir = ""; 126 while (baseST.hasMoreTokens() && targetST.hasMoreTokens() && baseDir.equals(targetDir)) { 127 baseDir = baseST.nextToken(); 128 targetDir = targetST.nextToken(); 129 } 130 StringBuffer parentPrefix = new StringBuffer (!baseDir.equals(targetDir) ? PARENT : ""); 132 while (baseST.hasMoreTokens()) { 133 parentPrefix.append(PARENT); 134 baseST.nextToken(); 135 } 136 StringBuffer targetSB = new StringBuffer (!baseDir.equals(targetDir) ? targetDir + DELIM : ""); 138 while (targetST.hasMoreTokens()) 139 targetSB.append(targetST.nextToken() + DELIM); 140 141 targetPath = parentPrefix.toString() + targetSB.toString() + target.getNameExt(); 143 144 return targetPath; 145 } 146 147 public static void insert(String s, JTextComponent target) 148 throws BadLocationException 149 { 150 insert(s, target, true); 151 } 152 153 public static void insert(String s, JTextComponent target, boolean reformat) 154 throws BadLocationException 155 { 156 157 if (s == null) 158 s = ""; 159 160 Document doc = target.getDocument(); 161 if (doc == null) 162 return; 163 164 if (doc instanceof BaseDocument) 165 ((BaseDocument)doc).atomicLock(); 166 167 int start = insert(s, target, doc); 168 169 if (reformat && start >= 0 && doc instanceof BaseDocument) { int end = start + s.length(); 171 Formatter f = ((BaseDocument)doc).getFormatter(); 172 f.reformat((BaseDocument)doc, start, end); 173 } 174 175 183 if (doc instanceof BaseDocument) 184 ((BaseDocument)doc).atomicUnlock(); 185 186 } 187 188 private static int insert(String s, JTextComponent target, Document doc) 189 throws BadLocationException 190 { 191 192 int start = -1; 193 try { 194 Caret caret = target.getCaret(); 196 int p0 = Math.min(caret.getDot(), caret.getMark()); 197 int p1 = Math.max(caret.getDot(), caret.getMark()); 198 doc.remove(p0, p1 - p0); 199 200 start = caret.getDot(); 202 doc.insertString(start, s, null); 203 } 204 catch (BadLocationException ble) {} 205 206 return start; 207 } 208 209 } 210 | Popular Tags |