home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / SwingSet2 / src / FileChooserDemo.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  12.8 KB  |  422 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)FileChooserDemo.java    1.12 02/06/13
  38.  */
  39.  
  40.  
  41. import javax.swing.*;
  42. import javax.swing.event.*;
  43. import javax.swing.text.*;
  44. import javax.swing.border.*;
  45. import javax.swing.colorchooser.*;
  46. import javax.swing.filechooser.*;
  47. import javax.accessibility.*;
  48.  
  49. import java.awt.*;
  50. import java.awt.event.*;
  51. import java.beans.*;
  52. import java.util.*;
  53. import java.io.*;
  54. import java.applet.*;
  55. import java.net.*;
  56.  
  57. /**
  58.  * JFileChooserDemo
  59.  *
  60.  * @version 1.1 07/16/99
  61.  * @author Jeff Dinkins
  62.  */
  63. public class FileChooserDemo extends DemoModule {
  64.     JLabel theImage;
  65.     Icon jpgIcon; 
  66.     Icon gifIcon;
  67.  
  68.     /**
  69.      * main method allows us to run as a standalone demo.
  70.      */
  71.     public static void main(String[] args) {
  72.     FileChooserDemo demo = new FileChooserDemo(null);
  73.     demo.mainImpl();
  74.     }
  75.  
  76.     /**
  77.      * FileChooserDemo Constructor
  78.      */
  79.     public FileChooserDemo(SwingSet2 swingset) {
  80.     // Set the title for this demo, and an icon used to represent this
  81.     // demo inside the SwingSet2 app.
  82.     super(swingset, "FileChooserDemo", "toolbar/JFileChooser.gif");
  83.     createFileChooserDemo();
  84.     }
  85.  
  86.     public void createFileChooserDemo() {
  87.     theImage = new JLabel("");
  88.     jpgIcon = createImageIcon("filechooser/jpgIcon.jpg", "jpg image");
  89.     gifIcon = createImageIcon("filechooser/gifIcon.gif", "gif image");
  90.  
  91.     JPanel demoPanel = getDemoPanel();
  92.     demoPanel.setLayout(new BoxLayout(demoPanel, BoxLayout.Y_AXIS));
  93.  
  94.     JPanel innerPanel = new JPanel();
  95.     innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
  96.  
  97.     demoPanel.add(Box.createRigidArea(VGAP20));
  98.     demoPanel.add(innerPanel);
  99.     demoPanel.add(Box.createRigidArea(VGAP20));
  100.  
  101.     innerPanel.add(Box.createRigidArea(HGAP20));
  102.  
  103.     // Create a panel to hold buttons
  104.     JPanel buttonPanel = new JPanel() {
  105.         public Dimension getMaximumSize() {
  106.         return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
  107.         }
  108.     };
  109.     buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
  110.  
  111.     buttonPanel.add(Box.createRigidArea(VGAP15));
  112.     buttonPanel.add(createPlainFileChooserButton());
  113.     buttonPanel.add(Box.createRigidArea(VGAP15));
  114.     buttonPanel.add(createPreviewFileChooserButton());
  115.     buttonPanel.add(Box.createRigidArea(VGAP15));
  116.     buttonPanel.add(createCustomFileChooserButton());
  117.     buttonPanel.add(Box.createVerticalGlue());
  118.  
  119.     // Create a panel to hold the image
  120.     JPanel imagePanel = new JPanel();
  121.     imagePanel.setLayout(new BorderLayout());
  122.     imagePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
  123.     JScrollPane scroller = new JScrollPane(theImage);
  124.         scroller.getVerticalScrollBar().setUnitIncrement(10);
  125.         scroller.getHorizontalScrollBar().setUnitIncrement(10);
  126.     imagePanel.add(scroller, BorderLayout.CENTER);
  127.  
  128.     // add buttons and image panels to inner panel
  129.     innerPanel.add(buttonPanel);
  130.     innerPanel.add(Box.createRigidArea(HGAP30));
  131.     innerPanel.add(imagePanel);
  132.     innerPanel.add(Box.createRigidArea(HGAP20));
  133.     }
  134.  
  135.     public JFileChooser createFileChooser() {
  136.     // create a filechooser
  137.     JFileChooser fc = new JFileChooser();
  138.     
  139.     // set the current directory to be the images directory
  140.     File swingFile = new File("resources/images/About.jpg");
  141.     if(swingFile.exists()) {
  142.         fc.setCurrentDirectory(swingFile);
  143.         fc.setSelectedFile(swingFile);
  144.     }
  145.     
  146.     return fc;
  147.     }
  148.  
  149.  
  150.     public JButton createPlainFileChooserButton() {
  151.     Action a = new AbstractAction(getString("FileChooserDemo.plainbutton")) {
  152.         public void actionPerformed(ActionEvent e) {
  153.         JFileChooser fc = createFileChooser();
  154.  
  155.         // show the filechooser
  156.         int result = fc.showOpenDialog(getDemoPanel());
  157.         
  158.         // if we selected an image, load the image
  159.         if(result == JFileChooser.APPROVE_OPTION) {
  160.             loadImage(fc.getSelectedFile().getPath());
  161.         }
  162.         }
  163.     };
  164.     return createButton(a);
  165.     }
  166.  
  167.     public JButton createPreviewFileChooserButton() {
  168.     Action a = new AbstractAction(getString("FileChooserDemo.previewbutton")) {
  169.         public void actionPerformed(ActionEvent e) {
  170.         JFileChooser fc = createFileChooser();
  171.  
  172.         // Add filefilter & fileview
  173.         ExampleFileFilter filter = new ExampleFileFilter(
  174.             new String[] {"jpg", "gif"}, getString("FileChooserDemo.filterdescription")
  175.         );
  176.         ExampleFileView fileView = new ExampleFileView();
  177.         fileView.putIcon("jpg", jpgIcon);
  178.         fileView.putIcon("gif", gifIcon);
  179.         fc.setFileView(fileView);
  180.         fc.addChoosableFileFilter(filter);
  181.         fc.setFileFilter(filter);
  182.         
  183.         // add preview accessory
  184.         fc.setAccessory(new FilePreviewer(fc));
  185.  
  186.         // show the filechooser
  187.         int result = fc.showOpenDialog(getDemoPanel());
  188.         
  189.         // if we selected an image, load the image
  190.         if(result == JFileChooser.APPROVE_OPTION) {
  191.             loadImage(fc.getSelectedFile().getPath());
  192.         }
  193.         }
  194.     };
  195.     return createButton(a);
  196.     }
  197.  
  198.     JDialog dialog;
  199.     JFileChooser fc;
  200.  
  201.     public JButton createCustomFileChooserButton() {
  202.     Action a = new AbstractAction(getString("FileChooserDemo.custombutton")) {
  203.         public void actionPerformed(ActionEvent e) {
  204.         fc = createFileChooser();
  205.  
  206.         // Add filefilter & fileview
  207.         ExampleFileFilter filter = new ExampleFileFilter(
  208.             new String[] {"jpg", "gif"}, getString("FileChooserDemo.filterdescription")
  209.         );
  210.         ExampleFileView fileView = new ExampleFileView();
  211.         fileView.putIcon("jpg", jpgIcon);
  212.         fileView.putIcon("gif", gifIcon);
  213.         fc.setFileView(fileView);
  214.         fc.addChoosableFileFilter(filter);
  215.  
  216.         // add preview accessory
  217.         fc.setAccessory(new FilePreviewer(fc));
  218.  
  219.         // remove the approve/cancel buttons
  220.         fc.setControlButtonsAreShown(false);
  221.  
  222.         // make custom controls
  223.         //wokka
  224.         JPanel custom = new JPanel();
  225.         custom.setLayout(new BoxLayout(custom, BoxLayout.Y_AXIS));
  226.         custom.add(Box.createRigidArea(VGAP10));
  227.         JLabel description = new JLabel(getString("FileChooserDemo.description"));
  228.         description.setAlignmentX(JLabel.CENTER_ALIGNMENT);
  229.         custom.add(description);
  230.         custom.add(Box.createRigidArea(VGAP10));
  231.         custom.add(fc);
  232.  
  233.         Action okAction = createOKAction();
  234.         fc.addActionListener(okAction);
  235.  
  236.         JPanel buttons = new JPanel();
  237.         buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
  238.         buttons.add(Box.createRigidArea(HGAP10));
  239.         buttons.add(createImageButton(createFindAction()));
  240.         buttons.add(Box.createRigidArea(HGAP10));
  241.         buttons.add(createButton(createAboutAction()));
  242.         buttons.add(Box.createRigidArea(HGAP10));
  243.         buttons.add(createButton(okAction));
  244.         buttons.add(Box.createRigidArea(HGAP10));
  245.         buttons.add(createButton(createCancelAction()));
  246.         buttons.add(Box.createRigidArea(HGAP10));
  247.         buttons.add(createImageButton(createHelpAction()));
  248.         buttons.add(Box.createRigidArea(HGAP10));
  249.  
  250.         custom.add(buttons);
  251.         custom.add(Box.createRigidArea(VGAP10));
  252.         
  253.         // show the filechooser
  254.         Frame parent = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, getDemoPanel());
  255.         dialog = new JDialog(parent, getString("FileChooserDemo.dialogtitle"), true);
  256.                 dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  257.         dialog.getContentPane().add(custom, BorderLayout.CENTER);
  258.         dialog.pack();
  259.         dialog.setLocationRelativeTo(getDemoPanel());
  260.         dialog.show();
  261.         }
  262.     };
  263.     return createButton(a);
  264.     }
  265.  
  266.     public Action createAboutAction() {
  267.     return new AbstractAction(getString("FileChooserDemo.about")) {
  268.         public void actionPerformed(ActionEvent e) {
  269.         File file = fc.getSelectedFile();
  270.         String text;
  271.         if(file == null) {
  272.             text = getString("FileChooserDemo.nofileselected");
  273.         } else {
  274.             text = "<html>" + getString("FileChooserDemo.thefile");
  275.             text += "<br><font color=green>" + file.getName() + "</font><br>";
  276.             text += getString("FileChooserDemo.isprobably") + "</html>";
  277.         }
  278.         JOptionPane.showMessageDialog(getDemoPanel(), text);
  279.         }
  280.     };
  281.     }
  282.  
  283.     public Action createOKAction() {
  284.     return new AbstractAction(getString("FileChooserDemo.ok")) {
  285.         public void actionPerformed(ActionEvent e) {
  286.         dialog.dispose();
  287.         if (!e.getActionCommand().equals(JFileChooser.CANCEL_SELECTION)
  288.             && fc.getSelectedFile() != null) {
  289.  
  290.             loadImage(fc.getSelectedFile().getPath());
  291.         }
  292.         }
  293.     };
  294.     }
  295.  
  296.     public Action createCancelAction() {
  297.     return new AbstractAction(getString("FileChooserDemo.cancel")) {
  298.         public void actionPerformed(ActionEvent e) {
  299.         dialog.dispose();
  300.         }
  301.     };
  302.     }
  303.  
  304.     public Action createFindAction() {
  305.     Icon icon = createImageIcon("filechooser/find.gif", getString("FileChooserDemo.find"));
  306.     return new AbstractAction("", icon) {
  307.         public void actionPerformed(ActionEvent e) {
  308.                 String result = JOptionPane.showInputDialog(getDemoPanel(), getString("FileChooserDemo.findquestion"));
  309.         if (result != null) {
  310.             JOptionPane.showMessageDialog(getDemoPanel(), getString("FileChooserDemo.findresponse"));
  311.         }
  312.         }
  313.     };
  314.     }
  315.  
  316.     public Action createHelpAction() {
  317.     Icon icon = createImageIcon("filechooser/help.gif", getString("FileChooserDemo.help"));
  318.     return new AbstractAction("", icon) {
  319.         public void actionPerformed(ActionEvent e) {
  320.         JOptionPane.showMessageDialog(getDemoPanel(), getString("FileChooserDemo.helptext"));
  321.         }
  322.     };
  323.     }
  324.     
  325.     class MyImageIcon extends ImageIcon {
  326.     public MyImageIcon(String filename) {
  327.         super(filename);
  328.     };
  329.     public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
  330.         g.setColor(Color.white);
  331.         g.fillRect(0,0, c.getWidth(), c.getHeight());
  332.         if(getImageObserver() == null) {
  333.         g.drawImage(
  334.             getImage(),
  335.             c.getWidth()/2 - getIconWidth()/2,
  336.             c.getHeight()/2 - getIconHeight()/2,
  337.             c
  338.         );
  339.         } else {
  340.         g.drawImage(
  341.             getImage(),
  342.             c.getWidth()/2 - getIconWidth()/2,
  343.             c.getHeight()/2 - getIconHeight()/2,
  344.             getImageObserver()
  345.         );
  346.         }
  347.     }
  348.     }
  349.  
  350.     public void loadImage(String filename) {
  351.     theImage.setIcon(new MyImageIcon(filename));
  352.     }
  353.  
  354.     public JButton createButton(Action a) {
  355.     JButton b = new JButton(a) {
  356.         public Dimension getMaximumSize() {
  357.         int width = Short.MAX_VALUE;
  358.         int height = super.getMaximumSize().height;
  359.         return new Dimension(width, height);
  360.         }
  361.     };
  362.     return b;
  363.     }
  364.  
  365.     public JButton createImageButton(Action a) {
  366.     JButton b = new JButton(a);
  367.     b.setMargin(new Insets(0,0,0,0));
  368.     return b;
  369.     }
  370. }
  371.  
  372. class FilePreviewer extends JComponent implements PropertyChangeListener {
  373.     ImageIcon thumbnail = null;
  374.     
  375.     public FilePreviewer(JFileChooser fc) {
  376.     setPreferredSize(new Dimension(100, 50));
  377.     fc.addPropertyChangeListener(this);
  378.     setBorder(new BevelBorder(BevelBorder.LOWERED));
  379.     }
  380.     
  381.     public void loadImage(File f) {
  382.         if (f == null) {
  383.             thumbnail = null;
  384.         } else {
  385.         ImageIcon tmpIcon = new ImageIcon(f.getPath());
  386.         if(tmpIcon.getIconWidth() > 90) {
  387.         thumbnail = new ImageIcon(
  388.             tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
  389.         } else {
  390.         thumbnail = tmpIcon;
  391.         }
  392.     }
  393.     }
  394.     
  395.     public void propertyChange(PropertyChangeEvent e) {
  396.     String prop = e.getPropertyName();
  397.     if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
  398.         if(isShowing()) {
  399.                 loadImage((File) e.getNewValue());
  400.         repaint();
  401.         }
  402.     }
  403.     }
  404.     
  405.     public void paint(Graphics g) {
  406.     super.paint(g);
  407.     if(thumbnail != null) {
  408.         int x = getWidth()/2 - thumbnail.getIconWidth()/2;
  409.         int y = getHeight()/2 - thumbnail.getIconHeight()/2;
  410.         if(y < 0) {
  411.         y = 0;
  412.         }
  413.         
  414.         if(x < 5) {
  415.         x = 5;
  416.         }
  417.         thumbnail.paintIcon(this, g, x, y);
  418.     }
  419.     }
  420. }
  421.  
  422.