/[classpath]/classpath/javax/swing/JEditorPane.java
ViewVC logotype

Diff of /classpath/javax/swing/JEditorPane.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.12.2.7 by gnu_andrew, Sat Sep 10 15:31:48 2005 UTC revision 1.12.2.8 by gnu_andrew, Wed Nov 2 00:43:45 2005 UTC
# Line 41  package javax.swing; Line 41  package javax.swing;
41  import java.awt.Dimension;  import java.awt.Dimension;
42  import java.io.IOException;  import java.io.IOException;
43  import java.io.InputStream;  import java.io.InputStream;
44    import java.io.InputStreamReader;
45    import java.io.Reader;
46    import java.net.MalformedURLException;
47  import java.net.URL;  import java.net.URL;
48    
49  import javax.accessibility.AccessibleContext;  import javax.accessibility.AccessibleContext;
50    import javax.accessibility.AccessibleHyperlink;
51    import javax.accessibility.AccessibleHypertext;
52    import javax.accessibility.AccessibleStateSet;
53    import javax.accessibility.AccessibleText;
54  import javax.swing.event.HyperlinkEvent;  import javax.swing.event.HyperlinkEvent;
55  import javax.swing.event.HyperlinkListener;  import javax.swing.event.HyperlinkListener;
56  import javax.swing.text.BadLocationException;  import javax.swing.text.BadLocationException;
57  import javax.swing.text.DefaultEditorKit;  import javax.swing.text.DefaultEditorKit;
58    import javax.swing.text.Document;
59  import javax.swing.text.EditorKit;  import javax.swing.text.EditorKit;
60    import javax.swing.text.Element;
61  import javax.swing.text.JTextComponent;  import javax.swing.text.JTextComponent;
62    import javax.swing.text.html.HTML;
63    import javax.swing.text.html.HTMLDocument;
64    import javax.swing.text.html.HTMLEditorKit;
65    
66  /**  /**
67   * A powerful text editor component that can handle different types of   * A powerful text editor component that can handle different types of
# Line 76  import javax.swing.text.JTextComponent; Line 88  import javax.swing.text.JTextComponent;
88   */   */
89  public class JEditorPane extends JTextComponent  public class JEditorPane extends JTextComponent
90  {  {
91      /**
92       * Provides accessibility support for <code>JEditorPane</code>.
93       *
94       * @author Roman Kennke (kennke@aicas.com)
95       */
96      protected class AccessibleJEditorPane extends AccessibleJTextComponent
97      {
98    
99        /**
100         * Creates a new <code>AccessibleJEditorPane</code> object.
101         */
102        protected AccessibleJEditorPane()
103        {
104          super();
105        }
106    
107        /**
108         * Returns a description of this <code>AccessibleJEditorPane</code>. If
109         * this property is not set, then this returns the content-type of the
110         * editor pane.
111         *
112         * @return a description of this AccessibleJEditorPane
113         */
114        public String getAccessibleDescription()
115        {
116          String descr = super.getAccessibleDescription();
117          if (descr == null)
118            return getContentType();
119          else
120            return descr;
121        }
122    
123        /**
124         * Returns the accessible state of this <code>AccessibleJEditorPane</code>.
125         *
126         * @return  the accessible state of this <code>AccessibleJEditorPane</code>
127         */
128        public AccessibleStateSet getAccessibleStateSet()
129        {
130          AccessibleStateSet state = super.getAccessibleStateSet();
131          // TODO: Figure out what state must be added here to the super's state.
132          return state;
133        }
134      }
135    
136      /**
137       * Provides accessibility support for <code>JEditorPane</code>s, when the
138       * editor kit is an instance of {@link HTMLEditorKit}.
139       *
140       * @author Roman Kennke (kennke@aicas.com)
141       */
142      protected class AccessibleJEditorPaneHTML extends AccessibleJEditorPane
143      {
144        /**
145         * Returns the accessible text of the <code>JEditorPane</code>. This will
146         * be an instance of
147         * {@link JEditorPaneAccessibleHypertextSupport}.
148         *
149         * @return the accessible text of the <code>JEditorPane</code>
150         */
151        public AccessibleText getAccessibleText()
152        {
153          return new JEditorPaneAccessibleHypertextSupport();
154        }
155      }
156    
157      /**
158       * This is the accessible text that is returned by
159       * {@link AccessibleJEditorPaneHTML#getAccessibleText()}.
160       *
161       * @author Roman Kennke (kennke@aicas.com)
162       */
163      protected class JEditorPaneAccessibleHypertextSupport
164        extends AccessibleJEditorPane implements AccessibleHypertext
165      {
166    
167        /**
168         * The accessible representation of a HTML link.
169         *
170         * @author Roman Kennke (kennke@aicas.com)
171         */
172        public class HTMLLink extends AccessibleHyperlink
173        {
174    
175          /**
176           * The element in the document that represents the link.
177           */
178          Element element;
179    
180          /**
181           * Creates a new <code>HTMLLink</code>.
182           *
183           * @param el the link element
184           */
185          public HTMLLink(Element el)
186          {
187            this.element = el;
188          }
189    
190          /**
191           * Returns <code>true</code> if this <code>HTMLLink</code> is still
192           * valid. A <code>HTMLLink</code> can become invalid when the document
193           * changes.
194           *
195           * @return <code>true</code> if this <code>HTMLLink</code> is still
196           *         valid
197           */
198          public boolean isValid()
199          {
200            // I test here if the element at our element's start offset is the
201            // same as the element in the document at this offset. If this is true,
202            // I consider the link valid, if not, then this link no longer
203            // represented by this HTMLLink and therefor invalid.
204            HTMLDocument doc = (HTMLDocument) getDocument();
205            return doc.getCharacterElement(element.getStartOffset()) == element;
206          }
207    
208          /**
209           * Returns the number of AccessibleActions in this link object. In
210           * general, link have 1 AccessibleAction associated with them. There are
211           * special cases where links can have multiple actions associated, like
212           * in image maps.
213           *
214           * @return the number of AccessibleActions in this link object
215           */
216          public int getAccessibleActionCount()
217          {
218            // TODO: Implement the special cases.
219            return 1;
220          }
221    
222          /**
223           * Performs the specified action on the link object. This ususally means
224           * activating the link.
225           *
226           * @return <code>true</code> if the action has been performed
227           *         successfully, <code>false</code> otherwise
228           */
229          public boolean doAccessibleAction(int i)
230          {
231            String href = (String) element.getAttributes().getAttribute("href");
232            HTMLDocument doc = (HTMLDocument) getDocument();
233            try
234              {
235                URL url = new URL(doc.getBase(), href);
236                setPage(url);
237                String desc = doc.getText(element.getStartOffset(),
238                                element.getEndOffset() - element.getStartOffset());
239                HyperlinkEvent ev =
240                  new HyperlinkEvent(JEditorPane.this,
241                                     HyperlinkEvent.EventType.ACTIVATED, url, desc,
242                                     element);
243                fireHyperlinkUpdate(ev);
244                return true;
245              }
246            catch (Exception ex)
247              {
248                return false;
249              }
250          }
251    
252          /**
253           * Returns the description of the action at action index <code>i</code>.
254           * This method returns the text within the element associated with this
255           * link.
256           *
257           * @param i the action index
258           *
259           * @return the description of the action at action index <code>i</code>
260           */
261          public String getAccessibleActionDescription(int i)
262          {
263            HTMLDocument doc = (HTMLDocument) getDocument();
264            try
265              {
266                return doc.getText(element.getStartOffset(),
267                                element.getEndOffset() - element.getStartOffset());
268              }
269            catch (BadLocationException ex)
270              {
271                throw (AssertionError)
272                new AssertionError("BadLocationException must not be thrown "
273                                   + "here.")
274                  .initCause(ex);
275              }
276          }
277    
278          /**
279           * Returns an {@link URL} object, that represents the action at action
280           * index <code>i</code>.
281           *
282           * @param i the action index
283           *
284           * @return an {@link URL} object, that represents the action at action
285           *         index <code>i</code>
286           */
287          public Object getAccessibleActionObject(int i)
288          {
289            String href = (String) element.getAttributes().getAttribute("href");
290            HTMLDocument doc = (HTMLDocument) getDocument();
291            try
292              {
293                URL url = new URL(doc.getBase(), href);
294                return url;
295              }
296            catch (MalformedURLException ex)
297              {
298                return null;
299              }
300          }
301    
302          /**
303           * Returns an object that represents the link anchor. For examples, if
304           * the link encloses a string, then a <code>String</code> object is
305           * returned, if the link encloses an &lt;img&gt; tag, then an
306           * <code>ImageIcon</code> object is returned.
307           *
308           * @return an object that represents the link anchor
309           */
310          public Object getAccessibleActionAnchor(int i)
311          {
312            // TODO: This is only the String case. Implement all cases.
313            return getAccessibleActionDescription(i);
314          }
315    
316          /**
317           * Returns the start index of the hyperlink element.
318           *
319           * @return the start index of the hyperlink element
320           */
321          public int getStartIndex()
322          {
323            return element.getStartOffset();
324          }
325    
326          /**
327           * Returns the end index of the hyperlink element.
328           *
329           * @return the end index of the hyperlink element
330           */
331          public int getEndIndex()
332          {
333            return element.getEndOffset();
334          }
335          
336        }
337    
338        /**
339         * Returns the number of hyperlinks in the document.
340         *
341         * @return the number of hyperlinks in the document
342         */
343        public int getLinkCount()
344        {
345          HTMLDocument doc = (HTMLDocument) getDocument();
346          HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
347          int count = 0;
348          while (linkIter.isValid())
349            {
350              count++;
351              linkIter.next();
352            }
353          return count;
354        }
355    
356        /**
357         * Returns the <code>i</code>-th hyperlink in the document or
358         * <code>null</code> if there is no hyperlink with the specified index.
359         *
360         * @param i the index of the hyperlink to return
361         *
362         * @return the <code>i</code>-th hyperlink in the document or
363         *         <code>null</code> if there is no hyperlink with the specified
364         *         index
365         */
366        public AccessibleHyperlink getLink(int i)
367        {
368          HTMLDocument doc = (HTMLDocument) getDocument();
369          HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
370          int count = 0;
371          while (linkIter.isValid())
372            {
373              count++;
374              if (count == i)
375                break;
376              linkIter.next();
377            }
378          if (linkIter.isValid())
379            {
380              int offset = linkIter.getStartOffset();
381              // TODO: I fetch the element for the link via getCharacterElement().
382              // I am not sure that this is correct, maybe we must use
383              // getParagraphElement()?
384              Element el = doc.getCharacterElement(offset);
385              HTMLLink link = new HTMLLink(el);
386              return link;
387            }
388          else
389            return null;
390        }
391    
392        /**
393         * Returns the index of the link element at the character position
394         * <code>c</code> within the document, or <code>-1</code> if there is no
395         * link at the specified position.
396         *
397         * @param c the character index from which to fetch the link index
398         *
399         * @return the index of the link element at the character position
400         *         <code>c</code> within the document, or <code>-1</code> if there
401         *         is no link at the specified position
402         */
403        public int getLinkIndex(int c)
404        {
405          HTMLDocument doc = (HTMLDocument) getDocument();
406          HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
407          int count = 0;
408          while (linkIter.isValid())
409            {
410              if (linkIter.getStartOffset() <= c && linkIter.getEndOffset() > c)
411                break;
412              count++;
413              linkIter.next();
414            }
415          if (linkIter.isValid())
416            return count;
417          else
418            return -1;
419        }
420    
421        /**
422         * Returns the link text of the link at index <code>i</code>, or
423         * <code>null</code>, if there is no link at the specified position.
424         *
425         * @param i the index of the link
426         *
427         * @return  the link text of the link at index <code>i</code>, or
428         *          <code>null</code>, if there is no link at the specified
429         *          position
430         */
431        public String getLinkText(int i)
432        {
433          HTMLDocument doc = (HTMLDocument) getDocument();
434          HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
435          int count = 0;
436          while (linkIter.isValid())
437            {
438              count++;
439              if (count == i)
440                break;
441              linkIter.next();
442            }
443          if (linkIter.isValid())
444            {
445              int offset = linkIter.getStartOffset();
446              // TODO: I fetch the element for the link via getCharacterElement().
447              // I am not sure that this is correct, maybe we must use
448              // getParagraphElement()?
449              Element el = doc.getCharacterElement(offset);
450              try
451                {
452                  String text = doc.getText(el.getStartOffset(),
453                                          el.getEndOffset() - el.getStartOffset());
454                  return text;
455                }
456              catch (BadLocationException ex)
457                {
458                  throw (AssertionError)
459                    new AssertionError("BadLocationException must not be thrown "
460                                       + "here.")
461                      .initCause(ex);
462                }
463            }
464          else
465            return null;
466        }
467      }
468    
469    private static final long serialVersionUID = 3140472492599046285L;    private static final long serialVersionUID = 3140472492599046285L;
470        
471    private URL page;    private URL page;
# Line 128  public class JEditorPane extends JTextCo Line 518  public class JEditorPane extends JTextCo
518         listeners[index].hyperlinkUpdate(event);         listeners[index].hyperlinkUpdate(event);
519    }    }
520    
521      /**
522       * Returns the accessible context associated with this editor pane.
523       *
524       * @return the accessible context associated with this editor pane
525       */
526    public AccessibleContext getAccessibleContext()    public AccessibleContext getAccessibleContext()
527    {    {
528      return null;      if (accessibleContext == null)
529          {
530            if (getEditorKit() instanceof HTMLEditorKit)
531              accessibleContext = new AccessibleJEditorPaneHTML();
532            else
533              accessibleContext = new AccessibleJEditorPane();
534          }
535        return accessibleContext;
536    }    }
537    
538    public final String getContentType()    public final String getContentType()
# Line 169  public class JEditorPane extends JTextCo Line 571  public class JEditorPane extends JTextCo
571    
572    public boolean getScrollableTracksViewportHeight()    public boolean getScrollableTracksViewportHeight()
573    {    {
574      return false;    /*  Container parent = getParent();
575        return (parent instanceof JViewport &&
576            parent.isValid());*/
577        return isValid();
578    }    }
579    
580    public boolean getScrollableTracksViewportWidth()    public boolean getScrollableTracksViewportWidth()
581    {    {
582      return false;      /*Container parent = getParent();
583        return (parent instanceof JViewport &&
584            parent.isValid());*/
585        return isValid();
586    }    }
587    
588    public URL getPage()    public URL getPage()
# Line 211  public class JEditorPane extends JTextCo Line 619  public class JEditorPane extends JTextCo
619    /**    /**
620     * This method initializes from a stream.     * This method initializes from a stream.
621     */     */
622    public void read(InputStream in, Object desc)    public void read(InputStream in, Object desc) throws IOException
     throws IOException  
