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: KFileChooser.java,v $
023   Revision 1.4  2004/05/13 00:36:52  markl
024   comment block updates
025
026   Revision 1.3  2004/03/15 06:13:42  markl
027   Set selected files to null when cancelled.
028
029   Revision 1.2  2003/02/06 07:40:47  markl
030   removed debug statement
031
032   Revision 1.1  2003/01/19 09:37:59  markl
033   New class.
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.ui;
038
039import java.awt.*;
040import java.awt.event.*;
041import java.io.*;
042import javax.swing.*;
043import javax.swing.filechooser.*;
044
045import kiwi.ui.dialog.*;
046import kiwi.util.*;
047
048/** A direct replacement for <code>JFileChooser</code> that supports background
049 * texturing. All methods behave equivalently to the superclass, with the
050 * exception of <code>getSelectedFile()</code>, which now returns
051 * <code>null</code> if the <i>Cancel</i> button was pressed.
052 *
053 * <p><center>
054 * <img src="snapshot/KFileChooser.gif"><br>
055 * <i>An example KFileChooser.</i>
056 * </center>
057 *
058 * @author Mark Lindner
059 * @since Kiwi 1.4
060 * @see kiwi.ui.dialog.KFileChooserDialog
061 */
062
063public class KFileChooser extends JFileChooser
064  {
065  protected KDialog _dialog = null;
066  protected int _returnValue = ERROR_OPTION;
067
068  /**
069   */
070
071  public KFileChooser()
072    {
073    super();
074
075    _init();
076    }
077
078  /**
079   */
080  
081  public KFileChooser(File currentDirectory)
082    {
083    super(currentDirectory);
084
085    _init();
086    }
087
088  /**
089   */
090  
091  public KFileChooser(File currentDirectory, FileSystemView fsv)
092    {
093    super(currentDirectory, fsv);
094
095    _init();
096    }
097
098  /**
099   */
100  
101  public KFileChooser(FileSystemView fsv)
102    {
103    super(fsv);
104
105    _init();
106    }
107
108  /**
109   */
110  
111  public KFileChooser(String currentDirectoryPath)
112    {
113    super(currentDirectoryPath);
114
115    _init();
116    }
117
118  /**
119   */
120  
121  public KFileChooser(String currentDirectoryPath, FileSystemView fsv)
122    {
123    super(currentDirectoryPath, fsv);
124
125    _init();
126    }
127
128  /*
129   */
130  
131  private void _init()
132    {
133    _removeOpacity(this);
134    }
135
136  /*
137   */
138  
139  private void _removeOpacity(Component c)
140    {
141    if(!(c instanceof JComponent))
142      return;
143    
144    if((c instanceof JPanel) || (c instanceof AbstractButton)
145      || (c instanceof JComboBox))
146      ((JComponent)c).setOpaque(false);
147
148    Container cont = (Container)c;
149    
150    for(int i = 0; i < cont.getComponentCount(); i++)
151      _removeOpacity(cont.getComponent(i));
152    }
153
154  /**
155   */
156
157  public int showDialog(Component parent, String approveButtonText)
158    {
159    if(approveButtonText != null)
160      {
161      setApproveButtonText(approveButtonText);
162      setDialogType(CUSTOM_DIALOG);
163      }
164
165    Frame frame = ((parent instanceof Frame) ? (Frame)parent
166                   : (Frame)SwingUtilities.getAncestorOfClass(Frame.class,
167                                                              parent));
168
169    String title = null;
170
171    if(getDialogTitle() != null)
172      title = getDialogTitle(); //dialogTitle;
173    else
174      title = getUI().getDialogTitle(this);
175
176    if(_dialog == null)
177      _dialog = new _Dialog(frame, title, true, this);
178    
179    _dialog.setLocationRelativeTo(parent);
180
181    rescanCurrentDirectory();
182 
183    _dialog.setVisible(true);
184
185    return(_returnValue);
186    }
187
188  /*
189   */
190
191  private class _Dialog extends KDialog
192    {
193    public _Dialog(Frame parent, String title, boolean modal,
194                         KFileChooser chooser)
195      {
196      super(parent, title, modal);
197
198      KPanel p = getMainContainer();
199      p.setLayout(new GridLayout(1, 0));
200      p.add(chooser);
201
202      pack();
203      }
204    }
205
206  /**
207   */
208  
209  public void approveSelection()
210    {
211    // ugly hack because "returnValue" and "dialog" are private in the
212    // superclass
213    
214    super.approveSelection();
215    if(_dialog != null)
216      _dialog.setVisible(false);
217    
218    _returnValue = APPROVE_OPTION;
219    }
220
221  /**
222   */
223  
224  public void cancelSelection()
225    {
226    // ugly hack because "returnValue" and "dialog" are private in the
227    // superclass
228
229    super.cancelSelection();
230    if(_dialog != null)
231      _dialog.setVisible(false);
232
233    setSelectedFile(null); // how did they overlook this???
234    setSelectedFiles(null);
235    _returnValue = CANCEL_OPTION;    
236    }
237
238  }
239
240/* end of source file */