1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| package BROWSE;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Locale;
import java.util.prefs.Preferences;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.poi.openxml4j.util.ZipSecureFile;
/**
* <h1>My custom JFileChooser</h1>
*
* @author t0163126
*/
public class FileChooser extends JFileChooser
{
private static final long serialVersionUID = 1L;
/**
* <p>This is a filter for the JFileChooser to excel</p>
*/
private final FileFilter filterExcel;
/**
* <p>This is the description file</p>
*/
private final String description;
/**
* <p>This is the XLS format</p>
*/
private final String xls;
/**
* <p>This is the XLSX format</p>
*/
private final String xlsx;
/**
* <p>This is the current file path</p>
*/
private File curDir;
/**
* <p>This is the last path</p>
*/
private static String lastPath;
/**
* <p>Constructor no-argument to customized the fileChooser</p>
*/
public FileChooser()
{
// Change the JVM local in english
Locale myLocale = Locale.ENGLISH;
Locale.setDefault(myLocale);
// Change the LookAndFeel :
UIManager.getDefaults().setDefaultLocale(myLocale);
// Change the new components
JComponent.setDefaultLocale(myLocale);
//The title dialog
UIManager.put("FileChooser.openDialogTitleText", "I'm here : ");
// change title
this.curDir = this.getCurrentDirectory();
this.setDialogTitle("" + this.curDir.getAbsolutePath());
this.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent evt)
{
if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(evt.getPropertyName()))
{
File curDir = getCurrentDirectory();
setDialogTitle("" + curDir.getAbsolutePath());
}
}
});
// The look In label
UIManager.put("FileChooser.lookInLabelText", "Look In :");
// The file name label
UIManager.put("FileChooser.fileNameLabelText", "File name :");
// The file of type label
UIManager.put("FileChooser.filesOfTypeLabelText", "File type :");
// The open button
UIManager.put("FileChooser.openButtonText", "Load it");
//The open button tool tip
UIManager.put("FileChooser.openButtonToolTipText", "Load selected file");
// The cancel button
UIManager.put("FileChooser.cancelButtonText", "Cancel");
//The cancel button tool tip
UIManager.put("FileChooser.cancelButtonToolTipText","Cancel");
//The file name header button
UIManager.put("FileChooser.fileNameHeaderText","File name");
//The up folder button
UIManager.put("FileChooser.upFolderToolTipText", "Up one level");
//The desktop button tool tip
UIManager.put("FileChooser.homeFolderToolTipText","Desktop");
//The new folder button tool tip
UIManager.put("FileChooser.newFolderToolTipText","Create new folder");
//The list button tool tip
UIManager.put("FileChooser.listViewButtonToolTipText","List");
//The new folder button
UIManager.put("FileChooser.newFolderButtonText","Create new folder");
//The rename file button
UIManager.put("FileChooser.renameFileButtonText", "Rename file");
//The delete file button
UIManager.put("FileChooser.deleteFileButtonText", "Delete file");
//The details view button tool tip
UIManager.put("FileChooser.detailsViewButtonToolTipText", "Details");
//The size header
UIManager.put("FileChooser.fileSizeHeaderText","Size : ");
//The date header
UIManager.put("FileChooser.fileDateHeaderText", "Date modified : ");
/*
//Set icons for the JFileChooser
UIManager.put("FileView.directoryIcon", new ImageIcon(MyFileView.class.getResource("img/folder.png")));
UIManager.put("FileChooser.homeFolderIcon", new ImageIcon(MyFileView.class.getResource("img/user-home.png")));
UIManager.put("FileView.computerIcon", new ImageIcon(MyFileView.class.getResource("img/computer.png")));
UIManager.put("FIleView.floppyDriveIcon", new ImageIcon(MyFileView.class.getResource("img/media-floppy.png")));
UIManager.put("FileView.hardDriveIcon", new ImageIcon(MyFileView.class.getResource("img/drive-harddisk.png")));
UIManager.put("FileView.fileIcon", new ImageIcon(MyFileView.class.getResource("img/file.png")));
UIManager.put("FileChooser.upFolderIcon", new ImageIcon(MyFileView.class.getResource("img/go.png")));
UIManager.put("FileChooser.newFolderIcon", new ImageIcon(MyFileView.class.getResource("img/folder-new.png")));
UIManager.put("FileView.fileIcon", new ImageIcon(MyFileView.class.getResource("img/file.png")));
UIManager.put("FileChooser.listViewIcon", new ImageIcon(MyFileView.class.getResource("img/listIcon.png")));
UIManager.put("FileChooser.detailsViewIcon", new ImageIcon(MyFileView.class.getResource("img/details.png")));
*/
//Update UI
SwingUtilities.updateComponentTreeUI(this);
// Window explorer
this.setCurrentDirectory(new File("/"));
this.changeToParentDirectory();
//Add the filter to the JFileChooser
this.description = "Excel File (.xls, .xlsx)";
this.xls = "xls";
this.xlsx = "xlsx";
this.filterExcel = new FileNameExtensionFilter(this.description,
this.xls, this.xlsx);
this.addChoosableFileFilter(filterExcel);
}
/**
* <p>Choose a file</p>
*
* @return the file
*/
public File fileChooser()
{
Preferences prefs = Preferences.userNodeForPackage(FileChooser.class);
FileChooser.lastPath = prefs.get("LastPath", System.getProperty("user.dir"));
this.setCurrentDirectory(new File(lastPath));
if (this.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
final double ratio = 0.009900990099009901;
ZipSecureFile.setMinInflateRatio(ratio);
File file = this.getSelectedFile();
file = this.getCurrentDirectory();
prefs.put("LastPath", file.getAbsolutePath());
file.setWritable(true);
return file;
}
return null;
}
//============================================================
//getter and setter
/**
* <p>This is the getter for the last path</p>
*
* @see FileChooser#lastPath
* @return
*/
public static String getLastPath() {
return FileChooser.lastPath;
}
} |
Partager