Source for javax.swing.JEditorPane

   1: /* JEditorPane.java --
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import java.awt.Dimension;
  42: import java.io.IOException;
  43: import java.io.InputStream;
  44: import java.net.URL;
  45: 
  46: import javax.accessibility.AccessibleContext;
  47: import javax.swing.event.HyperlinkEvent;
  48: import javax.swing.event.HyperlinkListener;
  49: import javax.swing.text.BadLocationException;
  50: import javax.swing.text.DefaultEditorKit;
  51: import javax.swing.text.EditorKit;
  52: import javax.swing.text.JTextComponent;
  53: 
  54: /**
  55:  * A powerful text editor component that can handle different types of
  56:  * content.
  57:  *
  58:  * The JEditorPane text component is driven by an instance of
  59:  * {@link EditorKit}. The editor kit is responsible for providing
  60:  * a default {@link Document} implementation, a mechanism for loading
  61:  * and saving documents of its supported content type and providing
  62:  * a set of {@link Action}s for manipulating the content.
  63:  *
  64:  * By default the following content types are supported:
  65:  * <ul>
  66:  * <li><code>text/plain</code>: Plain text, handled by
  67:  *   {@link javax.swing.text.DefaultEditorKit}.</li>
  68:  * <li><code>text/html</code>: HTML 4.0 styled text, handled by
  69:  *   {@link javax.swing.text.html.HTMLEditorKit}.</li>
  70:  * <li><code>text/rtf</code>: RTF text, handled by
  71:  *   {@link javax.swing.text.rtf.RTFEditorKit}.</li>
  72:  * </ul>
  73:  *
  74:  * @author original author unknown
  75:  * @author Roman Kennke (roman@kennke.org)
  76:  */
  77: public class JEditorPane extends JTextComponent
  78: {
  79:   private static final long serialVersionUID = 3140472492599046285L;
  80:   
  81:   private URL page;
  82:   private EditorKit editorKit;
  83:   
  84:   boolean focus_root;
  85: 
  86:   public JEditorPane()
  87:   {
  88:     setEditorKit(createDefaultEditorKit());
  89:   }
  90: 
  91:   public JEditorPane(String url) throws IOException
  92:   {
  93:     this(new URL(url));
  94:   }
  95: 
  96:   public JEditorPane(String type, String text)
  97:   {
  98:     setEditorKit(createEditorKitForContentType(type));
  99:     setText(text);
 100:   }
 101: 
 102:   public JEditorPane(URL url) throws IOException
 103:   {
 104:     this();
 105:     setPage(url);
 106:   }
 107: 
 108:   protected EditorKit createDefaultEditorKit()
 109:   {
 110:     return new DefaultEditorKit();
 111:   }
 112: 
 113:   public static EditorKit createEditorKitForContentType(String type)
 114:   {
 115:     return new DefaultEditorKit();
 116:   }
 117: 
 118:   /**
 119:    * Sends a given <code>HyperlinkEvent</code> to all registered listeners.
 120:    *
 121:    * @param event the event to send
 122:    */
 123:   public void fireHyperlinkUpdate(HyperlinkEvent event)
 124:   {
 125:     HyperlinkListener[] listeners = getHyperlinkListeners();
 126: 
 127:     for (int index = 0; index < listeners.length; ++index)
 128:        listeners[index].hyperlinkUpdate(event);
 129:   }
 130: 
 131:   public AccessibleContext getAccessibleContext()
 132:   {
 133:     return null;
 134:   }
 135: 
 136:   public final String getContentType()
 137:   {
 138:     return getEditorKit().getContentType();
 139:   }
 140: 
 141:   /**
 142:    * Returns the EditorKit. If there is no EditorKit set this method
 143:    * calls createDefaultEditorKit() and setEditorKit() first.
 144:    */
 145:   public EditorKit getEditorKit()
 146:   {
 147:     if (editorKit == null)
 148:       setEditorKit(createDefaultEditorKit());
 149:     return editorKit;
 150:   }
 151: 
 152:   public static String getEditorKitClassNameForContentType(String type)
 153:   {
 154:     return "text/plain";
 155:   }
 156: 
 157:   public EditorKit getEditorKitForContentType(String type)
 158:   {
 159:     return editorKit;
 160:   }
 161: 
 162:   /**
 163:    * Returns the preferred size for the JEditorPane.  
 164:    */
 165:   public Dimension getPreferredSize()
 166:   {
 167:     return super.getPreferredSize();
 168:   }
 169: 
 170:   public boolean getScrollableTracksViewportHeight()
 171:   {
 172:     return false;
 173:   }
 174: 
 175:   public boolean getScrollableTracksViewportWidth()
 176:   {
 177:     return false;
 178:   }
 179: 
 180:   public URL getPage()
 181:   {
 182:     return page;
 183:   }
 184: 
 185:   protected InputStream getStream(URL page)
 186:     throws IOException
 187:   {
 188:     return page.openStream();
 189:   }
 190: 
 191:   public String getText()
 192:   {
 193:     return super.getText();
 194:   }
 195: 
 196:   public String getUIClassID()
 197:   {
 198:     return "EditorPaneUI";
 199:   }
 200: 
 201:   public boolean isFocusCycleRoot()
 202:   {
 203:     return focus_root;
 204:   }
 205: 
 206:   protected String paramString()
 207:   {
 208:     return "JEditorPane";
 209:   }
 210: 
 211:   /**
 212:    * This method initializes from a stream. 
 213:    */
 214:   public void read(InputStream in, Object desc)
 215:     throws IOException
 216:   {
 217:   }
 218: 
 219:   /**
 220:    * Establishes the default bindings of type to classname. 
 221:    */
 222:   public static void registerEditorKitForContentType(String type,
 223:                                                      String classname)
 224:   {
 225:   }
 226: 
 227:   /**
 228:    * Establishes the default bindings of type to classname.
 229:    */
 230:   public static void registerEditorKitForContentType(String type,
 231:                                                      String classname,
 232:                                                      ClassLoader loader)
 233:   {
 234:   }
 235: 
 236:   /**
 237:    * Replaces the currently selected content with new content represented
 238:    * by the given string.
 239:    */
 240:   public void replaceSelection(String content)
 241:   {
 242:   }
 243: 
 244:   /**
 245:    * Scrolls the view to the given reference location (that is, the value
 246:    * returned by the UL.getRef method for the URL being displayed).
 247:    */
 248:   public void scrollToReference(String reference)
 249:   {
 250:   }
 251: 
 252:   public final void setContentType(String type)
 253:   {
 254:     if (editorKit != null
 255:     && editorKit.getContentType().equals(type))
 256:       return;
 257:               
 258:     EditorKit kit = getEditorKitForContentType(type);
 259:             
 260:     if (kit != null)
 261:       setEditorKit(kit);
 262:   }
 263: 
 264:   public void setEditorKit(EditorKit newValue)
 265:   {
 266:     if (editorKit == newValue)
 267:       return;
 268:         
 269:     if (editorKit != null)
 270:       editorKit.deinstall(this);
 271:                 
 272:     EditorKit oldValue = editorKit;
 273:     editorKit = newValue;
 274:                     
 275:     if (editorKit != null)
 276:       {
 277:     editorKit.install(this);
 278:     setDocument(editorKit.createDefaultDocument());
 279:       }
 280:                             
 281:     firePropertyChange("editorKit", oldValue, newValue);
 282:     invalidate();
 283:     repaint();
 284:   }
 285: 
 286:   public void setEditorKitForContentType(String type, EditorKit k)
 287:   {
 288:     // FIXME: editorKitCache.put(type, kit);
 289:   }
 290: 
 291:   /**
 292:    * Sets the current URL being displayed.  
 293:    */
 294:   public void setPage(String url) throws IOException
 295:   {
 296:     setPage(new URL(url));
 297:   }
 298: 
 299:   /**
 300:    * Sets the current URL being displayed.  
 301:    */
 302:   public void setPage(URL page) throws IOException
 303:   {
 304:     if (page == null)
 305:       throw new IOException("invalid url");
 306: 
 307:     try
 308:       {
 309:     this.page = page;
 310:     getEditorKit().read(page.openStream(), getDocument(), 0);
 311:       }
 312:     catch (BadLocationException e)
 313:       {
 314:     // Ignored. '0' is always a valid offset.
 315:       }
 316:   }
 317: 
 318:   public void setText(String t)
 319:   {
 320:     super.setText(t);
 321:   }
 322: 
 323:   /**
 324:    * Add a <code>HyperlinkListener</code> object to this editor pane.
 325:    *
 326:    * @param listener the listener to add
 327:    */
 328:   public void addHyperlinkListener(HyperlinkListener listener)
 329:   {
 330:     listenerList.add(HyperlinkListener.class, listener);
 331:   }
 332: 
 333:   /**
 334:    * Removes a <code>HyperlinkListener</code> object to this editor pane.
 335:    *
 336:    * @param listener the listener to remove
 337:    */
 338:   public void removeHyperlinkListener(HyperlinkListener listener)
 339:   {
 340:     listenerList.remove(HyperlinkListener.class, listener);
 341:   }
 342: 
 343:   /**
 344:    * Returns all added <code>HyperlinkListener</code> objects.
 345:    *
 346:    * @return array of listeners
 347:    *
 348:    * @since 1.4
 349:    */
 350:   public HyperlinkListener[] getHyperlinkListeners()
 351:   {
 352:     return (HyperlinkListener[]) getListeners(HyperlinkListener.class);
 353:   }
 354: }