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.awt.event.KeyEvent;
  43: import java.io.IOException;
  44: import java.io.InputStream;
  45: import java.net.URL;
  46: 
  47: import javax.accessibility.AccessibleContext;
  48: import javax.swing.event.HyperlinkEvent;
  49: import javax.swing.event.HyperlinkListener;
  50: import javax.swing.text.BadLocationException;
  51: import javax.swing.text.DefaultEditorKit;
  52: import javax.swing.text.EditorKit;
  53: import javax.swing.text.JTextComponent;
  54: 
  55: 
  56: public class JEditorPane extends JTextComponent
  57: {
  58:   private static final long serialVersionUID = 3140472492599046285L;
  59:   
  60:   private URL page;
  61:   private EditorKit editorKit;
  62:   
  63:   boolean focus_root;
  64: 
  65:   public JEditorPane()
  66:   {
  67:     setEditorKit(createDefaultEditorKit());
  68:   }
  69: 
  70:   public JEditorPane(String url) throws IOException
  71:   {
  72:     this(new URL(url));
  73:   }
  74: 
  75:   public JEditorPane(String type, String text)
  76:   {
  77:     setEditorKit(createEditorKitForContentType(type));
  78:     setText(text);
  79:   }
  80: 
  81:   public JEditorPane(URL url) throws IOException
  82:   {
  83:     this();
  84:     setPage(url);
  85:   }
  86: 
  87:   protected EditorKit createDefaultEditorKit()
  88:   {
  89:     return new DefaultEditorKit();
  90:   }
  91: 
  92:   public static EditorKit createEditorKitForContentType(String type)
  93:   {
  94:     return new DefaultEditorKit();
  95:   }
  96: 
  97:   /**
  98:    * Sends a given <code>HyperlinkEvent</code> to all registered listeners.
  99:    *
 100:    * @param event the event to send
 101:    */
 102:   public void fireHyperlinkUpdate(HyperlinkEvent event)
 103:   {
 104:     HyperlinkListener[] listeners = getHyperlinkListeners();
 105: 
 106:     for (int index = 0; index < listeners.length; ++index)
 107:        listeners[index].hyperlinkUpdate(event);
 108:   }
 109: 
 110:   public AccessibleContext getAccessibleContext()
 111:   {
 112:     return null;
 113:   }
 114: 
 115:   public final String getContentType()
 116:   {
 117:     return getEditorKit().getContentType();
 118:   }
 119: 
 120:   /**
 121:    * Returns the EditorKit. If there is no EditorKit set this method
 122:    * calls createDefaultEditorKit() and setEditorKit() first.
 123:    */
 124:   public EditorKit getEditorKit()
 125:   {
 126:     if (editorKit == null)
 127:       setEditorKit(createDefaultEditorKit());
 128:     return editorKit;
 129:   }
 130: 
 131:   public static String getEditorKitClassNameForContentType(String type)
 132:   {
 133:     return "text/plain";
 134:   }
 135: 
 136:   public EditorKit getEditorKitForContentType(String type)
 137:   {
 138:     return editorKit;
 139:   }
 140: 
 141:   /**
 142:    * Returns the preferred size for the JEditorPane.  
 143:    */
 144:   public Dimension getPreferredSize()
 145:   {
 146:     return super.getPreferredSize();
 147:   }
 148: 
 149:   public boolean getScrollableTracksViewportHeight()
 150:   {
 151:     return false;
 152:   }
 153: 
 154:   public boolean getScrollableTracksViewportWidth()
 155:   {
 156:     return false;
 157:   }
 158: 
 159:   public URL getPage()
 160:   {
 161:     return page;
 162:   }
 163: 
 164:   protected InputStream getStream(URL page)
 165:     throws IOException
 166:   {
 167:     return page.openStream();
 168:   }
 169: 
 170:   public String getText()
 171:   {
 172:     return super.getText();
 173:   }
 174: 
 175:   public String getUIClassID()
 176:   {
 177:     return "EditorPaneUI";
 178:   }
 179: 
 180:   public boolean isFocusCycleRoot()
 181:   {
 182:     return focus_root;
 183:   }
 184: 
 185:   protected String paramString()
 186:   {
 187:     return "JEditorPane";
 188:   }
 189: 
 190:   /**
 191:    * This method initializes from a stream. 
 192:    */
 193:   public void read(InputStream in, Object desc)
 194:     throws IOException
 195:   {
 196:   }
 197: 
 198:   /**
 199:    * Establishes the default bindings of type to classname. 
 200:    */
 201:   public static void registerEditorKitForContentType(String type,
 202:                                                      String classname)
 203:   {
 204:   }
 205: 
 206:   /**
 207:    * Establishes the default bindings of type to classname.
 208:    */
 209:   public static void registerEditorKitForContentType(String type,
 210:                                                      String classname,
 211:                                                      ClassLoader loader)
 212:   {
 213:   }
 214: 
 215:   /**
 216:    * Replaces the currently selected content with new content represented
 217:    * by the given string.
 218:    */
 219:   public void replaceSelection(String content)
 220:   {
 221:   }
 222: 
 223:   /**
 224:    * Scrolls the view to the given reference location (that is, the value
 225:    * returned by the UL.getRef method for the URL being displayed).
 226:    */
 227:   public void scrollToReference(String reference)
 228:   {
 229:   }
 230: 
 231:   public final void setContentType(String type)
 232:   {
 233:     if (editorKit != null
 234:     && editorKit.getContentType().equals(type))
 235:       return;
 236:               
 237:     EditorKit kit = getEditorKitForContentType(type);
 238:             
 239:     if (kit != null)
 240:       setEditorKit(kit);
 241:   }
 242: 
 243:   public void setEditorKit(EditorKit newValue)
 244:   {
 245:     if (editorKit == newValue)
 246:       return;
 247:         
 248:     if (editorKit != null)
 249:       editorKit.deinstall(this);
 250:                 
 251:     EditorKit oldValue = editorKit;
 252:     editorKit = newValue;
 253:                     
 254:     if (editorKit != null)
 255:       {
 256:     editorKit.install(this);
 257:     setDocument(editorKit.createDefaultDocument());
 258:       }
 259:                             
 260:     firePropertyChange("editorKit", oldValue, newValue);
 261:     invalidate();
 262:     repaint();
 263:   }
 264: 
 265:   public void setEditorKitForContentType(String type, EditorKit k)
 266:   {
 267:     // FIXME: editorKitCache.put(type, kit);
 268:   }
 269: 
 270:   /**
 271:    * Sets the current URL being displayed.  
 272:    */
 273:   public void setPage(String url) throws IOException
 274:   {
 275:     setPage(new URL(url));
 276:   }
 277: 
 278:   /**
 279:    * Sets the current URL being displayed.  
 280:    */
 281:   public void setPage(URL page) throws IOException
 282:   {
 283:     if (page == null)
 284:       throw new IOException("invalid url");
 285: 
 286:     try
 287:       {
 288:     this.page = page;
 289:     getEditorKit().read(page.openStream(), getDocument(), 0);
 290:       }
 291:     catch (BadLocationException e)
 292:       {
 293:     // Ignored. '0' is always a valid offset.
 294:       }
 295:   }
 296: 
 297:   public void setText(String t)
 298:   {
 299:     super.setText(t);
 300:   }
 301: 
 302:   /**
 303:    * Add a <code>HyperlinkListener</code> object to this editor pane.
 304:    *
 305:    * @param listener the listener to add
 306:    */
 307:   public void addHyperlinkListener(HyperlinkListener listener)
 308:   {
 309:     listenerList.add(HyperlinkListener.class, listener);
 310:   }
 311: 
 312:   /**
 313:    * Removes a <code>HyperlinkListener</code> object to this editor pane.
 314:    *
 315:    * @param listener the listener to remove
 316:    */
 317:   public void removeHyperlinkListener(HyperlinkListener listener)
 318:   {
 319:     listenerList.remove(HyperlinkListener.class, listener);
 320:   }
 321: 
 322:   /**
 323:    * Returns all added <code>HyperlinkListener</code> objects.
 324:    *
 325:    * @return array of listeners
 326:    *
 327:    * @since 1.4
 328:    */
 329:   public HyperlinkListener[] getHyperlinkListeners()
 330:   {
 331:     return (HyperlinkListener[]) getListeners(HyperlinkListener.class);
 332:   }
 333: }