1 19 20 package org.netbeans.modules.web.core.jsploader; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStream ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectInputStream ; 28 import java.util.Date ; 29 import java.beans.*; 30 import javax.swing.JEditorPane ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.EditorKit ; 33 import javax.swing.text.StyledDocument ; 34 import javax.swing.text.Document ; 35 import javax.swing.Timer ; 36 import javax.swing.event.CaretListener ; 37 import javax.swing.text.Caret ; 38 import javax.swing.event.CaretEvent ; 39 40 import org.openide.cookies.EditorCookie; 41 import org.openide.cookies.CloseCookie; 42 import org.openide.cookies.PrintCookie; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.loaders.DataObject; 46 import org.openide.loaders.MultiDataObject; 47 import org.openide.loaders.DataNode; 48 import org.openide.nodes.Node; 49 import org.openide.text.*; 50 import org.openide.windows.CloneableOpenSupport; 51 import org.openide.util.NbBundle; 52 import org.openide.util.actions.SystemAction; 53 54 import org.openide.filesystems.FileChangeAdapter; 55 import org.openide.filesystems.FileEvent; 56 import org.openide.util.WeakListeners; 57 58 66 67 public class ServletEditor extends CloneableEditorSupport 68 implements EditorCookie.Observable, CloseCookie, PrintCookie, PropertyChangeListener { 69 70 static final String ATTR_FILE_ENCODING = "Content-Encoding"; 72 75 public ServletEditor(JspDataObject jspdo) { 76 super(new JspEnv(jspdo)); 77 jspdo.addPropertyChangeListener(WeakListeners.propertyChange(this, jspdo)); 78 } 79 80 protected CloneableEditor createCloneableEditor () { 81 return new ServletEditorComponent (this); 82 } 83 84 protected JspEnv jspEnv() { 85 return (JspEnv)env; 86 } 87 88 89 protected void loadFromStreamToKit (StyledDocument doc, InputStream stream, EditorKit kit) throws IOException , BadLocationException { 90 FileObject fo = getServlet().getPrimaryFile(); 91 String encoding = (String ) fo.getAttribute(ATTR_FILE_ENCODING); 93 if (encoding == null) { 94 encoding = "ISO-8859-1"; } 96 InputStreamReader reader = new InputStreamReader (stream, encoding); 97 kit.read(reader, doc, 0); 98 } 99 100 protected JspServletDataObject getServlet() { 101 return jspEnv().getJspDataObject().getServletDataObject(); 102 } 103 104 109 protected String messageSave () { 110 return ""; 111 } 112 113 117 protected String messageName () { 118 DataObject dobj = getServlet(); 119 if (dobj == null) return "..."; if (! dobj.isValid()) return ""; 122 if(DataNode.getShowFileExtensions()) { 123 return dobj.getPrimaryFile().getNameExt(); 124 } else { 125 return dobj.getPrimaryFile().getName(); 126 } 127 } 128 129 133 protected String messageToolTip () { 134 DataObject dobj = getServlet(); 135 if (dobj != null) { 136 return FileUtil.getFileDisplayName(dobj.getPrimaryFile()); 138 } 139 else 140 return "..."; } 142 143 146 protected String messageOpening () { 147 DataObject obj = getServlet(); 148 if (obj == null) 149 return ""; 150 151 return NbBundle.getMessage (ServletEditor.class , "CTL_ObjectOpen", obj.getPrimaryFile().getNameExt(), 153 FileUtil.getFileDisplayName(obj.getPrimaryFile()) 154 ); 155 } 156 157 158 161 protected String messageOpened () { 162 DataObject obj = getServlet(); 163 if (obj == null) 164 return ""; 165 166 return NbBundle.getMessage (ServletEditor.class, "CTL_ObjectOpened", obj.getPrimaryFile().getNameExt(), 168 FileUtil.getFileDisplayName(obj.getPrimaryFile()) 169 ); 170 } 171 172 173 MultiDataObject.Entry getJavaEntry() { 174 return getServlet().getPrimaryEntry(); 175 } 176 177 183 protected StyledDocument createStyledDocument(EditorKit kit) { 184 StyledDocument doc = super.createStyledDocument(kit); 185 186 setDocumentProperties(doc); 187 188 return doc; 189 } 190 191 193 void setDocumentProperties(Document doc) { 194 DataObject obj = getServlet(); 195 if (obj != null) { 196 doc.putProperty(javax.swing.text.Document.TitleProperty, obj.getPrimaryFile().getPath()); 198 doc.putProperty(javax.swing.text.Document.StreamDescriptionProperty, obj); 200 } 201 } 202 203 public void propertyChange(PropertyChangeEvent evt) { 204 if (evt.getPropertyName().equals(JspDataObject.PROP_SERVLET_DATAOBJECT)) { 205 StyledDocument doc = getDocument(); 206 if (doc != null) { 207 setDocumentProperties(doc); 208 } 209 } 210 } 211 212 public static class ServletEditorComponent extends CloneableEditor { 213 214 static final int SELECTED_NODES_DELAY = 1000; Timer timerSelNodes; 217 CaretListener caretListener; 219 int lastCaretOffset = -1; 221 222 public ServletEditorComponent() { 223 super(); 224 } 225 226 public ServletEditorComponent(ServletEditor support) { 227 super(support); 228 init(); 229 } 230 231 protected void componentShowing() { 232 super.componentShowing(); pane.setEditable(false); 234 } 235 236 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 237 super.readExternal(in); 238 init(); 239 } 240 241 protected JEditorPane getPane() { 242 return pane; 243 } 244 245 248 249 250 private void init() { 251 if ( null != getPane()) { 253 getPane().setEditable(false); 254 } 255 ServletEditor se = (ServletEditor)cloneableEditorSupport(); 259 if (se != null) { 260 se.jspEnv().addPropertyChangeListener( 261 new PropertyChangeListener() { 262 public void propertyChange(PropertyChangeEvent evt) { 263 if (JspEnv.PROP_TIME.equals(evt.getPropertyName())) 264 initializeJavaNodes(); 265 ServletEditorComponent.this.updateName(); 266 } 267 } 268 ); 269 } 270 initializeJavaNodes(); 271 } 272 273 278 protected void componentActivated () { 279 pane.addCaretListener(caretListener); 280 super.componentActivated (); 281 } 282 283 287 void selectElementsAtOffset(final int offset) { 288 ServletEditor se = (ServletEditor)cloneableEditorSupport(); 289 if (se != null) { 290 JspDataObject jspdo = se.jspEnv().getJspDataObject(); 291 if (javax.swing.SwingUtilities.isEventDispatchThread()) { 292 setNodes(jspdo, offset); 293 } 294 else{ 295 javax.swing.SwingUtilities.invokeLater(new NodesThread(jspdo, offset)); 296 } 297 } 298 } 299 300 private class NodesThread implements Runnable { 301 JspDataObject jspdo; 302 int offset; 303 304 NodesThread (JspDataObject jspdo, int offset){ 305 this.jspdo = jspdo; 306 this.offset = offset; 307 } 308 309 public void run() { 310 setNodes(jspdo, offset); 311 } 312 } 313 314 private void setNodes (JspDataObject jspdo, final int offset){ 315 JspServletDataObject servlet = jspdo.getServletDataObject(); 316 if (servlet==null) { 317 setActivatedNodes(new Node[] { jspdo.getNodeDelegate() }); 318 return; 319 } 320 321 } 347 348 351 private void initializeJavaNodes() { 352 ServletEditor se = (ServletEditor)cloneableEditorSupport(); 354 if (se != null) { 355 timerSelNodes = new Timer (100, new java.awt.event.ActionListener () { 356 public void actionPerformed(java.awt.event.ActionEvent e) { 357 if (lastCaretOffset == -1 && pane != null) { 358 Caret caret = pane.getCaret(); 359 if (caret != null) 360 lastCaretOffset = caret.getDot(); 361 } 362 selectElementsAtOffset(lastCaretOffset); 363 } 364 }); 365 timerSelNodes.setInitialDelay(100); 366 timerSelNodes.setRepeats(false); 367 timerSelNodes.restart(); 368 caretListener = new CaretListener () { 369 public void caretUpdate(CaretEvent e) { 370 restartTimerSelNodes(e.getDot()); 371 } 372 }; 373 } 374 if (lastCaretOffset == -1 && pane != null) { 375 Caret caret = pane.getCaret(); 376 if (caret != null) lastCaretOffset = caret.getDot(); 377 } 378 selectElementsAtOffset(lastCaretOffset); 379 } 380 381 387 public SystemAction[] getSystemActions() { 388 selectElementsAtOffset(lastCaretOffset); 389 timerSelNodes.stop(); 390 return super.getSystemActions(); 391 } 392 393 398 void restartTimerSelNodes(int pos) { 399 timerSelNodes.setInitialDelay(SELECTED_NODES_DELAY); 400 timerSelNodes.restart(); 401 lastCaretOffset = pos; 402 } 403 404 } 406 private static class JspEnv extends FileChangeAdapter implements CloneableEditorSupport.Env, 407 java.io.Serializable , PropertyChangeListener { 408 409 private static final long serialVersionUID = -5748207023470614141L; 410 411 412 protected JspDataObject jspdo; 413 414 private DataObject servlet; 415 416 418 private transient PropertyChangeSupport propSupp; 419 420 public JspEnv(JspDataObject jspdo) { 421 this.jspdo = jspdo; 422 init(); 423 } 424 425 private DataObject getServlet() { 426 synchronized (jspdo) { 427 DataObject newServlet = jspdo.getServletDataObject(); 428 if (servlet != newServlet) { 429 if (servlet != null) { 430 servlet.getPrimaryFile().removeFileChangeListener(this); 431 } 432 if (newServlet != null) { 433 newServlet.getPrimaryFile().addFileChangeListener(this); 434 } 435 servlet = newServlet; 436 } 437 return servlet; 438 } 439 } 440 441 private void readObject (ObjectInputStream ois) 442 throws IOException , ClassNotFoundException { 443 ois.defaultReadObject(); 444 init(); 445 } 446 447 private void init() { 448 jspdo.addPropertyChangeListener(WeakListeners.propertyChange (this, jspdo)); 449 } 450 451 public JspDataObject getJspDataObject() { 452 return jspdo; 453 } 454 455 public void propertyChange(PropertyChangeEvent ev) { 456 if (JspDataObject.PROP_SERVLET_DATAOBJECT.equals (ev.getPropertyName())) { 457 DataObject servlet = getServlet(); 458 if (servlet == null) { 459 firePropertyChange(PROP_VALID, Boolean.TRUE, Boolean.FALSE); 460 } 461 else { 462 firePropertyChange(PROP_TIME, null, null); 463 } 464 } 465 466 firePropertyChange ( 467 ev.getPropertyName (), 468 ev.getOldValue (), 469 ev.getNewValue () 470 ); 471 } 472 473 478 protected void firePropertyChange (String name, Object oldValue, Object newValue) { 479 prop ().firePropertyChange (name, oldValue, newValue); 480 } 481 482 484 private PropertyChangeSupport prop () { 485 if (propSupp == null) { 486 synchronized (this) { 487 if (propSupp == null) { 488 propSupp = new PropertyChangeSupport (this); 489 } 490 } 491 } 492 return propSupp; 493 } 494 495 498 public InputStream inputStream () throws IOException { 499 DataObject servlet = getServlet(); 500 if (servlet != null) 501 return servlet.getPrimaryFile().getInputStream(); 502 else 503 return null; 504 } 505 506 509 public OutputStream outputStream () throws IOException { 510 return null; 512 } 513 514 516 public Date getTime () { 517 521 return null; 522 } 523 524 527 public String getMimeType () { 528 return "text/x-java"; } 530 532 public void addPropertyChangeListener (PropertyChangeListener l) { 533 prop ().addPropertyChangeListener (l); 534 } 535 537 public void removePropertyChangeListener (PropertyChangeListener l) { 538 prop ().removePropertyChangeListener (l); 539 } 540 541 543 public void addVetoableChangeListener (VetoableChangeListener l) { 544 } 545 547 public void removeVetoableChangeListener (VetoableChangeListener l) { 548 } 549 550 556 public boolean isValid () { 557 DataObject servlet = getServlet(); 558 return (servlet != null); 559 } 560 563 public boolean isModified () { 564 return false; 565 } 566 567 572 public void markModified () throws java.io.IOException { 573 } 575 576 579 public void unmarkModified () { 580 } 581 582 585 public CloneableOpenSupport findCloneableOpenSupport () { 586 return (CloneableOpenSupport)jspdo.getServletEditor(); 587 } 588 589 591 592 public void fileDeleted(FileEvent fe) { 593 fe.getFile().removeFileChangeListener(this); 594 595 jspdo.refreshPlugin(true); 596 } 597 598 601 public void fileChanged(FileEvent fe) { 602 firePropertyChange(PROP_TIME, null, null); 603 } 604 605 606 } 607 } 608 | Popular Tags |