/ org.htmlparser / src / org / htmlparser / beans / HTMLTextBean.java
HTMLTextBean.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/HTMLTextBean.java,v $
  8  // $Author: derrickoswald $
  9  // $Date: 2005/05/15 11:49:03 $
 10  // $Revision: 1.24 $
 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.URLConnection;
 35  
 36  import javax.swing.JTextArea;
 37  
 38  /**
 39   * Display the textual URL contents.
 40   * @author Derrick Oswald
 41   * Created on December 24, 2002, 3:49 PM
 42   */
 43  public class HTMLTextBean
 44      extends
 45          JTextArea
 46      implements
 47          Serializable,
 48          PropertyChangeListener
 49  {
 50      /**
 51       * The underlying bean that provides our htmlparser specific properties.
 52       */
 53      protected StringBean mBean;
 54  
 55      /**
 56       * Creates a new HTMLTextBean.
 57       * This uses an underlying StringBean and displays the text.
 58       */
 59      public HTMLTextBean ()
 60      {
 61          getBean ().addPropertyChangeListener (this);
 62      }
 63  
 64      /**
 65       * Return the minimum dimension for this visible bean.
 66       * @return a minimum bounding box for this bean.
 67       */
 68      public Dimension getMinimumSize ()
 69      {
 70          FontMetrics met;
 71          int width;
 72          int height;
 73  
 74          met = getFontMetrics (getFont ());
 75          width = met.stringWidth ("Hello World");
 76          height = met.getLeading () + met.getHeight () + met.getDescent ();
 77  
 78          return (new Dimension (width, height));
 79      }
 80  
 81      /**
 82       * Add a PropertyChangeListener to the listener list.
 83       * The listener is registered for all properties.
 84       * <p><em>Delegates to the underlying StringBean</em>
 85       * @param listener The PropertyChangeListener to be added.
 86       */
 87      public void addPropertyChangeListener (PropertyChangeListener listener)
 88      {
 89          super.addPropertyChangeListener (listener);
 90          getBean ().addPropertyChangeListener (listener);
 91      }
 92  
 93      /**
 94       * Remove a PropertyChangeListener from the listener list.
 95       * This removes a registered PropertyChangeListener.
 96       * <p><em>Delegates to the underlying StringBean</em>
 97       * @param listener The PropertyChangeListener to be removed.
 98       */
 99      public void removePropertyChangeListener (PropertyChangeListener listener)
100      {
101          super.addPropertyChangeListener (listener);
102          getBean ().removePropertyChangeListener (listener);
103      }
104  
105      //
106      // Properties
107      //
108  
109      /**
110       * Return the underlying bean object.
111       * Creates a new one if it hasn't been initialized yet.
112       * @return The StringBean this bean uses to fetch text.
113       */
114      public StringBean getBean ()
115      {
116          if (null == mBean)
117              mBean = new StringBean ();
118  
119          return (mBean);
120      }
121  
122      /**
123       * Getter for property strings.
124       * <p><em>Delegates to the underlying StringBean</em>
125       * @return Value of property strings.
126       */
127      public String getStrings ()
128      {
129          return (getBean ().getStrings ());
130      }
131  
132      /**
133       * Getter for property links.
134       * <p><em>Delegates to the underlying StringBean</em>
135       * @return Value of property links.
136       */
137      public boolean getLinks ()
138      {
139          return (getBean ().getLinks ());
140      }
141  
142      /**
143       * Setter for property links.
144       * <p><em>Delegates to the underlying StringBean</em>
145       * @param links New value of property links.
146       */
147      public void setLinks (boolean links)
148      {
149          getBean ().setLinks (links);
150      }
151  
152      /**
153       * Getter for property URL.
154       * <p><em>Delegates to the underlying StringBean</em>
155       * @return Value of property URL.
156       */
157      public String getURL ()
158      {
159          return (getBean ().getURL ());
160      }
161  
162      /**
163       * Setter for property URL.
164       * <p><em>Delegates to the underlying StringBean</em>
165       * @param url New value of property URL.
166       */
167      public void setURL (String url)
168      {
169          getBean ().setURL (url);
170      }
171  
172      /**
173       * Get the current 'replace non breaking spaces' state.
174       * @return The <code>true</code> if non-breaking spaces (character '\u00a0',
175       * numeric character reference &160; or character entity reference &nbsp;)
176       * are to be replaced with normal spaces (character '\u0020').
177       */
178      public boolean getReplaceNonBreakingSpaces ()
179      {
180          return (getBean ().getReplaceNonBreakingSpaces ());
181      }
182  
183      /**
184       * Set the 'replace non breaking spaces' state.
185       * @param replace <code>true</code> if non-breaking spaces (character '\u00a0',
186       * numeric character reference &160; or character entity reference &nbsp;)
187       * are to be replaced with normal spaces (character '\u0020').
188       */
189      public void setReplaceNonBreakingSpaces (boolean replace)
190      {
191          getBean ().setReplaceNonBreakingSpaces (replace);
192      }
193  
194      /**
195       * Get the current 'collapse whitespace' state.
196       * If set to <code>true</code> this emulates the operation of browsers
197       * in interpretting text where auser agents should collapse input white
198       * space sequences when producing output inter-word space.
199       * See HTML specification section 9.1 White space
200       * http://www.w3.org/TR/html4/struct/text.html#h-9.1
201       * @return <code>true</code> if sequences of whitespace (space '\u0020',
202       * tab '\u0009', form feed '\u000C', zero-width space '\u200B',
203       * carriage-return '\r' and newline '\n') are to be replaced with a single
204       * space.
205       */
206      public boolean getCollapse ()
207      {
208          return (getBean ().getCollapse ());
209      }
210  
211      /**
212       * Set the current 'collapse whitespace' state.
213       * @param collapse If <code>true</code>, sequences of whitespace
214       * will be reduced to a single space.
215       */
216      public void setCollapse (boolean collapse)
217      {
218          getBean ().setCollapse (collapse);
219      }
220  
221      /**
222       * Getter for property Connection.
223       * @return Value of property Connection.
224       */
225      public URLConnection getConnection ()
226      {
227          return (getBean ().getConnection ());
228      }
229  
230      /**
231       * Setter for property Connection.
232       * @param connection New value of property Connection.
233       */
234      public void setConnection (URLConnection connection)
235      {
236          getBean ().setConnection (connection);
237      }
238  
239      //
240      // PropertyChangeListener inteface
241      //
242  
243      /**
244       * Responds to changes in the underlying bean's properties.
245       * @param event The event triggering this listener method call.
246       */
247      public void propertyChange (PropertyChangeEvent event)
248      {
249          if (event.getPropertyName ().equals (StringBean.PROP_STRINGS_PROPERTY))
250          {
251              setText (getBean ().getStrings ());
252              setCaretPosition (0);
253          }
254      }
255  
256  //    /**
257  //     * Unit test.
258  //     */
259  //    public static void main (String[] args)
260  //    {
261  //        HTMLTextBean tb = new HTMLTextBean ();
262  //        tb.setURL ("http://cbc.ca");
263  //        // tb.setLinks (true);
264  //        javax.swing.JFrame frame = new javax.swing.JFrame ();
265  //        frame.getContentPane ().setLayout (new BorderLayout ());
266  //        frame.getContentPane ().add (new JScrollPane (tb),
267  //            BorderLayout.CENTER);
268  //        frame.addWindowListener (new java.awt.event.WindowListener () {
269  //            public void windowOpened (java.awt.event.WindowEvent e) {}
270  //            public void windowClosing (java.awt.event.WindowEvent e)
271  //            {
272  //                System.exit (0);
273  //            }
274  //            public void windowClosed (java.awt.event.WindowEvent e) {}
275  //            public void windowDeiconified (java.awt.event.WindowEvent e) {}
276  //            public void windowIconified (java.awt.event.WindowEvent e) {}
277  //            public void windowActivated (java.awt.event.WindowEvent e) {}
278  //            public void windowDeactivated (java.awt.event.WindowEvent e) {}
279  //        });
280  //        frame.setBounds (100, 100, 640, 480);
281  //        frame.show ();
282  //    }
283  }