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-2016 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 */ 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 GENERIC_FILE 061 } 062 063 /** 064 * Constructor for the BrowseActionListener. 065 * 066 * @param field 067 * the text component that will be updated when the user selects 068 * something in the file browser dialog. 069 * @param type 070 * the type of file browse dialog that will be displayed. 071 * @param parent 072 * component that will be used as reference to display the file 073 * browse dialog. 074 */ 075 public BrowseActionListener(JTextComponent field, BrowseType type, 076 Component parent) 077 { 078 this.field = field; 079 this.type = type; 080 this.parent = parent; 081 082 fc = new JFileChooser(); 083 switch (type) 084 { 085 case LOCATION_DIRECTORY: 086 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 087 fc.setDialogType(JFileChooser.OPEN_DIALOG); 088 fc.setDialogTitle(INFO_OPEN_SERVER_LOCATION_DIALOG_TITLE 089 .get().toString()); 090 break; 091 092 case OPEN_LDIF_FILE: 093 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 094 fc.setDialogType(JFileChooser.OPEN_DIALOG); 095 fc.setDialogTitle(INFO_OPEN_LDIF_FILE_DIALOG_TITLE.get().toString()); 096 ExtensionFileFilter ldifFiles = 097 new ExtensionFileFilter("ldif", 098 INFO_LDIF_FILES_DESCRIPTION.get().toString()); 099 100 fc.addChoosableFileFilter(ldifFiles); 101 fc.setFileFilter(ldifFiles); 102 break; 103 104 case OPEN_ZIP_FILE: 105 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 106 fc.setDialogType(JFileChooser.OPEN_DIALOG); 107 fc.setDialogTitle(INFO_OPEN_ZIP_FILE_DIALOG_TITLE.get().toString()); 108 ExtensionFileFilter zipFiles = 109 new ExtensionFileFilter("zip", 110 INFO_ZIP_FILES_DESCRIPTION.get().toString()); 111 112 fc.addChoosableFileFilter(zipFiles); 113 fc.setFileFilter(zipFiles); 114 break; 115 116 case GENERIC_FILE: 117 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 118 fc.setDialogType(JFileChooser.OPEN_DIALOG); 119 fc.setDialogTitle(INFO_OPEN_GENERIC_FILE_DIALOG_TITLE.get().toString()); 120 121 break; 122 123 default: 124 throw new IllegalArgumentException("Unknown BrowseType: " + type); 125 } 126 } 127 128 /** 129 * ActionListener implementation. It will display a file browser dialog and 130 * then will update the text component if the user selects something on the 131 * dialog. 132 * 133 * @param e the ActionEvent we receive. 134 */ 135 @Override 136 public void actionPerformed(ActionEvent e) 137 { 138 int returnVal; 139 140 /* If we can get the current field parent directory set to it */ 141 String path = field.getText(); 142 if (path != null && path.trim().length() > 0) 143 { 144 File f = new File(path); 145 while (f != null && !f.isDirectory()) 146 { 147 f = f.getParentFile(); 148 } 149 if (f != null) 150 { 151 fc.setCurrentDirectory(f); 152 } 153 } 154 155 switch (type) 156 { 157 case LOCATION_DIRECTORY: 158 returnVal = fc.showOpenDialog(parent); 159 break; 160 161 case OPEN_LDIF_FILE: 162 returnVal = fc.showOpenDialog(parent); 163 break; 164 165 case OPEN_ZIP_FILE: 166 returnVal = fc.showOpenDialog(parent); 167 break; 168 169 case GENERIC_FILE: 170 returnVal = fc.showOpenDialog(parent); 171 break; 172 173 default: 174 throw new IllegalStateException("Unknown type: " + type); 175 } 176 177 if (returnVal == JFileChooser.APPROVE_OPTION) 178 { 179 File file = fc.getSelectedFile(); 180 field.setText(file.getAbsolutePath()); 181 field.requestFocusInWindow(); 182 field.selectAll(); 183 } 184 } 185}