1 27 package org.htmlparser.beans; 28 29 import javax.swing.JCheckBoxMenuItem ; 30 import javax.swing.JMenuItem ; 31 32 37 public class BeanyBaby 38 extends 39 javax.swing.JFrame 40 implements 41 java.beans.PropertyChangeListener , 42 java.awt.event.ActionListener , 43 java.awt.event.MouseListener 44 { 45 48 java.util.Vector mTrail; 49 50 53 int mCrumb; 54 55 56 public BeanyBaby () 57 { 58 initComponents (); 59 mTrail = new java.util.Vector (25); 60 mCrumb = -1; 61 62 setVisible (true); 64 mSplitPane.setDividerLocation(0.5); 65 setVisible (false); 66 67 mLinkBean.addPropertyChangeListener (this); 69 mStringBean.addPropertyChangeListener (this); 70 mTextField.addActionListener (this); 72 mLinkBean.addMouseListener (this); 73 74 mLinks.setSelected (mStringBean.getLinks ()); 76 mCollapse.setSelected (mStringBean.getCollapse ()); 77 mNobreak.setSelected (mStringBean.getReplaceNonBreakingSpaces ()); 78 } 79 80 84 91 public void propertyChange (java.beans.PropertyChangeEvent event) 92 { 93 Object source; 94 String 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 name = event.getPropertyName (); 108 if (name.equals (StringBean.PROP_LINKS_PROPERTY)) 109 mLinks.setSelected (((Boolean )event.getNewValue ()).booleanValue ()); 110 else if (name.equals (StringBean.PROP_COLLAPSE_PROPERTY)) 111 mCollapse.setSelected (((Boolean )event.getNewValue ()).booleanValue ()); 112 else if (name.equals (StringBean.PROP_REPLACE_SPACE_PROPERTY)) 113 mNobreak.setSelected (((Boolean )event.getNewValue ()).booleanValue ()); 114 } 115 } 116 117 121 public void actionPerformed (java.awt.event.ActionEvent event) 122 { 123 Object source; 124 String url; 125 String name; 126 JMenuItem 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 ) 136 { 137 item = (JMenuItem )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 ) 147 { 148 name = ((JMenuItem )source).getName (); 149 if ("Back".equals (name)) 150 { 151 if (mCrumb > 0) 152 { 153 mCrumb--; 154 url = (String )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 )mTrail.elementAt (mCrumb); 165 mCrumb--; 166 setURL (url); 167 } 168 } 169 } 170 171 } 172 173 179 public void mouseClicked (java.awt.event.MouseEvent event) 180 { 181 int index; 182 String 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 195 public void mouseEntered (java.awt.event.MouseEvent event) 196 { 197 } 198 199 202 public void mouseExited (java.awt.event.MouseEvent event) 203 { 204 } 205 206 209 public void mousePressed (java.awt.event.MouseEvent event) 210 { 211 } 212 213 216 public void mouseReleased (java.awt.event.MouseEvent event) 217 { 218 } 219 220 public void setURL (String 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 mBack.setEnabled (mCrumb > 0); 235 mForward.setEnabled (mCrumb + 1 < mTrail.size ()); 236 } 237 238 243 private void initComponents() { 245 javax.swing.JMenuBar menubar; 246 javax.swing.JScrollPane pane1; 247 javax.swing.JScrollPane pane2; 248 javax.swing.JMenu go; 249 javax.swing.JMenu options; 250 javax.swing.JPanel panel; 251 252 menubar = new javax.swing.JMenuBar (); 253 setJMenuBar (menubar); 254 go = new javax.swing.JMenu (); 255 mBack = new javax.swing.JMenuItem (); 256 mForward = new javax.swing.JMenuItem (); 257 options = new javax.swing.JMenu (); 258 mLinks = new javax.swing.JCheckBoxMenuItem (); 259 mCollapse = new javax.swing.JCheckBoxMenuItem (); 260 mNobreak = new javax.swing.JCheckBoxMenuItem (); 261 panel = new javax.swing.JPanel (); 262 mSplitPane = new javax.swing.JSplitPane (); 263 pane1 = new javax.swing.JScrollPane (); 264 mLinkBean = new org.htmlparser.beans.HTMLLinkBean(); 265 pane2 = new javax.swing.JScrollPane (); 266 mStringBean = new org.htmlparser.beans.HTMLTextBean(); 267 mTextField = new javax.swing.JTextField (); 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 () 316 { 317 public void windowClosing(java.awt.event.WindowEvent evt) 318 { 319 exitForm(evt); 320 } 321 }); 322 323 panel.setLayout(new java.awt.BorderLayout ()); 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 screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 342 setSize(new java.awt.Dimension (640, 480)); 343 setLocation((screenSize.width-640)/2,(screenSize.height-480)/2); 344 } 346 347 private void exitForm (java.awt.event.WindowEvent evt) 348 { System.exit (0); 350 } 352 private org.htmlparser.beans.HTMLLinkBean mLinkBean; 354 private javax.swing.JMenuItem mForward; 355 private javax.swing.JMenuItem mBack; 356 private javax.swing.JCheckBoxMenuItem mCollapse; 357 private javax.swing.JTextField mTextField; 358 private javax.swing.JSplitPane mSplitPane; 359 private javax.swing.JCheckBoxMenuItem mLinks; 360 private org.htmlparser.beans.HTMLTextBean mStringBean; 361 private javax.swing.JCheckBoxMenuItem mNobreak; 362 364 367 public static void main (String [] 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 |