jdk/src/solaris/classes/sun/awt/X11/XFileDialogPeer.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 12653 07d5ca30e79e
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        userDir = (String)AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                public Object run() {
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        pathChoice = new Choice() {
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
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        pathPanel = new Panel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        pathPanel.setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        pathPanel.add(pathField,BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        pathPanel.add(pathChoice,BorderLayout.EAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        //addComponent(pathField, gbl, gbc, 0, 1, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        //             GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        //             1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        //addComponent(pathChoice, gbl, gbc, 1, 1, GridBagConstraints.RELATIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         //            GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
          //           1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        addComponent(pathPanel, gbl, gbc, 0, 1, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                   1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        label = new Label(filterLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        addComponent(label, gbl, gbc, 0, 2, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        addComponent(filterField, gbl, gbc, 0, 3, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                     1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        label = new Label(foldersLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        addComponent(label, gbl, gbc, 0, 4, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        label = new Label(filesLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        addComponent(label, gbl, gbc, 1, 4, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        addComponent(directoryList, gbl, gbc, 0, 5, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                     1, 1, GridBagConstraints.BOTH, leftListInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        addComponent(fileList, gbl, gbc, 1, 5, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                     1, 1, GridBagConstraints.BOTH, rightListInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        label = new Label(enterFileNameLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        label.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        addComponent(label, gbl, gbc, 0, 6, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                     1, 0, GridBagConstraints.NONE, labelInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        addComponent(selectionField, gbl, gbc, 0, 7, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                     1, 0, GridBagConstraints.HORIZONTAL, textFieldInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        addComponent(new Separator(fileDialog.size().width, 2, Separator.HORIZONTAL), gbl, gbc, 0, 8, 15,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                     1, 0, GridBagConstraints.HORIZONTAL, separatorInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // add buttons to GridBagLayout Buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        addComponent(openButton, gblButtons, gbc, 0, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                     GridBagConstraints.WEST, (Container)buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                     1, 0, GridBagConstraints.NONE, noInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        addComponent(filterButton, gblButtons, gbc, 1, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                     GridBagConstraints.CENTER, (Container)buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                     1, 0, GridBagConstraints.NONE, noInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        addComponent(cancelButton, gblButtons, gbc, 2, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                     GridBagConstraints.EAST, (Container)buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                     1, 0, GridBagConstraints.NONE, noInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // add ButtonPanel to the GridBagLayout of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        addComponent(buttons, gbl, gbc, 0, 9, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                     GridBagConstraints.WEST, (Container)fileDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                     1, 0, GridBagConstraints.HORIZONTAL, buttonsInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        fileDialog.setSize(400, 400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // Update choice's popup width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        choicePeer.setDrawSelectedItem(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        choicePeer.setAlignUnder(pathField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        filterField.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        selectionField.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        directoryList.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        directoryList.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        fileList.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        fileList.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        openButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        filterButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        cancelButton.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        pathChoice.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        pathField.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // b6227750 FileDialog is not disposed when clicking the 'close' (X) button on the top right corner, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        target.addWindowListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            new WindowAdapter(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                public void windowClosing(WindowEvent e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    handleCancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
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
        // 6259434 PIT: Choice in FileDialog is not responding to keyboard interactions, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        pathChoice.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void updateMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public void updateIconImages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (winAttr.icons == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            winAttr.iconsInherited = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            winAttr.icons = getDefaultIconInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            setIconHints(winAttr.icons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
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
     * add Component comp to the container cont.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * add the component to the correct GridBagLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    void addComponent(Component comp, GridBagLayout gb, GridBagConstraints c, int gridx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                      int gridy, int gridwidth, int anchor, Container cont, int weightx, int weighty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                      int fill, Insets in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        c.gridx = gridx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        c.gridy = gridy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        c.gridwidth = gridwidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        c.anchor = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        c.weightx = weightx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        c.weighty = weighty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        c.fill = fill;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        c.insets = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        gb.setConstraints(comp, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        cont.add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * get fileName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    String getFileName(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        int index = str.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return str.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /** handleFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    void handleFilter(String f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        setFilterEntry(dir,f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // Fixed within 6259434: PIT: Choice in FileDialog is not responding to keyboard interactions, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // Here we restoring Motif behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        directoryList.select(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (fileList.getItemCount() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            fileList.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            directoryList.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
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
     * handle the selection event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    void handleSelection(String file) {
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   388
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   389
        int index = file.lastIndexOf(java.io.File.separatorChar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            savedDir = this.dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            savedFile = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            savedDir = file.substring(0, index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            savedFile = file.substring(index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   398
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   399
        AWTAccessor.FileDialogAccessor fileDialogAccessor = AWTAccessor.getFileDialogAccessor();
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   400
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   401
        fileDialogAccessor.setDirectory(target, savedDir);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   402
        fileDialogAccessor.setFile(target, savedFile);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   403
        fileDialogAccessor.setFiles(target, savedDir, fileList.getSelectedItems());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * handle the cancel event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    void handleCancel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            .removeKeyEventDispatcher(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        setSelectionField(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        setFilterField(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        directoryList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        fileList.clear();
4913
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   417
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   418
        AWTAccessor.FileDialogAccessor fileDialogAccessor = AWTAccessor.getFileDialogAccessor();
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   419
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   420
        fileDialogAccessor.setDirectory(target, null);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   421
        fileDialogAccessor.setFile(target, null);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   422
        fileDialogAccessor.setFiles(target, null, null);
9b3caa50afcf 6705345: Enable multiple file selection in AWT FileDialog
dcherepanov
parents: 3938
diff changeset
   423
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        handleQuitButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * handle the quit event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    void handleQuitButton() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        dir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        file = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        target.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * set the entry of the new dir with f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    void setFilterEntry(String d, String f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        File fe = new File(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (fe.isDirectory() && fe.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            // Fixed 6260659: File Name set programmatically in FileDialog is overridden during navigation, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            // Here we restoring Motif behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            setSelectionField(target.getFile());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (f.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                f = "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                setFilterField(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                setFilterField(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            String l[];
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
                l = fe.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                // REMIND: fileDialogFilter is not implemented yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                FileDialogFilter ff = new FileDialogFilter(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                l = fe.list(ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            // Fixed 6358953: handling was added in case of I/O error happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                this.dir = getParentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            directoryList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            fileList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            directoryList.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            fileList.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            directoryList.addItem("..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            Arrays.sort(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            for (int i = 0 ; i < l.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                File file = new File(d + l[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                if (file.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    directoryList.addItem(l[i] + "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    if (filter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        if (filter.accept(new File(l[i]),l[i]))  fileList.addItem(l[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    else fileList.addItem(l[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            this.dir = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            pathField.setText(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // Some code was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            // Now we do updating of the pathChoice at the time of the choice opening
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            target.setDirectory(this.dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            directoryList.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            fileList.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    String[] getDirList(String dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (!dir.endsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            dir = dir + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        char[] charr = dir.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        int numSlashes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        for (int i=0;i<charr.length;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
           if (charr[i] == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
               numSlashes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        String[] starr =  new String[numSlashes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        int j=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        for (int i=charr.length-1;i>=0;i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (charr[i] == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                starr[j++] = new String(charr,0,i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        return starr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * set the text in the selectionField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    void setSelectionField(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        selectionField.setText(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * set the text in the filterField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    void setFilterField(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        filterField.setText(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
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
     * @see java.awt.event.ItemEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * ItemEvent.ITEM_STATE_CHANGED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public void itemStateChanged(ItemEvent itemEvent){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (itemEvent.getID() != ItemEvent.ITEM_STATE_CHANGED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            itemEvent.getStateChange() == ItemEvent.DESELECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        Object source = itemEvent.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (source == pathChoice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
             * Update the selection ('folder name' text field) after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
             * the current item changing in the unfurled choice by the arrow keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
             * See 6259434, 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            String dir = pathChoice.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            pathField.setText(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        } else if (directoryList == source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            setFilterField(getFileName(filterField.getText()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        } else if (fileList == source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            String file = fileList.getItem((Integer)itemEvent.getItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            setSelectionField(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Updates the current directory only if directoryList-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * action occurred. Returns false if the forward directory is inaccessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    boolean updateDirectoryByUserAction(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        String dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (str.equals("..")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            dir = getParentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            dir = this.dir + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        File fe = new File(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (fe.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            this.dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    String getParentDirectory(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        String parent = this.dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (!this.dir.equals("/"))   // If the current directory is "/" leave it alone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            if (dir.endsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                parent = parent.substring(0,parent.lastIndexOf("/"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            parent = parent.substring(0,parent.lastIndexOf("/")+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public void actionPerformed( ActionEvent actionEvent ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        String actionCommand = actionEvent.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        Object source = actionEvent.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (actionCommand.equals(actionButtonText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            handleSelection( selectionField.getText() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            handleQuitButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        } else if (actionCommand.equals(filterLabelText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            handleFilter( filterField.getText() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        } else if (actionCommand.equals(cancelButtonText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            handleCancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        } else if ( source instanceof TextField ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if ( selectionField == ((TextField)source) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                // Fixed within 6259434: PIT: Choice in FileDialog is not responding to keyboard interactions, XToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                // We should handle the action based on the selection field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                // Looks like mistake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                handleSelection(selectionField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                handleQuitButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            } else if (filterField == ((TextField)source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                handleFilter(filterField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            } else if (pathField == ((TextField)source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                target.setDirectory(pathField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        } else if (source instanceof List) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if (directoryList == ((List)source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                //handleFilter( actionCommand + getFileName( filterField.getText() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                if (updateDirectoryByUserAction(actionCommand)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    handleFilter( getFileName( filterField.getText() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            } else if (fileList == ((List)source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                handleSelection( actionCommand );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                handleQuitButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        int id = keyEvent.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        int keyCode = keyEvent.getKeyCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (id == KeyEvent.KEY_PRESSED && keyCode == KeyEvent.VK_ESCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            synchronized (target.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                Component comp = (Component) keyEvent.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                while (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    // 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
   642
                    // See also 6259493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    if (comp == pathChoice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        if (choicePeer.isUnfurled()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    if (comp.getPeer() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        handleCancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    comp = comp.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * set the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    public void setFile(String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        if (file == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            this.file = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (this.dir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            String d = "./";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            File f = new File(d, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            if (f.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                this.file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                setDirectory(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            File f = new File(this.dir, file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            if (f.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                this.file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        setSelectionField(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * set the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * FIXME: we should update 'savedDir' after programmatically 'setDirectory'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Otherwise, SavedDir will be not null before second showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * So the current directory of the file dialog will be incorrect after second showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * since 'setDirectory' will be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * We cann't update savedDir here now since it used very often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    public void setDirectory(String dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if (dir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            this.dir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if (dir.equals(this.dir)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if ((i=dir.indexOf("~")) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            dir = dir.substring(0,i) + System.getProperty("user.home") + dir.substring(i+1,dir.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        File fe = new File(dir).getAbsoluteFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        log.fine("Current directory : " + fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (!fe.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            dir = "./";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            fe = new File(dir).getAbsoluteFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            if (!fe.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            dir = this.dir = fe.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        } catch (java.io.IOException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            dir = this.dir = fe.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        pathField.setText(this.dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        if (dir.endsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            this.dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            handleFilter("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            this.dir = dir + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            handleFilter("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        // Some code was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // Now we do updating of the pathChoice at the time of the choice opening
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // Fixed problem:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        // The exception java.awt.IllegalComponentStateException will be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        // if the user invoke setDirectory after the closing of the file dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * set filenameFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public void setFilenameFilter(FilenameFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
2458
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   757
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   758
    public void dispose() {
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   759
        FileDialog fd = (FileDialog)fileDialog;
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   760
        if (fd != null) {
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   761
            fd.removeAll();
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   762
        }
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   763
        super.dispose();
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   764
    }
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   765
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   766
    // 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
   767
    public void setVisible(boolean b){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (fileDialog == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            init((FileDialog)target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (savedDir != null || userDir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            setDirectory(savedDir != null ? savedDir : userDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        if (savedFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            // Actually in Motif implementation lost file value which was saved after prevously showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // Seems we shouldn't restore Motif behaviour in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            setFile(savedFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        super.setVisible(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (b == true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            // See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            choicePeer.addXChoicePeerListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            // See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            XChoicePeer choicePeer = (XChoicePeer)pathChoice.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            choicePeer.removeXChoicePeerListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
2458
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   794
f4bee3ba17ee 6806224: PIT : Getting java.lang.NullPointerException while opening Filedialog
dcherepanov
parents: 715
diff changeset
   795
        selectionField.requestFocusInWindow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * Adding items to the path choice based on the text string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    public void addItemsToPathChoice(String text){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        String dirList[] = getDirList(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        for (int i = 0; i < dirList.length; i++) pathChoice.addItem(dirList[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * 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
   809
     * See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public void unfurledChoiceOpening(ListHelper choiceHelper){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        // 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
   814
        if (choiceHelper.getItemCount() == 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            addItemsToPathChoice(pathField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        // If the set of the directories the exactly same as the used to be then dummy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (pathChoice.getItem(0).equals(pathField.getText()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        pathChoice.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        addItemsToPathChoice(pathField.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * 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
   829
     * See 6240074 for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    public void unfurledChoiceClosing(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
          // This is the exactly same code as invoking later at the time of the itemStateChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
          // Here is we restore Windows behaviour: change current directory if user press 'ESC'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
          String dir = pathChoice.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
          target.setDirectory(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
class Separator extends Canvas {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public final static int HORIZONTAL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    public final static int VERTICAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    int orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public Separator(int length, int thickness, int orient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        orientation = orient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (orient == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            resize(length, thickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            resize(thickness, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        int x1, y1, x2, y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        Rectangle bbox = bounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        Color c = getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        Color brighter = c.brighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        Color darker = c.darker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (orientation == HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            x1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            x2 = bbox.width - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            y1 = y2 = bbox.height/2 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            x1 = x2 = bbox.width/2 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            y1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            y2 = bbox.height - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        g.setColor(darker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        g.drawLine(x1, y2, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        g.setColor(brighter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if (orientation == HORIZONTAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            g.drawLine(x1, y2+1, x2, y2+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            g.drawLine(x1+1, y2, x2+1, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
 * Motif file dialogs let the user specify a filter that controls the files that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
 * are displayed in the dialog. This filter is generally specified as a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
 * expression. The class is used to implement Motif-like filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
class FileDialogFilter implements FilenameFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    String filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    public FileDialogFilter(String f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        filter = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Tells whether or not the specified file should be included in a file list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public boolean accept(File dir, String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        File f = new File(dir, fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if (f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            return matches(fileName, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * Tells whether or not the input string matches the given filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    private boolean matches(String input, String filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        String regex = convert(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        return input.matches(regex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * Converts the filter into the form which is acceptable by Java's regexps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    private String convert(String filter) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   922
        String regex = "^" + filter + "$";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        regex = regex.replaceAll("\\.", "\\\\.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        regex = regex.replaceAll("\\?", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        regex = regex.replaceAll("\\*", ".*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return regex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
}