jdk/src/share/classes/sun/swing/plaf/synth/SynthFileChooserUIImpl.java
author alexsch
Fri, 24 Jan 2014 16:23:22 +0400
changeset 23270 b5073d208bbf
parent 20100 d127097b7717
child 23281 93c0a5484bb5
permissions -rw-r--r--
8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API Reviewed-by: pchelko, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5597
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
2491
123954ad0034 6738668: JFileChooser cannot be created under SecurityManager
rupashka
parents: 1853
diff changeset
    32
import java.security.AccessController;
123954ad0034 6738668: JFileChooser cannot be created under SecurityManager
rupashka
parents: 1853
diff changeset
    33
import java.security.PrivilegedAction;
2
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.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.filechooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.filechooser.FileFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.plaf.synth.*;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
    41
import javax.swing.plaf.ActionMapUIResource;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.awt.shell.ShellFolder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Synth FileChooserUI implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Note that the classes in the com.sun.java.swing.plaf.synth
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * package are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * part of the core Java APIs. They are a part of Sun's JDK and JRE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * distributions. Although other licensees may choose to distribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * these classes, developers cannot depend on their availability in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * non-Sun implementations. Additionally this API may change in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * incompatible ways between releases. While this class is public, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * shoud be considered an implementation detail, and subject to change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Leif Samuelsson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class SynthFileChooserUIImpl extends SynthFileChooserUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private JLabel lookInLabel;
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
    63
    private JComboBox<File> directoryComboBox;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private DirectoryComboBoxModel directoryComboBoxModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private Action directoryComboBoxAction = new DirectoryComboBoxAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private FilterComboBoxModel filterComboBoxModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private JTextField fileNameTextField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private FilePane filePane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private JToggleButton listViewButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private JToggleButton detailsViewButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean readOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private JPanel buttonPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private JPanel bottomPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
    80
    private JComboBox<FileFilter> filterComboBox;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final Dimension hstrut5 = new Dimension(5, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final Insets shrinkwrap = new Insets(0,0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Preferred and Minimum sizes for the dialog box
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static Dimension LIST_PREF_SIZE = new Dimension(405, 135);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // Labels, mnemonics, and tooltips (oh my!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private int    lookInLabelMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private String lookInLabelText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private String saveInLabelText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private int    fileNameLabelMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private String fileNameLabelText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private int    folderNameLabelMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private String folderNameLabelText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int    filesOfTypeLabelMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private String filesOfTypeLabelText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private String upFolderToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private String upFolderAccessibleName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private String homeFolderToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private String homeFolderAccessibleName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private String newFolderToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private String newFolderAccessibleName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private String listViewButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private String listViewButtonAccessibleName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private String detailsViewButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private String detailsViewButtonAccessibleName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private AlignedLabel fileNameLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private final PropertyChangeListener modeListener = new PropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (fileNameLabel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                populateFileNameLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private void populateFileNameLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (getFileChooser().getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            fileNameLabel.setText(folderNameLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            fileNameLabel.setDisplayedMnemonic(folderNameLabelMnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            fileNameLabel.setText(fileNameLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            fileNameLabel.setDisplayedMnemonic(fileNameLabelMnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public SynthFileChooserUIImpl(JFileChooser b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        super(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private class SynthFileChooserUIAccessor implements FilePane.FileChooserUIAccessor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public JFileChooser getFileChooser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return SynthFileChooserUIImpl.this.getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public BasicDirectoryModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return SynthFileChooserUIImpl.this.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        public JPanel createList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public JPanel createDetailsView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        public boolean isDirectorySelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return SynthFileChooserUIImpl.this.isDirectorySelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public File getDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return SynthFileChooserUIImpl.this.getDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public Action getChangeToParentDirectoryAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return SynthFileChooserUIImpl.this.getChangeToParentDirectoryAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public Action getApproveSelectionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return SynthFileChooserUIImpl.this.getApproveSelectionAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public Action getNewFolderAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return SynthFileChooserUIImpl.this.getNewFolderAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        public MouseListener createDoubleClickListener(JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return SynthFileChooserUIImpl.this.createDoubleClickListener(getFileChooser(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                                                     list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        public ListSelectionListener createListSelectionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return SynthFileChooserUIImpl.this.createListSelectionListener(getFileChooser());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    protected void installDefaults(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        super.installDefaults(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        readOnly = UIManager.getBoolean("FileChooser.readOnly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public void installComponents(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        super.installComponents(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        SynthContext context = getContext(fc, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        fc.setLayout(new BorderLayout(0, 11));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // ********************************* //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // **** Construct the top panel **** //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // ********************************* //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // Directory manipulation buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        JPanel topPanel = new JPanel(new BorderLayout(11, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    JPanel topButtonPanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    topButtonPanel.setLayout(new BoxLayout(topButtonPanel, BoxLayout.LINE_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    topPanel.add(topButtonPanel, BorderLayout.AFTER_LINE_ENDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Add the top panel to the fileChooser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        fc.add(topPanel, BorderLayout.NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // ComboBox Label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        lookInLabel = new JLabel(lookInLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        lookInLabel.setDisplayedMnemonic(lookInLabelMnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        topPanel.add(lookInLabel, BorderLayout.BEFORE_LINE_BEGINS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // CurrentDir ComboBox
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   219
        directoryComboBox = new JComboBox<File>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        directoryComboBox.getAccessibleContext().setAccessibleDescription(lookInLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        directoryComboBox.putClientProperty( "JComboBox.isTableCellEditor", Boolean.TRUE );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        lookInLabel.setLabelFor(directoryComboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        directoryComboBoxModel = createDirectoryComboBoxModel(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        directoryComboBox.setModel(directoryComboBoxModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        directoryComboBox.addActionListener(directoryComboBoxAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        directoryComboBox.setRenderer(createDirectoryComboBoxRenderer(fc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        directoryComboBox.setAlignmentX(JComponent.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        directoryComboBox.setAlignmentY(JComponent.TOP_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        directoryComboBox.setMaximumRowCount(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        topPanel.add(directoryComboBox, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        filePane = new FilePane(new SynthFileChooserUIAccessor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        fc.addPropertyChangeListener(filePane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Add 'Go Up' to context menu, plus 'Go Home' if on Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        JPopupMenu contextMenu = filePane.getComponentPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (contextMenu != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            contextMenu.insert(getChangeToParentDirectoryAction(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (File.separatorChar == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                contextMenu.insert(getGoHomeAction(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    FileSystemView fsv = fc.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    // Up Button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    JButton upFolderButton = new JButton(getChangeToParentDirectoryAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    upFolderButton.setText(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    upFolderButton.setIcon(upFolderIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    upFolderButton.setToolTipText(upFolderToolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    upFolderButton.getAccessibleContext().setAccessibleName(upFolderAccessibleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    upFolderButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    upFolderButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    upFolderButton.setMargin(shrinkwrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    topButtonPanel.add(upFolderButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    topButtonPanel.add(Box.createRigidArea(hstrut5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    // Home Button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    File homeDir = fsv.getHomeDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    String toolTipText = homeFolderToolTipText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    if (fsv.isRoot(homeDir)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        toolTipText = getFileView(fc).getName(homeDir); // Probably "Desktop".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    JButton b = new JButton(homeFolderIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    b.setToolTipText(toolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    b.getAccessibleContext().setAccessibleName(homeFolderAccessibleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    b.setAlignmentX(JComponent.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    b.setAlignmentY(JComponent.CENTER_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    b.setMargin(shrinkwrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    b.addActionListener(getGoHomeAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    topButtonPanel.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    topButtonPanel.add(Box.createRigidArea(hstrut5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // New Directory Button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    if (!readOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        b = new JButton(filePane.getNewFolderAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        b.setText(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        b.setIcon(newFolderIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        b.setToolTipText(newFolderToolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        b.getAccessibleContext().setAccessibleName(newFolderAccessibleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        b.setAlignmentX(JComponent.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        b.setAlignmentY(JComponent.CENTER_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        b.setMargin(shrinkwrap);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   287
        topButtonPanel.add(b);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   288
        topButtonPanel.add(Box.createRigidArea(hstrut5));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // View button group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    ButtonGroup viewButtonGroup = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    // List Button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    listViewButton = new JToggleButton(listViewIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    listViewButton.setToolTipText(listViewButtonToolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    listViewButton.getAccessibleContext().setAccessibleName(listViewButtonAccessibleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    listViewButton.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    listViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    listViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    listViewButton.setMargin(shrinkwrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    listViewButton.addActionListener(filePane.getViewTypeAction(FilePane.VIEWTYPE_LIST));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    topButtonPanel.add(listViewButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    viewButtonGroup.add(listViewButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    // Details Button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    detailsViewButton = new JToggleButton(detailsViewIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    detailsViewButton.setToolTipText(detailsViewButtonToolTipText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    detailsViewButton.getAccessibleContext().setAccessibleName(detailsViewButtonAccessibleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    detailsViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    detailsViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    detailsViewButton.setMargin(shrinkwrap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    detailsViewButton.addActionListener(filePane.getViewTypeAction(FilePane.VIEWTYPE_DETAILS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    topButtonPanel.add(detailsViewButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    viewButtonGroup.add(detailsViewButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    filePane.addPropertyChangeListener(new PropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if ("viewType".equals(e.getPropertyName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                int viewType = filePane.getViewType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                switch (viewType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    case FilePane.VIEWTYPE_LIST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        listViewButton.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    case FilePane.VIEWTYPE_DETAILS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        detailsViewButton.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // ************************************** //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // ******* Add the directory pane ******* //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // ************************************** //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        fc.add(getAccessoryPanel(), BorderLayout.AFTER_LINE_ENDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        JComponent accessory = fc.getAccessory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (accessory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            getAccessoryPanel().add(accessory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        filePane.setPreferredSize(LIST_PREF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        fc.add(filePane, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // ********************************** //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // **** Construct the bottom panel ** //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // ********************************** //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        bottomPanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        fc.add(bottomPanel, BorderLayout.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // FileName label and textfield
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        JPanel fileNamePanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        fileNamePanel.setLayout(new BoxLayout(fileNamePanel, BoxLayout.LINE_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        bottomPanel.add(fileNamePanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        bottomPanel.add(Box.createRigidArea(new Dimension(1, 5)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        fileNameLabel = new AlignedLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        populateFileNameLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        fileNamePanel.add(fileNameLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        fileNameTextField = new JTextField(35) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            public Dimension getMaximumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        fileNamePanel.add(fileNameTextField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        fileNameLabel.setLabelFor(fileNameTextField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        fileNameTextField.addFocusListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            new FocusAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    if (!getFileChooser().isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        filePane.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (fc.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            setFileName(fileNameString(fc.getSelectedFiles()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            setFileName(fileNameString(fc.getSelectedFile()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        // Filetype label and combobox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        JPanel filesOfTypePanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        filesOfTypePanel.setLayout(new BoxLayout(filesOfTypePanel, BoxLayout.LINE_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        bottomPanel.add(filesOfTypePanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        AlignedLabel filesOfTypeLabel = new AlignedLabel(filesOfTypeLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        filesOfTypeLabel.setDisplayedMnemonic(filesOfTypeLabelMnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        filesOfTypePanel.add(filesOfTypeLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        filterComboBoxModel = createFilterComboBoxModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        fc.addPropertyChangeListener(filterComboBoxModel);
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   396
        filterComboBox = new JComboBox<FileFilter>(filterComboBoxModel);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        filterComboBox.getAccessibleContext().setAccessibleDescription(filesOfTypeLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        filesOfTypeLabel.setLabelFor(filterComboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        filterComboBox.setRenderer(createFilterComboBoxRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        filesOfTypePanel.add(filterComboBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        buttonPanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        buttonPanel.setLayout(new ButtonAreaLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        buttonPanel.add(getApproveButton(fc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        buttonPanel.add(getCancelButton(fc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (fc.getControlButtonsAreShown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            addControlButtons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        groupLabels(new AlignedLabel[] { fileNameLabel, filesOfTypeLabel });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    protected void installListeners(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        super.installListeners(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        fc.addPropertyChangeListener(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY, modeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    protected void uninstallListeners(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        fc.removePropertyChangeListener(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY, modeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        super.uninstallListeners(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private String fileNameString(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (file == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                return file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                return file.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private String fileNameString(File[] files) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        for (int i = 0; files != null && i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                buf.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (files.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                buf.append("\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            buf.append(fileNameString(files[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (files.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                buf.append("\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // Remove listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        c.removePropertyChangeListener(filterComboBoxModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        c.removePropertyChangeListener(filePane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (filePane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            filePane.uninstallUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            filePane = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        super.uninstallUI(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    protected void installStrings(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        super.installStrings(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        Locale l = fc.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        lookInLabelMnemonic = getMnemonic("FileChooser.lookInLabelMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        lookInLabelText = UIManager.getString("FileChooser.lookInLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        saveInLabelText = UIManager.getString("FileChooser.saveInLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        fileNameLabelMnemonic = getMnemonic("FileChooser.fileNameLabelMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        folderNameLabelMnemonic = getMnemonic("FileChooser.folderNameLabelMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        folderNameLabelText = UIManager.getString("FileChooser.folderNameLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        filesOfTypeLabelMnemonic = getMnemonic("FileChooser.filesOfTypeLabelMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    upFolderToolTipText =  UIManager.getString("FileChooser.upFolderToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    homeFolderToolTipText =  UIManager.getString("FileChooser.homeFolderToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    detailsViewButtonAccessibleName = UIManager.getString("FileChooser.detailsViewButtonAccessibleName",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private int getMnemonic(String key, Locale l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return SwingUtilities2.getUIDefaultsInt(key, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public String getFileName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (fileNameTextField != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            return fileNameTextField.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public void setFileName(String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (fileNameTextField != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            fileNameTextField.setText(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   522
    @Override public void rescanCurrentDirectory(JFileChooser fc) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   523
        filePane.rescanCurrentDirectory();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   524
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    protected void doSelectedFileChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        super.doSelectedFileChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        File f = (File) e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (f != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            && ((fc.isFileSelectionEnabled() && !f.isDirectory())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                || (f.isDirectory() && fc.isDirectorySelectionEnabled()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            setFileName(fileNameString(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    protected void doSelectedFilesChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        super.doSelectedFilesChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        File[] files = (File[]) e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (files != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            && files.length > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            && (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            setFileName(fileNameString(files));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    protected void doDirectoryChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        super.doDirectoryChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        FileSystemView fsv = fc.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        File currentDirectory = fc.getCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (!readOnly && currentDirectory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            getNewFolderAction().setEnabled(filePane.canWrite(currentDirectory));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (currentDirectory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            JComponent cb = getDirectoryComboBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if (cb instanceof JComboBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                ComboBoxModel model = ((JComboBox)cb).getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                if (model instanceof DirectoryComboBoxModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    ((DirectoryComboBoxModel)model).addItem(currentDirectory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                if (fsv.isFileSystem(currentDirectory)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    setFileName(currentDirectory.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    setFileName(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                }
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
    protected void doFileSelectionModeChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        super.doFileSelectionModeChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        File currentDirectory = fc.getCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (currentDirectory != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            && fc.isDirectorySelectionEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            && !fc.isFileSelectionEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            && fc.getFileSystemView().isFileSystem(currentDirectory)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            setFileName(currentDirectory.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            setFileName(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    protected void doAccessoryChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (getAccessoryPanel() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if (e.getOldValue() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                getAccessoryPanel().remove((JComponent)e.getOldValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            JComponent accessory = (JComponent)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (accessory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                getAccessoryPanel().add(accessory, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    protected void doControlButtonsChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        super.doControlButtonsChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (getFileChooser().getControlButtonsAreShown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            addControlButtons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            removeControlButtons();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    protected void addControlButtons() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (bottomPanel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            bottomPanel.add(buttonPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    protected void removeControlButtons() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        if (bottomPanel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            bottomPanel.remove(buttonPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    // *******************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    // ************ FileChooser UI PLAF methods **************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    // *******************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   639
    protected ActionMap createActionMap() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   640
        ActionMap map = new ActionMapUIResource();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   641
        // add standard actions
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   642
        FilePane.addActionsToMap(map, filePane.getActions());
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   643
        // add synth only actions
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   644
        map.put("fileNameCompletion", getFileNameCompletionAction());
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   645
        return map;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   646
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    // *****************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    // ***** Directory Actions *****
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    // *****************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    protected JComponent getDirectoryComboBox() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        return directoryComboBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    protected Action getDirectoryComboBoxAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return directoryComboBoxAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    protected DirectoryComboBoxRenderer createDirectoryComboBoxRenderer(JFileChooser fc) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   661
        return new DirectoryComboBoxRenderer(directoryComboBox.getRenderer());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    // Renderer for DirectoryComboBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    //
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   667
    // Synth has some odd behavior with regards to renderers. Renderers are styled
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   668
    // in a specific manner by the SynthComboBoxUI. If we extend DefaultListCellRenderer
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   669
    // here, then we get none of those benefits or behaviors, leading to poor
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   670
    // looking combo boxes.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   671
    // So what we do here is delegate most jobs to the "real" or original renderer,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   672
    // and simply monkey with the icon and text of the renderer.
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   673
    private class DirectoryComboBoxRenderer implements ListCellRenderer<File> {
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   674
        private ListCellRenderer<? super File> delegate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        IndentIcon ii = new IndentIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   677
        private DirectoryComboBoxRenderer(ListCellRenderer<? super File> delegate) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   678
            this.delegate = delegate;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   679
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   681
        @Override
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   682
        public Component getListCellRendererComponent(JList<? extends File> list, File value, int index, boolean isSelected, boolean cellHasFocus) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   683
            Component c = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   684
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   685
            assert c instanceof JLabel;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   686
            JLabel label = (JLabel)c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            if (value == null) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   688
                label.setText("");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   689
                return label;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   691
            label.setText(getFileChooser().getName(value));
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   692
            Icon icon = getFileChooser().getIcon(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            ii.icon = icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            ii.depth = directoryComboBoxModel.getDepth(index);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   695
            label.setIcon(ii);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   697
            return label;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    final static int space = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    class IndentIcon implements Icon {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        Icon icon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        int depth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        public void paintIcon(Component c, Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                if (c.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    icon.paintIcon(c, g, x+depth*space, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    icon.paintIcon(c, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        public int getIconWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            return ((icon != null) ? icon.getIconWidth() : 0) + depth*space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        public int getIconHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return (icon != null) ? icon.getIconHeight() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    // DataModel for DirectoryComboxbox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return new DirectoryComboBoxModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Data model for a type-face selection combo-box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   737
    protected class DirectoryComboBoxModel extends AbstractListModel<File> implements ComboBoxModel<File> {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   738
        Vector<File> directories = new Vector<File>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        int[] depths = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        File selectedDirectory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        JFileChooser chooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        FileSystemView fsv = chooser.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        public DirectoryComboBoxModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            // Add the current directory to the model, and make it the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            // selectedDirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            File dir = getFileChooser().getCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (dir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                addItem(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * Adds the directory to the model and sets it to be selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * additionally clears out the previous selected directory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         * the paths leading up to it, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        public void addItem(File directory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (directory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
1842
824c7f885a64 6776856: Code with useShellFolder field shuold be simplify
rupashka
parents: 1290
diff changeset
   764
            boolean useShellFolder = FilePane.usesShellFolder(chooser);
824c7f885a64 6776856: Code with useShellFolder field shuold be simplify
rupashka
parents: 1290
diff changeset
   765
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            int oldSize = directories.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            directories.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            if (oldSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                fireIntervalRemoved(this, 0, oldSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            File[] baseFolders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            if (useShellFolder) {
2491
123954ad0034 6738668: JFileChooser cannot be created under SecurityManager
rupashka
parents: 1853
diff changeset
   774
                baseFolders = AccessController.doPrivileged(new PrivilegedAction<File[]>() {
123954ad0034 6738668: JFileChooser cannot be created under SecurityManager
rupashka
parents: 1853
diff changeset
   775
                    public File[] run() {
123954ad0034 6738668: JFileChooser cannot be created under SecurityManager
rupashka
parents: 1853
diff changeset
   776
                        return (File[]) ShellFolder.get("fileChooserComboBoxFolders");
123954ad0034 6738668: JFileChooser cannot be created under SecurityManager
rupashka
parents: 1853
diff changeset
   777
                    }
123954ad0034 6738668: JFileChooser cannot be created under SecurityManager
rupashka
parents: 1853
diff changeset
   778
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                baseFolders = fsv.getRoots();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            directories.addAll(Arrays.asList(baseFolders));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            // Get the canonical (full) path. This has the side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            // benefit of removing extraneous chars from the path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            // for example /foo/bar/ becomes /foo/bar
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   787
            File canonical;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            try {
5581
9c2282c6f080 6938481: 4906607 is not fixed for NIMBUS L&F
rupashka
parents: 2658
diff changeset
   789
                canonical = ShellFolder.getNormalizedFile(directory);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                // Maybe drive is not ready. Can't abort here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                canonical = directory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            // create File instances of each directory leading up to the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                File sf = useShellFolder ? ShellFolder.getShellFolder(canonical)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                         : canonical;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                File f = sf;
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   800
                Vector<File> path = new Vector<File>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    path.addElement(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                } while ((f = f.getParentFile()) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                int pathCount = path.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                // Insert chain at appropriate place in vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                for (int i = 0; i < pathCount; i++) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   808
                    f = path.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    if (directories.contains(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        int topIndex = directories.indexOf(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                        for (int j = i-1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                            directories.insertElementAt(path.get(j), topIndex+i-j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                calculateDepths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                setSelectedItem(sf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            } catch (FileNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                calculateDepths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        private void calculateDepths() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            depths = new int[directories.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            for (int i = 0; i < depths.length; i++) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   827
                File dir = directories.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                File parent = dir.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                depths[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    for (int j = i-1; j >= 0; j--) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   832
                        if (parent.equals(directories.get(j))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                            depths[i] = depths[j] + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        public int getDepth(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            return (depths != null && i >= 0 && i < depths.length) ? depths[i] : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        public void setSelectedItem(Object selectedDirectory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            this.selectedDirectory = (File)selectedDirectory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            fireContentsChanged(this, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        public Object getSelectedItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return selectedDirectory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        public int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            return directories.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   858
        public File getElementAt(int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            return directories.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * Acts when DirectoryComboBox has changed the selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    protected class DirectoryComboBoxAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        protected DirectoryComboBoxAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            super("DirectoryComboBoxAction");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            directoryComboBox.hidePopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            JComponent cb = getDirectoryComboBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            if (cb instanceof JComboBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                File f = (File)((JComboBox)cb).getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                getFileChooser().setCurrentDirectory(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    // Renderer for Types ComboBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    protected FilterComboBoxRenderer createFilterComboBoxRenderer() {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   885
        return new FilterComboBoxRenderer(filterComboBox.getRenderer());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * Render different type sizes and styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   891
    public class FilterComboBoxRenderer implements ListCellRenderer<FileFilter> {
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   892
        private ListCellRenderer<? super FileFilter> delegate;
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   893
        private FilterComboBoxRenderer(ListCellRenderer<? super FileFilter> delegate) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   894
            this.delegate = delegate;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   895
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   897
        public Component getListCellRendererComponent(JList<? extends FileFilter> list, FileFilter value, int index,
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   898
                                                      boolean isSelected, boolean cellHasFocus) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   899
            Component c = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   901
            String text = null;
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   902
            if (value != null) {
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   903
                text = value.getDescription();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   906
            //this should always be true, since SynthComboBoxUI's SynthComboBoxRenderer
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   907
            //extends JLabel
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   908
            assert c instanceof JLabel;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   909
            if (text != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   910
                ((JLabel)c).setText(text);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   911
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2491
diff changeset
   912
            return c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    // DataModel for Types Comboxbox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    protected FilterComboBoxModel createFilterComboBoxModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        return new FilterComboBoxModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Data model for a type-face selection combo-box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     */
23270
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   926
    protected class FilterComboBoxModel extends AbstractListModel<FileFilter> implements ComboBoxModel<FileFilter>,
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   927
            PropertyChangeListener {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   928
        protected FileFilter[] filters;
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   929
        protected FilterComboBoxModel() {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   930
            super();
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   931
            filters = getFileChooser().getChoosableFileFilters();
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   932
        }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   933
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   934
        public void propertyChange(PropertyChangeEvent e) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   935
            String prop = e.getPropertyName();
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   936
            if(prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   937
                filters = (FileFilter[]) e.getNewValue();
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   938
                fireContentsChanged(this, -1, -1);
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   939
            } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   940
                fireContentsChanged(this, -1, -1);
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   941
            }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   942
        }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   943
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   944
        public void setSelectedItem(Object filter) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   945
            if(filter != null) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   946
                getFileChooser().setFileFilter((FileFilter) filter);
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   947
                fireContentsChanged(this, -1, -1);
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   948
            }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   949
        }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   950
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   951
        public Object getSelectedItem() {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   952
            // Ensure that the current filter is in the list.
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   953
            // NOTE: we shouldnt' have to do this, since JFileChooser adds
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   954
            // the filter to the choosable filters list when the filter
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   955
            // is set. Lets be paranoid just in case someone overrides
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   956
            // setFileFilter in JFileChooser.
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   957
            FileFilter currentFilter = getFileChooser().getFileFilter();
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   958
            boolean found = false;
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   959
            if(currentFilter != null) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   960
                for (FileFilter filter : filters) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   961
                    if (filter == currentFilter) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   962
                        found = true;
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   963
                    }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   964
                }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   965
                if(found == false) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   966
                    getFileChooser().addChoosableFileFilter(currentFilter);
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   967
                }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   968
            }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   969
            return getFileChooser().getFileFilter();
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   970
        }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   971
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   972
        public int getSize() {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   973
            if(filters != null) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   974
                return filters.length;
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   975
            } else {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   976
                return 0;
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   977
            }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   978
        }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   979
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   980
        public FileFilter getElementAt(int index) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   981
            if(index > getSize() - 1) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   982
                // This shouldn't happen. Try to recover gracefully.
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   983
                return getFileChooser().getFileFilter();
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   984
            }
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   985
            if(filters != null) {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   986
                return filters[index];
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   987
            } else {
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   988
                return null;
b5073d208bbf 8032063: javax.swing.plaf.metal.MetalFileChooserUI$FilterComboBoxModel extends non-standard API
alexsch
parents: 20100
diff changeset
   989
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * <code>ButtonAreaLayout</code> behaves in a similar manner to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * <code>FlowLayout</code>. It lays out all components from left to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * right, flushed right. The widths of all components will be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * to the largest preferred size width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    private static class ButtonAreaLayout implements LayoutManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        private int hGap = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        private int topMargin = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        public void addLayoutComponent(String string, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        public void layoutContainer(Container container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            Component[] children = container.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            if (children != null && children.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                int         numChildren = children.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                Dimension[] sizes = new Dimension[numChildren];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                Insets      insets = container.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                int         yLocation = insets.top + topMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                int         maxWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                for (int counter = 0; counter < numChildren; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    sizes[counter] = children[counter].getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    maxWidth = Math.max(maxWidth, sizes[counter].width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                int xLocation, xOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                if (container.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    xLocation = container.getSize().width - insets.left - maxWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    xOffset = hGap + maxWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                    xLocation = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    xOffset = -(hGap + maxWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                for (int counter = numChildren - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    children[counter].setBounds(xLocation, yLocation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                                                maxWidth, sizes[counter].height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                    xLocation -= xOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        public Dimension minimumLayoutSize(Container c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                Component[] children = c.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                if (children != null && children.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    int       numChildren = children.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    int       height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    Insets    cInsets = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    int       extraHeight = topMargin + cInsets.top + cInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    int       extraWidth = cInsets.left + cInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                    int       maxWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    for (int counter = 0; counter < numChildren; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                        Dimension aSize = children[counter].getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                        height = Math.max(height, aSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                        maxWidth = Math.max(maxWidth, aSize.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    return new Dimension(extraWidth + numChildren * maxWidth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                                         (numChildren - 1) * hGap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                                         extraHeight + height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        public Dimension preferredLayoutSize(Container c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            return minimumLayoutSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        public void removeLayoutComponent(Component c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    private static void groupLabels(AlignedLabel[] group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        for (int i = 0; i < group.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            group[i].group = group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    private class AlignedLabel extends JLabel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        private AlignedLabel[] group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        private int maxWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        AlignedLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            setAlignmentX(JComponent.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        AlignedLabel(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            super(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            setAlignmentX(JComponent.LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            Dimension d = super.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            // Align the width with all other labels in group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            return new Dimension(getMaxWidth() + 11, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        private int getMaxWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            if (maxWidth == 0 && group != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                int max = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                for (int i = 0; i < group.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    max = Math.max(group[i].getSuperPreferredWidth(), max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                for (int i = 0; i < group.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    group[i].maxWidth = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            return maxWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        private int getSuperPreferredWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            return super.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
}