/ org.htmlparser / src / org / htmlparser / beans / HTMLLinkBean.java
HTMLLinkBean.java
  1  // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML
  2  // http://sourceforge.org/projects/htmlparser
  3  // Copyright (C) 2004 Derrick Oswald
  4  //
  5  // Revision Control Information
  6  //
  7  // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/beans/HTMLLinkBean.java,v $
  8  // $Author: derrickoswald $
  9  // $Date: 2005/05/15 11:49:03 $
 10  // $Revision: 1.23 $
 11  //
 12  // This library is free software; you can redistribute it and/or
 13  // modify it under the terms of the GNU Lesser General Public
 14  // License as published by the Free Software Foundation; either
 15  // version 2.1 of the License, or (at your option) any later version.
 16  //
 17  // This library is distributed in the hope that it will be useful,
 18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 20  // Lesser General Public License for more details.
 21  //
 22  // You should have received a copy of the GNU Lesser General Public
 23  // License along with this library; if not, write to the Free Software
 24  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 25  //
 26  
 27  package org.htmlparser.beans;
 28  
 29  import java.awt.Dimension;
 30  import java.awt.FontMetrics;
 31  import java.beans.PropertyChangeEvent;
 32  import java.beans.PropertyChangeListener;
 33  import java.io.Serializable;
 34  import java.net.URL;
 35  import java.net.URLConnection;
 36  
 37  import javax.swing.JList;
 38  
 39  /**
 40   * Display the links from a URL.
 41   * @author Derrick Oswald
 42   * Created on December 24, 2002, 3:49 PM
 43   */
 44  public class HTMLLinkBean
 45      extends
 46          JList
 47      implements
 48          Serializable,
 49          PropertyChangeListener
 50  {
 51      /**
 52       * The underlying bean that provides our htmlparser specific properties.
 53       */
 54      protected LinkBean mBean;
 55  
 56      /**
 57       * Creates a new HTMLTextBean.
 58       * This uses an underlying StringBean and displays the text.
 59       */
 60      public HTMLLinkBean ()
 61      {
 62          getBean ().addPropertyChangeListener (this);
 63      }
 64  
 65      /**
 66       * Return the underlying bean object.
 67       * Creates a new one if it hasn't been initialized yet.
 68       * @return The StringBean this bean uses to fetch text.
 69       */
 70      protected LinkBean getBean ()
 71      {
 72          if (null == mBean)
 73              mBean = new LinkBean ();
 74  
 75          return (mBean);
 76      }
 77  
 78      /**
 79       * Return the minimum dimension for this visible bean.
 80       * @return a minimum bounding box for this bean.
 81       */
 82      public Dimension getMinimumSize ()
 83      {
 84          FontMetrics met;
 85          int width;
 86          int height;
 87  
 88          met = getFontMetrics (getFont ());
 89          width = met.stringWidth ("http://localhost");
 90          height = met.getLeading () + met.getHeight () + met.getDescent ();
 91  
 92          return (new Dimension (width, height));
 93      }
 94  
 95      /**
 96       * Add a PropertyChangeListener to the listener list.
 97       * The listener is registered for all properties.
 98       * <p><em>Delegates to the underlying StringBean</em>
 99       * @param listener The PropertyChangeListener to be added.
100       */
101      public void addPropertyChangeListener (PropertyChangeListener listener)
102      {
103          super.addPropertyChangeListener (listener);
104          getBean ().addPropertyChangeListener (listener);
105      }
106  
107      /**
108       * Remove a PropertyChangeListener from the listener list.
109       * This removes a registered PropertyChangeListener.
110       * <p><em>Delegates to the underlying StringBean</em>
111       * @param listener The PropertyChangeListener to be removed.
112       */
113      public void removePropertyChangeListener (PropertyChangeListener listener)
114      {
115          super.addPropertyChangeListener (listener);
116          getBean ().removePropertyChangeListener (listener);
117      }
118  
119      //
120      // Properties
121      //
122  
123      /**
124       * Getter for property links.
125       * <p><em>Delegates to the underlying StringBean</em>
126       * @return Value of property links.
127       */
128      public URL[] getLinks ()
129      {
130          return (getBean ().getLinks ());
131      }
132  
133      /**
134       * Getter for property URL.
135       * <p><em>Delegates to the underlying StringBean</em>
136       * @return Value of property URL.
137       */
138      public String getURL ()
139      {
140          return (getBean ().getURL ());
141      }
142  
143      /**
144       * Setter for property URL.
145       * <p><em>Delegates to the underlying StringBean</em>
146       * @param url New value of property URL.
147       */
148      public void setURL (String url)
149      {
150          getBean ().setURL (url);
151      }
152  
153      /**
154       * Getter for property Connection.
155       * @return Value of property Connection.
156       */
157      public URLConnection getConnection ()
158      {
159          return (getBean ().getConnection ());
160      }
161  
162      /**
163       * Setter for property Connection.
164       * @param connection New value of property Connection.
165       */
166      public void setConnection (URLConnection connection)
167      {
168          getBean ().setConnection (connection);
169      }
170  
171      //
172      // PropertyChangeListener inteface
173      //
174  
175      /**
176       * Responds to changes in the underlying bean's properties.
177       * @param event The event triggering this listener method call.
178       */
179      public void propertyChange (PropertyChangeEvent event)
180      {
181          if (event.getPropertyName ().equals (LinkBean.PROP_LINKS_PROPERTY))
182          {
183              setListData (getBean ().getLinks ());
184          }
185      }
186  
187  //    /**
188  //     * Unit test.
189  //     */
190  //    public static void main (String[] args)
191  //    {
192  //        HTMLLinkBean lb = new HTMLLinkBean ();
193  //        lb.setURL ("http://cbc.ca");
194  //        javax.swing.JFrame frame = new javax.swing.JFrame ();
195  //        frame.getContentPane ().setLayout (new BorderLayout ());
196  //        frame.getContentPane ().add (new JScrollPane (lb),
197  //            BorderLayout.CENTER);
198  //        frame.addWindowListener (new java.awt.event.WindowListener () {
199  //            public void windowOpened (java.awt.event.WindowEvent e) {}
200  //            public void windowClosing (java.awt.event.WindowEvent e)
201  //            {
202  //                System.exit (0);
203  //            }
204  //            public void windowClosed (java.awt.event.WindowEvent e) {}
205  //            public void windowDeiconified (java.awt.event.WindowEvent e) {}
206  //            public void windowIconified (java.awt.event.WindowEvent e) {}
207  //            public void windowActivated (java.awt.event.WindowEvent e) {}
208  //            public void windowDeactivated (java.awt.event.WindowEvent e) {}
209  //        });
210  //        frame.setBounds (100, 100, 640, 480);
211  //        frame.show ();
212  //    }
213  }
214  
215  
216