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