KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > beans > HTMLTextBean


1 // HTMLParser Library $Name: v1_5_20050313 $ - 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: 2004/01/02 16:24:53 $
10
// $Revision: 1.22 $
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 JavaDoc;
30 import java.awt.FontMetrics JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.io.Serializable JavaDoc;
34 import java.net.URLConnection JavaDoc;
35
36 import javax.swing.JTextArea JavaDoc;
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 extends JTextArea JavaDoc implements Serializable JavaDoc, PropertyChangeListener JavaDoc
44 {
45     /**
46      * The underlying bean that provides our htmlparser specific properties.
47      */

48     protected StringBean mBean;
49
50     /**
51      * Creates a new HTMLTextBean.
52      * This uses an underlying StringBean and displays the text.
53      */

54     public HTMLTextBean ()
55     {
56         getBean ().addPropertyChangeListener (this);
57     }
58
59     /**
60      * Return the minimum dimension for this visible bean.
61      */

62     public Dimension JavaDoc getMinimumSize ()
63     {
64         FontMetrics JavaDoc metrics;
65         int width;
66         int height;
67
68         metrics = getFontMetrics (getFont ());
69         width = metrics.stringWidth ("Hello World");
70         height = metrics.getLeading () + metrics.getHeight () + metrics.getDescent ();
71
72         return (new Dimension JavaDoc (width, height));
73     }
74
75     /**
76      * Add a PropertyChangeListener to the listener list.
77      * The listener is registered for all properties.
78      * <p><em>Delegates to the underlying StringBean</em>
79      * @param listener The PropertyChangeListener to be added.
80      */

81     public void addPropertyChangeListener (PropertyChangeListener JavaDoc listener)
82     {
83         super.addPropertyChangeListener (listener);
84         getBean ().addPropertyChangeListener (listener);
85     }
86
87     /**
88      * Remove a PropertyChangeListener from the listener list.
89      * This removes a PropertyChangeListener that was registered for all properties.
90      * <p><em>Delegates to the underlying StringBean</em>
91      * @param listener The PropertyChangeListener to be removed.
92      */

93     public void removePropertyChangeListener (PropertyChangeListener JavaDoc listener)
94     {
95         super.addPropertyChangeListener (listener);
96         getBean ().removePropertyChangeListener (listener);
97     }
98
99     //
100
// Properties
101
//
102

103     /**
104      * Return the underlying bean object.
105      * Creates a new one if it hasn't been initialized yet.
106      * @return The StringBean this bean uses to fetch text.
107      */

108     public StringBean getBean ()
109     {
110         if (null == mBean)
111             mBean = new StringBean ();
112
113         return (mBean);
114     }
115
116     /**
117      * Getter for property strings.
118      * <p><em>Delegates to the underlying StringBean</em>
119      * @return Value of property strings.
120      */

121     public String JavaDoc getStrings ()
122     {
123         return (getBean ().getStrings ());
124     }
125
126     /**
127      * Getter for property links.
128      * <p><em>Delegates to the underlying StringBean</em>
129      * @return Value of property links.
130      */

131     public boolean getLinks ()
132     {
133         return (getBean ().getLinks ());
134     }
135
136     /**
137      * Setter for property links.
138      * <p><em>Delegates to the underlying StringBean</em>
139      * @param links New value of property links.
140      */

141     public void setLinks (boolean links)
142     {
143         getBean ().setLinks (links);
144     }
145
146     /**
147      * Getter for property URL.
148      * <p><em>Delegates to the underlying StringBean</em>
149      * @return Value of property URL.
150      */

151     public String JavaDoc getURL ()
152     {
153         return (getBean ().getURL ());
154     }
155
156     /**
157      * Setter for property URL.
158      * <p><em>Delegates to the underlying StringBean</em>
159      * @param url New value of property URL.
160      */

161     public void setURL (String JavaDoc url)
162     {
163         getBean ().setURL (url);
164     }
165
166     /**
167      * Get the current 'replace non breaking spaces' state.
168      * @return The <code>true</code> if non-breaking spaces (character ' ',
169      * numeric character reference &160; or character entity reference &nbsp;)
170      * are to be replaced with normal spaces (character ' ').
171      */

172     public boolean getReplaceNonBreakingSpaces ()
173     {
174         return (getBean ().getReplaceNonBreakingSpaces ());
175     }
176
177     /**
178      * Set the 'replace non breaking spaces' state.
179      * @param replace_space <code>true</code> if non-breaking spaces (character ' ',
180      * numeric character reference &160; or character entity reference &nbsp;)
181      * are to be replaced with normal spaces (character ' ').
182      */

183     public void setReplaceNonBreakingSpaces (boolean replace_space)
184     {
185         getBean ().setReplaceNonBreakingSpaces (replace_space);
186     }
187
188     /**
189      * Get the current 'collapse whitespace' state.
190      * If set to <code>true</code> this emulates the operation of browsers
191      * in interpretting text where auser agents should collapse input white
192      * space sequences when producing output inter-word space.
193      * See HTML specification section 9.1 White space
194      * http://www.w3.org/TR/html4/struct/text.html#h-9.1
195      * @return <code>true</code> if sequences of whitespace (space ' ',
196      * tab ' ', form feed ' ', zero-width space '?',
197      * carriage-return '\r' and newline '\n') are to be replaced with a single
198      * space.
199      */

200     public boolean getCollapse ()
201     {
202         return (getBean ().getCollapse ());
203     }
204
205     /**
206      * Set the current 'collapse whitespace' state.
207      * @param collapse_whitespace If <code>true</code>, sequences of whitespace
208      * will be reduced to a single space.
209      */

210     public void setCollapse (boolean collapse_whitespace)
211     {
212         getBean ().setCollapse (collapse_whitespace);
213     }
214
215     /**
216      * Getter for property Connection.
217      * @return Value of property Connection.
218      */

219     public URLConnection JavaDoc getConnection ()
220     {
221         return (getBean ().getConnection ());
222     }
223
224     /**
225      * Setter for property Connection.
226      * @param connection New value of property Connection.
227      */

228     public void setConnection (URLConnection JavaDoc connection)
229     {
230         getBean ().setConnection (connection);
231     }
232
233     //
234
// PropertyChangeListener inteface
235
//
236

237     /**
238      * Responds to changes in the underlying bean's properties.
239      * @param event The event triggering this listener method call.
240      */

241     public void propertyChange (PropertyChangeEvent JavaDoc event)
242     {
243         if (event.getPropertyName ().equals (StringBean.PROP_STRINGS_PROPERTY))
244         {
245             setText (getBean ().getStrings ());
246             setCaretPosition (0);
247         }
248     }
249
250 // /**
251
// * Unit test.
252
// */
253
// public static void main (String[] args)
254
// {
255
// HTMLTextBean tb = new HTMLTextBean ();
256
// tb.setURL ("http://cbc.ca");
257
// // tb.setLinks (true);
258
// javax.swing.JFrame frame = new javax.swing.JFrame ();
259
// frame.getContentPane ().setLayout (new BorderLayout ());
260
// frame.getContentPane ().add (new JScrollPane (tb), BorderLayout.CENTER);
261
// frame.addWindowListener (new java.awt.event.WindowListener () {
262
// public void windowOpened (java.awt.event.WindowEvent e) {}
263
// public void windowClosing (java.awt.event.WindowEvent e) {System.exit (0);}
264
// public void windowClosed (java.awt.event.WindowEvent e) {}
265
// public void windowDeiconified (java.awt.event.WindowEvent e) {}
266
// public void windowIconified (java.awt.event.WindowEvent e) {}
267
// public void windowActivated (java.awt.event.WindowEvent e) {}
268
// public void windowDeactivated (java.awt.event.WindowEvent e) {}
269
// });
270
// frame.setBounds (100, 100, 640, 480);
271
// frame.show ();
272
// }
273
}
274
Popular Tags