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