Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / JEditorPane.java
blob8361b20371b927941a0d5536300f82f5ade62419
1 /* JEditorPane.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package javax.swing;
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;
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;
56 public class JEditorPane extends JTextComponent
58 private static final long serialVersionUID = 3140472492599046285L;
60 private URL page;
61 private EditorKit editorKit;
63 boolean focus_root;
64 boolean manages_focus;
66 public JEditorPane()
68 setEditorKit(createDefaultEditorKit());
71 public JEditorPane(String url) throws IOException
73 this(new URL(url));
76 public JEditorPane(String type, String text)
78 setEditorKit(createEditorKitForContentType(type));
79 setText(text);
82 public JEditorPane(URL url) throws IOException
84 this();
85 setPage(url);
88 protected EditorKit createDefaultEditorKit()
90 return new DefaultEditorKit();
93 public static EditorKit createEditorKitForContentType(String type)
95 return new DefaultEditorKit();
98 /**
99 * Sends a given <code>HyperlinkEvent</code> to all registered listeners.
101 * @param event the event to send
103 public void fireHyperlinkUpdate(HyperlinkEvent event)
105 HyperlinkListener[] listeners = getHyperlinkListeners();
107 for (int index = 0; index < listeners.length; ++index)
108 listeners[index].hyperlinkUpdate(event);
111 public AccessibleContext getAccessibleContext()
113 return null;
116 public final String getContentType()
118 return getEditorKit().getContentType();
121 public EditorKit getEditorKit()
123 return editorKit;
126 public static String getEditorKitClassNameForContentType(String type)
128 return "text/plain";
131 public EditorKit getEditorKitForContentType(String type)
133 return editorKit;
137 * Returns the preferred size for the JEditorPane.
139 public Dimension getPreferredSize()
141 return super.getPreferredSize();
144 public boolean getScrollableTracksViewportHeight()
146 return false;
149 public boolean getScrollableTracksViewportWidth()
151 return false;
154 public URL getPage()
156 return page;
159 protected InputStream getStream(URL page)
160 throws IOException
162 return page.openStream();
165 public String getText()
167 return super.getText();
170 public String getUIClassID()
172 return "EditorPaneUI";
175 public boolean isFocusCycleRoot()
177 return focus_root;
180 public boolean isManagingFocus()
182 return manages_focus;
185 protected String paramString()
187 return "JEditorPane";
191 * Overridden to handle processing of tab/shift tab.
193 protected void processComponentKeyEvent(KeyEvent e)
198 * Make sure that TAB and Shift-TAB events get consumed,
199 * so that awt doesn't attempt focus traversal.
201 protected void processKeyEvent(KeyEvent e)
206 * This method initializes from a stream.
208 public void read(InputStream in, Object desc)
209 throws IOException
214 * Establishes the default bindings of type to classname.
216 public static void registerEditorKitForContentType(String type,
217 String classname)
222 * Establishes the default bindings of type to classname.
224 public static void registerEditorKitForContentType(String type,
225 String classname,
226 ClassLoader loader)
231 * Replaces the currently selected content with new content represented
232 * by the given string.
234 public void replaceSelection(String content)
239 * Scrolls the view to the given reference location (that is, the value
240 * returned by the UL.getRef method for the URL being displayed).
242 public void scrollToReference(String reference)
246 public final void setContentType(String type)
248 if (editorKit != null
249 && editorKit.getContentType().equals(type))
250 return;
252 EditorKit kit = getEditorKitForContentType(type);
254 if (kit != null)
255 setEditorKit(kit);
258 public void setEditorKit(EditorKit newValue)
260 if (editorKit == newValue)
261 return;
263 if (editorKit != null)
264 editorKit.deinstall(this);
266 EditorKit oldValue = editorKit;
267 editorKit = newValue;
269 if (editorKit != null)
271 editorKit.install(this);
272 setDocument(editorKit.createDefaultDocument());
275 firePropertyChange("editorKit", oldValue, newValue);
276 invalidate();
277 repaint();
280 public void setEditorKitForContentType(String type, EditorKit k)
282 // FIXME: editorKitCache.put(type, kit);
286 * Sets the current URL being displayed.
288 public void setPage(String url) throws IOException
290 setPage(new URL(url));
294 * Sets the current URL being displayed.
296 public void setPage(URL page) throws IOException
298 if (page == null)
299 throw new IOException("invalid url");
303 this.page = page;
304 getEditorKit().read(page.openStream(), getDocument(), 0);
306 catch (BadLocationException e)
308 // Ignored. '0' is always a valid offset.
312 public void setText(String t)
314 super.setText(t);
318 * Add a <code>HyperlinkListener</code> object to this editor pane.
320 * @param listener the listener to add
322 public void addHyperlinkListener(HyperlinkListener listener)
324 listenerList.add(HyperlinkListener.class, listener);
328 * Removes a <code>HyperlinkListener</code> object to this editor pane.
330 * @param listener the listener to remove
332 public void removeHyperlinkListener(HyperlinkListener listener)
334 listenerList.remove(HyperlinkListener.class, listener);
338 * Returns all added <code>HyperlinkListener</code> objects.
340 * @return array of listeners
342 * @since 1.4
344 public HyperlinkListener[] getHyperlinkListeners()
346 return (HyperlinkListener[]) getListeners(HyperlinkListener.class);