KickJava   Java API By Example, From Geeks To Geeks.

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


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/BeanyBaby.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/03/12 21:39:45 $
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 javax.swing.JCheckBoxMenuItem JavaDoc;
30 import javax.swing.JMenuItem JavaDoc;
31
32 /**
33  * Demo of beans.
34  * Created on December 30, 2002, 7:54 PM
35  * @author Derrick Oswald
36  */

37 public class BeanyBaby
38     extends
39         javax.swing.JFrame JavaDoc
40     implements
41         java.beans.PropertyChangeListener JavaDoc,
42         java.awt.event.ActionListener JavaDoc,
43         java.awt.event.MouseListener JavaDoc
44 {
45     /**
46      * Bread crumb trail of visited URLs.
47      */

48     java.util.Vector JavaDoc mTrail;
49
50     /**
51      * Current position on the bread crumb trail.
52      */

53     int mCrumb;
54
55     /** Creates new form BeanyBaby */
56     public BeanyBaby ()
57     {
58         initComponents ();
59         mTrail = new java.util.Vector JavaDoc (25);
60         mCrumb = -1;
61
62         // shenanigans to get the splitter bar at the midpoint
63
setVisible (true);
64         mSplitPane.setDividerLocation(0.5);
65         setVisible (false);
66
67         // set up twinning
68
mLinkBean.addPropertyChangeListener (this);
69         mStringBean.addPropertyChangeListener (this);
70         // set up user input
71
mTextField.addActionListener (this);
72         mLinkBean.addMouseListener (this);
73
74         // set initial checkbox states
75
mLinks.setSelected (mStringBean.getLinks ());
76         mCollapse.setSelected (mStringBean.getCollapse ());
77         mNobreak.setSelected (mStringBean.getReplaceNonBreakingSpaces ());
78     }
79
80     //
81
// PropertyChangeListener interface
82
//
83

84     /**
85      * This method ties the two beans together on the same connection.
86      * Whenever a property changes on one bean, make sure the URL properties
87      * agree by setting the URL from one to the other.
88      * @param event The event describing the event source
89      * and the property that has changed.
90      */

91     public void propertyChange (java.beans.PropertyChangeEvent JavaDoc event)
92     {
93         Object JavaDoc source;
94         String JavaDoc name;
95
96         source = event.getSource ();
97         if (source == mLinkBean)
98         {
99             if (!mLinkBean.getURL ().equals (mStringBean.getURL ()))
100                 mStringBean.setURL (mLinkBean.getURL ());
101         }
102         else if (source == mStringBean)
103         {
104             if (!mStringBean.getURL ().equals (mLinkBean.getURL ()))
105                 mLinkBean.setURL (mStringBean.getURL ());
106             // check for menu status changes
107
name = event.getPropertyName ();
108             if (name.equals (StringBean.PROP_LINKS_PROPERTY))
109                 mLinks.setSelected (((Boolean JavaDoc)event.getNewValue ()).booleanValue ());
110             else if (name.equals (StringBean.PROP_COLLAPSE_PROPERTY))
111                 mCollapse.setSelected (((Boolean JavaDoc)event.getNewValue ()).booleanValue ());
112             else if (name.equals (StringBean.PROP_REPLACE_SPACE_PROPERTY))
113                 mNobreak.setSelected (((Boolean JavaDoc)event.getNewValue ()).booleanValue ());
114         }
115     }
116
117     //
118
// ActionListener interface
119
//
120

121     public void actionPerformed (java.awt.event.ActionEvent JavaDoc event)
122     {
123         Object JavaDoc source;
124         String JavaDoc url;
125         String JavaDoc name;
126         JMenuItem JavaDoc item;
127
128         source = event.getSource ();
129         if (source == mTextField)
130         {
131             url = mTextField.getText ();
132             mTextField.selectAll ();
133             setURL (url);
134         }
135         else if (source instanceof JCheckBoxMenuItem JavaDoc)
136         {
137             item = (JMenuItem JavaDoc)source;
138             name = item.getName ();
139             if ("Links".equals (name))
140                 mStringBean.setLinks (item.isSelected ());
141             else if ("Collapse".equals (name))
142                 mStringBean.setCollapse (item.isSelected ());
143             else if ("Nobreak".equals (name))
144                 mStringBean.setReplaceNonBreakingSpaces (item.isSelected ());
145         }
146         else if (source instanceof JMenuItem JavaDoc)
147         {
148             name = ((JMenuItem JavaDoc)source).getName ();
149             if ("Back".equals (name))
150             {
151                 if (mCrumb > 0)
152                 {
153                     mCrumb--;
154                     url = (String JavaDoc)mTrail.elementAt (mCrumb);
155                     mCrumb--;
156                     setURL (url);
157                 }
158             }
159             else if ("Forward".equals (name))
160             {
161                 if (mCrumb < mTrail.size ())
162                 {
163                     mCrumb++;
164                     url = (String JavaDoc)mTrail.elementAt (mCrumb);
165                     mCrumb--;
166                     setURL (url);
167                 }
168             }
169         }
170
171     }
172
173     //
174
// MouseListener interface
175
//
176
/**
177      * Invoked when the mouse button has been clicked (pressed and released) on a component.
178      */

179     public void mouseClicked (java.awt.event.MouseEvent JavaDoc event)
180     {
181         int index;
182         String JavaDoc url;
183
184         if (2 == event.getClickCount())
185         {
186             index = mLinkBean.locationToIndex (event.getPoint ());
187             url = mLinkBean.getModel ().getElementAt (index).toString ();
188             setURL (url);
189         }
190     }
191
192     /**
193      * Invoked when the mouse enters a component.
194      */

195     public void mouseEntered (java.awt.event.MouseEvent JavaDoc event)
196     {
197     }
198
199     /**
200      * Invoked when the mouse exits a component.
201      */

202     public void mouseExited (java.awt.event.MouseEvent JavaDoc event)
203     {
204     }
205
206     /**
207      * Invoked when a mouse button has been pressed on a component.
208      */

209     public void mousePressed (java.awt.event.MouseEvent JavaDoc event)
210     {
211     }
212
213     /**
214      * Invoked when a mouse button has been released on a component.
215      */

216     public void mouseReleased (java.awt.event.MouseEvent JavaDoc event)
217     {
218     }
219
220     //
221
// API control
222
//
223
public void setURL (String JavaDoc url)
224     {
225         mTextField.setText (url);
226         mCrumb++;
227         if (mTrail.size () <= mCrumb)
228             mTrail.addElement (url);
229         else
230             mTrail.setElementAt (url, mCrumb);
231         mLinkBean.setURL (url);
232
233         // update navigation menu
234
mBack.setEnabled (mCrumb > 0);
235         mForward.setEnabled (mCrumb + 1 < mTrail.size ());
236     }
237
238     /** This method is called from within the constructor to
239      * initialize the form.
240      * WARNING: Do NOT modify this code. The content of this method is
241      * always regenerated by the Form Editor.
242      */

243     private void initComponents()//GEN-BEGIN:initComponents
244
{
245         javax.swing.JMenuBar JavaDoc menubar;
246         javax.swing.JScrollPane JavaDoc pane1;
247         javax.swing.JScrollPane JavaDoc pane2;
248         javax.swing.JMenu JavaDoc go;
249         javax.swing.JMenu JavaDoc options;
250         javax.swing.JPanel JavaDoc panel;
251
252         menubar = new javax.swing.JMenuBar JavaDoc();
253         setJMenuBar (menubar);
254         go = new javax.swing.JMenu JavaDoc();
255         mBack = new javax.swing.JMenuItem JavaDoc();
256         mForward = new javax.swing.JMenuItem JavaDoc();
257         options = new javax.swing.JMenu JavaDoc();
258         mLinks = new javax.swing.JCheckBoxMenuItem JavaDoc();
259         mCollapse = new javax.swing.JCheckBoxMenuItem JavaDoc();
260         mNobreak = new javax.swing.JCheckBoxMenuItem JavaDoc();
261         panel = new javax.swing.JPanel JavaDoc();
262         mSplitPane = new javax.swing.JSplitPane JavaDoc();
263         pane1 = new javax.swing.JScrollPane JavaDoc();
264         mLinkBean = new org.htmlparser.beans.HTMLLinkBean();
265         pane2 = new javax.swing.JScrollPane JavaDoc();
266         mStringBean = new org.htmlparser.beans.HTMLTextBean();
267         mTextField = new javax.swing.JTextField JavaDoc();
268
269         go.setMnemonic('G');
270         go.setText("Go");
271         go.setToolTipText("crude URL navigation");
272         mBack.setMnemonic('B');
273         mBack.setText("Back");
274         mBack.setToolTipText("back one URL");
275         mBack.setName("Back");
276         mBack.addActionListener (this);
277         go.add(mBack);
278
279         mForward.setMnemonic('F');
280         mForward.setText("Forward");
281         mForward.setToolTipText("forward one URL");
282         mForward.setName("Forward");
283         mForward.addActionListener (this);
284         go.add(mForward);
285
286         menubar.add(go);
287
288         options.setMnemonic('O');
289         options.setText("Options");
290         options.setToolTipText("Bean settings");
291         mLinks.setMnemonic('L');
292         mLinks.setText("Links");
293         mLinks.setToolTipText("show/hide links in text");
294         mLinks.setName("Links");
295         mLinks.addActionListener (this);
296         options.add(mLinks);
297
298         mCollapse.setMnemonic('C');
299         mCollapse.setText("Collapse");
300         mCollapse.setToolTipText("collapse/retain whitespace sequences");
301         mCollapse.setName("Collapse");
302         mCollapse.addActionListener (this);
303         options.add(mCollapse);
304
305         mNobreak.setMnemonic('N');
306         mNobreak.setText("Non-breaking Spaces");
307         mNobreak.setToolTipText("replace/retain non-breaking spaces");
308         mNobreak.setName("Nobreak");
309         mNobreak.addActionListener (this);
310         options.add(mNobreak);
311
312         menubar.add(options);
313
314         setTitle("BeanyBaby");
315         addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
316         {
317             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt)
318             {
319                 exitForm(evt);
320             }
321         });
322
323         panel.setLayout(new java.awt.BorderLayout JavaDoc());
324
325         pane1.setViewportView(mLinkBean);
326
327         mSplitPane.setLeftComponent(pane1);
328
329         pane2.setViewportView(mStringBean);
330
331         mSplitPane.setRightComponent(pane2);
332
333         panel.add(mSplitPane, java.awt.BorderLayout.CENTER);
334
335         mTextField.setToolTipText("Enter the URL to view");
336         panel.add(mTextField, java.awt.BorderLayout.SOUTH);
337
338         getContentPane().add(panel, java.awt.BorderLayout.CENTER);
339
340         pack();
341         java.awt.Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
342         setSize(new java.awt.Dimension JavaDoc(640, 480));
343         setLocation((screenSize.width-640)/2,(screenSize.height-480)/2);
344     }//GEN-END:initComponents
345

