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