1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import sun.swing.SwingUtilities2; 28 29 import java.awt.*; 30 import java.awt.event.*; 31 import java.lang.reflect.*; 32 import java.net.*; 33 import java.util.*; 34 import java.io.*; 35 import java.util.*; 36 37 import javax.swing.plaf.*; 38 import javax.swing.text.*; 39 import javax.swing.event.*; 40 import javax.swing.text.html.*; 41 import javax.accessibility.*; 42 43 /** 44 * A text component to edit various kinds of content. 45 * You can find how-to information and examples of using editor panes in 46 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/text.html">Using Text Components</a>, 47 * a section in <em>The Java Tutorial.</em> 48 * 49 * <p> 50 * This component uses implementations of the 51 * <code>EditorKit</code> to accomplish its behavior. It effectively 52 * morphs into the proper kind of text editor for the kind 53 * of content it is given. The content type that editor is bound 54 * to at any given time is determined by the <code>EditorKit</code> currently 55 * installed. If the content is set to a new URL, its type is used 56 * to determine the <code>EditorKit</code> that should be used to 57 * load the content. 58 * <p> 59 * By default, the following types of content are known: 60 * <dl> 61 * <dt><b>text/plain</b> 62 * <dd>Plain text, which is the default the type given isn't 63 * recognized. The kit used in this case is an extension of 64 * <code>DefaultEditorKit</code> that produces a wrapped plain text view. 65 * <dt><b>text/html</b> 66 * <dd>HTML text. The kit used in this case is the class 67 * <code>javax.swing.text.html.HTMLEditorKit</code> 68 * which provides HTML 3.2 support. 69 * <dt><b>text/rtf</b> 70 * <dd>RTF text. The kit used in this case is the class 71 * <code>javax.swing.text.rtf.RTFEditorKit</code> 72 * which provides a limited support of the Rich Text Format. 73 * </dl> 74 * <p> 75 * There are several ways to load content into this component. 76 * <ol> 77 * <li> 78 * The {@link #setText setText} method can be used to initialize 79 * the component from a string. In this case the current 80 * <code>EditorKit</code> will be used, and the content type will be 81 * expected to be of this type. 82 * <li> 83 * The {@link #read read} method can be used to initialize the 84 * component from a <code>Reader</code>. Note that if the content type is HTML, 85 * relative references (e.g. for things like images) can't be resolved 86 * unless the <base> tag is used or the <em>Base</em> property 87 * on <code>HTMLDocument</code> is set. 88 * In this case the current <code>EditorKit</code> will be used, 89 * and the content type will be expected to be of this type. 90 * <li> 91 * The {@link #setPage setPage} method can be used to initialize 92 * the component from a URL. In this case, the content type will be 93 * determined from the URL, and the registered <code>EditorKit</code> 94 * for that content type will be set. 95 * </ol> 96 * <p> 97 * Some kinds of content may provide hyperlink support by generating 98 * hyperlink events. The HTML <code>EditorKit</code> will generate 99 * hyperlink events if the <code>JEditorPane</code> is <em>not editable</em> 100 * (<code>JEditorPane.setEditable(false);</code> has been called). 101 * If HTML frames are embedded in the document, the typical response would be 102 * to change a portion of the current document. The following code 103 * fragment is a possible hyperlink listener implementation, that treats 104 * HTML frame events specially, and simply displays any other activated 105 * hyperlinks. 106 * <pre> 107 108 class Hyperactive implements HyperlinkListener { 109 110 public void hyperlinkUpdate(HyperlinkEvent e) { 111 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 112 JEditorPane pane = (JEditorPane) e.getSource(); 113 if (e instanceof HTMLFrameHyperlinkEvent) { 114 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e; 115 HTMLDocument doc = (HTMLDocument)pane.getDocument(); 116 doc.processHTMLFrameHyperlinkEvent(evt); 117 } else { 118 try { 119 pane.setPage(e.getURL()); 120 } catch (Throwable t) { 121 t.printStackTrace(); 122 } 123 } 124 } 125 } 126 } 127 128 * </pre> 129 * <p> 130 * For information on customizing how <b>text/html</b> is rendered please see 131 * {@link #W3C_LENGTH_UNITS} and {@link #HONOR_DISPLAY_PROPERTIES} 132 * <p> 133 * Culturally dependent information in some documents is handled through 134 * a mechanism called character encoding. Character encoding is an 135 * unambiguous mapping of the members of a character set (letters, ideographs, 136 * digits, symbols, or control functions) to specific numeric code values. It 137 * represents the way the file is stored. Example character encodings are 138 * ISO-8859-1, ISO-8859-5, Shift-jis, Euc-jp, and UTF-8. When the file is 139 * passed to an user agent (<code>JEditorPane</code>) it is converted to 140 * the document character set (ISO-10646 aka Unicode). 141 * <p> 142 * There are multiple ways to get a character set mapping to happen 143 * with <code>JEditorPane</code>. 144 * <ol> 145 * <li> 146 * One way is to specify the character set as a parameter of the MIME 147 * type. This will be established by a call to the 148 * {@link #setContentType setContentType} method. If the content 149 * is loaded by the {@link #setPage setPage} method the content 150 * type will have been set according to the specification of the URL. 151 * It the file is loaded directly, the content type would be expected to 152 * have been set prior to loading. 153 * <li> 154 * Another way the character set can be specified is in the document itself. 155 * This requires reading the document prior to determining the character set 156 * that is desired. To handle this, it is expected that the 157 * <code>EditorKit</code>.read operation throw a 158 * <code>ChangedCharSetException</code> which will 159 * be caught. The read is then restarted with a new Reader that uses 160 * the character set specified in the <code>ChangedCharSetException</code> 161 * (which is an <code>IOException</code>). 162 * </ol> 163 * 164 * <dl> 165 * <dt><b>Newlines</b> 166 * <dd> 167 * For a discussion on how newlines are handled, see 168 * <a href="text/DefaultEditorKit.html">DefaultEditorKit</a>. 169 * </dl> 170 * 171 * <p> 172 * <strong>Warning:</strong> Swing is not thread safe. For more 173 * information see <a 174 * href="package-summary.html#threading">Swing's Threading 175 * Policy</a>. 176 * <p> 177 * <strong>Warning:</strong> 178 * Serialized objects of this class will not be compatible with 179 * future Swing releases. The current serialization support is 180 * appropriate for short term storage or RMI between applications running 181 * the same version of Swing. As of 1.4, support for long term storage 182 * of all JavaBeans™ 183 * has been added to the <code>java.beans</code> package. 184 * Please see {@link java.beans.XMLEncoder}. 185 * 186 * @beaninfo 187 * attribute: isContainer false 188 * description: A text component to edit various types of content. 189 * 190 * @author Timothy Prinzing 191 * @since 1.2 192 */ 193 @SuppressWarnings("serial") // Same-version serialization only 194 public class JEditorPane extends JTextComponent { 195 196 /** 197 * Creates a new <code>JEditorPane</code>. 198 * The document model is set to <code>null</code>. 199 */ 200 public JEditorPane() { 201 super(); 202 setFocusCycleRoot(true); 203 setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() { 204 public Component getComponentAfter(Container focusCycleRoot, 205 Component aComponent) { 206 if (focusCycleRoot != JEditorPane.this || 207 (!isEditable() && getComponentCount() > 0)) { 208 return super.getComponentAfter(focusCycleRoot, 209 aComponent); 210 } else { 211 Container rootAncestor = getFocusCycleRootAncestor(); 212 return (rootAncestor != null) 213 ? rootAncestor.getFocusTraversalPolicy(). 214 getComponentAfter(rootAncestor, 215 JEditorPane.this) 216 : null; 217 } 218 } 219 public Component getComponentBefore(Container focusCycleRoot, 220 Component aComponent) { 221 if (focusCycleRoot != JEditorPane.this || 222 (!isEditable() && getComponentCount() > 0)) { 223 return super.getComponentBefore(focusCycleRoot, 224 aComponent); 225 } else { 226 Container rootAncestor = getFocusCycleRootAncestor(); 227 return (rootAncestor != null) 228 ? rootAncestor.getFocusTraversalPolicy(). 229 getComponentBefore(rootAncestor, 230 JEditorPane.this) 231 : null; 232 } 233 } 234 public Component getDefaultComponent(Container focusCycleRoot) 235 { 236 return (focusCycleRoot != JEditorPane.this || 237 (!isEditable() && getComponentCount() > 0)) 238 ? super.getDefaultComponent(focusCycleRoot) 239 : null; 240 } 241 protected boolean accept(Component aComponent) { 242 return (aComponent != JEditorPane.this) 243 ? super.accept(aComponent) 244 : false; 245 } 246 }); 247 LookAndFeel.installProperty(this, 248 "focusTraversalKeysForward", 249 JComponent. 250 getManagingFocusForwardTraversalKeys()); 251 LookAndFeel.installProperty(this, 252 "focusTraversalKeysBackward", 253 JComponent. 254 getManagingFocusBackwardTraversalKeys()); 255 } 256 257 /** 258 * Creates a <code>JEditorPane</code> based on a specified URL for input. 259 * 260 * @param initialPage the URL 261 * @exception IOException if the URL is <code>null</code> 262 * or cannot be accessed 263 */ 264 public JEditorPane(URL initialPage) throws IOException { 265 this(); 266 setPage(initialPage); 267 } 268 269 /** 270 * Creates a <code>JEditorPane</code> based on a string containing 271 * a URL specification. 272 * 273 * @param url the URL 274 * @exception IOException if the URL is <code>null</code> or 275 * cannot be accessed 276 */ 277 public JEditorPane(String url) throws IOException { 278 this(); 279 setPage(url); 280 } 281 282 /** 283 * Creates a <code>JEditorPane</code> that has been initialized 284 * to the given text. This is a convenience constructor that calls the 285 * <code>setContentType</code> and <code>setText</code> methods. 286 * 287 * @param type mime type of the given text 288 * @param text the text to initialize with; may be <code>null</code> 289 * @exception NullPointerException if the <code>type</code> parameter 290 * is <code>null</code> 291 */ 292 public JEditorPane(String type, String text) { 293 this(); 294 setContentType(type); 295 setText(text); 296 } 297 298 /** 299 * Adds a hyperlink listener for notification of any changes, for example 300 * when a link is selected and entered. 301 * 302 * @param listener the listener 303 */ 304 public synchronized void addHyperlinkListener(HyperlinkListener listener) { 305 listenerList.add(HyperlinkListener.class, listener); 306 } 307 308 /** 309 * Removes a hyperlink listener. 310 * 311 * @param listener the listener 312 */ 313 public synchronized void removeHyperlinkListener(HyperlinkListener listener) { 314 listenerList.remove(HyperlinkListener.class, listener); 315 } 316 317 /** 318 * Returns an array of all the <code>HyperLinkListener</code>s added 319 * to this JEditorPane with addHyperlinkListener(). 320 * 321 * @return all of the <code>HyperLinkListener</code>s added or an empty 322 * array if no listeners have been added 323 * @since 1.4 324 */ 325 public synchronized HyperlinkListener[] getHyperlinkListeners() { 326 return listenerList.getListeners(javax.swing.event.HyperlinkListener.class); 327 } 328 329 /** 330 * Notifies all listeners that have registered interest for 331 * notification on this event type. This is normally called 332 * by the currently installed <code>EditorKit</code> if a content type 333 * that supports hyperlinks is currently active and there 334 * was activity with a link. The listener list is processed 335 * last to first. 336 * 337 * @param e the event 338 * @see EventListenerList 339 */ 340 public void fireHyperlinkUpdate(HyperlinkEvent e) { 341 // Guaranteed to return a non-null array 342 Object[] listeners = listenerList.getListenerList(); 343 // Process the listeners last to first, notifying 344 // those that are interested in this event 345 for (int i = listeners.length-2; i>=0; i-=2) { 346 if (listeners[i]==HyperlinkListener.class) { 347 ((HyperlinkListener)listeners[i+1]).hyperlinkUpdate(e); 348 } 349 } 350 } 351 352 353 /** 354 * Sets the current URL being displayed. The content type of the 355 * pane is set, and if the editor kit for the pane is 356 * non-<code>null</code>, then 357 * a new default document is created and the URL is read into it. 358 * If the URL contains and reference location, the location will 359 * be scrolled to by calling the <code>scrollToReference</code> 360 * method. If the desired URL is the one currently being displayed, 361 * the document will not be reloaded. To force a document 362 * reload it is necessary to clear the stream description property 363 * of the document. The following code shows how this can be done: 364 * 365 * <pre> 366 * Document doc = jEditorPane.getDocument(); 367 * doc.putProperty(Document.StreamDescriptionProperty, null); 368 * </pre> 369 * 370 * If the desired URL is not the one currently being 371 * displayed, the <code>getStream</code> method is called to 372 * give subclasses control over the stream provided. 373 * <p> 374 * This may load either synchronously or asynchronously 375 * depending upon the document returned by the <code>EditorKit</code>. 376 * If the <code>Document</code> is of type 377 * <code>AbstractDocument</code> and has a value returned by 378 * <code>AbstractDocument.getAsynchronousLoadPriority</code> 379 * that is greater than or equal to zero, the page will be 380 * loaded on a separate thread using that priority. 381 * <p> 382 * If the document is loaded synchronously, it will be 383 * filled in with the stream prior to being installed into 384 * the editor with a call to <code>setDocument</code>, which 385 * is bound and will fire a property change event. If an 386 * <code>IOException</code> is thrown the partially loaded 387 * document will 388 * be discarded and neither the document or page property 389 * change events will be fired. If the document is 390 * successfully loaded and installed, a view will be 391 * built for it by the UI which will then be scrolled if 392 * necessary, and then the page property change event 393 * will be fired. 394 * <p> 395 * If the document is loaded asynchronously, the document 396 * will be installed into the editor immediately using a 397 * call to <code>setDocument</code> which will fire a 398 * document property change event, then a thread will be 399 * created which will begin doing the actual loading. 400 * In this case, the page property change event will not be 401 * fired by the call to this method directly, but rather will be 402 * fired when the thread doing the loading has finished. 403 * It will also be fired on the event-dispatch thread. 404 * Since the calling thread can not throw an <code>IOException</code> 405 * in the event of failure on the other thread, the page 406 * property change event will be fired when the other 407 * thread is done whether the load was successful or not. 408 * 409 * @param page the URL of the page 410 * @exception IOException for a <code>null</code> or invalid 411 * page specification, or exception from the stream being read 412 * @see #getPage 413 * @beaninfo 414 * description: the URL used to set content 415 * bound: true 416 * expert: true 417 */ 418 public void setPage(URL page) throws IOException { 419 if (page == null) { 420 throw new IOException("invalid url"); 421 } 422 URL loaded = getPage(); 423 424 425 // reset scrollbar 426 if (!page.equals(loaded) && page.getRef() == null) { 427 scrollRectToVisible(new Rectangle(0,0,1,1)); 428 } 429 boolean reloaded = false; 430 Object postData = getPostData(); 431 if ((loaded == null) || !loaded.sameFile(page) || (postData != null)) { 432 // different url or POST method, load the new content 433 434 int p = getAsynchronousLoadPriority(getDocument()); 435 if (p < 0) { 436 // open stream synchronously 437 InputStream in = getStream(page); 438 if (kit != null) { 439 Document doc = initializeModel(kit, page); 440 441 // At this point, one could either load up the model with no 442 // view notifications slowing it down (i.e. best synchronous 443 // behavior) or set the model and start to feed it on a separate 444 // thread (best asynchronous behavior). 445 p = getAsynchronousLoadPriority(doc); 446 if (p >= 0) { 447 // load asynchronously 448 setDocument(doc); 449 synchronized(this) { 450 pageLoader = new PageLoader(doc, in, loaded, page); 451 pageLoader.execute(); 452 } 453 return; 454 } 455 read(in, doc); 456 setDocument(doc); 457 reloaded = true; 458 } 459 } else { 460 // we may need to cancel background loading 461 if (pageLoader != null) { 462 pageLoader.cancel(true); 463 } 464 465 // Do everything in a background thread. 466 // Model initialization is deferred to that thread, too. 467 pageLoader = new PageLoader(null, null, loaded, page); 468 pageLoader.execute(); 469 return; 470 } 471 } 472 final String reference = page.getRef(); 473 if (reference != null) { 474 if (!reloaded) { 475 scrollToReference(reference); 476 } 477 else { 478 // Have to scroll after painted. 479 SwingUtilities.invokeLater(new Runnable() { 480 public void run() { 481 scrollToReference(reference); 482 } 483 }); 484 } 485 getDocument().putProperty(Document.StreamDescriptionProperty, page); 486 } 487 firePropertyChange("page", loaded, page); 488 } 489 490 /** 491 * Create model and initialize document properties from page properties. 492 */ 493 private Document initializeModel(EditorKit kit, URL page) { 494 Document doc = kit.createDefaultDocument(); 495 if (pageProperties != null) { 496 // transfer properties discovered in stream to the 497 // document property collection. 498 for (Enumeration<String> e = pageProperties.keys(); e.hasMoreElements() ;) { 499 String key = e.nextElement(); 500 doc.putProperty(key, pageProperties.get(key)); 501 } 502 pageProperties.clear(); 503 } 504 if (doc.getProperty(Document.StreamDescriptionProperty) == null) { 505 doc.putProperty(Document.StreamDescriptionProperty, page); 506 } 507 return doc; 508 } 509 510 /** 511 * Return load priority for the document or -1 if priority not supported. 512 */ 513 private int getAsynchronousLoadPriority(Document doc) { 514 return (doc instanceof AbstractDocument ? 515 ((AbstractDocument) doc).getAsynchronousLoadPriority() : -1); 516 } 517 518 /** 519 * This method initializes from a stream. If the kit is 520 * set to be of type <code>HTMLEditorKit</code>, and the 521 * <code>desc</code> parameter is an <code>HTMLDocument</code>, 522 * then it invokes the <code>HTMLEditorKit</code> to initiate 523 * the read. Otherwise it calls the superclass 524 * method which loads the model as plain text. 525 * 526 * @param in the stream from which to read 527 * @param desc an object describing the stream 528 * @exception IOException as thrown by the stream being 529 * used to initialize 530 * @see JTextComponent#read 531 * @see #setDocument 532 */ 533 public void read(InputStream in, Object desc) throws IOException { 534 535 if (desc instanceof HTMLDocument && 536 kit instanceof HTMLEditorKit) { 537 HTMLDocument hdoc = (HTMLDocument) desc; 538 setDocument(hdoc); 539 read(in, hdoc); 540 } else { 541 String charset = (String) getClientProperty("charset"); 542 Reader r = (charset != null) ? new InputStreamReader(in, charset) : 543 new InputStreamReader(in); 544 super.read(r, desc); 545 } 546 } 547 548 549 /** 550 * This method invokes the <code>EditorKit</code> to initiate a 551 * read. In the case where a <code>ChangedCharSetException</code> 552 * is thrown this exception will contain the new CharSet. 553 * Therefore the <code>read</code> operation 554 * is then restarted after building a new Reader with the new charset. 555 * 556 * @param in the inputstream to use 557 * @param doc the document to load 558 * 559 */ 560 void read(InputStream in, Document doc) throws IOException { 561 if (! Boolean.TRUE.equals(doc.getProperty("IgnoreCharsetDirective"))) { 562 final int READ_LIMIT = 1024 * 10; 563 in = new BufferedInputStream(in, READ_LIMIT); 564 in.mark(READ_LIMIT); 565 } 566 try { 567 String charset = (String) getClientProperty("charset"); 568 Reader r = (charset != null) ? new InputStreamReader(in, charset) : 569 new InputStreamReader(in); 570 kit.read(r, doc, 0); 571 } catch (BadLocationException e) { 572 throw new IOException(e.getMessage()); 573 } catch (ChangedCharSetException changedCharSetException) { 574 String charSetSpec = changedCharSetException.getCharSetSpec(); 575 if (changedCharSetException.keyEqualsCharSet()) { 576 putClientProperty("charset", charSetSpec); 577 } else { 578 setCharsetFromContentTypeParameters(charSetSpec); 579 } 580 try { 581 in.reset(); 582 } catch (IOException exception) { 583 //mark was invalidated 584 in.close(); 585 URL url = (URL)doc.getProperty(Document.StreamDescriptionProperty); 586 if (url != null) { 587 URLConnection conn = url.openConnection(); 588 in = conn.getInputStream(); 589 } else { 590 //there is nothing we can do to recover stream 591 throw changedCharSetException; 592 } 593 } 594 try { 595 doc.remove(0, doc.getLength()); 596 } catch (BadLocationException e) {} 597 doc.putProperty("IgnoreCharsetDirective", Boolean.valueOf(true)); 598 read(in, doc); 599 } 600 } 601 602 603 /** 604 * Loads a stream into the text document model. 605 */ 606 class PageLoader extends SwingWorker<URL, Object> { 607 608 /** 609 * Construct an asynchronous page loader. 610 */ 611 PageLoader(Document doc, InputStream in, URL old, URL page) { 612 this.in = in; 613 this.old = old; 614 this.page = page; 615 this.doc = doc; 616 } 617 618 /** 619 * Try to load the document, then scroll the view 620 * to the reference (if specified). When done, fire 621 * a page property change event. 622 */ 623 protected URL doInBackground() { 624 boolean pageLoaded = false; 625 try { 626 if (in == null) { 627 in = getStream(page); 628 if (kit == null) { 629 // We received document of unknown content type. 630 UIManager.getLookAndFeel(). 631 provideErrorFeedback(JEditorPane.this); 632 return old; 633 } 634 } 635 636 if (doc == null) { 637 try { 638 SwingUtilities.invokeAndWait(new Runnable() { 639 public void run() { 640 doc = initializeModel(kit, page); 641 setDocument(doc); 642 } 643 }); 644 } catch (InvocationTargetException ex) { 645 UIManager.getLookAndFeel().provideErrorFeedback( 646 JEditorPane.this); 647 return old; 648 } catch (InterruptedException ex) { 649 UIManager.getLookAndFeel().provideErrorFeedback( 650 JEditorPane.this); 651 return old; 652 } 653 } 654 655 read(in, doc); 656 URL page = (URL) doc.getProperty(Document.StreamDescriptionProperty); 657 String reference = page.getRef(); 658 if (reference != null) { 659 // scroll the page if necessary, but do it on the 660 // event thread... that is the only guarantee that 661 // modelToView can be safely called. 662 Runnable callScrollToReference = new Runnable() { 663 public void run() { 664 URL u = (URL) getDocument().getProperty 665 (Document.StreamDescriptionProperty); 666 String ref = u.getRef(); 667 scrollToReference(ref); 668 } 669 }; 670 SwingUtilities.invokeLater(callScrollToReference); 671 } 672 pageLoaded = true; 673 } catch (IOException ioe) { 674 UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this); 675 } finally { 676 if (pageLoaded) { 677 SwingUtilities.invokeLater(new Runnable() { 678 public void run() { 679 JEditorPane.this.firePropertyChange("page", old, page); 680 } 681 }); 682 } 683 } 684 return (pageLoaded ? page : old); 685 } 686 687 /** 688 * The stream to load the document with 689 */ 690 InputStream in; 691 692 /** 693 * URL of the old page that was replaced (for the property change event) 694 */ 695 URL old; 696 697 /** 698 * URL of the page being loaded (for the property change event) 699 */ 700 URL page; 701 702 /** 703 * The Document instance to load into. This is cached in case a 704 * new Document is created between the time the thread this is created 705 * and run. 706 */ 707 Document doc; 708 } 709 710 /** 711 * Fetches a stream for the given URL, which is about to 712 * be loaded by the <code>setPage</code> method. By 713 * default, this simply opens the URL and returns the 714 * stream. This can be reimplemented to do useful things 715 * like fetch the stream from a cache, monitor the progress 716 * of the stream, etc. 717 * <p> 718 * This method is expected to have the the side effect of 719 * establishing the content type, and therefore setting the 720 * appropriate <code>EditorKit</code> to use for loading the stream. 721 * <p> 722 * If this the stream was an http connection, redirects 723 * will be followed and the resulting URL will be set as 724 * the <code>Document.StreamDescriptionProperty</code> so that relative 725 * URL's can be properly resolved. 726 * 727 * @param page the URL of the page 728 */ 729 protected InputStream getStream(URL page) throws IOException { 730 final URLConnection conn = page.openConnection(); 731 if (conn instanceof HttpURLConnection) { 732 HttpURLConnection hconn = (HttpURLConnection) conn; 733 hconn.setInstanceFollowRedirects(false); 734 Object postData = getPostData(); 735 if (postData != null) { 736 handlePostData(hconn, postData); 737 } 738 int response = hconn.getResponseCode(); 739 boolean redirect = (response >= 300 && response <= 399); 740 741 /* 742 * In the case of a redirect, we want to actually change the URL 743 * that was input to the new, redirected URL 744 */ 745 if (redirect) { 746 String loc = conn.getHeaderField("Location"); 747 if (loc.startsWith("http", 0)) { 748 page = new URL(loc); 749 } else { 750 page = new URL(page, loc); 751 } 752 return getStream(page); 753 } 754 } 755 756 // Connection properties handler should be forced to run on EDT, 757 // as it instantiates the EditorKit. 758 if (SwingUtilities.isEventDispatchThread()) { 759 handleConnectionProperties(conn); 760 } else { 761 try { 762 SwingUtilities.invokeAndWait(new Runnable() { 763 public void run() { 764 handleConnectionProperties(conn); 765 } 766 }); 767 } catch (InterruptedException e) { 768 throw new RuntimeException(e); 769 } catch (InvocationTargetException e) { 770 throw new RuntimeException(e); 771 } 772 } 773 return conn.getInputStream(); 774 } 775 776 /** 777 * Handle URL connection properties (most notably, content type). 778 */ 779 private void handleConnectionProperties(URLConnection conn) { 780 if (pageProperties == null) { 781 pageProperties = new Hashtable<String, Object>(); 782 } 783 String type = conn.getContentType(); 784 if (type != null) { 785 setContentType(type); 786 pageProperties.put("content-type", type); 787 } 788 pageProperties.put(Document.StreamDescriptionProperty, conn.getURL()); 789 String enc = conn.getContentEncoding(); 790 if (enc != null) { 791 pageProperties.put("content-encoding", enc); 792 } 793 } 794 795 private Object getPostData() { 796 return getDocument().getProperty(PostDataProperty); 797 } 798 799 private void handlePostData(HttpURLConnection conn, Object postData) 800 throws IOException { 801 conn.setDoOutput(true); 802 DataOutputStream os = null; 803 try { 804 conn.setRequestProperty("Content-Type", 805 "application/x-www-form-urlencoded"); 806 os = new DataOutputStream(conn.getOutputStream()); 807 os.writeBytes((String) postData); 808 } finally { 809 if (os != null) { 810 os.close(); 811 } 812 } 813 } 814 815 816 /** 817 * Scrolls the view to the given reference location 818 * (that is, the value returned by the <code>UL.getRef</code> 819 * method for the URL being displayed). By default, this 820 * method only knows how to locate a reference in an 821 * HTMLDocument. The implementation calls the 822 * <code>scrollRectToVisible</code> method to 823 * accomplish the actual scrolling. If scrolling to a 824 * reference location is needed for document types other 825 * than HTML, this method should be reimplemented. 826 * This method will have no effect if the component 827 * is not visible. 828 * 829 * @param reference the named location to scroll to 830 */ 831 public void scrollToReference(String reference) { 832 Document d = getDocument(); 833 if (d instanceof HTMLDocument) { 834 HTMLDocument doc = (HTMLDocument) d; 835 HTMLDocument.Iterator iter = doc.getIterator(HTML.Tag.A); 836 for (; iter.isValid(); iter.next()) { 837 AttributeSet a = iter.getAttributes(); 838 String nm = (String) a.getAttribute(HTML.Attribute.NAME); 839 if ((nm != null) && nm.equals(reference)) { 840 // found a matching reference in the document. 841 try { 842 int pos = iter.getStartOffset(); 843 Rectangle r = modelToView(pos); 844 if (r != null) { 845 // the view is visible, scroll it to the 846 // center of the current visible area. 847 Rectangle vis = getVisibleRect(); 848 //r.y -= (vis.height / 2); 849 r.height = vis.height; 850 scrollRectToVisible(r); 851 setCaretPosition(pos); 852 } 853 } catch (BadLocationException ble) { 854 UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this); 855 } 856 } 857 } 858 } 859 } 860 861 /** 862 * Gets the current URL being displayed. If a URL was 863 * not specified in the creation of the document, this 864 * will return <code>null</code>, and relative URL's will not be 865 * resolved. 866 * 867 * @return the URL, or <code>null</code> if none 868 */ 869 public URL getPage() { 870 return (URL) getDocument().getProperty(Document.StreamDescriptionProperty); 871 } 872 873 /** 874 * Sets the current URL being displayed. 875 * 876 * @param url the URL for display 877 * @exception IOException for a <code>null</code> or invalid URL 878 * specification 879 */ 880 public void setPage(String url) throws IOException { 881 if (url == null) { 882 throw new IOException("invalid url"); 883 } 884 URL page = new URL(url); 885 setPage(page); 886 } 887 888 /** 889 * Gets the class ID for the UI. 890 * 891 * @return the string "EditorPaneUI" 892 * @see JComponent#getUIClassID 893 * @see UIDefaults#getUI 894 */ 895 public String getUIClassID() { 896 return uiClassID; 897 } 898 899 /** 900 * Creates the default editor kit (<code>PlainEditorKit</code>) for when 901 * the component is first created. 902 * 903 * @return the editor kit 904 */ 905 protected EditorKit createDefaultEditorKit() { 906 return new PlainEditorKit(); 907 } 908 909 /** 910 * Fetches the currently installed kit for handling content. 911 * <code>createDefaultEditorKit</code> is called to set up a default 912 * if necessary. 913 * 914 * @return the editor kit 915 */ 916 public EditorKit getEditorKit() { 917 if (kit == null) { 918 kit = createDefaultEditorKit(); 919 isUserSetEditorKit = false; 920 } 921 return kit; 922 } 923 924 /** 925 * Gets the type of content that this editor 926 * is currently set to deal with. This is 927 * defined to be the type associated with the 928 * currently installed <code>EditorKit</code>. 929 * 930 * @return the content type, <code>null</code> if no editor kit set 931 */ 932 public final String getContentType() { 933 return (kit != null) ? kit.getContentType() : null; 934 } 935 936 /** 937 * Sets the type of content that this editor 938 * handles. This calls <code>getEditorKitForContentType</code>, 939 * and then <code>setEditorKit</code> if an editor kit can 940 * be successfully located. This is mostly convenience method 941 * that can be used as an alternative to calling 942 * <code>setEditorKit</code> directly. 943 * <p> 944 * If there is a charset definition specified as a parameter 945 * of the content type specification, it will be used when 946 * loading input streams using the associated <code>EditorKit</code>. 947 * For example if the type is specified as 948 * <code>text/html; charset=EUC-JP</code> the content 949 * will be loaded using the <code>EditorKit</code> registered for 950 * <code>text/html</code> and the Reader provided to 951 * the <code>EditorKit</code> to load unicode into the document will 952 * use the <code>EUC-JP</code> charset for translating 953 * to unicode. If the type is not recognized, the content 954 * will be loaded using the <code>EditorKit</code> registered 955 * for plain text, <code>text/plain</code>. 956 * 957 * @param type the non-<code>null</code> mime type for the content editing 958 * support 959 * @see #getContentType 960 * @beaninfo 961 * description: the type of content 962 * @throws NullPointerException if the <code>type</code> parameter 963 * is <code>null</code> 964 */ 965 public final void setContentType(String type) { 966 // The type could have optional info is part of it, 967 // for example some charset info. We need to strip that 968 // of and save it. 969 int parm = type.indexOf(';'); 970 if (parm > -1) { 971 // Save the paramList. 972 String paramList = type.substring(parm); 973 // update the content type string. 974 type = type.substring(0, parm).trim(); 975 if (type.toLowerCase().startsWith("text/")) { 976 setCharsetFromContentTypeParameters(paramList); 977 } 978 } 979 if ((kit == null) || (! type.equals(kit.getContentType())) 980 || !isUserSetEditorKit) { 981 EditorKit k = getEditorKitForContentType(type); 982 if (k != null && k != kit) { 983 setEditorKit(k); 984 isUserSetEditorKit = false; 985 } 986 } 987 988 } 989 990 /** 991 * This method gets the charset information specified as part 992 * of the content type in the http header information. 993 */ 994 private void setCharsetFromContentTypeParameters(String paramlist) { 995 String charset; 996 try { 997 // paramlist is handed to us with a leading ';', strip it. 998 int semi = paramlist.indexOf(';'); 999 if (semi > -1 && semi < paramlist.length()-1) { 1000 paramlist = paramlist.substring(semi + 1); 1001 } 1002 1003 if (paramlist.length() > 0) { 1004 // parse the paramlist into attr-value pairs & get the 1005 // charset pair's value 1006 HeaderParser hdrParser = new HeaderParser(paramlist); 1007 charset = hdrParser.findValue("charset"); 1008 if (charset != null) { 1009 putClientProperty("charset", charset); 1010 } 1011 } 1012 } 1013 catch (IndexOutOfBoundsException e) { 1014 // malformed parameter list, use charset we have 1015 } 1016 catch (NullPointerException e) { 1017 // malformed parameter list, use charset we have 1018 } 1019 catch (Exception e) { 1020 // malformed parameter list, use charset we have; but complain 1021 System.err.println("JEditorPane.getCharsetFromContentTypeParameters failed on: " + paramlist); 1022 e.printStackTrace(); 1023 } 1024 } 1025 1026 1027 /** 1028 * Sets the currently installed kit for handling 1029 * content. This is the bound property that 1030 * establishes the content type of the editor. 1031 * Any old kit is first deinstalled, then if kit is 1032 * non-<code>null</code>, 1033 * the new kit is installed, and a default document created for it. 1034 * A <code>PropertyChange</code> event ("editorKit") is always fired when 1035 * <code>setEditorKit</code> is called. 1036 * <p> 1037 * <em>NOTE: This has the side effect of changing the model, 1038 * because the <code>EditorKit</code> is the source of how a 1039 * particular type 1040 * of content is modeled. This method will cause <code>setDocument</code> 1041 * to be called on behalf of the caller to ensure integrity 1042 * of the internal state.</em> 1043 * 1044 * @param kit the desired editor behavior 1045 * @see #getEditorKit 1046 * @beaninfo 1047 * description: the currently installed kit for handling content 1048 * bound: true 1049 * expert: true 1050 */ 1051 public void setEditorKit(EditorKit kit) { 1052 EditorKit old = this.kit; 1053 isUserSetEditorKit = true; 1054 if (old != null) { 1055 old.deinstall(this); 1056 } 1057 this.kit = kit; 1058 if (this.kit != null) { 1059 this.kit.install(this); 1060 setDocument(this.kit.createDefaultDocument()); 1061 } 1062 firePropertyChange("editorKit", old, kit); 1063 } 1064 1065 /** 1066 * Fetches the editor kit to use for the given type 1067 * of content. This is called when a type is requested 1068 * that doesn't match the currently installed type. 1069 * If the component doesn't have an <code>EditorKit</code> registered 1070 * for the given type, it will try to create an 1071 * <code>EditorKit</code> from the default <code>EditorKit</code> registry. 1072 * If that fails, a <code>PlainEditorKit</code> is used on the 1073 * assumption that all text documents can be represented 1074 * as plain text. 1075 * <p> 1076 * This method can be reimplemented to use some 1077 * other kind of type registry. This can 1078 * be reimplemented to use the Java Activation 1079 * Framework, for example. 1080 * 1081 * @param type the non-<code>null</code> content type 1082 * @return the editor kit 1083 */ 1084 public EditorKit getEditorKitForContentType(String type) { 1085 if (typeHandlers == null) { 1086 typeHandlers = new Hashtable<String, EditorKit>(3); 1087 } 1088 EditorKit k = typeHandlers.get(type); 1089 if (k == null) { 1090 k = createEditorKitForContentType(type); 1091 if (k != null) { 1092 setEditorKitForContentType(type, k); 1093 } 1094 } 1095 if (k == null) { 1096 k = createDefaultEditorKit(); 1097 } 1098 return k; 1099 } 1100 1101 /** 1102 * Directly sets the editor kit to use for the given type. A 1103 * look-and-feel implementation might use this in conjunction 1104 * with <code>createEditorKitForContentType</code> to install handlers for 1105 * content types with a look-and-feel bias. 1106 * 1107 * @param type the non-<code>null</code> content type 1108 * @param k the editor kit to be set 1109 */ 1110 public void setEditorKitForContentType(String type, EditorKit k) { 1111 if (typeHandlers == null) { 1112 typeHandlers = new Hashtable<String, EditorKit>(3); 1113 } 1114 typeHandlers.put(type, k); 1115 } 1116 1117 /** 1118 * Replaces the currently selected content with new content 1119 * represented by the given string. If there is no selection 1120 * this amounts to an insert of the given text. If there 1121 * is no replacement text (i.e. the content string is empty 1122 * or <code>null</code>) this amounts to a removal of the 1123 * current selection. The replacement text will have the 1124 * attributes currently defined for input. If the component is not 1125 * editable, beep and return. 1126 * 1127 * @param content the content to replace the selection with. This 1128 * value can be <code>null</code> 1129 */ 1130 @Override 1131 public void replaceSelection(String content) { 1132 if (! isEditable()) { 1133 UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this); 1134 return; 1135 } 1136 EditorKit kit = getEditorKit(); 1137 if(kit instanceof StyledEditorKit) { 1138 try { 1139 Document doc = getDocument(); 1140 Caret caret = getCaret(); 1141 boolean composedTextSaved = saveComposedText(caret.getDot()); 1142 int p0 = Math.min(caret.getDot(), caret.getMark()); 1143 int p1 = Math.max(caret.getDot(), caret.getMark()); 1144 if (doc instanceof AbstractDocument) { 1145 ((AbstractDocument)doc).replace(p0, p1 - p0, content, 1146 ((StyledEditorKit)kit).getInputAttributes()); 1147 } 1148 else { 1149 if (p0 != p1) { 1150 doc.remove(p0, p1 - p0); 1151 } 1152 if (content != null && content.length() > 0) { 1153 doc.insertString(p0, content, ((StyledEditorKit)kit). 1154 getInputAttributes()); 1155 } 1156 } 1157 if (composedTextSaved) { 1158 restoreComposedText(); 1159 } 1160 } catch (BadLocationException e) { 1161 UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this); 1162 } 1163 } 1164 else { 1165 super.replaceSelection(content); 1166 } 1167 } 1168 1169 /** 1170 * Creates a handler for the given type from the default registry 1171 * of editor kits. The registry is created if necessary. If the 1172 * registered class has not yet been loaded, an attempt 1173 * is made to dynamically load the prototype of the kit for the 1174 * given type. If the type was registered with a <code>ClassLoader</code>, 1175 * that <code>ClassLoader</code> will be used to load the prototype. 1176 * If there was no registered <code>ClassLoader</code>, 1177 * <code>Class.forName</code> will be used to load the prototype. 1178 * <p> 1179 * Once a prototype <code>EditorKit</code> instance is successfully 1180 * located, it is cloned and the clone is returned. 1181 * 1182 * @param type the content type 1183 * @return the editor kit, or <code>null</code> if there is nothing 1184 * registered for the given type 1185 */ 1186 public static EditorKit createEditorKitForContentType(String type) { 1187 Hashtable<String, EditorKit> kitRegistry = getKitRegisty(); 1188 EditorKit k = kitRegistry.get(type); 1189 if (k == null) { 1190 // try to dynamically load the support 1191 String classname = getKitTypeRegistry().get(type); 1192 ClassLoader loader = getKitLoaderRegistry().get(type); 1193 try { 1194 Class c; 1195 if (loader != null) { 1196 c = loader.loadClass(classname); 1197 } else { 1198 // Will only happen if developer has invoked 1199 // registerEditorKitForContentType(type, class, null). 1200 c = Class.forName(classname, true, Thread.currentThread(). 1201 getContextClassLoader()); 1202 } 1203 k = (EditorKit) c.newInstance(); 1204 kitRegistry.put(type, k); 1205 } catch (Throwable e) { 1206 k = null; 1207 } 1208 } 1209 1210 // create a copy of the prototype or null if there 1211 // is no prototype. 1212 if (k != null) { 1213 return (EditorKit) k.clone(); 1214 } 1215 return null; 1216 } 1217 1218 /** 1219 * Establishes the default bindings of <code>type</code> to 1220 * <code>classname</code>. 1221 * The class will be dynamically loaded later when actually 1222 * needed, and can be safely changed before attempted uses 1223 * to avoid loading unwanted classes. The prototype 1224 * <code>EditorKit</code> will be loaded with <code>Class.forName</code> 1225 * when registered with this method. 1226 * 1227 * @param type the non-<code>null</code> content type 1228 * @param classname the class to load later 1229 */ 1230 public static void registerEditorKitForContentType(String type, String classname) { 1231 registerEditorKitForContentType(type, classname,Thread.currentThread(). 1232 getContextClassLoader()); 1233 } 1234 1235 /** 1236 * Establishes the default bindings of <code>type</code> to 1237 * <code>classname</code>. 1238 * The class will be dynamically loaded later when actually 1239 * needed using the given <code>ClassLoader</code>, 1240 * and can be safely changed 1241 * before attempted uses to avoid loading unwanted classes. 1242 * 1243 * @param type the non-<code>null</code> content type 1244 * @param classname the class to load later 1245 * @param loader the <code>ClassLoader</code> to use to load the name 1246 */ 1247 public static void registerEditorKitForContentType(String type, String classname, ClassLoader loader) { 1248 getKitTypeRegistry().put(type, classname); 1249 getKitLoaderRegistry().put(type, loader); 1250 getKitRegisty().remove(type); 1251 } 1252 1253 /** 1254 * Returns the currently registered <code>EditorKit</code> 1255 * class name for the type <code>type</code>. 1256 * 1257 * @param type the non-<code>null</code> content type 1258 * 1259 * @since 1.3 1260 */ 1261 public static String getEditorKitClassNameForContentType(String type) { 1262 return getKitTypeRegistry().get(type); 1263 } 1264 1265 private static Hashtable<String, String> getKitTypeRegistry() { 1266 loadDefaultKitsIfNecessary(); 1267 return (Hashtable)SwingUtilities.appContextGet(kitTypeRegistryKey); 1268 } 1269 1270 private static Hashtable<String, ClassLoader> getKitLoaderRegistry() { 1271 loadDefaultKitsIfNecessary(); 1272 return (Hashtable)SwingUtilities.appContextGet(kitLoaderRegistryKey); 1273 } 1274 1275 private static Hashtable<String, EditorKit> getKitRegisty() { 1276 Hashtable ht = (Hashtable)SwingUtilities.appContextGet(kitRegistryKey); 1277 if (ht == null) { 1278 ht = new Hashtable(3); 1279 SwingUtilities.appContextPut(kitRegistryKey, ht); 1280 } 1281 return ht; 1282 } 1283 1284 /** 1285 * This is invoked every time the registries are accessed. Loading 1286 * is done this way instead of via a static as the static is only 1287 * called once when running in plugin resulting in the entries only 1288 * appearing in the first applet. 1289 */ 1290 private static void loadDefaultKitsIfNecessary() { 1291 if (SwingUtilities.appContextGet(kitTypeRegistryKey) == null) { 1292 synchronized(defaultEditorKitMap) { 1293 if (defaultEditorKitMap.size() == 0) { 1294 defaultEditorKitMap.put("text/plain", 1295 "javax.swing.JEditorPane$PlainEditorKit"); 1296 defaultEditorKitMap.put("text/html", 1297 "javax.swing.text.html.HTMLEditorKit"); 1298 defaultEditorKitMap.put("text/rtf", 1299 "javax.swing.text.rtf.RTFEditorKit"); 1300 defaultEditorKitMap.put("application/rtf", 1301 "javax.swing.text.rtf.RTFEditorKit"); 1302 } 1303 } 1304 Hashtable ht = new Hashtable(); 1305 SwingUtilities.appContextPut(kitTypeRegistryKey, ht); 1306 ht = new Hashtable(); 1307 SwingUtilities.appContextPut(kitLoaderRegistryKey, ht); 1308 for (String key : defaultEditorKitMap.keySet()) { 1309 registerEditorKitForContentType(key,defaultEditorKitMap.get(key)); 1310 } 1311 1312 } 1313 } 1314 1315 // --- java.awt.Component methods -------------------------- 1316 1317 /** 1318 * Returns the preferred size for the <code>JEditorPane</code>. 1319 * The preferred size for <code>JEditorPane</code> is slightly altered 1320 * from the preferred size of the superclass. If the size 1321 * of the viewport has become smaller than the minimum size 1322 * of the component, the scrollable definition for tracking 1323 * width or height will turn to false. The default viewport 1324 * layout will give the preferred size, and that is not desired 1325 * in the case where the scrollable is tracking. In that case 1326 * the <em>normal</em> preferred size is adjusted to the 1327 * minimum size. This allows things like HTML tables to 1328 * shrink down to their minimum size and then be laid out at 1329 * their minimum size, refusing to shrink any further. 1330 * 1331 * @return a <code>Dimension</code> containing the preferred size 1332 */ 1333 public Dimension getPreferredSize() { 1334 Dimension d = super.getPreferredSize(); 1335 Container parent = SwingUtilities.getUnwrappedParent(this); 1336 if (parent instanceof JViewport) { 1337 JViewport port = (JViewport) parent; 1338 TextUI ui = getUI(); 1339 int prefWidth = d.width; 1340 int prefHeight = d.height; 1341 if (! getScrollableTracksViewportWidth()) { 1342 int w = port.getWidth(); 1343 Dimension min = ui.getMinimumSize(this); 1344 if (w != 0 && w < min.width) { 1345 // Only adjust to min if we have a valid size 1346 prefWidth = min.width; 1347 } 1348 } 1349 if (! getScrollableTracksViewportHeight()) { 1350 int h = port.getHeight(); 1351 Dimension min = ui.getMinimumSize(this); 1352 if (h != 0 && h < min.height) { 1353 // Only adjust to min if we have a valid size 1354 prefHeight = min.height; 1355 } 1356 } 1357 if (prefWidth != d.width || prefHeight != d.height) { 1358 d = new Dimension(prefWidth, prefHeight); 1359 } 1360 } 1361 return d; 1362 } 1363 1364 // --- JTextComponent methods ----------------------------- 1365 1366 /** 1367 * Sets the text of this <code>TextComponent</code> to the specified 1368 * content, 1369 * which is expected to be in the format of the content type of 1370 * this editor. For example, if the type is set to <code>text/html</code> 1371 * the string should be specified in terms of HTML. 1372 * <p> 1373 * This is implemented to remove the contents of the current document, 1374 * and replace them by parsing the given string using the current 1375 * <code>EditorKit</code>. This gives the semantics of the 1376 * superclass by not changing 1377 * out the model, while supporting the content type currently set on 1378 * this component. The assumption is that the previous content is 1379 * relatively 1380 * small, and that the previous content doesn't have side effects. 1381 * Both of those assumptions can be violated and cause undesirable results. 1382 * To avoid this, create a new document, 1383 * <code>getEditorKit().createDefaultDocument()</code>, and replace the 1384 * existing <code>Document</code> with the new one. You are then assured the 1385 * previous <code>Document</code> won't have any lingering state. 1386 * <ol> 1387 * <li> 1388 * Leaving the existing model in place means that the old view will be 1389 * torn down, and a new view created, where replacing the document would 1390 * avoid the tear down of the old view. 1391 * <li> 1392 * Some formats (such as HTML) can install things into the document that 1393 * can influence future contents. HTML can have style information embedded 1394 * that would influence the next content installed unexpectedly. 1395 * </ol> 1396 * <p> 1397 * An alternative way to load this component with a string would be to 1398 * create a StringReader and call the read method. In this case the model 1399 * would be replaced after it was initialized with the contents of the 1400 * string. 1401 * 1402 * @param t the new text to be set; if <code>null</code> the old 1403 * text will be deleted 1404 * @see #getText 1405 * @beaninfo 1406 * description: the text of this component 1407 */ 1408 public void setText(String t) { 1409 try { 1410 Document doc = getDocument(); 1411 doc.remove(0, doc.getLength()); 1412 if (t == null || t.equals("")) { 1413 return; 1414 } 1415 Reader r = new StringReader(t); 1416 EditorKit kit = getEditorKit(); 1417 kit.read(r, doc, 0); 1418 } catch (IOException ioe) { 1419 UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this); 1420 } catch (BadLocationException ble) { 1421 UIManager.getLookAndFeel().provideErrorFeedback(JEditorPane.this); 1422 } 1423 } 1424 1425 /** 1426 * Returns the text contained in this <code>TextComponent</code> 1427 * in terms of the 1428 * content type of this editor. If an exception is thrown while 1429 * attempting to retrieve the text, <code>null</code> will be returned. 1430 * This is implemented to call <code>JTextComponent.write</code> with 1431 * a <code>StringWriter</code>. 1432 * 1433 * @return the text 1434 * @see #setText 1435 */ 1436 public String getText() { 1437 String txt; 1438 try { 1439 StringWriter buf = new StringWriter(); 1440 write(buf); 1441 txt = buf.toString(); 1442 } catch (IOException ioe) { 1443 txt = null; 1444 } 1445 return txt; 1446 } 1447 1448 // --- Scrollable ---------------------------------------- 1449 1450 /** 1451 * Returns true if a viewport should always force the width of this 1452 * <code>Scrollable</code> to match the width of the viewport. 1453 * 1454 * @return true if a viewport should force the Scrollables width to 1455 * match its own, false otherwise 1456 */ 1457 public boolean getScrollableTracksViewportWidth() { 1458 Container parent = SwingUtilities.getUnwrappedParent(this); 1459 if (parent instanceof JViewport) { 1460 JViewport port = (JViewport) parent; 1461 TextUI ui = getUI(); 1462 int w = port.getWidth(); 1463 Dimension min = ui.getMinimumSize(this); 1464 Dimension max = ui.getMaximumSize(this); 1465 if ((w >= min.width) && (w <= max.width)) { 1466 return true; 1467 } 1468 } 1469 return false; 1470 } 1471 1472 /** 1473 * Returns true if a viewport should always force the height of this 1474 * <code>Scrollable</code> to match the height of the viewport. 1475 * 1476 * @return true if a viewport should force the 1477 * <code>Scrollable</code>'s height to match its own, 1478 * false otherwise 1479 */ 1480 public boolean getScrollableTracksViewportHeight() { 1481 Container parent = SwingUtilities.getUnwrappedParent(this); 1482 if (parent instanceof JViewport) { 1483 JViewport port = (JViewport) parent; 1484 TextUI ui = getUI(); 1485 int h = port.getHeight(); 1486 Dimension min = ui.getMinimumSize(this); 1487 if (h >= min.height) { 1488 Dimension max = ui.getMaximumSize(this); 1489 if (h <= max.height) { 1490 return true; 1491 } 1492 } 1493 } 1494 return false; 1495 } 1496 1497 // --- Serialization ------------------------------------ 1498 1499 /** 1500 * See <code>readObject</code> and <code>writeObject</code> in 1501 * <code>JComponent</code> for more 1502 * information about serialization in Swing. 1503 */ 1504 private void writeObject(ObjectOutputStream s) throws IOException { 1505 s.defaultWriteObject(); 1506 if (getUIClassID().equals(uiClassID)) { 1507 byte count = JComponent.getWriteObjCounter(this); 1508 JComponent.setWriteObjCounter(this, --count); 1509 if (count == 0 && ui != null) { 1510 ui.installUI(this); 1511 } 1512 } 1513 } 1514 1515 // --- variables --------------------------------------- 1516 1517 private SwingWorker<URL, Object> pageLoader; 1518 1519 /** 1520 * Current content binding of the editor. 1521 */ 1522 private EditorKit kit; 1523 private boolean isUserSetEditorKit; 1524 1525 private Hashtable<String, Object> pageProperties; 1526 1527 /** Should be kept in sync with javax.swing.text.html.FormView counterpart. */ 1528 final static String PostDataProperty = "javax.swing.JEditorPane.postdata"; 1529 1530 /** 1531 * Table of registered type handlers for this editor. 1532 */ 1533 private Hashtable<String, EditorKit> typeHandlers; 1534 1535 /* 1536 * Private AppContext keys for this class's static variables. 1537 */ 1538 private static final Object kitRegistryKey = 1539 new StringBuffer("JEditorPane.kitRegistry"); 1540 private static final Object kitTypeRegistryKey = 1541 new StringBuffer("JEditorPane.kitTypeRegistry"); 1542 private static final Object kitLoaderRegistryKey = 1543 new StringBuffer("JEditorPane.kitLoaderRegistry"); 1544 1545 /** 1546 * @see #getUIClassID 1547 * @see #readObject 1548 */ 1549 private static final String uiClassID = "EditorPaneUI"; 1550 1551 1552 /** 1553 * Key for a client property used to indicate whether 1554 * <a href="http://www.w3.org/TR/CSS21/syndata.html#length-units"> 1555 * w3c compliant</a> length units are used for html rendering. 1556 * <p> 1557 * By default this is not enabled; to enable 1558 * it set the client {@link #putClientProperty property} with this name 1559 * to <code>Boolean.TRUE</code>. 1560 * 1561 * @since 1.5 1562 */ 1563 public static final String W3C_LENGTH_UNITS = "JEditorPane.w3cLengthUnits"; 1564 1565 /** 1566 * Key for a client property used to indicate whether 1567 * the default font and foreground color from the component are 1568 * used if a font or foreground color is not specified in the styled 1569 * text. 1570 * <p> 1571 * The default varies based on the look and feel; 1572 * to enable it set the client {@link #putClientProperty property} with 1573 * this name to <code>Boolean.TRUE</code>. 1574 * 1575 * @since 1.5 1576 */ 1577 public static final String HONOR_DISPLAY_PROPERTIES = "JEditorPane.honorDisplayProperties"; 1578 1579 static final Map<String, String> defaultEditorKitMap = new HashMap<String, String>(0); 1580 1581 /** 1582 * Returns a string representation of this <code>JEditorPane</code>. 1583 * This method 1584 * is intended to be used only for debugging purposes, and the 1585 * content and format of the returned string may vary between 1586 * implementations. The returned string may be empty but may not 1587 * be <code>null</code>. 1588 * 1589 * @return a string representation of this <code>JEditorPane</code> 1590 */ 1591 protected String paramString() { 1592 String kitString = (kit != null ? 1593 kit.toString() : ""); 1594 String typeHandlersString = (typeHandlers != null ? 1595 typeHandlers.toString() : ""); 1596 1597 return super.paramString() + 1598 ",kit=" + kitString + 1599 ",typeHandlers=" + typeHandlersString; 1600 } 1601 1602 1603 ///////////////// 1604 // Accessibility support 1605 //////////////// 1606 1607 1608 /** 1609 * Gets the AccessibleContext associated with this JEditorPane. 1610 * For editor panes, the AccessibleContext takes the form of an 1611 * AccessibleJEditorPane. 1612 * A new AccessibleJEditorPane instance is created if necessary. 1613 * 1614 * @return an AccessibleJEditorPane that serves as the 1615 * AccessibleContext of this JEditorPane 1616 */ 1617 public AccessibleContext getAccessibleContext() { 1618 if (getEditorKit() instanceof HTMLEditorKit) { 1619 if (accessibleContext == null || accessibleContext.getClass() != 1620 AccessibleJEditorPaneHTML.class) { 1621 accessibleContext = new AccessibleJEditorPaneHTML(); 1622 } 1623 } else if (accessibleContext == null || accessibleContext.getClass() != 1624 AccessibleJEditorPane.class) { 1625 accessibleContext = new AccessibleJEditorPane(); 1626 } 1627 return accessibleContext; 1628 } 1629 1630 /** 1631 * This class implements accessibility support for the 1632 * <code>JEditorPane</code> class. It provides an implementation of the 1633 * Java Accessibility API appropriate to editor pane user-interface 1634 * elements. 1635 * <p> 1636 * <strong>Warning:</strong> 1637 * Serialized objects of this class will not be compatible with 1638 * future Swing releases. The current serialization support is 1639 * appropriate for short term storage or RMI between applications running 1640 * the same version of Swing. As of 1.4, support for long term storage 1641 * of all JavaBeans™ 1642 * has been added to the <code>java.beans</code> package. 1643 * Please see {@link java.beans.XMLEncoder}. 1644 */ 1645 @SuppressWarnings("serial") // Same-version serialization only 1646 protected class AccessibleJEditorPane extends AccessibleJTextComponent { 1647 1648 /** 1649 * Gets the accessibleDescription property of this object. If this 1650 * property isn't set, returns the content type of this 1651 * <code>JEditorPane</code> instead (e.g. "plain/text", "html/text"). 1652 * 1653 * @return the localized description of the object; <code>null</code> 1654 * if this object does not have a description 1655 * 1656 * @see #setAccessibleName 1657 */ 1658 public String getAccessibleDescription() { 1659 String description = accessibleDescription; 1660 1661 // fallback to client property 1662 if (description == null) { 1663 description = (String)getClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY); 1664 } 1665 if (description == null) { 1666 description = JEditorPane.this.getContentType(); 1667 } 1668 return description; 1669 } 1670 1671 /** 1672 * Gets the state set of this object. 1673 * 1674 * @return an instance of AccessibleStateSet describing the states 1675 * of the object 1676 * @see AccessibleStateSet 1677 */ 1678 public AccessibleStateSet getAccessibleStateSet() { 1679 AccessibleStateSet states = super.getAccessibleStateSet(); 1680 states.add(AccessibleState.MULTI_LINE); 1681 return states; 1682 } 1683 } 1684 1685 /** 1686 * This class provides support for <code>AccessibleHypertext</code>, 1687 * and is used in instances where the <code>EditorKit</code> 1688 * installed in this <code>JEditorPane</code> is an instance of 1689 * <code>HTMLEditorKit</code>. 1690 * <p> 1691 * <strong>Warning:</strong> 1692 * Serialized objects of this class will not be compatible with 1693 * future Swing releases. The current serialization support is 1694 * appropriate for short term storage or RMI between applications running 1695 * the same version of Swing. As of 1.4, support for long term storage 1696 * of all JavaBeans™ 1697 * has been added to the <code>java.beans</code> package. 1698 * Please see {@link java.beans.XMLEncoder}. 1699 */ 1700 @SuppressWarnings("serial") // Same-version serialization only 1701 protected class AccessibleJEditorPaneHTML extends AccessibleJEditorPane { 1702 1703 private AccessibleContext accessibleContext; 1704 1705 public AccessibleText getAccessibleText() { 1706 return new JEditorPaneAccessibleHypertextSupport(); 1707 } 1708 1709 protected AccessibleJEditorPaneHTML () { 1710 HTMLEditorKit kit = (HTMLEditorKit)JEditorPane.this.getEditorKit(); 1711 accessibleContext = kit.getAccessibleContext(); 1712 } 1713 1714 /** 1715 * Returns the number of accessible children of the object. 1716 * 1717 * @return the number of accessible children of the object. 1718 */ 1719 public int getAccessibleChildrenCount() { 1720 if (accessibleContext != null) { 1721 return accessibleContext.getAccessibleChildrenCount(); 1722 } else { 1723 return 0; 1724 } 1725 } 1726 1727 /** 1728 * Returns the specified Accessible child of the object. The Accessible 1729 * children of an Accessible object are zero-based, so the first child 1730 * of an Accessible child is at index 0, the second child is at index 1, 1731 * and so on. 1732 * 1733 * @param i zero-based index of child 1734 * @return the Accessible child of the object 1735 * @see #getAccessibleChildrenCount 1736 */ 1737 public Accessible getAccessibleChild(int i) { 1738 if (accessibleContext != null) { 1739 return accessibleContext.getAccessibleChild(i); 1740 } else { 1741 return null; 1742 } 1743 } 1744 1745 /** 1746 * Returns the Accessible child, if one exists, contained at the local 1747 * coordinate Point. 1748 * 1749 * @param p The point relative to the coordinate system of this object. 1750 * @return the Accessible, if it exists, at the specified location; 1751 * otherwise null 1752 */ 1753 public Accessible getAccessibleAt(Point p) { 1754 if (accessibleContext != null && p != null) { 1755 try { 1756 AccessibleComponent acomp = 1757 accessibleContext.getAccessibleComponent(); 1758 if (acomp != null) { 1759 return acomp.getAccessibleAt(p); 1760 } else { 1761 return null; 1762 } 1763 } catch (IllegalComponentStateException e) { 1764 return null; 1765 } 1766 } else { 1767 return null; 1768 } 1769 } 1770 } 1771 1772 /** 1773 * What's returned by 1774 * <code>AccessibleJEditorPaneHTML.getAccessibleText</code>. 1775 * 1776 * Provides support for <code>AccessibleHypertext</code> in case 1777 * there is an HTML document being displayed in this 1778 * <code>JEditorPane</code>. 1779 * 1780 */ 1781 protected class JEditorPaneAccessibleHypertextSupport 1782 extends AccessibleJEditorPane implements AccessibleHypertext { 1783 1784 public class HTMLLink extends AccessibleHyperlink { 1785 Element element; 1786 1787 public HTMLLink(Element e) { 1788 element = e; 1789 } 1790 1791 /** 1792 * Since the document a link is associated with may have 1793 * changed, this method returns whether this Link is valid 1794 * anymore (with respect to the document it references). 1795 * 1796 * @return a flag indicating whether this link is still valid with 1797 * respect to the AccessibleHypertext it belongs to 1798 */ 1799 public boolean isValid() { 1800 return JEditorPaneAccessibleHypertextSupport.this.linksValid; 1801 } 1802 1803 /** 1804 * Returns the number of accessible actions available in this Link 1805 * If there are more than one, the first one is NOT considered the 1806 * "default" action of this LINK object (e.g. in an HTML imagemap). 1807 * In general, links will have only one AccessibleAction in them. 1808 * 1809 * @return the zero-based number of Actions in this object 1810 */ 1811 public int getAccessibleActionCount() { 1812 return 1; 1813 } 1814 1815 /** 1816 * Perform the specified Action on the object 1817 * 1818 * @param i zero-based index of actions 1819 * @return true if the the action was performed; else false. 1820 * @see #getAccessibleActionCount 1821 */ 1822 public boolean doAccessibleAction(int i) { 1823 if (i == 0 && isValid() == true) { 1824 URL u = (URL) getAccessibleActionObject(i); 1825 if (u != null) { 1826 HyperlinkEvent linkEvent = 1827 new HyperlinkEvent(JEditorPane.this, HyperlinkEvent.EventType.ACTIVATED, u); 1828 JEditorPane.this.fireHyperlinkUpdate(linkEvent); 1829 return true; 1830 } 1831 } 1832 return false; // link invalid or i != 0 1833 } 1834 1835 /** 1836 * Return a String description of this particular 1837 * link action. The string returned is the text 1838 * within the document associated with the element 1839 * which contains this link. 1840 * 1841 * @param i zero-based index of the actions 1842 * @return a String description of the action 1843 * @see #getAccessibleActionCount 1844 */ 1845 public String getAccessibleActionDescription(int i) { 1846 if (i == 0 && isValid() == true) { 1847 Document d = JEditorPane.this.getDocument(); 1848 if (d != null) { 1849 try { 1850 return d.getText(getStartIndex(), 1851 getEndIndex() - getStartIndex()); 1852 } catch (BadLocationException exception) { 1853 return null; 1854 } 1855 } 1856 } 1857 return null; 1858 } 1859 1860 /** 1861 * Returns a URL object that represents the link. 1862 * 1863 * @param i zero-based index of the actions 1864 * @return an URL representing the HTML link itself 1865 * @see #getAccessibleActionCount 1866 */ 1867 public Object getAccessibleActionObject(int i) { 1868 if (i == 0 && isValid() == true) { 1869 AttributeSet as = element.getAttributes(); 1870 AttributeSet anchor = 1871 (AttributeSet) as.getAttribute(HTML.Tag.A); 1872 String href = (anchor != null) ? 1873 (String) anchor.getAttribute(HTML.Attribute.HREF) : null; 1874 if (href != null) { 1875 URL u; 1876 try { 1877 u = new URL(JEditorPane.this.getPage(), href); 1878 } catch (MalformedURLException m) { 1879 u = null; 1880 } 1881 return u; 1882 } 1883 } 1884 return null; // link invalid or i != 0 1885 } 1886 1887 /** 1888 * Return an object that represents the link anchor, 1889 * as appropriate for that link. E.g. from HTML: 1890 * <a href="http://www.sun.com/access">Accessibility</a> 1891 * this method would return a String containing the text: 1892 * 'Accessibility'. 1893 * 1894 * Similarly, from this HTML: 1895 * <a HREF="#top"><img src="top-hat.gif" alt="top hat"></a> 1896 * this might return the object ImageIcon("top-hat.gif", "top hat"); 1897 * 1898 * @param i zero-based index of the actions 1899 * @return an Object representing the hypertext anchor 1900 * @see #getAccessibleActionCount 1901 */ 1902 public Object getAccessibleActionAnchor(int i) { 1903 return getAccessibleActionDescription(i); 1904 } 1905 1906 1907 /** 1908 * Get the index with the hypertext document at which this 1909 * link begins 1910 * 1911 * @return index of start of link 1912 */ 1913 public int getStartIndex() { 1914 return element.getStartOffset(); 1915 } 1916 1917 /** 1918 * Get the index with the hypertext document at which this 1919 * link ends 1920 * 1921 * @return index of end of link 1922 */ 1923 public int getEndIndex() { 1924 return element.getEndOffset(); 1925 } 1926 } 1927 1928 private class LinkVector extends Vector<HTMLLink> { 1929 public int baseElementIndex(Element e) { 1930 HTMLLink l; 1931 for (int i = 0; i < elementCount; i++) { 1932 l = elementAt(i); 1933 if (l.element == e) { 1934 return i; 1935 } 1936 } 1937 return -1; 1938 } 1939 } 1940 1941 LinkVector hyperlinks; 1942 boolean linksValid = false; 1943 1944 /** 1945 * Build the private table mapping links to locations in the text 1946 */ 1947 private void buildLinkTable() { 1948 hyperlinks.removeAllElements(); 1949 Document d = JEditorPane.this.getDocument(); 1950 if (d != null) { 1951 ElementIterator ei = new ElementIterator(d); 1952 Element e; 1953 AttributeSet as; 1954 AttributeSet anchor; 1955 String href; 1956 while ((e = ei.next()) != null) { 1957 if (e.isLeaf()) { 1958 as = e.getAttributes(); 1959 anchor = (AttributeSet) as.getAttribute(HTML.Tag.A); 1960 href = (anchor != null) ? 1961 (String) anchor.getAttribute(HTML.Attribute.HREF) : null; 1962 if (href != null) { 1963 hyperlinks.addElement(new HTMLLink(e)); 1964 } 1965 } 1966 } 1967 } 1968 linksValid = true; 1969 } 1970 1971 /** 1972 * Make one of these puppies 1973 */ 1974 public JEditorPaneAccessibleHypertextSupport() { 1975 hyperlinks = new LinkVector(); 1976 Document d = JEditorPane.this.getDocument(); 1977 if (d != null) { 1978 d.addDocumentListener(new DocumentListener() { 1979 public void changedUpdate(DocumentEvent theEvent) { 1980 linksValid = false; 1981 } 1982 public void insertUpdate(DocumentEvent theEvent) { 1983 linksValid = false; 1984 } 1985 public void removeUpdate(DocumentEvent theEvent) { 1986 linksValid = false; 1987 } 1988 }); 1989 } 1990 } 1991 1992 /** 1993 * Returns the number of links within this hypertext doc. 1994 * 1995 * @return number of links in this hypertext doc. 1996 */ 1997 public int getLinkCount() { 1998 if (linksValid == false) { 1999 buildLinkTable(); 2000 } 2001 return hyperlinks.size(); 2002 } 2003 2004 /** 2005 * Returns the index into an array of hyperlinks that 2006 * is associated with this character index, or -1 if there 2007 * is no hyperlink associated with this index. 2008 * 2009 * @param charIndex index within the text 2010 * @return index into the set of hyperlinks for this hypertext doc. 2011 */ 2012 public int getLinkIndex(int charIndex) { 2013 if (linksValid == false) { 2014 buildLinkTable(); 2015 } 2016 Element e = null; 2017 Document doc = JEditorPane.this.getDocument(); 2018 if (doc != null) { 2019 for (e = doc.getDefaultRootElement(); ! e.isLeaf(); ) { 2020 int index = e.getElementIndex(charIndex); 2021 e = e.getElement(index); 2022 } 2023 } 2024 2025 // don't need to verify that it's an HREF element; if 2026 // not, then it won't be in the hyperlinks Vector, and 2027 // so indexOf will return -1 in any case 2028 return hyperlinks.baseElementIndex(e); 2029 } 2030 2031 /** 2032 * Returns the index into an array of hyperlinks that 2033 * index. If there is no hyperlink at this index, it returns 2034 * null. 2035 * 2036 * @param linkIndex into the set of hyperlinks for this hypertext doc. 2037 * @return string representation of the hyperlink 2038 */ 2039 public AccessibleHyperlink getLink(int linkIndex) { 2040 if (linksValid == false) { 2041 buildLinkTable(); 2042 } 2043 if (linkIndex >= 0 && linkIndex < hyperlinks.size()) { 2044 return hyperlinks.elementAt(linkIndex); 2045 } else { 2046 return null; 2047 } 2048 } 2049 2050 /** 2051 * Returns the contiguous text within the document that 2052 * is associated with this hyperlink. 2053 * 2054 * @param linkIndex into the set of hyperlinks for this hypertext doc. 2055 * @return the contiguous text sharing the link at this index 2056 */ 2057 public String getLinkText(int linkIndex) { 2058 if (linksValid == false) { 2059 buildLinkTable(); 2060 } 2061 Element e = (Element) hyperlinks.elementAt(linkIndex); 2062 if (e != null) { 2063 Document d = JEditorPane.this.getDocument(); 2064 if (d != null) { 2065 try { 2066 return d.getText(e.getStartOffset(), 2067 e.getEndOffset() - e.getStartOffset()); 2068 } catch (BadLocationException exception) { 2069 return null; 2070 } 2071 } 2072 } 2073 return null; 2074 } 2075 } 2076 2077 static class PlainEditorKit extends DefaultEditorKit implements ViewFactory { 2078 2079 /** 2080 * Fetches a factory that is suitable for producing 2081 * views of any models that are produced by this 2082 * kit. The default is to have the UI produce the 2083 * factory, so this method has no implementation. 2084 * 2085 * @return the view factory 2086 */ 2087 public ViewFactory getViewFactory() { 2088 return this; 2089 } 2090 2091 /** 2092 * Creates a view from the given structural element of a 2093 * document. 2094 * 2095 * @param elem the piece of the document to build a view of 2096 * @return the view 2097 * @see View 2098 */ 2099 public View create(Element elem) { 2100 Document doc = elem.getDocument(); 2101 Object i18nFlag 2102 = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/); 2103 if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) { 2104 // build a view that support bidi 2105 return createI18N(elem); 2106 } else { 2107 return new WrappedPlainView(elem); 2108 } 2109 } 2110 2111 View createI18N(Element elem) { 2112 String kind = elem.getName(); 2113 if (kind != null) { 2114 if (kind.equals(AbstractDocument.ContentElementName)) { 2115 return new PlainParagraph(elem); 2116 } else if (kind.equals(AbstractDocument.ParagraphElementName)){ 2117 return new BoxView(elem, View.Y_AXIS); 2118 } 2119 } 2120 return null; 2121 } 2122 2123 /** 2124 * Paragraph for representing plain-text lines that support 2125 * bidirectional text. 2126 */ 2127 static class PlainParagraph extends javax.swing.text.ParagraphView { 2128 2129 PlainParagraph(Element elem) { 2130 super(elem); 2131 layoutPool = new LogicalView(elem); 2132 layoutPool.setParent(this); 2133 } 2134 2135 protected void setPropertiesFromAttributes() { 2136 Component c = getContainer(); 2137 if ((c != null) 2138 && (! c.getComponentOrientation().isLeftToRight())) 2139 { 2140 setJustification(StyleConstants.ALIGN_RIGHT); 2141 } else { 2142 setJustification(StyleConstants.ALIGN_LEFT); 2143 } 2144 } 2145 2146 /** 2147 * Fetch the constraining span to flow against for 2148 * the given child index. 2149 */ 2150 public int getFlowSpan(int index) { 2151 Component c = getContainer(); 2152 if (c instanceof JTextArea) { 2153 JTextArea area = (JTextArea) c; 2154 if (! area.getLineWrap()) { 2155 // no limit if unwrapped 2156 return Integer.MAX_VALUE; 2157 } 2158 } 2159 return super.getFlowSpan(index); 2160 } 2161 2162 protected SizeRequirements calculateMinorAxisRequirements(int axis, 2163 SizeRequirements r) 2164 { 2165 SizeRequirements req 2166 = super.calculateMinorAxisRequirements(axis, r); 2167 Component c = getContainer(); 2168 if (c instanceof JTextArea) { 2169 JTextArea area = (JTextArea) c; 2170 if (! area.getLineWrap()) { 2171 // min is pref if unwrapped 2172 req.minimum = req.preferred; 2173 } 2174 } 2175 return req; 2176 } 2177 2178 /** 2179 * This class can be used to represent a logical view for 2180 * a flow. It keeps the children updated to reflect the state 2181 * of the model, gives the logical child views access to the 2182 * view hierarchy, and calculates a preferred span. It doesn't 2183 * do any rendering, layout, or model/view translation. 2184 */ 2185 static class LogicalView extends CompositeView { 2186 2187 LogicalView(Element elem) { 2188 super(elem); 2189 } 2190 2191 protected int getViewIndexAtPosition(int pos) { 2192 Element elem = getElement(); 2193 if (elem.getElementCount() > 0) { 2194 return elem.getElementIndex(pos); 2195 } 2196 return 0; 2197 } 2198 2199 protected boolean 2200 updateChildren(DocumentEvent.ElementChange ec, 2201 DocumentEvent e, ViewFactory f) 2202 { 2203 return false; 2204 } 2205 2206 protected void loadChildren(ViewFactory f) { 2207 Element elem = getElement(); 2208 if (elem.getElementCount() > 0) { 2209 super.loadChildren(f); 2210 } else { 2211 View v = new GlyphView(elem); 2212 append(v); 2213 } 2214 } 2215 2216 public float getPreferredSpan(int axis) { 2217 if( getViewCount() != 1 ) 2218 throw new Error("One child view is assumed."); 2219 2220 View v = getView(0); 2221 //((GlyphView)v).setGlyphPainter(null); 2222 return v.getPreferredSpan(axis); 2223 } 2224 2225 /** 2226 * Forward the DocumentEvent to the given child view. This 2227 * is implemented to reparent the child to the logical view 2228 * (the children may have been parented by a row in the flow 2229 * if they fit without breaking) and then execute the 2230 * superclass behavior. 2231 * 2232 * @param v the child view to forward the event to. 2233 * @param e the change information from the associated document 2234 * @param a the current allocation of the view 2235 * @param f the factory to use to rebuild if the view has 2236 * children 2237 * @see #forwardUpdate 2238 * @since 1.3 2239 */ 2240 protected void forwardUpdateToView(View v, DocumentEvent e, 2241 Shape a, ViewFactory f) { 2242 v.setParent(this); 2243 super.forwardUpdateToView(v, e, a, f); 2244 } 2245 2246 // The following methods don't do anything useful, they 2247 // simply keep the class from being abstract. 2248 2249 public void paint(Graphics g, Shape allocation) { 2250 } 2251 2252 protected boolean isBefore(int x, int y, Rectangle alloc) { 2253 return false; 2254 } 2255 2256 protected boolean isAfter(int x, int y, Rectangle alloc) { 2257 return false; 2258 } 2259 2260 protected View getViewAtPoint(int x, int y, Rectangle alloc) { 2261 return null; 2262 } 2263 2264 protected void childAllocation(int index, Rectangle a) { 2265 } 2266 } 2267 } 2268 } 2269 2270 /* This is useful for the nightmare of parsing multi-part HTTP/RFC822 headers 2271 * sensibly: 2272 * From a String like: 'timeout=15, max=5' 2273 * create an array of Strings: 2274 * { {"timeout", "15"}, 2275 * {"max", "5"} 2276 * } 2277 * From one like: 'Basic Realm="FuzzFace" Foo="Biz Bar Baz"' 2278 * create one like (no quotes in literal): 2279 * { {"basic", null}, 2280 * {"realm", "FuzzFace"} 2281 * {"foo", "Biz Bar Baz"} 2282 * } 2283 * keys are converted to lower case, vals are left as is.... 2284 * 2285 * author Dave Brown 2286 */ 2287 2288 2289 static class HeaderParser { 2290 2291 /* table of key/val pairs - maxes out at 10!!!!*/ 2292 String raw; 2293 String[][] tab; 2294 2295 public HeaderParser(String raw) { 2296 this.raw = raw; 2297 tab = new String[10][2]; 2298 parse(); 2299 } 2300 2301 private void parse() { 2302 2303 if (raw != null) { 2304 raw = raw.trim(); 2305 char[] ca = raw.toCharArray(); 2306 int beg = 0, end = 0, i = 0; 2307 boolean inKey = true; 2308 boolean inQuote = false; 2309 int len = ca.length; 2310 while (end < len) { 2311 char c = ca[end]; 2312 if (c == '=') { // end of a key 2313 tab[i][0] = new String(ca, beg, end-beg).toLowerCase(); 2314 inKey = false; 2315 end++; 2316 beg = end; 2317 } else if (c == '\"') { 2318 if (inQuote) { 2319 tab[i++][1]= new String(ca, beg, end-beg); 2320 inQuote=false; 2321 do { 2322 end++; 2323 } while (end < len && (ca[end] == ' ' || ca[end] == ',')); 2324 inKey=true; 2325 beg=end; 2326 } else { 2327 inQuote=true; 2328 end++; 2329 beg=end; 2330 } 2331 } else if (c == ' ' || c == ',') { // end key/val, of whatever we're in 2332 if (inQuote) { 2333 end++; 2334 continue; 2335 } else if (inKey) { 2336 tab[i++][0] = (new String(ca, beg, end-beg)).toLowerCase(); 2337 } else { 2338 tab[i++][1] = (new String(ca, beg, end-beg)); 2339 } 2340 while (end < len && (ca[end] == ' ' || ca[end] == ',')) { 2341 end++; 2342 } 2343 inKey = true; 2344 beg = end; 2345 } else { 2346 end++; 2347 } 2348 } 2349 // get last key/val, if any 2350 if (--end > beg) { 2351 if (!inKey) { 2352 if (ca[end] == '\"') { 2353 tab[i++][1] = (new String(ca, beg, end-beg)); 2354 } else { 2355 tab[i++][1] = (new String(ca, beg, end-beg+1)); 2356 } 2357 } else { 2358 tab[i][0] = (new String(ca, beg, end-beg+1)).toLowerCase(); 2359 } 2360 } else if (end == beg) { 2361 if (!inKey) { 2362 if (ca[end] == '\"') { 2363 tab[i++][1] = String.valueOf(ca[end-1]); 2364 } else { 2365 tab[i++][1] = String.valueOf(ca[end]); 2366 } 2367 } else { 2368 tab[i][0] = String.valueOf(ca[end]).toLowerCase(); 2369 } 2370 } 2371 } 2372 2373 } 2374 2375 public String findKey(int i) { 2376 if (i < 0 || i > 10) 2377 return null; 2378 return tab[i][0]; 2379 } 2380 2381 public String findValue(int i) { 2382 if (i < 0 || i > 10) 2383 return null; 2384 return tab[i][1]; 2385 } 2386 2387 public String findValue(String key) { 2388 return findValue(key, null); 2389 } 2390 2391 public String findValue(String k, String Default) { 2392 if (k == null) 2393 return Default; 2394 k = k.toLowerCase(); 2395 for (int i = 0; i < 10; ++i) { 2396 if (tab[i][0] == null) { 2397 return Default; 2398 } else if (k.equals(tab[i][0])) { 2399 return tab[i][1]; 2400 } 2401 } 2402 return Default; 2403 } 2404 2405 public int findInt(String k, int Default) { 2406 try { 2407 return Integer.parseInt(findValue(k, String.valueOf(Default))); 2408 } catch (Throwable t) { 2409 return Default; 2410 } 2411 } 2412 } 2413 2414 }