346     /** Exit the Application */
347     private void exitForm (java.awt.event.WindowEvent JavaDoc evt)
348     {//GEN-FIRST:event_exitForm
349
System.exit (0);
350     }//GEN-LAST:event_exitForm
351

352     // Variables declaration - do not modify//GEN-BEGIN:variables
353
private org.htmlparser.beans.HTMLLinkBean mLinkBean;
354     private javax.swing.JMenuItem JavaDoc mForward;
355     private javax.swing.JMenuItem JavaDoc mBack;
356     private javax.swing.JCheckBoxMenuItem JavaDoc mCollapse;
357     private javax.swing.JTextField JavaDoc mTextField;
358     private javax.swing.JSplitPane JavaDoc mSplitPane;
359     private javax.swing.JCheckBoxMenuItem JavaDoc mLinks;
360     private org.htmlparser.beans.HTMLTextBean mStringBean;
361     private javax.swing.JCheckBoxMenuItem JavaDoc mNobreak;
362     // End of variables declaration//GEN-END:variables
363

364     /**
365      * Unit test.
366      */

367     public static void main (String JavaDoc[] args)
368     {
369         BeanyBaby bb = new BeanyBaby ();
370         bb.setVisible (true);
371         if (0 >= args.length)
372             bb.setURL ("http://www.slashdot.org");
373         else
374             bb.setURL (args[0]);
375     }
376 }
377
Popular Tags