1 27 package org.htmlparser.lexerapplications.thumbelina; 28 29 import java.awt.BorderLayout ; 30 import java.awt.Component ; 31 import java.awt.Dimension ; 32 import java.awt.Rectangle ; 33 import java.awt.Toolkit ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.ItemEvent ; 37 import java.awt.event.ItemListener ; 38 import java.awt.event.WindowEvent ; 39 import java.awt.event.WindowListener ; 40 import java.beans.PropertyChangeEvent ; 41 import java.beans.PropertyChangeListener ; 42 import java.net.HttpURLConnection ; 43 import java.net.MalformedURLException ; 44 import java.net.URL ; 45 import java.util.ArrayList ; 46 import java.util.prefs.BackingStoreException ; 47 import java.util.prefs.Preferences ; 48 import javax.swing.ImageIcon ; 49 import javax.swing.JCheckBoxMenuItem ; 50 51 import javax.swing.JFrame ; 52 import javax.swing.JMenu ; 53 import javax.swing.JMenuBar ; 54 import javax.swing.JMenuItem ; 55 import javax.swing.JOptionPane ; 56 import javax.swing.JSeparator ; 57 import org.htmlparser.lexer.Lexer; 58 59 66 public class ThumbelinaFrame 67 extends 68 JFrame 69 implements 70 WindowListener , 71 ActionListener , 72 ItemListener , 73 PropertyChangeListener 74 { 75 78 private static final String TITLE = "Thumbelina"; 79 80 83 private static final String FRAMESIZE = "FrameSize"; 84 85 88 private static final int BORDERPERCENT = 5; 89 90 93 private static final String MRULENGTH = "MRULength"; 94 95 98 private static final String MRUMAX = "MRUMax"; 99 100 103 private static final String MRUPREFIX = "MRUListItem"; 104 105 108 private static final String GOOGLEQUERY = "GoogleQuery"; 109 110 113 private static final String DEFAULTGOOGLEQUERY = "thumbs"; 114 115 118 private static final String [] DEFAULTMRULIST = 119 { 120 "www.a2zcds.com", 121 "www.stoneschool.com/Japan/", 122 "www.tommys-bookmarks.com", 123 "www.unitedmedia.com/comics/dilbert/archive", 124 "www.pastelartists.com", 125 }; 126 127 130 private static final boolean USE_MOZILLA_HEADERS = false; 131 132 135 private static final String STATUSBARSTATE = "StatusBarVisible"; 136 137 140 private static final String HISTORYLISTSTATE = "HistoryListVisible"; 141 142 145 private static final String SEQUENCERACTIVE = "SequencerActive"; 146 147 150 private static final String BACKGROUNDTHREADACTIVE = 151 "BackgroundThreadActive"; 152 153 156 private static final String DISPLAYSPEED = "DisplaySpeed"; 157 158 161 protected JMenuBar mMenu; 162 163 166 protected JMenu mURL; 167 168 171 protected JMenuItem mOpen; 172 173 176 protected JMenuItem mGoogle; 177 178 181 protected JSeparator mSeparator1; 182 183 186 protected JSeparator mSeparator2; 187 188 191 protected JMenuItem mExit; 192 193 196 protected JMenu mView; 197 198 201 protected JCheckBoxMenuItem mStatusVisible; 202 203 206 protected JCheckBoxMenuItem mHistoryVisible; 207 208 211 protected JMenu mCommand; 212 213 216 protected JMenuItem mReset; 217 218 221 protected JMenuItem mClear; 222 223 226 protected JMenu mHelp; 227 228 231 protected JMenuItem mAbout; 232 233 236 public ThumbelinaFrame () 237 { 238 this (new Thumbelina ()); 239 } 240 241 247 public ThumbelinaFrame (final String url) 248 throws 249 MalformedURLException 250 { 251 this (new Thumbelina (url)); 252 } 253 254 258 public ThumbelinaFrame (final URL url) 259 { 260 this (new Thumbelina (url)); 261 } 262 263 267 public ThumbelinaFrame (final Thumbelina thumbelina) 268 { 269 setTitle (TITLE); 270 thumbelina.addPropertyChangeListener (this); 271 getContentPane ().add (thumbelina, BorderLayout.CENTER); 272 addWindowListener (this); 273 makeMenu (); 274 setJMenuBar (mMenu); 275 restoreSize (); 276 initState (); 277 updateMenu (); 278 } 279 280 284 public Thumbelina getThumbelina () 285 { 286 return ((Thumbelina)getContentPane ().getComponent (0)); 287 } 288 289 295 public void initState () 296 { 297 Preferences prefs; 298 299 prefs = Preferences.userNodeForPackage (getClass ()); 300 if (-1 == prefs.getInt (MRULENGTH, -1)) 301 for (int i = 0; i < DEFAULTMRULIST.length; i++) 302 updateMRU (DEFAULTMRULIST[i]); 303 getThumbelina ().setStatusBarVisible ( 304 prefs.getBoolean (STATUSBARSTATE, 305 getThumbelina ().getStatusBarVisible ())); 306 mStatusVisible.setSelected (getThumbelina ().getStatusBarVisible ()); 307 getThumbelina ().setHistoryListVisible ( 308 prefs.getBoolean (HISTORYLISTSTATE, 309 getThumbelina ().getHistoryListVisible ())); 310 mHistoryVisible.setSelected (getThumbelina ().getHistoryListVisible ()); 311 getThumbelina ().setSequencerActive ( 312 prefs.getBoolean (SEQUENCERACTIVE, 313 getThumbelina ().getSequencerActive ())); 314 getThumbelina ().setBackgroundThreadActive ( 315 prefs.getBoolean (BACKGROUNDTHREADACTIVE, 316 getThumbelina ().getBackgroundThreadActive ())); 317 getThumbelina ().setSpeed ( 318 prefs.getInt (DISPLAYSPEED, getThumbelina ().getSpeed ())); 319 } 320 321 326 public void saveState () 327 { 328 Preferences prefs; 329 330 prefs = Preferences.userNodeForPackage (getClass ()); 331 if (NORMAL == getExtendedState ()) 333 prefs.put (FRAMESIZE, toString (getBounds ())); 334 prefs.putBoolean (STATUSBARSTATE, 335 getThumbelina ().getStatusBarVisible ()); 336 prefs.putBoolean (HISTORYLISTSTATE, 337 getThumbelina ().getHistoryListVisible ()); 338 prefs.putBoolean (SEQUENCERACTIVE, 339 getThumbelina ().getSequencerActive ()); 340 prefs.putBoolean (BACKGROUNDTHREADACTIVE, 341 getThumbelina ().getBackgroundThreadActive ()); 342 prefs.putInt (DISPLAYSPEED, 343 getThumbelina ().getSpeed ()); 344 try 345 { 346 prefs.flush (); 347 } 348 catch (BackingStoreException bse) 349 { 350 bse.printStackTrace (); 351 } 352 } 353 354 359 public void initSize () 360 { 361 Toolkit tk; 362 Dimension dim; 363 int borderx; 364 int bordery; 365 366 tk = getToolkit (); 367 dim = tk.getScreenSize (); 368 borderx = dim.width * BORDERPERCENT / 100; 369 bordery = dim.height * BORDERPERCENT / 100; 370 setBounds ( 371 borderx, 372 bordery, 373 dim.width - (2 * borderx), 374 dim.height - (2 * bordery)); 375 } 376 377 381 public void restoreSize () 382 { 383 Preferences prefs; 384 String size; 385 Rectangle rectangle; 386 387 prefs = Preferences.userNodeForPackage (getClass ()); 388 size = prefs.get (FRAMESIZE, ""); 389 if ("".equals (size)) 390 initSize (); 391 else 392 try 393 { 394 rectangle = fromString (size); 395 if (rational (rectangle)) 396 setBounds ( 397 rectangle.x, 398 rectangle.y, 399 rectangle.width, 400 rectangle.height); 401 else 402 initSize (); 403 } 404 catch (IllegalArgumentException iae) 405 { 406 initSize (); 407 } 408 } 409 410 420 protected String toString (final Rectangle r) 421 { 422 return ("[" + r.x + "," + r.y + "," + r.width + "," + r.height + "]"); 423 } 424 425 433 protected Rectangle fromString (final String value) 434 throws 435 IllegalArgumentException 436 { 437 String guts; 438 int current; 439 int[] values; 440 int index; 441 Rectangle ret; 442 443 try 444 { 445 if (value.startsWith ("[") && value.endsWith ("]")) 447 { 448 guts = value.substring (1, value.length () - 1) + ","; 449 current = 0; 450 values = new int[4]; 451 for (int i = 0; i < 4; i++) 452 { 453 index = guts.indexOf (",", current); 454 if (-1 == index) 455 throw new IllegalArgumentException ( 456 "invalid format \"" + value + "\""); 457 else 458 { 459 values[i] = Integer.parseInt ( 460 guts.substring (current, index)); 461 current = index + 1; 462 } 463 } 464 ret = new Rectangle ( 465 values[0], values[1], values[2], values[3]); 466 } 467 else 468 throw new IllegalArgumentException ( 469 "invalid format \"" + value + "\""); 470 } 471 catch (NumberFormatException nfe) 472 { 473 throw new IllegalArgumentException (nfe.getMessage ()); 474 } 475 476 return (ret); 477 } 478 479 484 private boolean rational (final Rectangle r) 485 { 486 Toolkit tk; 487 Dimension winsize; 488 489 tk = getToolkit (); 490 winsize = tk.getScreenSize(); 491 return ( (r.x >= r.width / -10) 495 && (r.y >= 0) 496 && (r.width > 0) 497 && (r.height > 0) 498 && (r.x + r.width <= winsize.width + r.width / 10) 499 && (r.y + r.height <= winsize.height + r.height / 10)); 500 } 501 502 506 public void makeMenu () 507 { 508 mMenu = new JMenuBar (); 509 mURL = new JMenu (); 510 mOpen = new JMenuItem (); 511 mGoogle = new JMenuItem (); 512 mSeparator1 = new JSeparator (); 513 mSeparator2 = new JSeparator (); 514 mExit = new JMenuItem (); 515 516 mView = new JMenu (); 517 mStatusVisible = new JCheckBoxMenuItem (); 518 mHistoryVisible = new JCheckBoxMenuItem (); 519 520 mHelp = new JMenu (); 521 mAbout = new JMenuItem (); 522 523 mCommand = new JMenu (); 524 mReset = new JMenuItem (); 525 mClear = new JMenuItem (); 526 527 mURL.setMnemonic ('U'); 528 mURL.setText ("URL"); 529 mOpen.setMnemonic ('O'); 530 mOpen.setText ("Open"); 531 mOpen.setToolTipText ("Open a URL."); 532 mURL.add (mOpen); 533 534 mGoogle.setMnemonic ('G'); 535 mGoogle.setText ("Google"); 536 mGoogle.setToolTipText ("Search Google."); 537 mURL.add (mGoogle); 538 539 mURL.add (mSeparator1); 540 mURL.add (mSeparator2); 541 542 mExit.setMnemonic ('E'); 543 mExit.setText ("Exit"); 544 mExit.setToolTipText ("Quit Thumbelina."); 545 mURL.add (mExit); 546 547 mMenu.add (mURL); 548 549 mView.setMnemonic ('V'); 550 mView.setText ("View"); 551 mStatusVisible.setMnemonic ('S'); 552 mStatusVisible.setSelected (getThumbelina ().getStatusBarVisible ()); 553 mStatusVisible.setText ("Status Bar"); 554 mStatusVisible.setToolTipText ("Show/Hide the status bar."); 555 mView.add (mStatusVisible); 556 mHistoryVisible.setMnemonic ('H'); 557 mHistoryVisible.setSelected (getThumbelina ().getHistoryListVisible ()); 558 mHistoryVisible.setText ("History List"); 559 mHistoryVisible.setToolTipText ("Show/Hide the history list."); 560 mView.add (mHistoryVisible); 561 mMenu.add (mView); 562 563 mCommand.setMnemonic ('C'); 564 mCommand.setText ("Command"); 565 mReset.setMnemonic ('R'); 566 mReset.setText ("Reset"); 567 mReset.setToolTipText ("Reset Thumbelina."); 568 mClear.setMnemonic ('L'); 569 mClear.setText ("Clear"); 570 mClear.setToolTipText ("Clear display."); 571 mCommand.add (mReset); 572 mCommand.add (mClear); 573 mCommand.add (mHelp); 574 mMenu.add (mCommand); 575 576 mHelp.setMnemonic ('H'); 577 mHelp.setText ("Help"); 578 mAbout.setMnemonic ('A'); 579 mAbout.setText ("About"); 580 mAbout.setToolTipText ("Information about Thumbelina."); 581 mHelp.add (mAbout); 582 mMenu.add (mHelp); 583 584 mOpen.addActionListener (this); 585 mGoogle.addActionListener (this); 586 mExit.addActionListener (this); 587 mStatusVisible.addItemListener (this); 588 mHistoryVisible.addItemListener (this); 589 mReset.addActionListener (this); 590 mClear.addActionListener (this); 591 mAbout.addActionListener (this); 592 } 593 594 599 public void updateMenu () 600 { 601 Preferences prefs; 602 int start; 603 int end; 604 Component component; 605 JMenuItem item; 606 int count; 607 String string; 608 609 prefs = Preferences.userNodeForPackage (getClass ()); 610 start = -1; 611 end = -1; 612 for (int i = 0; i < mURL.getItemCount (); i++) 613 { 614 component = mURL.getMenuComponent (i); 615 if (component == mSeparator1) 616 start = i + 1; 617 else if (component == mSeparator2) 618 end = i; 619 } 620 621 if ((-1 != start) && (-1 != end)) 622 { 623 for (int i = start; i < end; i++) 624 mURL.remove (start); 625 626 count = prefs.getInt (MRULENGTH, 0); 627 for (int i = 0; i < count; i++) 628 { 629 string = prefs.get (MRUPREFIX + i, ""); 630 if (!"".equals (string)) 631 { 632 item = new JMenuItem (); 633 item.setActionCommand (string); 634 if (string.length () > 40) 635 string = string.substring (0, 38) + "..."; 636 item.setText (string); 637 item.addActionListener (this); 638 mURL.add (item, start++); 639 } 640 } 641 } 642 } 643 644 648 653 public void windowOpened (final WindowEvent event) 654 { 655 } 656 657 662 public void windowClosing (final WindowEvent event) 663 { 664 exit (); 665 } 666 667 673 public void windowClosed (final WindowEvent event) 674 { 675 } 676 677 685 public void windowIconified (final WindowEvent event) 686 { 687 } 688 689 695 public void windowDeiconified (final WindowEvent event) 696 { 697 } 698 699 706 public void windowActivated (final WindowEvent event) 707 { 708 } 709 710 717 public void windowDeactivated (final WindowEvent event) 718 { 719 } 720 721 725 730 public void actionPerformed (final ActionEvent actionEvent) 731 { 732 String action; 733 734 action = actionEvent.getActionCommand (); 735 if (action.equals ("Open")) 736 open (); 737 else if (action.equals ("Google")) 738 googlesearch (); 739 else if (action.equals ("Reset")) 740 getThumbelina ().reset (); 741 else if (action.equals ("Clear")) 742 getThumbelina ().getPicturePanel ().reset (); 743 else if (action.equals ("About")) 744 about (); 745 else if (action.equals ("Exit")) 746 exit (); 747 else 748 { 749 getThumbelina ().open (action); 751 updateMRU (action); 752 updateMenu (); 753 } 754 } 755 756 760 764 public void itemStateChanged (final ItemEvent event) 765 { 766 Object source; 767 boolean visible; 768 769 source = event.getItemSelectable (); 770 visible = ItemEvent.SELECTED == event.getStateChange (); 771 if (source == mStatusVisible) 772 getThumbelina ().setStatusBarVisible (visible); 773 else if (source == mHistoryVisible) 774 getThumbelina ().setHistoryListVisible (visible); 775 } 776 777 781 785 public void propertyChange (final PropertyChangeEvent event) 786 { 787 String url; 788 789 if (event.getPropertyName ().equals ( 790 Thumbelina.PROP_CURRENT_URL_PROPERTY)) 791 { 792 url = (String )event.getNewValue (); 793 if (null == url) 794 setTitle ("Thumbelina"); 795 else 796 setTitle ("Thumbelina - " + url); 797 } 798 } 799 800 804 public void updateMRU (String url) 805 { 806 Preferences prefs; 807 int count; 808 ArrayList list; 809 String string; 810 int max; 811 812 if (url.startsWith ("http://")) 813 url = url.substring (7); 814 prefs = Preferences.userNodeForPackage (getClass ()); 815 count = prefs.getInt (MRULENGTH, -1); 816 list = new ArrayList (); 817 for (int i = 0; i < count; i++) 818 { 819 string = prefs.get (MRUPREFIX + i, ""); 820 if (!"".equals (string) && !url.equalsIgnoreCase (string)) 821 list.add (string); 822 } 823 list.add (0, url); 824 max = prefs.getInt (MRUMAX, -1); 825 if (-1 == max) 826 max = 8; 827 while (list.size () > max) 828 list.remove (max); 829 prefs.putInt (MRULENGTH, list.size ()); 830 prefs.putInt (MRUMAX, max); 831 for (int i = 0; i < list.size (); i++) 832 prefs.put (MRUPREFIX + i, (String )list.get (i)); 833 try 834 { 835 prefs.flush (); 836 } 837 catch (BackingStoreException bse) 838 { 839 bse.printStackTrace (); 840 } 841 } 842 843 846 public void open () 847 { 848 String result; 849 850 result = JOptionPane.showInputDialog ( 851 this, 852 "Enter the URL:", 853 "Open URL", 854 JOptionPane.PLAIN_MESSAGE); 855 if (null != result) 856 { 857 getThumbelina ().open (result); 858 updateMRU (result); 859 updateMenu (); 860 } 861 } 862 863 905 public void googlesearch () 906 { 907 908 Preferences prefs; 909 String query; 910 String terms; 911 StringBuffer buffer; 912 HttpURLConnection connection; 913 URL url; 914 Lexer lexer; 915 URL [][] results; 916 917 prefs = Preferences.userNodeForPackage (getClass ()); 918 query = prefs.get (GOOGLEQUERY, DEFAULTGOOGLEQUERY); 919 try 920 { 921 query = (String )JOptionPane.showInputDialog ( 922 this, 923 "Enter the search term:", 924 "Search Google", 925 JOptionPane.PLAIN_MESSAGE, 926 null, 927 null, 928 query); 929 if (null != query) 930 { 931 terms = query.replace (' ', '+'); 933 buffer = new StringBuffer (1024); 934 buffer.append ("http://www.google.ca/search?"); 935 buffer.append ("q="); 936 buffer.append (terms); 937 buffer.append ("&ie=UTF-8"); 938 buffer.append ("&oe=UTF-8"); 939 buffer.append ("&hl=en"); 940 buffer.append ("&btnG=Google+Search"); 941 buffer.append ("&meta="); 942 url = new URL (buffer.toString ()); 943 connection = (HttpURLConnection )url.openConnection (); 944 if (USE_MOZILLA_HEADERS) 945 { 946 959 connection.setRequestProperty ("Referer", "http://www.google.ca"); 960 connection.setRequestProperty ("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1"); 961 connection.setRequestProperty ("Accept-Language", "en-us, en;q=0.50"); 962 connection.setRequestProperty ("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225"); 963 connection.setRequestProperty ("Accept-Charset", "ISO-8859-1, utf-8;q=0.66, *;q=0.66"); 964 } 965 else 966 { 967 978 connection.setRequestProperty ("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); 979 connection.setRequestProperty ("Accept-Language", "en-ca"); 980 connection.setRequestProperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; trieste; .NET CLR 1.1.4322; .NET CLR 1.0.3705)"); 981 } 982 connection.setDoOutput (true); 983 connection.setDoInput (true); 984 connection.setUseCaches (false); 985 lexer = new Lexer (connection); 986 results = getThumbelina ().extractImageLinks (lexer, url); 987 getThumbelina ().reset (); 989 for (int i = 0; i < results[1].length; i++) 991 { 992 String found = results[1][i].toExternalForm (); 993 if (-1 == found.indexOf ("google")) 994 getThumbelina ().append (results[1][i]); 995 } 996 prefs.put (GOOGLEQUERY, query); 997 try 998 { 999 prefs.flush (); 1000 } 1001 catch (BackingStoreException bse) 1002 { 1003 bse.printStackTrace (); 1004 } 1005 } 1006 } 1007 catch (Exception e) 1008 { 1009 System.out.println (e.getMessage ()); 1010 } 1011 } 1012 1013 1016 public void about () 1017 { 1018 URL url; 1019 1020 try 1021 { 1022 url = new URL ("http://sourceforge.net/sflogo.php?group_id=24399"); 1023 } 1024 catch (MalformedURLException murle) 1025 { 1026 url = null; 1027 } 1028 JOptionPane.showMessageDialog ( 1029 this, 1030 "Scan and display the images behind thumbnails.\n" 1031 + "\n" 1032 + "An example application using the HTML Parser project.\n" 1033 + "Visit http://htmlparser.sourceforge.org for the latest\n" 1034 + "version and source code.\n", 1035 "Thumbelina - About", 1036 JOptionPane.PLAIN_MESSAGE, 1037 new ImageIcon (url)); 1038 } 1039 1040 1044 public void exit () 1045 { 1046 saveState (); 1047 System.exit (0); 1048 } 1049 1050 1057 public static void main (final String [] args) 1058 { 1059 String url; 1060 ThumbelinaFrame thumbelina; 1061 1062 System.setProperty ("sun.net.client.defaultReadTimeout", "7000"); 1063 System.setProperty ("sun.net.client.defaultConnectTimeout", "7000"); 1064 1065 url = null; 1066 if (0 != args.length) 1067 if (args[0].equalsIgnoreCase ("help") 1068 || args[0].equalsIgnoreCase ("-help") 1069 || args[0].equalsIgnoreCase ("-h") 1070 || args[0].equalsIgnoreCase ("?") 1071 || args[0].equalsIgnoreCase ("-?")) 1072 Thumbelina.help (); 1073 else 1074 url = args[0]; 1075 1076 try 1077 { 1078 thumbelina = new ThumbelinaFrame (url); 1079 thumbelina.setVisible (true); 1080 } 1081 catch (MalformedURLException murle) 1082 { 1083 System.err.println (murle.getMessage ()); 1084 Thumbelina.help (); 1085 } 1086 } 1087} 1088 | Popular Tags |