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-2016 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 */
040public class BrowseActionListener implements ActionListener
041{
042  private JFileChooser fc;
043
044  private JTextComponent field;
045
046  private Component parent;
047
048  private BrowseType type;
049
050  /** Enumeration used to specify which kind of file browser dialog must be displayed. */
051  public enum BrowseType
052  {
053    /** The Browser is used to retrieve a directory. */
054    LOCATION_DIRECTORY,
055    /** The Browser is used to retrieve an LDIF file. */
056    OPEN_LDIF_FILE,
057    /** The Browser is used to retrieve a .zip file. */
058    OPEN_ZIP_FILE,
059    /** The Browser is used to retrieve a generic file. */
060    OPEN_GENERIC_FILE,
061    /** The Browser is used to create a generic file. */
062    CREATE_GENERIC_FILE,
063    /** The Browser is used to create an LDIF file. */
064    CREATE_LDIF_FILE,
065    /** The Browser is used to create a generic directory. */
066    CREATE_DIRECTORY
067  }
068
069  /**
070   * Constructor for the BrowseActionListener.
071   *
072   * @param field
073   *          the text component that will be updated when the user selects
074   *          something in the file browser dialog.
075   * @param type
076   *          the type of file browse dialog that will be displayed.
077   * @param parent
078   *          component that will be used as reference to display the file
079   *          browse dialog.
080   */
081  public BrowseActionListener(JTextComponent field, BrowseType type,
082      Component parent)
083  {
084    this.field = field;
085    this.type = type;
086    this.parent = parent;
087
088    fc = new JFileChooser();
089    switch (type)
090    {
091    case LOCATION_DIRECTORY:
092      fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
093      fc.setDialogType(JFileChooser.OPEN_DIALOG);
094      fc.setDialogTitle("Choose Directory");
095      break;
096
097    case CREATE_DIRECTORY:
098      fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
099      fc.setDialogType(JFileChooser.SAVE_DIALOG);
100      fc.setDialogTitle("Choose Directory");
101      break;
102
103    case OPEN_LDIF_FILE:
104      fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
105      fc.setDialogType(JFileChooser.OPEN_DIALOG);
106      fc.setDialogTitle(INFO_OPEN_LDIF_FILE_DIALOG_TITLE.get().toString());
107      ExtensionFileFilter ldifFiles =
108          new ExtensionFileFilter("ldif",
109              INFO_LDIF_FILES_DESCRIPTION.get().toString());
110
111      fc.addChoosableFileFilter(ldifFiles);
112      fc.setFileFilter(ldifFiles);
113      break;
114
115    case CREATE_LDIF_FILE:
116      fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
117      fc.setDialogType(JFileChooser.SAVE_DIALOG);
118      fc.setDialogTitle(INFO_OPEN_LDIF_FILE_DIALOG_TITLE.get().toString());
119      ldifFiles = new ExtensionFileFilter("ldif",
120              INFO_LDIF_FILES_DESCRIPTION.get().toString());
121
122      fc.addChoosableFileFilter(ldifFiles);
123      fc.setFileFilter(ldifFiles);
124      break;
125
126    case OPEN_ZIP_FILE:
127        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
128        fc.setDialogType(JFileChooser.OPEN_DIALOG);
129        fc.setDialogTitle(INFO_OPEN_ZIP_FILE_DIALOG_TITLE.get().toString());
130        ExtensionFileFilter zipFiles =
131            new ExtensionFileFilter("zip",
132                INFO_ZIP_FILES_DESCRIPTION.get().toString());
133
134        fc.addChoosableFileFilter(zipFiles);
135        fc.setFileFilter(zipFiles);
136        break;
137
138    case OPEN_GENERIC_FILE:
139      fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
140      fc.setDialogType(JFileChooser.OPEN_DIALOG);
141      fc.setDialogTitle(INFO_OPEN_GENERIC_FILE_DIALOG_TITLE.get().toString());
142
143      break;
144
145    case CREATE_GENERIC_FILE:
146      fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
147      fc.setDialogType(JFileChooser.SAVE_DIALOG);
148      fc.setDialogTitle(INFO_OPEN_GENERIC_FILE_DIALOG_TITLE.get().toString());
149      break;
150
151    default:
152      throw new IllegalArgumentException("Unknown BrowseType: " + type);
153    }
154  }
155
156  /**
157   * ActionListener implementation. It will display a file browser dialog and
158   * then will update the text component if the user selects something on the
159   * dialog.
160   *
161   * @param e the ActionEvent we receive.
162   */
163  @Override
164  public void actionPerformed(ActionEvent e)
165  {
166    int returnVal;
167
168    /* If we can get the current field parent directory set to it */
169    String path = field.getText();
170    if (path != null && path.trim().length() > 0)
171    {
172      File f = new File(path);
173      while (f != null && !f.isDirectory())
174      {
175        f = f.getParentFile();
176      }
177      if (f != null)
178      {
179        fc.setCurrentDirectory(f);
180      }
181    }
182
183    switch (type)
184    {
185    case LOCATION_DIRECTORY:
186      returnVal = fc.showOpenDialog(parent);
187      break;
188
189    case OPEN_LDIF_FILE:
190      returnVal = fc.showOpenDialog(parent);
191      break;
192
193    case OPEN_ZIP_FILE:
194      returnVal = fc.showOpenDialog(parent);
195      break;
196
197    case OPEN_GENERIC_FILE:
198      returnVal = fc.showOpenDialog(parent);
199      break;
200    case CREATE_GENERIC_FILE:
201      returnVal = fc.showSaveDialog(parent);
202      break;
203
204    case CREATE_LDIF_FILE:
205      returnVal = fc.showSaveDialog(parent);
206      break;
207
208    case CREATE_DIRECTORY:
209      returnVal = fc.showSaveDialog(parent);
210      break;
211
212    default:
213      throw new RuntimeException("Unknown type: " + type);
214    }
215
216    if (returnVal == JFileChooser.APPROVE_OPTION)
217    {
218      File file = fc.getSelectedFile();
219      field.setText(file.getAbsolutePath());
220      field.requestFocusInWindow();
221      field.selectAll();
222      fieldUpdated();
223    }
224  }
225
226  /** The method that is called after the text field is updated. */
227  protected void fieldUpdated()
228  {
229  }
230}