1 19 20 package org.netbeans.modules.editor; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import javax.swing.SwingUtilities ; 25 import javax.swing.Timer ; 26 import javax.swing.JEditorPane ; 27 import javax.swing.text.AbstractDocument ; 28 import javax.swing.text.Document ; 29 import javax.swing.text.BadLocationException ; 30 import javax.swing.text.Element ; 31 import javax.swing.text.JTextComponent ; 32 import org.netbeans.editor.BaseDocument; 33 import org.netbeans.editor.Utilities; 34 import org.netbeans.editor.JumpList; 35 import org.netbeans.editor.ext.ExtSyntaxSupport; 36 import org.openide.cookies.LineCookie; 37 import org.openide.cookies.EditorCookie; 38 import org.openide.loaders.DataObject; 39 import org.openide.text.Line; 40 import org.openide.windows.TopComponent; 41 import org.openide.util.Lookup; 42 import org.openide.ErrorManager; 43 import org.openide.util.NbBundle; 44 import java.util.MissingResourceException ; 45 import java.awt.Toolkit ; 46 import javax.swing.text.EditorKit ; 47 import org.openide.filesystems.FileObject; 48 49 55 56 public class NbEditorUtilities { 57 58 59 public static DataObject getDataObject(Document doc) { 60 Object sdp = doc.getProperty(Document.StreamDescriptionProperty); 61 if (sdp instanceof DataObject) { 62 return (DataObject)sdp; 63 } 64 return null; 65 } 66 67 71 public static boolean isDocumentActive(Document doc) { 72 DataObject dob = getDataObject(doc); 73 if (dob != null) { 74 EditorCookie editorCookie = (EditorCookie)dob.getCookie(EditorCookie.class); 75 if (editorCookie != null) { 76 Document ecDoc = editorCookie.getDocument(); if (ecDoc == doc) { return true; 79 } 80 } 81 } 82 83 return false; 84 } 85 86 87 public static FileObject getFileObject(Document doc) { 88 Object sdp = doc.getProperty(Document.StreamDescriptionProperty); 89 if (sdp instanceof FileObject) { 90 return (FileObject)sdp; 91 } 92 if (sdp instanceof DataObject) { 93 return ((DataObject)sdp).getPrimaryFile(); 94 } 95 return null; 96 } 97 98 107 public static int[] getIdentifierAndMethodBlock(BaseDocument doc, int offset) 108 throws BadLocationException { 109 int[] idBlk = Utilities.getIdentifierBlock(doc, offset); 110 if (idBlk != null) { 111 int[] funBlk = ((ExtSyntaxSupport)doc.getSyntaxSupport()).getFunctionBlock(idBlk); 112 if (funBlk != null) { 113 return new int[] { idBlk[0], idBlk[1], funBlk[1] }; 114 } 115 } 116 return idBlk; 117 } 118 119 127 public static Line getLine(BaseDocument doc, int offset, boolean original) { 128 DataObject dob = getDataObject(doc); 129 if (dob != null) { 130 LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class); 131 if (lc != null) { 132 Line.Set lineSet = lc.getLineSet(); 133 if (lineSet != null) { 134 try { 135 int lineOffset = Utilities.getLineOffset(doc, offset); 136 return original 137 ? lineSet.getOriginal(lineOffset) 138 : lineSet.getCurrent(lineOffset); 139 } catch (BadLocationException e) { 140 } 141 142 } 143 } 144 } 145 return null; 146 } 147 148 155 public static Line getLine(Document doc, int offset, boolean original) { 156 DataObject dob = getDataObject(doc); 157 if (dob != null) { 158 LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class); 159 if (lc != null) { 160 Line.Set lineSet = lc.getLineSet(); 161 if (lineSet != null) { 162 Element lineRoot = (doc instanceof AbstractDocument ) 163 ? ((AbstractDocument )doc).getParagraphElement(0).getParentElement() 164 : doc.getDefaultRootElement(); 165 int lineIndex = lineRoot.getElementIndex(offset); 166 return original 167 ? lineSet.getOriginal(lineIndex) 168 : lineSet.getCurrent(lineIndex); 169 } 170 } 171 } 172 return null; 173 } 174 175 176 public static Line getLine(JTextComponent target, boolean original) { 177 return getLine((BaseDocument)target.getDocument(), 178 target.getCaret().getDot(), original); 179 } 180 181 182 public static TopComponent getTopComponent(JTextComponent target) { 183 return (TopComponent)SwingUtilities.getAncestorOfClass(TopComponent.class, target); 184 } 185 186 187 public static TopComponent getOuterTopComponent(JTextComponent target) { 188 TopComponent tc = null; 189 TopComponent parent = (TopComponent)SwingUtilities.getAncestorOfClass(TopComponent.class, target); 190 while (parent != null) { 191 tc = parent; 192 parent = (TopComponent)SwingUtilities.getAncestorOfClass(TopComponent.class, tc); 193 } 194 return tc; 195 } 196 197 198 201 public static void addJumpListEntry(DataObject dob) { 202 final EditorCookie ec = (EditorCookie)dob.getCookie(EditorCookie.class); 203 if (ec != null) { 204 final Timer timer = new Timer (500, null); 205 timer.addActionListener( 206 new ActionListener () { 207 208 private int countDown = 10; 209 210 public void actionPerformed(ActionEvent evt) { 211 SwingUtilities.invokeLater( 212 new Runnable () { 213 public void run() { 214 if (--countDown >= 0) { 215 JEditorPane [] panes = ec.getOpenedPanes(); 216 if (panes != null && panes.length > 0) { 217 JumpList.checkAddEntry(panes[0]); 218 timer.stop(); 219 } 220 } else { 221 timer.stop(); 222 } 223 } 224 } 225 ); 226 } 227 } 228 ); 229 timer.start(); 230 } 231 } 232 233 234 public static String [] mergeStringArrays(String [] a1, String [] a2) { 235 String [] ret = new String [a1.length + a2.length]; 236 for (int i = 0; i < a1.length; i++) { 237 ret[i] = a1[i]; 238 } 239 for (int i = 0; i < a2.length; i++) { 240 ret[a1.length + i] = a2[i]; 241 } 242 return ret; 243 } 244 245 256 public static String getMimeType(Document doc) { 257 return (String )doc.getProperty(NbEditorDocument.MIME_TYPE_PROP); 258 } 259 260 272 public static String getMimeType(JTextComponent component) { 273 Document doc = component.getDocument(); 274 String mimeType = getMimeType(doc); 275 if (mimeType == null) { 276 EditorKit kit = component.getUI().getEditorKit(component); 277 if (kit != null) { 278 mimeType = kit.getContentType(); 279 } 280 } 281 return mimeType; 282 } 283 284 286 public static void invalidArgument(String bundleKey) { 287 IllegalArgumentException iae=new IllegalArgumentException ("Invalid argument"); Toolkit.getDefaultToolkit().beep(); 289 ErrorManager errMan=(ErrorManager)Lookup.getDefault().lookup(ErrorManager.class); 290 291 if (errMan!=null) { 292 errMan.annotate(iae, ErrorManager.USER, iae.getMessage(), getString(bundleKey), null, null); } 294 throw iae; 295 } 296 297 private static String getString(String key) { 298 try { 299 return NbBundle.getBundle(NbEditorUtilities.class).getString(key); 300 } catch (MissingResourceException e) { 301 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 302 return key; 303 } 304 } 305 306 307 } 308 | Popular Tags |