jdk/src/share/demo/jfc/FileChooserDemo/FileChooserDemo.java
author tbell
Fri, 27 Feb 2009 10:54:11 -0800
changeset 2091 7faffd237305
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.filechooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.FileChooserUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.basic.BasicFileChooserUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import static javax.swing.JFileChooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * A demo which makes extensive use of the file chooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class FileChooserDemo extends JPanel implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static JFrame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final Vector<SupportedLaF> supportedLaFs = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static class SupportedLaF {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        private final LookAndFeel laf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        SupportedLaF(String name, LookAndFeel laf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.laf = laf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private JButton showButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private JCheckBox showAllFilesFilterCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private JCheckBox showImageFilesFilterCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private JCheckBox showFullDescriptionCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private JCheckBox useFileViewCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private JCheckBox useFileSystemViewCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private JCheckBox accessoryCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private JCheckBox setHiddenCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private JCheckBox useEmbedInWizardCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private JCheckBox useControlsCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private JCheckBox enableDragCheckBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private JRadioButton singleSelectionRadioButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private JRadioButton multiSelectionRadioButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private JRadioButton openRadioButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private JRadioButton saveRadioButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private JRadioButton customButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private JComboBox lafComboBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private JRadioButton justFilesRadioButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private JRadioButton justDirectoriesRadioButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private JRadioButton bothFilesAndDirectoriesRadioButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private JTextField customField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private final ExampleFileView fileView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private final ExampleFileSystemView fileSystemView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private final static Dimension hpad10 = new Dimension(10,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private final static Dimension vpad20 = new Dimension(1,20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private final static Dimension vpad7 = new Dimension(1, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private final static Dimension vpad4 = new Dimension(1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private final static Insets insets = new Insets(5, 10, 0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private final FilePreviewer previewer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private final JFileChooser chooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public FileChooserDemo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        UIManager.LookAndFeelInfo[] installedLafs = UIManager.getInstalledLookAndFeels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (UIManager.LookAndFeelInfo lafInfo : installedLafs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                Class lnfClass = Class.forName(lafInfo.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                LookAndFeel laf = (LookAndFeel)(lnfClass.newInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (laf.isSupportedLookAndFeel()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    String name = lafInfo.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    supportedLaFs.add(new SupportedLaF(name, laf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } catch (Exception e) { // If ANYTHING weird happens, don't add it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        chooser = new JFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        previewer = new FilePreviewer(chooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // Create Custom FileView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        fileView = new ExampleFileView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        fileView.putIcon("jpg", new ImageIcon(getClass().getResource("/resources/images/jpgIcon.jpg")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        fileView.putIcon("gif", new ImageIcon(getClass().getResource("/resources/images/gifIcon.gif")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // Create Custom FileSystemView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        fileSystemView = new ExampleFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // create a radio listener to listen to option changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        OptionListener optionListener = new OptionListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // Create options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        openRadioButton = new JRadioButton("Open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        openRadioButton.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        openRadioButton.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        saveRadioButton = new JRadioButton("Save");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        saveRadioButton.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        customButton = new JRadioButton("Custom");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        customButton.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        customField = new JTextField(8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            public Dimension getMaximumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                return new Dimension(getPreferredSize().width, getPreferredSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        customField.setText("Doit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        customField.setAlignmentY(JComponent.TOP_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        customField.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        customField.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        ButtonGroup group1 = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        group1.add(openRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        group1.add(saveRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        group1.add(customButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // filter buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        showAllFilesFilterCheckBox = new JCheckBox("Show \"All Files\" Filter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        showAllFilesFilterCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        showAllFilesFilterCheckBox.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        showImageFilesFilterCheckBox = new JCheckBox("Show JPG and GIF Filters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        showImageFilesFilterCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        showImageFilesFilterCheckBox.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        accessoryCheckBox = new JCheckBox("Show Preview");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        accessoryCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        accessoryCheckBox.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // more options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        setHiddenCheckBox = new JCheckBox("Show Hidden Files");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        setHiddenCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        showFullDescriptionCheckBox = new JCheckBox("With File Extensions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        showFullDescriptionCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        showFullDescriptionCheckBox.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        showFullDescriptionCheckBox.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        useFileViewCheckBox = new JCheckBox("Use FileView");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        useFileViewCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        useFileViewCheckBox.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        useFileSystemViewCheckBox = new JCheckBox("Use FileSystemView", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        useFileSystemViewCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        useEmbedInWizardCheckBox = new JCheckBox("Embed in Wizard");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        useEmbedInWizardCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        useEmbedInWizardCheckBox.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        useControlsCheckBox = new JCheckBox("Show Control Buttons");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        useControlsCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        useControlsCheckBox.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        enableDragCheckBox = new JCheckBox("Enable Dragging");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        enableDragCheckBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // File or Directory chooser options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        ButtonGroup group3 = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        justFilesRadioButton = new JRadioButton("Just Select Files");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        justFilesRadioButton.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        group3.add(justFilesRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        justFilesRadioButton.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        justDirectoriesRadioButton = new JRadioButton("Just Select Directories");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        group3.add(justDirectoriesRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        justDirectoriesRadioButton.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        bothFilesAndDirectoriesRadioButton = new JRadioButton("Select Files or Directories");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        group3.add(bothFilesAndDirectoriesRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        bothFilesAndDirectoriesRadioButton.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        singleSelectionRadioButton = new JRadioButton("Single Selection", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        singleSelectionRadioButton.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        multiSelectionRadioButton = new JRadioButton("Multi Selection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        multiSelectionRadioButton.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        ButtonGroup group4 = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        group4.add(singleSelectionRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        group4.add(multiSelectionRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // Create show button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        showButton = new JButton("Show FileChooser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        showButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        showButton.setMnemonic('s');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // Create laf combo box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        lafComboBox = new JComboBox(supportedLaFs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        lafComboBox.setEditable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        lafComboBox.addActionListener(optionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // ******************** Dialog Type ***********************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        JPanel control1 = new InsetPanel(insets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        control1.setBorder(BorderFactory.createTitledBorder("Dialog Type"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        control1.setLayout(new BoxLayout(control1, BoxLayout.Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        control1.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        control1.add(openRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        control1.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        control1.add(saveRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        control1.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        control1.add(customButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        control1.add(Box.createRigidArea(vpad4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        JPanel fieldWrapper = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        fieldWrapper.setLayout(new BoxLayout(fieldWrapper, BoxLayout.X_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        fieldWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        fieldWrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        fieldWrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        fieldWrapper.add(customField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        control1.add(fieldWrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        control1.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        control1.add(Box.createGlue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // ***************** Filter Controls **********************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        JPanel control2 = new InsetPanel(insets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        control2.setBorder(BorderFactory.createTitledBorder("Filter Controls"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        control2.setLayout(new BoxLayout(control2, BoxLayout.Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        control2.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        control2.add(showAllFilesFilterCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        control2.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        control2.add(showImageFilesFilterCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        control2.add(Box.createRigidArea(vpad4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        JPanel checkWrapper = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        checkWrapper.setLayout(new BoxLayout(checkWrapper, BoxLayout.X_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        checkWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        checkWrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        checkWrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        checkWrapper.add(showFullDescriptionCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        control2.add(checkWrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        control2.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        control2.add(Box.createGlue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // ****************** Display Options *********************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        JPanel control3 = new InsetPanel(insets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        control3.setBorder(BorderFactory.createTitledBorder("Display Options"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        control3.setLayout(new BoxLayout(control3, BoxLayout.Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        control3.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        control3.add(setHiddenCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        control3.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        control3.add(useFileViewCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        control3.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        control3.add(useFileSystemViewCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        control3.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        control3.add(accessoryCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        control3.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        control3.add(useEmbedInWizardCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        control3.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        control3.add(useControlsCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        control3.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        control3.add(enableDragCheckBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        control3.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        control3.add(Box.createGlue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // ************* File & Directory Options *****************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        JPanel control4 = new InsetPanel(insets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        control4.setBorder(BorderFactory.createTitledBorder("File and Directory Options"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        control4.setLayout(new BoxLayout(control4, BoxLayout.Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        control4.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        control4.add(justFilesRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        control4.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        control4.add(justDirectoriesRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        control4.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        control4.add(bothFilesAndDirectoriesRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        control4.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        control4.add(singleSelectionRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        control4.add(Box.createRigidArea(vpad7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        control4.add(multiSelectionRadioButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        control4.add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        control4.add(Box.createGlue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // **************** Look & Feel Switch ********************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        JPanel panel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        panel.add(new JLabel("Look and Feel: "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        panel.add(lafComboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        panel.add(showButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // ****************** Wrap 'em all up *********************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // ********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        JPanel wrapper = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.X_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        wrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        wrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        wrapper.add(control1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        wrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        wrapper.add(control2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        wrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        wrapper.add(control3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        wrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        wrapper.add(control4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        wrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        wrapper.add(Box.createRigidArea(hpad10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        add(wrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        add(panel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        add(Box.createRigidArea(vpad20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (customButton.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            chooser.setApproveButtonText(customField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (chooser.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            chooser.setSelectedFiles(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            chooser.setSelectedFile(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // clear the preview from the previous display of the chooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        JComponent accessory = chooser.getAccessory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (accessory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            ((FilePreviewer)accessory).loadImage(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (useEmbedInWizardCheckBox.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            WizardDialog wizard = new WizardDialog(frame, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            wizard.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            wizard.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int retval = chooser.showDialog(frame, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (retval == APPROVE_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            JOptionPane.showMessageDialog(frame, getResultString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        } else if (retval == CANCEL_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            JOptionPane.showMessageDialog(frame, "User cancelled operation. No file was chosen.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } else if (retval == ERROR_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            JOptionPane.showMessageDialog(frame, "An error occured. No file was chosen.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            JOptionPane.showMessageDialog(frame, "Unknown operation occured.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private void resetFileFilters(boolean enableFilters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                  boolean showExtensionInDescription) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        chooser.resetChoosableFileFilters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (enableFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            FileFilter jpgFilter = createFileFilter("JPEG Compressed Image Files",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                         showExtensionInDescription, "jpg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            FileFilter gifFilter = createFileFilter("GIF Image Files",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                         showExtensionInDescription, "gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            FileFilter bothFilter = createFileFilter("JPEG and GIF Image Files",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                          showExtensionInDescription, "jpg",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                          "gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            chooser.addChoosableFileFilter(bothFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            chooser.addChoosableFileFilter(jpgFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            chooser.addChoosableFileFilter(gifFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    private FileFilter createFileFilter(String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            boolean showExtensionInDescription, String...extensions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (showExtensionInDescription) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            description = createFileNameFilterDescriptionFromExtensions(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    description, extensions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return new FileNameExtensionFilter(description, extensions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private String createFileNameFilterDescriptionFromExtensions(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            String description, String[] extensions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        String fullDescription = (description == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                "(" : description + " (";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        // build the description from the extension list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        fullDescription += "." + extensions[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        for (int i = 1; i < extensions.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            fullDescription += ", .";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            fullDescription += extensions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        fullDescription += ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return fullDescription;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    private class WizardDialog extends JDialog implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        CardLayout cardLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        JPanel cardPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        JLabel messageLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        JButton backButton, nextButton, closeButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        WizardDialog(JFrame frame, boolean modal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            super(frame, "Embedded JFileChooser Demo", modal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            cardLayout = new CardLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            cardPanel = new JPanel(cardLayout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            getContentPane().add(cardPanel, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            messageLabel = new JLabel("", JLabel.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            cardPanel.add(chooser, "fileChooser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            cardPanel.add(messageLabel, "label");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            cardLayout.show(cardPanel, "fileChooser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            chooser.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            JPanel buttonPanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            backButton = new JButton("< Back");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            nextButton = new JButton("Next >");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            closeButton = new JButton("Close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            buttonPanel.add(backButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            buttonPanel.add(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            buttonPanel.add(closeButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            backButton.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            getRootPane().setDefaultButton(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            backButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            nextButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            closeButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            setLocationRelativeTo(frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            Object src = evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            String cmd = evt.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (src == backButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                back();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            } else if (src == nextButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                FileChooserUI ui = chooser.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                if (ui instanceof BasicFileChooserUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    // Workaround for bug 4528663. This is necessary to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    // pick up the contents of the file chooser text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    // This will trigger an APPROVE_SELECTION action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    ((BasicFileChooserUI)ui).getApproveSelectionAction().actionPerformed(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            } else if (src == closeButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            } else if (cmd == APPROVE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            } else if (cmd == CANCEL_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        private void back() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            backButton.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            nextButton.setEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            cardLayout.show(cardPanel, "fileChooser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            getRootPane().setDefaultButton(nextButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            chooser.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        private void next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            backButton.setEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            nextButton.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            messageLabel.setText(getResultString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            cardLayout.show(cardPanel, "label");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            getRootPane().setDefaultButton(closeButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            closeButton.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        private void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            chooser.removeActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            // The chooser is hidden by CardLayout on remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            // so fix it here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            cardPanel.remove(chooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            chooser.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            super.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private String getResultString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        String resultString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        String filter = chooser.getFileFilter().getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        String path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        boolean isDirMode = (chooser.getFileSelectionMode() == DIRECTORIES_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        boolean isMulti = chooser.isMultiSelectionEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (isMulti) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            File [] files = chooser.getSelectedFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            if (files != null && files.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                for (File file : files) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    path = path + "<br>" + file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            File file = chooser.getSelectedFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (file != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                path = "<br>" + file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            path = path.replace(" ", "&nbsp;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            filter = filter.replace(" ", "&nbsp;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            resultString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                "<html>You chose " + (isMulti ? "these" : "this") + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                (isDirMode ? (isMulti ? "directories" : "directory")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                           : (isMulti ? "files" : "file")) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                ": <code>" + path +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                "</code><br><br>with filter: <br><code>" + filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            resultString = "Nothing was chosen";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return resultString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /** An ActionListener that listens to the radio buttons. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    private class OptionListener implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            JComponent c = (JComponent) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            boolean selected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            if (c instanceof JToggleButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                selected = ((JToggleButton)c).isSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if (c == openRadioButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                chooser.setDialogType(OPEN_DIALOG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                customField.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            } else if (c == useEmbedInWizardCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                useControlsCheckBox.setEnabled(!selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                useControlsCheckBox.setSelected(!selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                chooser.setControlButtonsAreShown(!selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            } else if (c == useControlsCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                chooser.setControlButtonsAreShown(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            } else if (c == enableDragCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                chooser.setDragEnabled(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            } else if (c == saveRadioButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                chooser.setDialogType(SAVE_DIALOG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                customField.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            } else if (c == customButton || c == customField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                customField.setEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                chooser.setDialogType(CUSTOM_DIALOG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            } else if (c == showAllFilesFilterCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                chooser.setAcceptAllFileFilterUsed(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            } else if (c == showImageFilesFilterCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                resetFileFilters(selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                 showFullDescriptionCheckBox.isSelected());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                showFullDescriptionCheckBox.setEnabled(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            } else if (c == setHiddenCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                chooser.setFileHidingEnabled(!selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            } else if (c == accessoryCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    chooser.setAccessory(previewer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    chooser.setAccessory(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            } else if (c == useFileViewCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    chooser.setFileView(fileView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    chooser.setFileView(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            } else if (c == useFileSystemViewCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    chooser.setFileSystemView(fileSystemView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    // Restore default behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    chooser.setFileSystemView(FileSystemView.getFileSystemView());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            } else if (c == showFullDescriptionCheckBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                resetFileFilters(showImageFilesFilterCheckBox.isSelected(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                 selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            } else if (c == justFilesRadioButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                chooser.setFileSelectionMode(FILES_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            } else if (c == justDirectoriesRadioButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                chooser.setFileSelectionMode(DIRECTORIES_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            } else if (c == bothFilesAndDirectoriesRadioButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                chooser.setFileSelectionMode(FILES_AND_DIRECTORIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            } else if (c == singleSelectionRadioButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    chooser.setMultiSelectionEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            } else if (c == multiSelectionRadioButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    chooser.setMultiSelectionEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            } else if (c == lafComboBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                SupportedLaF supportedLaF = ((SupportedLaF)lafComboBox.getSelectedItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                LookAndFeel laf = supportedLaF.laf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    UIManager.setLookAndFeel(laf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    SwingUtilities.updateComponentTreeUI(frame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    if(chooser != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        SwingUtilities.updateComponentTreeUI(chooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                } catch (UnsupportedLookAndFeelException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    // This should not happen because we already checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    ((DefaultComboBoxModel)lafComboBox.getModel()).removeElement(supportedLaF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    private class FilePreviewer extends JComponent implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        ImageIcon thumbnail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        public FilePreviewer(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            setPreferredSize(new Dimension(100, 50));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            fc.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        public void loadImage(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                thumbnail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                ImageIcon tmpIcon = new ImageIcon(f.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                if(tmpIcon.getIconWidth() > 90) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    thumbnail = new ImageIcon(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    thumbnail = tmpIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            String prop = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (prop == SELECTED_FILE_CHANGED_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                if(isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    loadImage((File) e.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            if(thumbnail != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                int x = getWidth()/2 - thumbnail.getIconWidth()/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                int y = getHeight()/2 - thumbnail.getIconHeight()/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                if(y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                if(x < 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    x = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                thumbnail.paintIcon(this, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public static void main(String s[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
           NOTE: By default, the look and feel will be set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
           Cross Platform Look and Feel (which is currently Metal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
           The user may someday be able to override the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
           via a system property. If you as the developer want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
           be sure that a particular L&F is set, you can do so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
           by calling UIManager.setLookAndFeel(). For example, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
           first code snippet below forcibly sets the UI to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
           System Look and Feel. The second code snippet forcibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
           sets the look and feel to the Cross Platform L&F.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
           Snippet 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
           } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
              System.err.println("Error loading L&F: " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
           Snippet 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
              UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
           } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
              System.err.println("Error loading L&F: " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        FileChooserDemo panel = new FileChooserDemo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        frame = new JFrame("FileChooserDemo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        frame.getContentPane().add("Center", panel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private static class InsetPanel extends JPanel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        Insets i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        InsetPanel(Insets i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            this.i = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        public Insets getInsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
}