001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2015 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.event;
019
020import static org.opends.messages.QuickSetupMessages.*;
021
022import java.awt.Component;
023import java.awt.event.ActionEvent;
024import java.awt.event.ActionListener;
025import java.io.File;
026
027import javax.swing.JFileChooser;
028import javax.swing.text.JTextComponent;
029
030import org.opends.quicksetup.util.ExtensionFileFilter;
031
032/**
033 * This is a class that automates the update of a text field with what the user
034 * selects in a file chooser.  The class is not in charge of creating the
035 * components or of updating the layout, it simply adds the required listeners
036 * in the buttons and text fields so that a file chooser will be displayed
037 * when the user clicks on the button and if the user chooses a file or a
038 * directory the text field will be updated accordingly.
039 *
040 */
041public class BrowseActionListener implements ActionListener
042{
043  private JFileChooser fc;
044
045  private JTextComponent field;
046
047  private Component parent;
048
049  private BrowseType type;
050
051  /**
052   * Enumeration used to specify which kind of file browser dialog must be
053   * displayed.
054   *
055   */
056  public enum BrowseType
057  {
058    /**
059     * The Browser is used to retrieve a directory.
060     */
061    LOCATION_DIRECTORY,
062    /**
063     * The Browser is used to retrieve an LDIF file.
064     */
065    OPEN_LDIF_FILE,
066    /**
067     * The Browser is used to retrieve a .zip file.
068     */
069    OPEN_ZIP_FILE,
070    /**
071     * The Browser is used to retrieve a generic file.
072     */
073    OPEN_GENERIC_FILE,
074    /**
075     * The Browser is used to create a generic file.
076     */
077    CREATE_GENERIC_FILE,
078    /**
079     * The Browser is used to create an LDIF file.
080     */
081    CREATE_LDIF_FILE,
082    /**
083     * The Browser is used to create a generic directory.
084     */
085    CREATE_DIRECTORY
086  }
087
088  /**
089   * Constructor for the BrowseActionListener.
090   *
091   * @param field
092   *          the text component that will be updated when the user selects
093   *          something in the file browser dialog.
094   * @param type
095   *          the type of file browse dialog that will be displayed.
096   * @param parent
097   *          component that will be used as reference to display the file
098   *          browse dialog.
099   */
100  public BrowseActionListener(JTextComponent field, BrowseType type,
101      Component parent)
102  {
103    this.field = field;
104    this.type = type;
105    this.parent = parent;
106
107    fc = new JFileChooser();
108    switch (type)
109    {
110    case LOCATION_DIRECTORY:
111      fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
112      fc.setDialogType(JFileChooser.OPEN_DIALOG);
113      fc.setDialogTitle("Choose Directory");
114      break;
115
116    case CREATE_DIRECTORY:
117      fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
118      fc.setDialogType(JFileChooser.SAVE_DIALOG);
119      fc.setDialogTitle("Choose Directory");
120      break;
121
122    case OPEN_LDIF_FILE:
123      fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
124      fc.setDialogType(JFileChooser.OPEN_DIALOG);
125      fc.setDialogTitle(INFO_OPEN_LDIF_FILE_DIALOG_TITLE.get().toString());
126      ExtensionFileFilter ldifFiles =
127          new ExtensionFileFilter("ldif",
128              INFO_LDIF_FILES_DESCRIPTION.get().toString());
129
130      fc.addChoosableFileFilter(ldifFiles);
131      fc.setFileFilter(ldifFiles);
132      break;
133
134    case CREATE_LDIF_FILE:
135      fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
136      fc.setDialogType(JFileChooser.SAVE_DIALOG);
137      fc.setDialogTitle(INFO_OPEN_LDIF_FILE_DIALOG_TITLE.get().toString());
138      ldifFiles = new ExtensionFileFilter("ldif",
139              INFO_LDIF_FILES_DESCRIPTION.get().toString());
140
141      fc.addChoosableFileFilter(ldifFiles);
142      fc.setFileFilter(ldifFiles);
143      break;
144
145    case OPEN_ZIP_FILE:
146        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
147        fc.setDialogType(JFileChooser.OPEN_DIALOG);
148        fc.setDialogTitle(INFO_OPEN_ZIP_FILE_DIALOG_TITLE.get().toString());
149        ExtensionFileFilter zipFiles =
150            new ExtensionFileFilter("zip",
151                INFO_ZIP_FILES_DESCRIPTION.get().toString());
152
153        fc.addChoosableFileFilter(zipFiles);
154        fc.setFileFilter(zipFiles);
155        break;
156
157    case OPEN_GENERIC_FILE:
158      fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
159      fc.setDialogType(JFileChooser.OPEN_DIALOG);
160      fc.setDialogTitle(INFO_OPEN_GENERIC_FILE_DIALOG_TITLE.get().toString());
161
162      break;
163
164    case CREATE_GENERIC_FILE:
165      fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
166      fc.setDialogType(JFileChooser.SAVE_DIALOG);
167      fc.setDialogTitle(INFO_OPEN_GENERIC_FILE_DIALOG_TITLE.get().toString());
168      break;
169
170    default:
171      throw new IllegalArgumentException("Unknown BrowseType: " + type);
172    }
173  }
174
175  /**
176   * ActionListener implementation. It will display a file browser dialog and
177   * then will update the text component if the user selects something on the
178   * dialog.
179   *
180   * @param e the ActionEvent we receive.
181   *
182   */
183  public void actionPerformed(ActionEvent e)
184  {
185    int returnVal;
186
187    /* If we can get the current field parent directory set to it */
188    String path = field.getText();
189    if (path != null && path.trim().length() > 0)
190    {
191      File f = new File(path);
192      while (f != null && !f.isDirectory())
193      {
194        f = f.getParentFile();
195      }
196      if (f != null)
197      {
198        fc.setCurrentDirectory(f);
199      }
200    }
201
202    switch (type)
203    {
204    case LOCATION_DIRECTORY:
205      returnVal = fc.showOpenDialog(parent);
206      break;
207
208    case OPEN_LDIF_FILE:
209      returnVal = fc.showOpenDialog(parent);
210      break;
211
212    case OPEN_ZIP_FILE:
213      returnVal = fc.showOpenDialog(parent);
214      break;
215
216    case OPEN_GENERIC_FILE:
217      returnVal = fc.showOpenDialog(parent);
218      break;
219    case CREATE_GENERIC_FILE:
220      returnVal = fc.showSaveDialog(parent);
221      break;
222
223    case CREATE_LDIF_FILE:
224      returnVal = fc.showSaveDialog(parent);
225      break;
226
227    case CREATE_DIRECTORY:
228      returnVal = fc.showSaveDialog(parent);
229      break;
230
231    default:
232      throw new RuntimeException("Unknown type: " + type);
233    }
234
235    if (returnVal == JFileChooser.APPROVE_OPTION)
236    {
237      File file = fc.getSelectedFile();
238      field.setText(file.getAbsolutePath());
239      field.requestFocusInWindow();
240      field.selectAll();
241      fieldUpdated();
242    }
243  }
244
245  /**
246   * The method that is called after the text field is updated.
247   *
248   */
249  protected void fieldUpdated()
250  {
251  }
252}