623    {    {
624        EditorKit kit = getEditorKit();
625        if (kit instanceof HTMLEditorKit && desc instanceof HTMLDocument)
626          {
627            Document doc = (Document) desc;
628            try
629              {
630                kit.read(in, doc, 0);
631              }
632            catch (BadLocationException ex)
633              {
634                assert false : "BadLocationException must not be thrown here.";
635              }
636          }
637        else
638          {
639            Reader inRead = new InputStreamReader(in);
640            super.read(inRead, desc);
641          }
642    }    }
643    
644    /**    /**
# Line 222  public class JEditorPane extends JTextCo Line 647  public class JEditorPane extends JTextCo
647    public static void registerEditorKitForContentType(String type,    public static void registerEditorKitForContentType(String type,
648                                                       String classname)                                                       String classname)
649    {    {
650        // TODO: Implement this properly.
651    }    }
652    
653    /**    /**
# Line 231  public class JEditorPane extends JTextCo Line 657  public class JEditorPane extends JTextCo
657                                                       String classname,                                                       String classname,
658                                                       ClassLoader loader)                                                       ClassLoader loader)
659    {    {
660        // TODO: Implement this properly.
661    }    }
662    
663    /**    /**
# Line 239  public class JEditorPane extends JTextCo Line 666  public class JEditorPane extends JTextCo
666     */     */
667    public void replaceSelection(String content)    public void replaceSelection(String content)
668    {    {
669        // TODO: Implement this properly.
670    }    }
671    
672    /**    /**
# Line 247  public class JEditorPane extends JTextCo Line 675  public class JEditorPane extends JTextCo
675     */     */
676    public void scrollToReference(String reference)    public void scrollToReference(String reference)
677    {    {
678        // TODO: Implement this properly.
679    }    }
680    
681    public final void setContentType(String type)    public final void setContentType(String type)
# Line 281  public class JEditorPane extends JTextCo Line 710  public class JEditorPane extends JTextCo
710      firePropertyChange("editorKit", oldValue, newValue);      firePropertyChange("editorKit", oldValue, newValue);
711      invalidate();      invalidate();
712      repaint();      repaint();
713        // Reset the accessibleContext since this depends on the editorKit.
714        accessibleContext = null;
715    }    }
716    
717    public void setEditorKitForContentType(String type, EditorKit k)    public void setEditorKitForContentType(String type, EditorKit k)

Legend:
Removed from v.1.12.2.7  
changed lines
  Added in v.1.12.2.8

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26