001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: KFileChooserDialog.java,v $ 023 Revision 1.6 2004/05/31 07:43:40 markl 024 added setFileSelectionMode() 025 026 Revision 1.5 2004/05/12 18:09:34 markl 027 javadoc updates 028 029 Revision 1.4 2004/05/05 23:20:24 markl 030 comment block updates 031 032 Revision 1.3 2004/03/18 07:21:57 markl 033 javadoc typo fix 034 035 Revision 1.2 2004/03/16 05:46:26 markl 036 added support for multiple selection 037 038 Revision 1.1 2003/01/19 09:38:00 markl 039 New class. 040 ---------------------------------------------------------------------------- 041*/ 042 043package kiwi.ui.dialog; 044 045import java.awt.*; 046import java.awt.event.*; 047import java.io.File; 048import javax.swing.*; 049 050import kiwi.ui.*; 051import kiwi.event.*; 052 053/** A standard Kiwi dialog wrapper for the <code>KFileChooser</code> 054 * component. This dialog behaves like other Kiwi dialogs and should be used 055 * in place of <code>KFileChooser.showDialog()</code>. 056 * 057 * <p><center> 058 * <img src="snapshot/KFileChooserDialog.gif"><br> 059 * <i>An example KFileChooserDialog.</i> 060 * </center> 061 * 062 * @author Mark Lindner 063 * @see kiwi.ui.KFileChooser 064 * @since Kiwi 1.4 065 */ 066 067public class KFileChooserDialog extends KDialog implements ActionListener 068 { 069 /** The <code>KFileChooser</code> used by this dialog. */ 070 protected KFileChooser fileChooser; 071 072 /** Construct a new modal <code>KFileChooserDialog</code> with the specified 073 * parent window and title. 074 * 075 * @param parent The parent window for the dialog. 076 * @param title The title for the dialog. 077 * @param type The dialog type; one of the constants 078 * <code>OPEN_DIALOG</code>, <code>SAVE_DIALOG</code>, or 079 * <code>CUSTOM_DIALOG</code>, which are defined in 080 * <code>KFileChooser</code>. 081 */ 082 083 public KFileChooserDialog(Frame parent, String title, int type) 084 { 085 super(parent, title, true); 086 087 _init(type); 088 } 089 090 /** Construct a new modal <code>KFileChooserDialog</code> with the specified 091 * parent dialog and title. 092 * 093 * @param parent The parent dialog. 094 * @param title The title for the dialog. 095 * @param type The dialog type; one of <code>OPEN_DIALOG</code>, 096 * <code>SAVE_DIALOG</code>, or <code>CUSTOM_DIALOG</code>. 097 */ 098 099 public KFileChooserDialog(Dialog parent, String title, int type) 100 { 101 super(parent, title, true); 102 103 _init(type); 104 } 105 106 /** Get a reference to the <code>KFileChooser</code> used by this dialog. 107 * 108 * @return The <code>KFileChooser</code> instance. 109 */ 110 111 public KFileChooser getFileChooser() 112 { 113 return(fileChooser); 114 } 115 116 /* 117 */ 118 119 private void _init(int type) 120 { 121 fileChooser = new KFileChooser(); 122 123 KPanel panel = getMainContainer(); 124 panel.setLayout(new GridLayout(1, 0)); 125 panel.add(fileChooser); 126 127 fileChooser.setDialogType(type); 128 fileChooser.addActionListener(this); 129 130 pack(); 131 } 132 133 /** A convenience method to enable or disable multiple selection. Delegates 134 * to the embedded <code>KFileChooser</code>. 135 * 136 * @since Kiwi 2.0 137 */ 138 139 public void setMultiSelectionEnabled(boolean flag) 140 { 141 fileChooser.setMultiSelectionEnabled(flag); 142 } 143 144 /** A convenience method to set the file selection mode. Delegates to the 145 * embedded <code>KFileChooser</code>. 146 * 147 * @since Kiwi 2.0 148 */ 149 150 public void setFileSelectionMode(int mode) 151 { 152 fileChooser.setFileSelectionMode(mode); 153 } 154 155 /** A convenience method to get the currently selected file. Delegates to 156 * the embedded <code>KFileChooser</code>. 157 */ 158 159 public File getSelectedFile() 160 { 161 return(fileChooser.getSelectedFile()); 162 } 163 164 /** A convenience method to get the currently selected files. Delegates to 165 * the embedded <code>KFileChooser</code>. 166 * 167 * @since Kiwi 2.0 168 */ 169 170 public File[] getSelectedFiles() 171 { 172 if(fileChooser.isMultiSelectionEnabled()) 173 return(fileChooser.getSelectedFiles()); 174 else 175 { 176 File files[] = new File[1]; 177 files[0] = fileChooser.getSelectedFile(); 178 return(files); 179 } 180 } 181 182 /** A convenience method to set the currently selected file. Delegates to 183 * the embedded <code>KFileChooser</code>. 184 */ 185 186 public void setSelectedFile(File file) 187 { 188 fileChooser.setSelectedFile(file); 189 } 190 191 /** This method is public as an implementation side-effect. 192 */ 193 194 public void actionPerformed(ActionEvent evt) 195 { 196 String command = evt.getActionCommand(); 197 198 if(command.equals(KFileChooser.APPROVE_SELECTION)) 199 doAccept(); 200 else if(command.equals(KFileChooser.CANCEL_SELECTION)) 201 doCancel(); 202 } 203 204 } 205 206/* end of source file */