1 19 package org.netbeans.modules.openfile; 20 21 import java.awt.Container ; 22 import java.awt.EventQueue ; 23 import java.awt.event.ActionEvent ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.IOException ; 27 import javax.swing.Action ; 28 import javax.swing.JEditorPane ; 29 import javax.swing.SwingUtilities ; 30 import javax.swing.text.Element ; 31 import javax.swing.text.StyledDocument ; 32 import org.openide.DialogDisplayer; 33 import org.openide.ErrorManager; 34 import org.openide.NotifyDescriptor; 35 import org.openide.actions.FileSystemAction; 36 import org.openide.actions.ToolsAction; 37 import org.openide.awt.StatusDisplayer; 38 import org.openide.cookies.EditCookie; 39 import org.openide.cookies.EditorCookie; 40 import org.openide.cookies.OpenCookie; 41 import org.openide.cookies.ViewCookie; 42 import org.openide.filesystems.FileObject; 43 import org.openide.loaders.DataObject; 44 import org.openide.loaders.DataObjectNotFoundException; 45 import org.openide.nodes.Node; 46 import org.openide.nodes.NodeOperation; 47 import org.openide.text.NbDocument; 48 import org.openide.util.NbBundle; 49 import org.openide.windows.TopComponent; 50 51 52 57 public class DefaultOpenFileImpl implements OpenFileImpl, Runnable { 58 59 60 static final String JAVA_EXT = ".JAVA"; 62 static final String TXT_EXT = ".TXT"; 69 private static final int OPEN_EDITOR_WAIT_PERIOD_MS = 100; 70 76 private static final int OPEN_EDITOR_TOTAL_TIMEOUT_MS = 1000; 77 78 private static final String ZIP_EXT = "zip"; private static final String JAR_EXT = "jar"; private static final String WAR_EXT = "war"; 82 86 private final FileObject fileObject; 87 92 private final int line; 93 94 107 private DefaultOpenFileImpl(FileObject fileObject, 108 int line) { 109 this.fileObject = fileObject; 110 this.line = line; 111 } 112 113 114 public DefaultOpenFileImpl() { 115 116 117 this.fileObject = null; 118 this.line = -1; 119 } 120 121 126 protected final void setStatusLine(String text) { 127 StatusDisplayer.getDefault().setStatusText(text); 128 } 129 130 139 protected void notifyCannotOpen(String fileName) { 140 assert EventQueue.isDispatchThread(); 141 142 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 143 NbBundle.getMessage(DefaultOpenFileImpl.class, 144 "MSG_cannotOpenWillClose", fileName))); 146 } 147 148 160 private boolean openEditor(final EditorCookie editorCookie, 161 final int line) { 162 assert EventQueue.isDispatchThread(); 163 164 165 JEditorPane [] openPanes = editorCookie.getOpenedPanes(); 166 if (openPanes != null) { 167 if (line >= 0) { 168 int cursorOffset = getCursorOffset(editorCookie.getDocument(), 169 line); 170 openPanes[0].setCaretPosition(cursorOffset); 171 } 172 Container c = SwingUtilities.getAncestorOfClass(TopComponent.class, 173 openPanes[0]); 174 assert c != null; 175 176 final TopComponent tc = (TopComponent) c; 177 EventQueue.invokeLater(new Runnable () { 178 public void run() { 179 tc.requestActive(); 180 } 181 }); 182 return true; 183 } 184 185 186 final StyledDocument doc; 187 try { 188 doc = editorCookie.openDocument(); 189 } catch (IOException ex) { 190 String msg = NbBundle.getMessage( 191 DefaultOpenFileImpl.class, 192 "MSG_cannotOpenWillClose"); ErrorManager.getDefault().notify( 194 ErrorManager.EXCEPTION, 195 ErrorManager.getDefault().annotate(ex, msg)); 196 return false; 197 } 198 199 if (line < 0) { 200 editorCookie.open(); 201 202 208 } else { 209 openDocAtLine(editorCookie, doc, line); 210 } 211 return true; 212 } 213 214 225 private void openDocAtLine(final EditorCookie editorCookie, 226 final StyledDocument doc, 227 final int line) { 228 assert EventQueue.isDispatchThread(); 229 assert line >= 0; 230 assert editorCookie.getDocument() == doc; 231 232 233 final int offset = getCursorOffset(doc, line); 234 235 class SetCursorTask implements Runnable { 236 private boolean completed = false; 237 private PropertyChangeListener listenerToUnregister; 238 private boolean perform() { 239 if (EventQueue.isDispatchThread()) { 240 run(); 241 } else { 242 try { 243 EventQueue.invokeAndWait(this); 244 } catch (Exception ex) { 245 ErrorManager.getDefault().notify(ex); 246 247 completed = true; } 249 } 250 return completed; 251 } 252 public void run() { 253 assert EventQueue.isDispatchThread(); 254 255 if (completed) { 256 return; 257 } 258 259 JEditorPane [] panes = editorCookie.getOpenedPanes(); 260 if (panes != null) { 261 panes[0].setCaretPosition(offset); 262 if (listenerToUnregister != null) { 263 ((EditorCookie.Observable) editorCookie) 264 .removePropertyChangeListener(listenerToUnregister); 265 } 266 completed = true; 267 } 268 } 269 private void setListenerToUnregister(PropertyChangeListener l) { 270 listenerToUnregister = l; 271 } 272 } 273 274 final SetCursorTask setCursorTask = new SetCursorTask(); 275 276 editorCookie.open(); 277 if (setCursorTask.perform()) { 278 return; 279 } 280 if (editorCookie instanceof EditorCookie.Observable) { 281 if (!setCursorTask.perform()) { 282 PropertyChangeListener openPanesListener 283 = new PropertyChangeListener () { 284 public void propertyChange(PropertyChangeEvent e) { 285 if (EditorCookie.Observable.PROP_OPENED_PANES 286 .equals(e.getPropertyName())) { 287 setCursorTask.perform(); 288 } 289 } 290 }; 291 setCursorTask.setListenerToUnregister(openPanesListener); 292 ((EditorCookie.Observable) editorCookie) 293 .addPropertyChangeListener(openPanesListener); 294 setCursorTask.perform(); 295 } 296 } else { 297 final int numberOfTries = OPEN_EDITOR_TOTAL_TIMEOUT_MS 298 / OPEN_EDITOR_WAIT_PERIOD_MS; 299 for (int i = 0; i < numberOfTries; i++) { 300 try { 301 Thread.currentThread().sleep(OPEN_EDITOR_WAIT_PERIOD_MS); 302 } catch (InterruptedException ex) { 303 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, 304 ex); 305 } 306 if (setCursorTask.perform()) { 307 break; 308 } 309 } 310 if (!setCursorTask.completed) { 311 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage( 312 DefaultOpenFileImpl.class, 313 "MSG_couldNotOpenAt")); } 315 } 316 } 317 318 328 private int getCursorOffset(StyledDocument doc, int line) { 329 assert EventQueue.isDispatchThread(); 330 assert line >= 0; 331 332 try { 333 return NbDocument.findLineOffset(doc, line); 334 } catch (IndexOutOfBoundsException ex) { 335 336 337 Element lineRootElement 338 = NbDocument.findLineRootElement(doc); 339 int lineCount = lineRootElement.getElementCount(); 340 if (line >= lineCount) { 341 return NbDocument.findLineOffset(doc, lineCount - 1); 342 } else { 343 throw ex; 344 } 345 } 346 } 347 348 369 protected boolean openByCookie(Node.Cookie cookie, 370 Class cookieClass, 371 final int line) { 372 assert EventQueue.isDispatchThread(); 373 374 if ((cookieClass == EditorCookie.Observable.class) 375 || (cookieClass == EditorCookie.Observable.class)) { 376 return openEditor((EditorCookie) cookie, line); 377 } else if (cookieClass == OpenCookie.class) { 378 ((OpenCookie) cookie).open(); 379 } else if (cookieClass == EditCookie.class) { 380 ((EditCookie) cookie).edit(); 381 } else if (cookieClass == ViewCookie.class) { 382 ((ViewCookie) cookie).view(); 383 } else { 384 throw new IllegalArgumentException (); 385 } 386 return true; 387 } 388 389 402 private final boolean openDataObjectByCookie(DataObject dataObject, 403 int line) { 404 405 Class cookieClass; 406 Node.Cookie cookie; 407 if( ( cookie = dataObject.getCookie(cookieClass = OpenCookie.class)) != null 408 || (cookie = dataObject.getCookie(cookieClass = EditCookie.class)) != null 409 || (cookie = dataObject.getCookie(cookieClass = ViewCookie.class)) != null) { 410 return openByCookie(cookie, cookieClass, line); 411 } 412 return false; 413 } 414 415 419 public void run() { 420 assert EventQueue.isDispatchThread(); 421 422 open(fileObject, line); 423 } 424 425 430 public boolean open(final FileObject fileObject, int line) { 431 if (!EventQueue.isDispatchThread()) { 432 EventQueue.invokeLater( 433 new DefaultOpenFileImpl(fileObject, line)); 434 return true; 435 } 436 437 438 assert EventQueue.isDispatchThread(); 439 440 String fileName = fileObject.getNameExt(); 441 442 443 final DataObject dataObject; 444 try { 445 dataObject = DataObject.find(fileObject); 446 } catch (DataObjectNotFoundException ex) { 447 ErrorManager.getDefault().notify(ex); 448 return false; 449 } 450 451 Class cookieClass; 452 Node.Cookie cookie; 453 454 if ( (line != -1 && ((cookie = dataObject.getCookie(cookieClass = EditorCookie.Observable.class)) != null 455 || (cookie = dataObject.getCookie(cookieClass = EditorCookie.class)) != null)) ){ 456 boolean ret = openByCookie(cookie,cookieClass, line); 457 return ret; 458 } 459 460 461 final Node dataNode = dataObject.getNodeDelegate(); 462 final Action action = dataNode.getPreferredAction(); 463 if (action != null && !(action instanceof FileSystemAction) && !(action instanceof ToolsAction)) { 464 EventQueue.invokeLater(new Runnable () { 465 public void run() { 466 action.actionPerformed(new ActionEvent (dataNode, 0, null)); 467 } 468 }); 469 return true; 470 } 471 472 473 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(DefaultOpenFileImpl.class, "MSG_opening", fileName)); 474 boolean success = openDataObjectByCookie(dataObject, line); 475 if (success) { 476 return true; 477 } 478 479 String ext = fileObject.getExt(); 480 if ( 481 ZIP_EXT.equalsIgnoreCase(ext) || 482 JAR_EXT.equalsIgnoreCase(ext) || 483 WAR_EXT.equalsIgnoreCase(ext) || 484 fileObject.isFolder() 485 ) { 486 Node node = dataObject.getNodeDelegate(); 488 if (node != null) { 489 NodeOperation.getDefault().explore(node); 490 return true; 491 } else { 492 return false; 493 } 494 } 495 496 return false; 497 } 498 499 } 500 | Popular Tags |