1 42 package org.jfree.ui.action; 43 44 import java.awt.Component ; 45 import java.io.File ; 46 import javax.swing.JFileChooser ; 47 48 import org.jfree.ui.ExtensionFileFilter; 49 import org.jfree.util.StringUtils; 50 51 57 public abstract class AbstractFileSelectionAction extends AbstractActionDowngrade { 58 61 private JFileChooser fileChooser; 62 65 private Component parent; 66 67 73 public AbstractFileSelectionAction(final Component parent) { 74 this.parent = parent; 75 } 76 77 82 protected abstract String getFileExtension(); 83 84 89 protected abstract String getFileDescription(); 90 91 97 protected File getCurrentDirectory() { 98 return new File ("."); 99 } 100 101 112 protected File performSelectFile(final File selectedFile, 113 final int dialogType, 114 final boolean appendExtension) { 115 if (this.fileChooser == null) { 116 this.fileChooser = createFileChooser(); 117 } 118 119 this.fileChooser.setSelectedFile(selectedFile); 120 this.fileChooser.setDialogType(dialogType); 121 final int option = this.fileChooser.showDialog(this.parent, null); 122 if (option == JFileChooser.APPROVE_OPTION) { 123 final File selFile = this.fileChooser.getSelectedFile(); 124 String selFileName = selFile.getAbsolutePath(); 125 if (StringUtils.endsWithIgnoreCase(selFileName, getFileExtension()) == false) { 126 selFileName = selFileName + getFileExtension(); 127 } 128 return new File (selFileName); 129 } 130 return null; 131 } 132 133 138 protected JFileChooser createFileChooser() { 139 final JFileChooser fc = new JFileChooser (); 140 fc.addChoosableFileFilter( 141 new ExtensionFileFilter(getFileDescription(), getFileExtension()) 142 ); 143 fc.setMultiSelectionEnabled(false); 144 fc.setCurrentDirectory(getCurrentDirectory()); 145 return fc; 146 } 147 148 } 149 | Popular Tags |