jdk/src/solaris/classes/sun/awt/X11/XFileDialogPeer.java
author anthony
Tue, 12 Aug 2014 14:22:05 +0400
changeset 26342 3637212ae8f2
parent 24538 25bf8153fbfe
permissions -rw-r--r--
8049065: [JLightweightFrame] Support DnD for SwingNode Summary: Delegate DnD operations to LightweightContent when appropriate Reviewed-by: ant, pchelko
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18178
diff changeset
     2
 * Copyright (c) 2003, 2014, 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: 4913
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: 4913
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: 4913
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4913
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4913
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.peer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.java.swing.plaf.motif.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2458
diff changeset
    39
import sun.util.logging.PlatformLogger;
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
    40
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class XFileDialogPeer extends XDialogPeer implements FileDialogPeer, ActionListener, ItemListener, KeyEventDispatcher, XChoicePeerListener {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2458
diff changeset
    43
    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XFileDialogPeer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    FileDialog  target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // This variable holds value exactly the same as value of the 'target.file' variable except:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // 1) is changed to null after quit (see handleQuitButton())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // 2) keep the same value if 'target.file' is incorrect (see setFile())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // It's not clear HOW we used it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // We should think about existence of this variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    String      file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    String      dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    String      title;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    int         mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    FilenameFilter  filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final int PATH_CHOICE_WIDTH = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Seems that the purpose of this variable is cashing of 'target.file' variable in order to help method show()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // We should think about using 'target.file' instead of 'savedFile'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // Perhaps, 'target.file' just more correct (see target.setFile())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    String      savedFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // Holds value of the directory which was chosen before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // We use it in order to restore previously selected directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // at the time of the next showing of the file dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    String      savedDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Holds value of the system property 'user.dir'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // in order to init current directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    String      userDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    Dialog      fileDialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    GridBagLayout       gbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    GridBagLayout       gblButtons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    GridBagConstraints  gbc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // ************** Components in the fileDialogWindow ***************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    TextField   filterField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // This variable holds the current text of the file which user select through the navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // It's important that updating of this variable must be correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // since this value is used at the time of the file dialog closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Namely, we invoke target.setFile() and then user can get this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // We update this field in cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // - ITEM_STATE_CHANGED was triggered on the file list component: set to the current selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // - at the time of the 'show': set to savedFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // - at the time of the programmatically setting: set to new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    TextField   selectionField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    List        directoryList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // This is the list component which is used for the showing of the file list of the current directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    List        fileList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    Panel       buttons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    Button      openButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    Button      filterButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    Button      cancelButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    Choice      pathChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    TextField   pathField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    Panel       pathPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    String cancelButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    String enterFileNameLabelText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    String filesLabelText= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    String foldersLabelText= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    String pathLabelText= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    String filterLabelText= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    String openButtonText= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    String saveButtonText= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    String actionButtonText= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void installStrings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Locale l = target.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        UIDefaults uid = XToolkit.getUIDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        cancelButtonText = uid.getString("FileChooser.cancelButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        enterFileNameLabelText = uid.getString("FileChooser.enterFileNameLabelText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        filesLabelText = uid.getString("FileChooser.filesLabelText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        foldersLabelText = uid.getString("FileChooser.foldersLabelText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        pathLabelText = uid.getString("FileChooser.pathLabelText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        filterLabelText = uid.getString("FileChooser.filterLabelText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        openButtonText = uid.getString("FileChooser.openButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        saveButtonText  = uid.getString("FileChooser.saveButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    XFileDialogPeer(FileDialog target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        super((Dialog)target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private void init(FileDialog target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        fileDialog = target; //new Dialog(target, target.getTitle(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.title = target.getTitle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this.mode = target.getMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        this.filter = target.getFilenameFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        savedFile = target.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        savedDir = target.getDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Shouldn't save 'user.dir' to 'savedDir'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // since getDirectory() will be incorrect after handleCancel
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 23647
diff changeset
   149
        userDir = AccessController.doPrivileged(
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 23647
diff changeset
   150
            new PrivilegedAction<String>() {
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 23647
diff changeset
   151
                public String run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    return System.getProperty("user.dir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        installStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        gbl = new GridBagLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        gblButtons = new GridBagLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        gbc = new GridBagConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        fileDialog.setLayout(gbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // create components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        buttons = new Panel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        buttons.setLayout(gblButtons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        actionButtonText = (target.getMode() == FileDialog.SAVE) ? saveButtonText : openButtonText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        openButton = new Button(actionButtonText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        filterButton = new Button(filterLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        cancelButton = new Button(cancelButtonText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        directoryList = new List();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        fileList = new List();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        filterField = new TextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        selectionField = new TextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   175
        boolean isMultipleMode =
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   176
            AWTAccessor.getFileDialogAccessor().isMultipleMode(target);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   177
        fileList.setMultipleMode(isMultipleMode);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   178
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // the insets used by the components in the fileDialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Insets noInset = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        Insets textFieldInset = new Insets(0, 8, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Insets leftListInset = new Insets(0, 8, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Insets rightListInset = new Insets(0, 4, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Insets separatorInset = new Insets(8, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Insets labelInset = new Insets(0, 8, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Insets buttonsInset = new Insets(10, 8, 10, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // add components to GridBagLayout "gbl"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Font f = new Font(Font.DIALOG, Font.PLAIN, 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Label label = new Label(pathLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        addComponent(label, gbl, gbc, 0, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Fixed 6260650: FileDialog.getDirectory() does not return null when file dialog is cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // After showing we should display 'user.dir' as current directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // if user didn't set directory programatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        pathField = new TextField(savedDir != null ? savedDir : userDir);
23647
41d22f2dbeb5 8033712: Fix more serial lint warnings in sun.awt
darcy
parents: 22584
diff changeset
   202
        @SuppressWarnings("serial") // Anonymous class
41d22f2dbeb5 8033712: Fix more serial lint warnings in sun.awt
darcy
parents: 22584
diff changeset
   203
        Choice tmp = new Choice() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    return new Dimension(PATH_CHOICE_WIDTH, pathField.getPreferredSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            };
23647
41d22f2dbeb5 8033712: Fix more serial lint warnings in sun.awt
darcy
parents: 22584
diff changeset
   208
        pathChoice = tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        pathPanel = new Panel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        pathPanel.setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        pathPanel.add(pathField,BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        pathPanel.add(pathChoice,BorderLayout.EAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        //addComponent(pathField, gbl, gbc, 0, 1, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        //             GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        //             1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        //addComponent(pathChoice, gbl, gbc, 1, 1, GridBagConstraints.RELATIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         //            GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
          //           1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        addComponent(pathPanel, gbl, gbc, 0, 1, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                   1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        label = new Label(filterLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        addComponent(label, gbl, gbc, 0, 2, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        addComponent(filterField, gbl, gbc, 0, 3, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                     1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        label = new Label(foldersLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        addComponent(label, gbl, gbc, 0, 4, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        label = new Label(filesLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        addComponent(label, gbl, gbc, 1, 4, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        addComponent(directoryList, gbl, gbc, 0, 5, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                     1, 1, GridBagConstraints.BOTH, leftListInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        addComponent(fileList, gbl, gbc, 1, 5, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                     1, 1, GridBagConstraints.BOTH, rightListInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        label = new Label(enterFileNameLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        addComponent(label, gbl, gbc, 0, 6, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        addComponent(selectionField, gbl, gbc, 0, 7, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                     1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        addComponent(new Separator(fileDialog.size().width, 2, Separator.HORIZONTAL), gbl, gbc, 0, 8, 15,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                     1, 0, GridBagConstraints.HORIZONTAL, separatorInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // add buttons to GridBagLayout Buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        addComponent(openButton, gblButtons, gbc, 0, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                     GridBagConstraints.WEST, (Container)buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                     1, 0, GridBagConstraints.NONE, noInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        addComponent(filterButton, gblButtons, gbc, 1, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                     GridBagConstraints.CENTER, (Container)buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                     1, 0, GridBagConstraints.NONE, noInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        addComponent(cancelButton, gblButtons, gbc, 2, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                     GridBagConstraints.EAST, (Container)buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                     1, 0, GridBagConstraints.NONE, noInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // add ButtonPanel to the GridBagLayout of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        addComponent(buttons, gbl, gbc, 0, 9, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                     1, 0, GridBagConstraints.HORIZONTAL, buttonsInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        fileDialog.setSize(400, 400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // Update choice's popup width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        choicePeer.setDrawSelectedItem(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        choicePeer.setAlignUnder(pathField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        filterField.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        selectionField.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        directoryList.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        directoryList.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        fileList.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        fileList.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        openButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        filterButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        cancelButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        pathChoice.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        pathField.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // b6227750 FileDialog is not disposed when clicking the 'close' (X) button on the top right corner, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        target.addWindowListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            new WindowAdapter(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                public void windowClosing(WindowEvent e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    handleCancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // 6259434 PIT: Choice in FileDialog is not responding to keyboard interactions, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        pathChoice.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public void updateMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public void updateIconImages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (winAttr.icons == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            winAttr.iconsInherited = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            winAttr.icons = getDefaultIconInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            setIconHints(winAttr.icons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * add Component comp to the container cont.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * add the component to the correct GridBagLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    void addComponent(Component comp, GridBagLayout gb, GridBagConstraints c, int gridx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                      int gridy, int gridwidth, int anchor, Container cont, int weightx, int weighty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                      int fill, Insets in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        c.gridx = gridx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        c.gridy = gridy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        c.gridwidth = gridwidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        c.anchor = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        c.weightx = weightx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        c.weighty = weighty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        c.fill = fill;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        c.insets = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        gb.setConstraints(comp, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        cont.add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * get fileName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    String getFileName(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        int index = str.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return str.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /** handleFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    void handleFilter(String f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        setFilterEntry(dir,f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // Fixed within 6259434: PIT: Choice in FileDialog is not responding to keyboard interactions, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // Here we restoring Motif behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        directoryList.select(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (fileList.getItemCount() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            fileList.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            directoryList.requestFocus();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * handle the selection event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    void handleSelection(String file) {
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   389
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   390
        int index = file.lastIndexOf(java.io.File.separatorChar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            savedDir = this.dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            savedFile = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            savedDir = file.substring(0, index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            savedFile = file.substring(index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   399
12653
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   400
        String[] fileNames = fileList.getSelectedItems();
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   401
        int filesNumber = (fileNames != null) ? fileNames.length : 0;
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   402
        File[] files = new File[filesNumber];
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   403
        for (int i = 0; i < filesNumber; i++) {
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   404
            files[i] = new File(savedDir, fileNames[i]);
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   405
        }
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   406
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   407
        AWTAccessor.FileDialogAccessor fileDialogAccessor = AWTAccessor.getFileDialogAccessor();
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   408
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   409
        fileDialogAccessor.setDirectory(target, savedDir);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   410
        fileDialogAccessor.setFile(target, savedFile);
12653
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   411
        fileDialogAccessor.setFiles(target, files);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * handle the cancel event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    void handleCancel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            .removeKeyEventDispatcher(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        setSelectionField(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        setFilterField(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        directoryList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        fileList.clear();
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   425
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   426
        AWTAccessor.FileDialogAccessor fileDialogAccessor = AWTAccessor.getFileDialogAccessor();
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   427
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   428
        fileDialogAccessor.setDirectory(target, null);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   429
        fileDialogAccessor.setFile(target, null);
12653
07d5ca30e79e 7124337: [macosx] FileDialog fails to select multiple files
dcherepanov
parents: 7668
diff changeset
   430
        fileDialogAccessor.setFiles(target, null);
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   431
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        handleQuitButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * handle the quit event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    void handleQuitButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        dir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        file = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        target.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * set the entry of the new dir with f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    void setFilterEntry(String d, String f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        File fe = new File(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (fe.isDirectory() && fe.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            // Fixed 6260659: File Name set programmatically in FileDialog is overridden during navigation, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            // Here we restoring Motif behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            setSelectionField(target.getFile());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (f.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                f = "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                setFilterField(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                setFilterField(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            String l[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (f.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                l = fe.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                // REMIND: fileDialogFilter is not implemented yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                FileDialogFilter ff = new FileDialogFilter(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                l = fe.list(ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // Fixed 6358953: handling was added in case of I/O error happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                this.dir = getParentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            directoryList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            fileList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            directoryList.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            fileList.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            directoryList.addItem("..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            Arrays.sort(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            for (int i = 0 ; i < l.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                File file = new File(d + l[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (file.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    directoryList.addItem(l[i] + "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    if (filter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        if (filter.accept(new File(l[i]),l[i]))  fileList.addItem(l[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    else fileList.addItem(l[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            this.dir = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            pathField.setText(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // Some code was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            // Now we do updating of the pathChoice at the time of the choice opening
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            target.setDirectory(this.dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            directoryList.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            fileList.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    String[] getDirList(String dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (!dir.endsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            dir = dir + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        char[] charr = dir.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        int numSlashes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        for (int i=0;i<charr.length;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
           if (charr[i] == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
               numSlashes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        String[] starr =  new String[numSlashes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        int j=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        for (int i=charr.length-1;i>=0;i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (charr[i] == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                starr[j++] = new String(charr,0,i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return starr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * set the text in the selectionField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    void setSelectionField(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        selectionField.setText(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * set the text in the filterField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    void setFilterField(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        filterField.setText(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @see java.awt.event.ItemEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * ItemEvent.ITEM_STATE_CHANGED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public void itemStateChanged(ItemEvent itemEvent){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (itemEvent.getID() != ItemEvent.ITEM_STATE_CHANGED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            itemEvent.getStateChange() == ItemEvent.DESELECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        Object source = itemEvent.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (source == pathChoice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
             * Update the selection ('folder name' text field) after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
             * the current item changing in the unfurled choice by the arrow keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
             * See 6259434, 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            String dir = pathChoice.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            pathField.setText(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        } else if (directoryList == source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            setFilterField(getFileName(filterField.getText()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        } else if (fileList == source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            String file = fileList.getItem((Integer)itemEvent.getItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            setSelectionField(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
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
     * Updates the current directory only if directoryList-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * action occurred. Returns false if the forward directory is inaccessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    boolean updateDirectoryByUserAction(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        String dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (str.equals("..")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            dir = getParentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            dir = this.dir + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        File fe = new File(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (fe.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            this.dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    String getParentDirectory(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        String parent = this.dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (!this.dir.equals("/"))   // If the current directory is "/" leave it alone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if (dir.endsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                parent = parent.substring(0,parent.lastIndexOf("/"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            parent = parent.substring(0,parent.lastIndexOf("/")+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public void actionPerformed( ActionEvent actionEvent ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        String actionCommand = actionEvent.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        Object source = actionEvent.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (actionCommand.equals(actionButtonText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            handleSelection( selectionField.getText() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            handleQuitButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        } else if (actionCommand.equals(filterLabelText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            handleFilter( filterField.getText() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        } else if (actionCommand.equals(cancelButtonText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            handleCancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        } else if ( source instanceof TextField ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if ( selectionField == ((TextField)source) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                // Fixed within 6259434: PIT: Choice in FileDialog is not responding to keyboard interactions, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                // We should handle the action based on the selection field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                // Looks like mistake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                handleSelection(selectionField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                handleQuitButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            } else if (filterField == ((TextField)source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                handleFilter(filterField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            } else if (pathField == ((TextField)source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                target.setDirectory(pathField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        } else if (source instanceof List) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            if (directoryList == ((List)source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                //handleFilter( actionCommand + getFileName( filterField.getText() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                if (updateDirectoryByUserAction(actionCommand)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    handleFilter( getFileName( filterField.getText() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            } else if (fileList == ((List)source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                handleSelection( actionCommand );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                handleQuitButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        int id = keyEvent.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        int keyCode = keyEvent.getKeyCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (id == KeyEvent.KEY_PRESSED && keyCode == KeyEvent.VK_ESCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            synchronized (target.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                Component comp = (Component) keyEvent.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                while (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    // Fix for 6240084 Disposing a file dialog when the drop-down is active does not dispose the dropdown menu, on Xtoolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    // See also 6259493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    if (comp == pathChoice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        if (choicePeer.isUnfurled()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    if (comp.getPeer() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        handleCancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    comp = comp.getParent();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * set the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    public void setFile(String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (file == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            this.file = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (this.dir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            String d = "./";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            File f = new File(d, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if (f.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                this.file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                setDirectory(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            File f = new File(this.dir, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (f.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                this.file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        setSelectionField(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * set the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * FIXME: we should update 'savedDir' after programmatically 'setDirectory'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * Otherwise, SavedDir will be not null before second showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * So the current directory of the file dialog will be incorrect after second showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * since 'setDirectory' will be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * We cann't update savedDir here now since it used very often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    public void setDirectory(String dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (dir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            this.dir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (dir.equals(this.dir)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if ((i=dir.indexOf("~")) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            dir = dir.substring(0,i) + System.getProperty("user.home") + dir.substring(i+1,dir.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        File fe = new File(dir).getAbsoluteFile();
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   724
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 12653
diff changeset
   725
            log.fine("Current directory : " + fe);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 12653
diff changeset
   726
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (!fe.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            dir = "./";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            fe = new File(dir).getAbsoluteFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (!fe.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            dir = this.dir = fe.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        } catch (java.io.IOException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            dir = this.dir = fe.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        pathField.setText(this.dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (dir.endsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            this.dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            handleFilter("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            this.dir = dir + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            handleFilter("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        // Some code was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // Now we do updating of the pathChoice at the time of the choice opening
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        // Fixed problem:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // The exception java.awt.IllegalComponentStateException will be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // if the user invoke setDirectory after the closing of the file dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * set filenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public void setFilenameFilter(FilenameFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
2458
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   767
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   768
    public void dispose() {
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   769
        FileDialog fd = (FileDialog)fileDialog;
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   770
        if (fd != null) {
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   771
            fd.removeAll();
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   772
        }
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   773
        super.dispose();
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   774
    }
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   775
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   776
    // 03/02/2005 b5097243 Pressing 'ESC' on a file dlg does not dispose the dlg on Xtoolkit
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   777
    public void setVisible(boolean b){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (fileDialog == null) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18178
diff changeset
   779
            init(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (savedDir != null || userDir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            setDirectory(savedDir != null ? savedDir : userDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        if (savedFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            // Actually in Motif implementation lost file value which was saved after prevously showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            // Seems we shouldn't restore Motif behaviour in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            setFile(savedFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        super.setVisible(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (b == true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            // See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            choicePeer.addXChoicePeerListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            // See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            choicePeer.removeXChoicePeerListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
2458
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   804
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   805
        selectionField.requestFocusInWindow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * Adding items to the path choice based on the text string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    public void addItemsToPathChoice(String text){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        String dirList[] = getDirList(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        for (int i = 0; i < dirList.length; i++) pathChoice.addItem(dirList[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Refresh the unfurled choice at the time of the opening choice according to the text of the path field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    public void unfurledChoiceOpening(ListHelper choiceHelper){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        // When the unfurled choice is opening the first time, we need only to add elements, otherwise we've got exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (choiceHelper.getItemCount() == 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            addItemsToPathChoice(pathField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        // If the set of the directories the exactly same as the used to be then dummy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (pathChoice.getItem(0).equals(pathField.getText()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        pathChoice.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        addItemsToPathChoice(pathField.getText());
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
     * Refresh the file dialog at the time of the closing choice according to the selected item of the choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    public void unfurledChoiceClosing(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
          // This is the exactly same code as invoking later at the time of the itemStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
          // Here is we restore Windows behaviour: change current directory if user press 'ESC'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
          String dir = pathChoice.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
          target.setDirectory(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
23647
41d22f2dbeb5 8033712: Fix more serial lint warnings in sun.awt
darcy
parents: 22584
diff changeset
   849
@SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
class Separator extends Canvas {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    public final static int HORIZONTAL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    public final static int VERTICAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    int orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    public Separator(int length, int thickness, int orient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        orientation = orient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (orient == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            resize(length, thickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            resize(thickness, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        int x1, y1, x2, y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        Rectangle bbox = bounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        Color c = getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        Color brighter = c.brighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        Color darker = c.darker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        if (orientation == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            x1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            x2 = bbox.width - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            y1 = y2 = bbox.height/2 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            x1 = x2 = bbox.width/2 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            y1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            y2 = bbox.height - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        g.setColor(darker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        g.drawLine(x1, y2, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        g.setColor(brighter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        if (orientation == HORIZONTAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            g.drawLine(x1, y2+1, x2, y2+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            g.drawLine(x1+1, y2, x2+1, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
 * Motif file dialogs let the user specify a filter that controls the files that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
 * are displayed in the dialog. This filter is generally specified as a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
 * expression. The class is used to implement Motif-like filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
class FileDialogFilter implements FilenameFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    String filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    public FileDialogFilter(String f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        filter = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * Tells whether or not the specified file should be included in a file list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    public boolean accept(File dir, String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        File f = new File(dir, fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return matches(fileName, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Tells whether or not the input string matches the given filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    private boolean matches(String input, String filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        String regex = convert(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return input.matches(regex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Converts the filter into the form which is acceptable by Java's regexps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    private String convert(String filter) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   933
        String regex = "^" + filter + "$";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        regex = regex.replaceAll("\\.", "\\\\.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        regex = regex.replaceAll("\\?", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        regex = regex.replaceAll("\\*", ".*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        return regex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
}