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 2006-2008 Sun Microsystems, Inc. 025 * Portions Copyright 2015 ForgeRock AS 026 */ 027 028package org.opends.quicksetup.event; 029 030import java.awt.Component; 031import java.awt.event.ActionEvent; 032import java.awt.event.ActionListener; 033import java.io.File; 034 035import javax.swing.JFileChooser; 036import javax.swing.text.JTextComponent; 037 038import org.opends.quicksetup.util.ExtensionFileFilter; 039import static org.opends.messages.QuickSetupMessages.*; 040 041/** 042 * This class is an action listener used to update a text component. When the 043 * class receives an ActionEvent it will display a File Browser Dialog and will 044 * update the text field depending on what the user chooses in the browser 045 * dialog. 046 * 047 * The class is used generally by adding it as ActionListener of a 'Browse' 048 * button. 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 GENERIC_FILE 084 } 085 086 /** 087 * Constructor for the BrowseActionListener. 088 * 089 * @param field 090 * the text component that will be updated when the user selects 091 * something in the file browser dialog. 092 * @param type 093 * the type of file browse dialog that will be displayed. 094 * @param parent 095 * component that will be used as reference to display the file 096 * browse dialog. 097 */ 098 public BrowseActionListener(JTextComponent field, BrowseType type, 099 Component parent) 100 { 101 this.field = field; 102 this.type = type; 103 this.parent = parent; 104 105 fc = new JFileChooser(); 106 switch (type) 107 { 108 case LOCATION_DIRECTORY: 109 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 110 fc.setDialogType(JFileChooser.OPEN_DIALOG); 111 fc.setDialogTitle(INFO_OPEN_SERVER_LOCATION_DIALOG_TITLE 112 .get().toString()); 113 break; 114 115 case OPEN_LDIF_FILE: 116 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 117 fc.setDialogType(JFileChooser.OPEN_DIALOG); 118 fc.setDialogTitle(INFO_OPEN_LDIF_FILE_DIALOG_TITLE.get().toString()); 119 ExtensionFileFilter ldifFiles = 120 new ExtensionFileFilter("ldif", 121 INFO_LDIF_FILES_DESCRIPTION.get().toString()); 122 123 fc.addChoosableFileFilter(ldifFiles); 124 fc.setFileFilter(ldifFiles); 125 break; 126 127 case OPEN_ZIP_FILE: 128 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 129 fc.setDialogType(JFileChooser.OPEN_DIALOG); 130 fc.setDialogTitle(INFO_OPEN_ZIP_FILE_DIALOG_TITLE.get().toString()); 131 ExtensionFileFilter zipFiles = 132 new ExtensionFileFilter("zip", 133 INFO_ZIP_FILES_DESCRIPTION.get().toString()); 134 135 fc.addChoosableFileFilter(zipFiles); 136 fc.setFileFilter(zipFiles); 137 break; 138 139 case GENERIC_FILE: 140 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 141 fc.setDialogType(JFileChooser.OPEN_DIALOG); 142 fc.setDialogTitle(INFO_OPEN_GENERIC_FILE_DIALOG_TITLE.get().toString()); 143 144 break; 145 146 default: 147 throw new IllegalArgumentException("Unknown BrowseType: " + type); 148 } 149 } 150 151 /** 152 * ActionListener implementation. It will display a file browser dialog and 153 * then will update the text component if the user selects something on the 154 * dialog. 155 * 156 * @param e the ActionEvent we receive. 157 * 158 */ 159 public void actionPerformed(ActionEvent e) 160 { 161 int returnVal; 162 163 /* If we can get the current field parent directory set to it */ 164 String path = field.getText(); 165 if (path != null && path.trim().length() > 0) 166 { 167 File f = new File(path); 168 while (f != null && !f.isDirectory()) 169 { 170 f = f.getParentFile(); 171 } 172 if (f != null) 173 { 174 fc.setCurrentDirectory(f); 175 } 176 } 177 178 switch (type) 179 { 180 case LOCATION_DIRECTORY: 181 returnVal = fc.showOpenDialog(parent); 182 break; 183 184 case OPEN_LDIF_FILE: 185 returnVal = fc.showOpenDialog(parent); 186 break; 187 188 case OPEN_ZIP_FILE: 189 returnVal = fc.showOpenDialog(parent); 190 break; 191 192 case GENERIC_FILE: 193 returnVal = fc.showOpenDialog(parent); 194 break; 195 196 default: 197 throw new IllegalStateException("Unknown type: " + type); 198 } 199 200 if (returnVal == JFileChooser.APPROVE_OPTION) 201 { 202 File file = fc.getSelectedFile(); 203 field.setText(file.getAbsolutePath()); 204 field.requestFocusInWindow(); 205 field.selectAll(); 206 } 207 } 208}