/ org.htmlparser / src / org / htmlparser / beans / BeanyBaby.java
BeanyBaby.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/BeanyBaby.java,v $
  8  // $Author: derrickoswald $
  9  // $Date: 2005/05/15 11:49:03 $
 10  // $Revision: 1.25 $
 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.BorderLayout;
 30  import java.awt.Dimension;
 31  import java.awt.Toolkit;
 32  import java.awt.event.ActionEvent;
 33  import java.awt.event.ActionListener;
 34  import java.awt.event.MouseEvent;
 35  import java.awt.event.MouseListener;
 36  import java.awt.event.WindowAdapter;
 37  import java.awt.event.WindowEvent;
 38  import java.beans.PropertyChangeListener;
 39  import java.util.Vector;
 40  
 41  import javax.swing.JCheckBoxMenuItem;
 42  import javax.swing.JMenuItem;
 43  
 44  /**
 45   * Demo of beans.
 46   * Created on December 30, 2002, 7:54 PM
 47   * @author Derrick Oswald
 48   */
 49  public class BeanyBaby
 50      extends
 51          javax.swing.JFrame
 52      implements
 53          PropertyChangeListener,
 54          ActionListener,
 55          MouseListener
 56  {
 57      /**
 58       * Bread crumb trail of visited URLs.
 59       */
 60      protected Vector mTrail;
 61  
 62      /**
 63       * Current position on the bread crumb trail.
 64       */
 65      protected int mCrumb;
 66  
 67      /** Creates new form BeanyBaby */
 68      public BeanyBaby ()
 69      {
 70          initComponents ();
 71          mTrail = new Vector ();
 72          mCrumb = -1;
 73  
 74          // shenanigans to get the splitter bar at the midpoint
 75          setVisible (true);
 76          mSplitPane.setDividerLocation (0.5); // 50% for each pane
 77          setVisible (false);
 78  
 79          // set up twinning
 80          mLinkBean.addPropertyChangeListener (this);
 81          mStringBean.addPropertyChangeListener (this);
 82          // set up user input
 83          mTextField.addActionListener (this);
 84          mLinkBean.addMouseListener (this);
 85  
 86          // set initial checkbox states
 87          mLinks.setSelected (mStringBean.getLinks ());
 88          mCollapse.setSelected (mStringBean.getCollapse ());
 89          mNobreak.setSelected (mStringBean.getReplaceNonBreakingSpaces ());
 90      }
 91  
 92      //
 93      // PropertyChangeListener interface
 94      //
 95  
 96      /**
 97       * This method ties the two beans together on the same connection.
 98       * Whenever a property changes on one bean, make sure the URL properties
 99       * agree by setting the URL from one to the other.
100       * @param event The event describing the event source
101       * and the property that has changed.
102       */
103      public void propertyChange (java.beans.PropertyChangeEvent event)
104      {
105          Object source;
106          String name;
107  
108          source = event.getSource ();
109          if (source == mLinkBean)
110          {
111              if (!mLinkBean.getURL ().equals (mStringBean.getURL ()))
112                  mStringBean.setURL (mLinkBean.getURL ());
113          }
114          else if (source == mStringBean)
115          {
116              if (!mStringBean.getURL ().equals (mLinkBean.getURL ()))
117                  mLinkBean.setURL (mStringBean.getURL ());
118              // check for menu status changes
119              name = event.getPropertyName ();
120              if (name.equals (StringBean.PROP_LINKS_PROPERTY))
121                  mLinks.setSelected (
122                      ((Boolean)event.getNewValue ()).booleanValue ());
123              else if (name.equals (StringBean.PROP_COLLAPSE_PROPERTY))
124                  mCollapse.setSelected (
125                      ((Boolean)event.getNewValue ()).booleanValue ());
126              else if (name.equals (StringBean.PROP_REPLACE_SPACE_PROPERTY))
127                  mNobreak.setSelected (
128                      ((Boolean)event.getNewValue ()).booleanValue ());
129          }
130      }
131  
132      //
133      // ActionListener interface
134      //
135  
136      /**
137       * Handles UI events.
138       * Changing the URL in the text field, altering a checkbox setting or
139       * choosing a menu item winds up here, where the appropriate change
140       * is made to the state of the bean.
141       * @param event Details about the action.
142       */
143      public void actionPerformed (ActionEvent event)
144      {
145          Object source;
146          String url;
147          String name;
148          JMenuItem item;
149  
150          source = event.getSource ();
151          if (source == mTextField)
152          {
153              url = mTextField.getText ();
154              mTextField.selectAll ();
155              setURL (url);
156          }
157          else if (source instanceof JCheckBoxMenuItem)
158          {
159              item = (JMenuItem)source;
160              name = item.getName ();
161              if ("Links".equals (name))
162                  mStringBean.setLinks (item.isSelected ());
163              else if ("Collapse".equals (name))
164                  mStringBean.setCollapse (item.isSelected ());
165              else if ("Nobreak".equals (name))
166                  mStringBean.setReplaceNonBreakingSpaces (item.isSelected ());
167          }
168          else if (source instanceof JMenuItem)
169          {
170              name = ((JMenuItem)source).getName ();
171              if ("Back".equals (name))
172              {
173                  if (mCrumb > 0)
174                  {
175                      mCrumb--;
176                      url = (String)mTrail.elementAt (mCrumb);
177                      mCrumb--;
178                      setURL (url);
179                  }
180              }
181              else if ("Forward".equals (name))
182              {
183                  if (mCrumb < mTrail.size ())
184                  {
185                      mCrumb++;
186                      url = (String)mTrail.elementAt (mCrumb);
187                      mCrumb--;
188                      setURL (url);
189                  }
190              }
191          }
192  
193      }
194  
195      //
196      // MouseListener interface
197      //
198  
199      /**
200       * Invoked when the mouse button has been clicked on a component.
201       * A mouse click is a press and release of a mouse button.
202       * @param event Details on the mouse event.
203       */
204      public void mouseClicked (MouseEvent event)
205      {
206          int index;
207          String url;
208  
209          if (2 == event.getClickCount())
210          {
211              index = mLinkBean.locationToIndex (event.getPoint ());
212              url = mLinkBean.getModel ().getElementAt (index).toString ();
213              setURL (url);
214          }
215      }
216  
217      /**
218       * Invoked when the mouse enters a component.
219       * @param event Details on the mouse event.
220       */
221      public void mouseEntered (MouseEvent event)
222      {
223      }
224  
225      /**
226       * Invoked when the mouse exits a component.
227       * @param event Details on the mouse event.
228       */
229      public void mouseExited (MouseEvent event)
230      {
231      }
232  
233      /**
234       * Invoked when a mouse button has been pressed on a component.
235       * @param event Details on the mouse event.
236       */
237      public void mousePressed (MouseEvent event)
238      {
239      }
240  
241      /**
242       * Invoked when a mouse button has been released on a component.
243       * @param event Details on the mouse event.
244       */
245      public void mouseReleased (MouseEvent event)
246      {
247      }
248  
249      //
250      // API control
251      //
252  
253      /**
254       * Set the URL for the bean.
255       * @param url The URL to use in the bean.
256       */
257      public void setURL (String url)
258      {
259          mTextField.setText (url);
260          mCrumb++;
261          if (mTrail.size () <= mCrumb)
262              mTrail.addElement (url);
263          else
264              mTrail.setElementAt (url, mCrumb);
265          mLinkBean.setURL (url);
266  
267          // update navigation menu
268          mBack.setEnabled (mCrumb > 0);
269          mForward.setEnabled (mCrumb + 1 < mTrail.size ());
270      }
271  
272      /** This method is called from within the constructor to
273       * initialize the form.
274       * WARNING: Do NOT modify this code. The content of this method is
275       * always regenerated by the Form Editor.
276       */
277      private void initComponents()//GEN-BEGIN:initComponents
278      {
279          javax.swing.JMenuBar menubar;
280          javax.swing.JScrollPane pane1;
281          javax.swing.JScrollPane pane2;
282          javax.swing.JMenu go;
283          javax.swing.JMenu options;
284          javax.swing.JPanel panel;
285  
286          menubar = new javax.swing.JMenuBar();
287          setJMenuBar (menubar);
288          go = new javax.swing.JMenu();
289          mBack = new javax.swing.JMenuItem();
290          mForward = new javax.swing.JMenuItem();
291          options = new javax.swing.JMenu();
292          mLinks = new javax.swing.JCheckBoxMenuItem();
293          mCollapse = new javax.swing.JCheckBoxMenuItem();
294          mNobreak = new javax.swing.JCheckBoxMenuItem();
295          panel = new javax.swing.JPanel();
296          mSplitPane = new javax.swing.JSplitPane();
297          pane1 = new javax.swing.JScrollPane();
298          mLinkBean = new org.htmlparser.beans.HTMLLinkBean();
299          pane2 = new javax.swing.JScrollPane();
300          mStringBean = new org.htmlparser.beans.HTMLTextBean();
301          mTextField = new javax.swing.JTextField();
302  
303          go.setMnemonic('G');
304          go.setText("Go");
305          go.setToolTipText("crude URL navigation");
306          mBack.setMnemonic('B');
307          mBack.setText("Back");
308          mBack.setToolTipText("back one URL");
309          mBack.setName("Back");
310          mBack.addActionListener (this);
311          go.add(mBack);
312  
313          mForward.setMnemonic('F');
314          mForward.setText("Forward");
315          mForward.setToolTipText("forward one URL");
316          mForward.setName("Forward");
317          mForward.addActionListener (this);
318          go.add(mForward);
319  
320          menubar.add(go);
321  
322          options.setMnemonic('O');
323          options.setText("Options");
324          options.setToolTipText("Bean settings");
325          mLinks.setMnemonic('L');
326          mLinks.setText("Links");
327          mLinks.setToolTipText("show/hide links in text");
328          mLinks.setName("Links");
329          mLinks.addActionListener (this);
330          options.add(mLinks);
331  
332          mCollapse.setMnemonic('C');
333          mCollapse.setText("Collapse");
334          mCollapse.setToolTipText("collapse/retain whitespace sequences");
335          mCollapse.setName("Collapse");
336          mCollapse.addActionListener (this);
337          options.add(mCollapse);
338  
339          mNobreak.setMnemonic('N');
340          mNobreak.setText("Non-breaking Spaces");
341          mNobreak.setToolTipText("replace/retain non-breaking spaces");
342          mNobreak.setName("Nobreak");
343          mNobreak.addActionListener (this);
344          options.add(mNobreak);
345  
346          menubar.add(options);
347  
348          setTitle("BeanyBaby");
349          addWindowListener(new WindowAdapter()
350          {
351              public void windowClosing(WindowEvent evt)
352              {
353                  exitForm(evt);
354              }
355          });
356  
357          panel.setLayout(new BorderLayout());
358  
359          pane1.setViewportView(mLinkBean);
360  
361          mSplitPane.setLeftComponent(pane1);
362  
363          pane2.setViewportView(mStringBean);
364  
365          mSplitPane.setRightComponent(pane2);
366  
367          panel.add(mSplitPane, BorderLayout.CENTER);
368  
369          mTextField.setToolTipText("Enter the URL to view");
370          panel.add(mTextField, BorderLayout.SOUTH);
371  
372          getContentPane().add(panel, BorderLayout.CENTER);
373  
374          pack();
375          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
376          setSize(new Dimension(640, 480));
377          setLocation((screenSize.width-640)/2,(screenSize.height-480)/2);
378      }//GEN-END:initComponents
379  
380      /** Exit the Application
381       * @param event Details about the window event. */
382      private void exitForm (WindowEvent event)
383      {//GEN-FIRST:event_exitForm
384          System.exit (0);
385      }//GEN-LAST:event_exitForm
386  
387      // Variables declaration - do not modify//GEN-BEGIN:variables
388      private org.htmlparser.beans.HTMLLinkBean mLinkBean;
389      private javax.swing.JMenuItem mForward;
390      private javax.swing.JMenuItem mBack;
391      private javax.swing.JCheckBoxMenuItem mCollapse;
392      private javax.swing.JTextField mTextField;
393      private javax.swing.JSplitPane mSplitPane;
394      private javax.swing.JCheckBoxMenuItem mLinks;
395      private org.htmlparser.beans.HTMLTextBean mStringBean;
396      private javax.swing.JCheckBoxMenuItem mNobreak;
397      // End of variables declaration//GEN-END:variables
398  
399      /**
400       * Unit test.
401       * @param args [0] The URL to use in the bean (optional).
402       */
403      public static void main (String[] args)
404      {
405          BeanyBaby bb = new BeanyBaby ();
406          bb.setVisible (true);
407          if (0 >= args.length)
408              bb.setURL ("http://www.slashdot.org");
409          else
410              bb.setURL (args[0]);
411      }
412  }