EditorPaneLinkVisitor.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: EditorPaneLinkVisitor.java,v 1.1 2005/10/12 08:48:28 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.io.IOException;
  25. import java.net.URL;
  26. import javax.swing.SwingUtilities;
  27. import javax.swing.event.HyperlinkEvent;
  28. import javax.swing.event.HyperlinkListener;
  29. import javax.swing.text.Document;
  30. /**
  31.  * A ActionListener using a JXEditorPane to "visit" a LinkModel.
  32.  * 
  33.  * adds an internal HyperlinkListener to visit links contained
  34.  * in the document. 
  35.  * 
  36.  * @author Jeanette Winzenburg
  37.  */
  38. public class EditorPaneLinkVisitor implements ActionListener {
  39.     private JXEditorPane editorPane;
  40.     private HyperlinkListener hyperlinkListener;
  41.     private LinkModel internalLink;
  42.     
  43.     public EditorPaneLinkVisitor() {
  44.         this(null);
  45.     }
  46.     
  47.     public EditorPaneLinkVisitor(JXEditorPane pane) {
  48.         if (editorPane != null) {
  49.             editorPane.removeHyperlinkListener(getHyperlinkListener());
  50.         }
  51.         if (pane == null) {
  52.             pane = createDefaultEditorPane();
  53.         }
  54.         this.editorPane = pane;
  55.         pane.addHyperlinkListener(getHyperlinkListener());
  56.     }
  57.     
  58.     public JXEditorPane getOutputComponent() {
  59.         return editorPane;
  60.     }
  61.     
  62.     public void actionPerformed(ActionEvent e) {
  63.         if (e.getSource() instanceof LinkModel) {
  64.             final LinkModel link = (LinkModel) e.getSource();
  65.             SwingUtilities.invokeLater(new Runnable() {
  66.                 public void run() {
  67.                     visit(link);
  68.                 }
  69.             });
  70.         }
  71.    
  72.     }
  73.     public void visit(LinkModel link) {
  74.         try {
  75.             // make sure to reload
  76.             editorPane.getDocument().putProperty(Document.StreamDescriptionProperty, null);
  77.             // JW: editorPane defaults to asynchronous loading
  78.             // no need to explicitly start a thread - really?
  79.             editorPane.setPage(link.getURL());
  80.             link.setVisited(true);
  81.         } catch (IOException e1) {
  82.             editorPane.setText("<html>Error 404: couldn't show " + link.getURL() + " </html>");
  83.         }
  84.     }
  85.     protected JXEditorPane createDefaultEditorPane() {
  86.         final JXEditorPane editorPane = new JXEditorPane();
  87.         editorPane.setEditable(false);
  88.         editorPane.setContentType("text/html");
  89.         return editorPane;
  90.     }
  91.     protected HyperlinkListener getHyperlinkListener() {
  92.         if (hyperlinkListener == null) {
  93.             hyperlinkListener = createHyperlinkListener();
  94.         }
  95.         return hyperlinkListener;
  96.     }
  97.     protected HyperlinkListener createHyperlinkListener() {
  98.         HyperlinkListener l = new HyperlinkListener() {
  99.             public void hyperlinkUpdate(HyperlinkEvent e) {
  100.                 if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
  101.                     visitInternal(e.getURL());
  102.                 }
  103.                 
  104.             }
  105.             
  106.         };
  107.         return l;
  108.     }
  109.     protected LinkModel getInternalLink() {
  110.         if (internalLink == null) {
  111.             internalLink = new LinkModel("internal");
  112.         }
  113.         return internalLink;
  114.     }
  115.     protected void visitInternal(URL url) {
  116.         try {
  117.             getInternalLink().setURL(url);
  118.             visit(getInternalLink());
  119.         } catch (Exception e) {
  120.             // todo: error feedback
  121.         }
  122.     }
  123. }