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