1 18 19 package org.apache.jmeter.gui.util; 20 21 import java.io.File ; 22 23 import javax.swing.JFileChooser ; 24 import javax.swing.filechooser.FileFilter ; 25 26 import org.apache.jmeter.gui.GuiPackage; 27 import org.apache.jmeter.gui.JMeterFileFilter; 28 import org.apache.jmeter.util.JMeterUtils; 29 30 34 public final class FileDialoger 35 { 36 39 public static String lastJFCDirectory = null; public static JFileChooser jfc = new JFileChooser (); 42 45 private FileDialoger() 46 { 47 } 48 49 60 public static JFileChooser promptToOpenFile(String [] exts) 61 { 62 64 if (lastJFCDirectory == null) 65 { 66 String start = JMeterUtils.getPropDefault("user.dir", ""); 67 68 if (!start.equals("")) 69 { 70 jfc.setCurrentDirectory(new File (start)); 71 } 72 } 73 clearFileFilters(); 74 jfc.addChoosableFileFilter(new JMeterFileFilter(exts)); 75 int retVal = 76 jfc.showOpenDialog(GuiPackage.getInstance().getMainFrame()); 77 lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath(); 78 79 if (retVal == JFileChooser.APPROVE_OPTION) 80 { 81 return jfc; 82 } 83 else 84 { 85 return null; 86 } 87 } 88 89 private static void clearFileFilters() 90 { 91 FileFilter [] filters = jfc.getChoosableFileFilters(); 92 for (int x = 0; x < filters.length; x++) 93 { 94 jfc.removeChoosableFileFilter(filters[x]); 95 } 96 } 97 98 public static JFileChooser promptToOpenFile() 99 { 100 return promptToOpenFile(new String [0]); 101 } 102 103 115 public static JFileChooser promptToSaveFile(String filename) 116 { 117 if (lastJFCDirectory == null) 118 { 119 String start = JMeterUtils.getPropDefault("user.dir", ""); 120 if (!start.equals("")) 121 { 122 jfc = new JFileChooser (new File (start)); 123 } 124 lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath(); 125 } 126 String ext = ".jmx"; 127 if (filename != null) 128 { 129 jfc.setSelectedFile(new File (lastJFCDirectory, filename)); 130 int i = -1; 131 if ((i = filename.indexOf(".")) > -1) 132 { 133 ext = filename.substring(i); 134 } 135 } 136 clearFileFilters(); 137 jfc.addChoosableFileFilter(new JMeterFileFilter(new String [] { ext })); 138 139 int retVal = 140 jfc.showSaveDialog(GuiPackage.getInstance().getMainFrame()); 141 lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath(); 142 if (retVal == JFileChooser.APPROVE_OPTION) 143 { 144 return jfc; 145 } 146 else 147 { 148 return null; 149 } 150 } 151 } 152 | Popular Tags |