/* File: PopupFileDialog.java */ package O2PlibSS.gui; import java.awt.*; import java.io.*; /** * Class PopupFileDialog is a file browser *
*
* This code is available at the HTMLtools project on SourceForge at * http://htmltools.sourceforge.org/ * under the "Common Public License Version 1.0" * * http://www.opensource.org/licenses/cpl1.0.php.
** It was derived and refactored from the open source * MAExplorer (http://maexplorer.sourceforge.org/), and * Open2Dprot (http://Open2Dprot.sourceforge.net/) Table modules. *
* $Date: 2009/07/05 11:45:56 $ $Revision: 1.28 $
*
* Copyright 2008, 2009 by Peter Lemkin
* E-Mail: lemkin@users.sourceforge.net
* http://lemkingroup.com/
*/
public class PopupFileDialog extends Frame
{ /* PopupFileDialog*/
/** frame id */
final static long
serialVersionUID= 0;
/** for use in popup FileDialog */
public FileDialog
fileDialog;
/**
* PopupFileDialog() - constructor
*/
public PopupFileDialog()
{ /* PopupFileDialog */
} /* PopupFileDialog */
/**
* popupFileDialog() - popup file dialog and get file name.
* May be null if did CANCEL.
* @param initialPath to start browsing
* @param extension to change the filename filter. Null is no filter.
* @param msg to display in prompt
* @param fileLoadFlag if true, else do a SAVE
* @return file name or null if did a cancel.
*/
public String popupFileDialog(String initialPath, String extension,
String sPrompt, boolean fileLoadFlag)
{ /* popupFileDialog */
Frame f= new Frame("File Name");
String
fileSeparator= System.getProperty("file.separator"),
userDir= System.getProperty("user.dir")+fileSeparator;
int
idx= (initialPath==null)
? -1
: initialPath.lastIndexOf(fileSeparator),
fdType= (fileLoadFlag) ? FileDialog.LOAD : FileDialog.SAVE;
String
file,
defDir= (idx==-1) ? userDir : initialPath.substring(0,idx),
defFile= (idx==-1) ? initialPath : initialPath.substring(idx+1),
directory= "",
fileName= ""; /* fileName to return */
fileDialog= new FileDialog(f, sPrompt, fdType);
if(fileDialog==null)
{
return(null);
}
else
{ /* run the modal popup file dialog box */
if(initialPath!=null)
{
fileDialog.setDirectory(defDir);
fileDialog.setFile(defFile);
}
if(extension!=null && extension.length()>0)
fileDialog.setFilenameFilter(new JavaExtensionFilter(extension));
fileDialog.pack();
fileDialog.setVisible(true);
fileName= fileDialog.getFile(); /* This is MODAL. */
directory= fileDialog.getDirectory();
if(fileName==null)
return(null);
file= directory + fileName;
}
return(file);
} /* popupFileDialog */
/**
* popupDirectoryDialog() - popup directory dialog and get directory name.
* May be null if did CANCEL.
* @param initialPath to start browsing
* @param msg to display in prompt
* @param floadFlag if true, else do a SAVE
* @return directory name or null if did a cancel.
*/
public String popupDirectoryDialog(String initialPath, String sPrompt,
boolean loadFlag)
{ /* popupDirectoryDialog */
String
fileSeparator= System.getProperty("file.separator"),
userDir= System.getProperty("user.dir")+fileSeparator;
int
idx= (initialPath==null)
? -1
: initialPath.lastIndexOf(fileSeparator),
fdType= (loadFlag) ? FileDialog.LOAD : FileDialog.SAVE;
String
fakeFile= "Browse to Folder - then press "+
((loadFlag) ? "'Open'" : "'Save'"),
//defDir= (idx==-1) ? userDir : initialPath.substring(0,idx);
defDir= (idx==-1) ? userDir : initialPath;
fileDialog= new FileDialog(this, sPrompt);
fileDialog.setMode(fdType);
fileDialog.setDirectory(defDir);
fileDialog.setFile(fakeFile);
fileDialog.setVisible(true);
String newDir= fileDialog.getDirectory(); /* get selected results */
if(newDir==null)
{
return(null);
}
return(newDir);
} /* popupDirectoryDialog */
/* FilenameFilter Class */
class JavaExtensionFilter implements FilenameFilter
{ /* JavaExtensionFilter */
public String
extension= null;
/**
* JavaExtensionFilter() - Constructor
* @param extension
*/
public JavaExtensionFilter(String extension)
{ this.extension= extension; }
/** accept() - called by FileDialog to filter names
* @param dir to search
* @param name to test
* @return true if name.endsWith(this.extension)
*/
public boolean accept(File dir, String name)
{ /* accept */
if(extension==null || extension.length()==0)
return(true);
return(name.endsWith(extension));
} /* accept */
} /* JavaExtensionFilter */
} /* End of class PopupFileDialog*/