PicWeb.java
上传用户:shengda799
上传日期:2007-01-10
资源大小:68k
文件大小:10k
源码类别:

图片显示

开发平台:

Java

  1. // PicWeb.java
  2. //
  3. // This software is Copyright 1998, 2000, 2001 by Mark Watson.
  4. // This software may be freely used for any purpose
  5. // and may be modified for any purpose as long as
  6. // you agree to the following:
  7. //
  8. // 1. Any bug fixes or enhancements that you make must be emailed
  9. //    back to Mark Watson (markw@markwatson.com) for possible
  10. //    inclusion in the source code base that is available at
  11. //    the web site www.markwatson.com (credit will be given
  12. //    to contributers).
  13. //
  14. // 2. This copyright notice must be kept with the source code
  15. //    and duplicated if you distribute a modified verison of
  16. //    this software in binary form.
  17. //
  18. // 3. You will not re-distribute the source code. Rather, any
  19. //    improvements to this program will be returned to Mark
  20. //    Watson so that there is always one source base at
  21. //    the web site www.markwatson.com (you may freely
  22. //    distribute compiled derived versions of this program
  23. //    without any restrictions as long as this entire
  24. //    copyright notice is reproduced with the application
  25. //    (e.g., in the 'help' or 'about' dialog box).
  26. //
  27. // 4. This software may not be used for any illegal purpose.
  28. //
  29. // 5. You take all responsibility for the use of this software:
  30. //
  31. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  32. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  35. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41. // SUCH DAMAGE.
  42. // OpenSource contributers: Original program: Mark Watson (markw@markwatson.com)
  43. //
  44. //   Name                      Email (optional) + changes
  45. //   ----                      --------------------------
  46. //   3.0  Achille Petrilli     Achille.Petrilli@cern.ch (directory pop-up, new fonts, larger icons)
  47. //   4.1  Tony Allan           tony@apms.com.au (sorted files, selectable html filename, saved options)
  48. //   4.2  Tony Allan           tony@apms.com.au (fix .html bug, added height/width to HTML, don't write any
  49. //                                               entry if the subdirectory doesn't have any images, add option
  50. //                                               to select if thumbnails are reprocessed)
  51. //   4.3  Reinout van Schouwen rmvschou@cs.vu.nl (added tables and cleaned up HTML generation)
  52. //   6.0  Mark Watson: added an option to not generate thumbnail images
  53. //   7.0  Mark Watson: separated PicWeb.java into two parts: a GUI (PicWeb.java) and and
  54. //                     engine to generate thumbnail image files and HTML files (Engine.java)
  55. import java.applet.*;
  56. import java.awt.*;
  57. import java.awt.image.*;
  58. import java.awt.event.*;
  59. import java.awt.Image;
  60. import java.awt.Graphics;
  61. import java.io.*;
  62. import java.net.*;
  63. import java.util.*;
  64. /** PicWeb class contains both the user interface and uses the separate class
  65.  *  Engine to produce Thumbnail image files and HTML files
  66.  */
  67. public class PicWeb extends Panel implements Serializable, Runnable {
  68.     protected static final String version = "7.0";
  69.     private static final int[] ALLFS = {10, 12, 14, 20, 24};
  70.     private static final int[] MACFS = {8, 10, 12, 14, 18};
  71.     Options options;
  72.     private OptionsDialog optionsDialog;
  73.     private FileDialog f_dialog;
  74.     private Logo logo;
  75.     private transient TextField root_dir;
  76.     private transient TextArea outputText;
  77.     private transient Button p_button;
  78.     private transient PicWeb picweb;
  79.     private boolean process;
  80.     private Thread workThread;
  81.     /**
  82.      * work engine tht generate thumbnail images nd HTML code
  83.      */
  84.     private Engine engine;
  85.     public static void main(String args []) {
  86.         String initializationFileName = "PicWeb.ini";
  87.         if (args.length >0) {
  88.             initializationFileName = args[0];
  89.         }
  90.         AppFrame f = new AppFrame();
  91.         PicWeb pw = new PicWeb(initializationFileName);
  92.         f.add(pw);
  93.         f.pack();
  94.         f.show();
  95.     }
  96.     /** PicWeb class constructor creates the user interface Panel
  97.      * and initializes a work thread
  98.      */
  99.     public PicWeb(String initializationFileName) {
  100.         options = new Options();
  101.         options.iniFile = initializationFileName;
  102.         options.ini = new Properties(); // (ta 4.1) properties support
  103.         try {
  104.             options.ini.load(new BufferedInputStream(new FileInputStream(options.iniFile)));
  105.         }
  106.         catch (FileNotFoundException e) {}                      // use defaults if file not found
  107.         catch (IOException e) {System.out.println(e);}  // print error then use defaults for other errors
  108.         options.updateFromFile();
  109.         try {
  110.             options.imageSizeChoice = Integer.parseInt(options.ini.getProperty("imageSizeChoice" ,"3"));
  111.         } catch (NumberFormatException e) {options.imageSizeChoice = 3;}
  112.         options.HTMLFilename = options.ini.getProperty("HTMLFilename" ,"pics.htm");
  113.         String os = System.getProperty("os.name","not found");
  114.         if (os.startsWith("Mac"))
  115.             options.fontSizes = MACFS;
  116.         else
  117.             options.fontSizes = ALLFS;
  118.         DialogFrame o_frame = new DialogFrame();
  119.         optionsDialog = new OptionsDialog(o_frame, this, options);
  120.         process = false;
  121.         setFont(new Font("Dialog", Font.PLAIN, options.fontSizes[1]));
  122.         setLayout(null);
  123.         Label l0 = new Label("PicWeb Builder " + version + "   www.markwatson.com");
  124.         l0.setFont(new Font("Dialog", Font.BOLD, options.fontSizes[3]));
  125.         add(l0);
  126.         l0.setBounds(5, 3, 414, 30);
  127.         Label l1 = new Label("directory:");
  128.         add(l1);
  129.         l1.setBounds(5, 50, 60, 30);
  130.         options.dirSpec = options.ini.getProperty("directory", (new File("").getAbsolutePath()));
  131.         root_dir = new TextField(options.dirSpec, 40);
  132.         add(root_dir);
  133.         root_dir.setBounds(65, 50, 170, 30);
  134.         f_dialog = new FileDialog(o_frame,"PicWeb - Select a directory...");
  135.         f_dialog.setModal(true);
  136.         f_dialog.setDirectory(root_dir.getText());
  137.         Button b_button = new Button("Browse...");
  138.         b_button.addActionListener(new Dbrowser());
  139.         add(b_button);
  140.         b_button.setBounds(240, 50, 60, 30);
  141.         p_button = new Button("Process");
  142.         p_button.addActionListener(new MouseProcess());
  143.         add(p_button);
  144.         p_button.setBounds(370, 50, 60, 30);
  145.         Button help_button = new Button("Options...");
  146.         help_button.addActionListener(new MouseOptions());
  147.         add(help_button);
  148.         help_button.setBounds(305, 50, 60, 30);
  149.         logo = new Logo(this, 50, 50);
  150.         add(logo);
  151.         logo.setBounds(440, 6, 50, 50);
  152.         int scrollBars = 0;
  153.         try {
  154.             scrollBars =
  155.                 Integer.parseInt(options.ini.getProperty("scrollBars" ,
  156.                                                          Integer.toString(TextArea.SCROLLBARS_BOTH)));
  157.         } catch (NumberFormatException e) {options.imageSizeChoice = TextArea.SCROLLBARS_BOTH;}
  158.         outputText = new TextArea(copyright, 6, 82, scrollBars);
  159.         add(outputText);
  160.         outputText.setBounds(5, 115, 550, 110);
  161.         setBounds(20, 40, 580, 230);
  162.         engine = new Engine(this, logo, options);
  163.         picweb = this; // used to start work thread in inner class MouseProcess
  164.     }
  165.     /**
  166.      * callback used by the Engine class
  167.      */
  168.     public void print(String s) {
  169.         outputText.append(s);
  170.     }
  171.     // ----------------------------------------------------------------------
  172.     class Dbrowser implements ActionListener, Serializable {
  173.         public void actionPerformed(ActionEvent e) {
  174.             f_dialog.setDirectory(root_dir.getText());
  175.             f_dialog.setFile("dummy.txt");
  176.             f_dialog.show();
  177.             String d = f_dialog.getDirectory();
  178.             if (d == null)
  179.                 d = "";
  180.             if (d.length() <= 1)
  181.                 d = "";
  182.             if (d.endsWith(File.separator))
  183.                 d = d.substring(0, d.length() - 1);
  184.             root_dir.setText(d);
  185.         }
  186.     }
  187.     /** The inner class MouseOptions is used to detect a mouse
  188.      * release event that occurs when a user clicks the 'Options'
  189.      *  button. The mouseReleased method makes the 'options' object
  190.      * visible
  191.      */
  192.     class MouseOptions implements ActionListener, Serializable {
  193.         public void actionPerformed(ActionEvent e) {
  194.             optionsDialog.setVisible(true);
  195.         }
  196.     }
  197.     /** The 'nl' and 'fs' objects are define for string constants
  198.      * instead of using literal strings in order to pass the
  199.      * "100% Pure" Java certification by Key Labs
  200.      */
  201.     private String nl = System.getProperty("line.separator");
  202.     private String fs = System.getProperty("file.separator");
  203.     /** The 'run' method is called when the work thread is started.
  204.      * The work thread sleeps until there is work to be done.  We
  205.      * use a separate work thread so that the user interface does not
  206.      * "freeze up" when PicWeb is processing image files and generating
  207.      * HTML documents.
  208.      */
  209.     public void run() {
  210.         engine.process(root_dir.getText().trim(), logo);
  211.         p_button.setEnabled(true);
  212.         print("Done." + nl);
  213.     }
  214.     /** The inner MouseProcess class is used to detect mouse released
  215.      *  events in the 'Process' button. The boolean flag 'process' is
  216.      *  set to 'true' which causes the 'run' method to process a
  217.      *  directory tree
  218.      */
  219.     class MouseProcess implements ActionListener, Serializable {
  220.         public void actionPerformed(ActionEvent e) {
  221.             p_button.setEnabled(false);
  222.             workThread = new Thread(picweb);
  223.             workThread.start();
  224.         }
  225.     }
  226.     String copyright =
  227.         "PicWeb(tm) Copyright 1997-1999 by Mark Watson." + nl +
  228.         "The JpegEncoder class is ' Copyright (C) 1998, James R. Weeks and BioElectroMech.'" + nl + nl +
  229.         "READ FULL COPYRIGHT NOTICES ON OPTIONS SCREEN" + nl;
  230. }