1 2 25 package org.enhydra.tool.common; 26 27 import javax.swing.JFileChooser ; 29 import javax.swing.UIManager ; 30 import java.awt.Dialog ; 31 import java.awt.Frame ; 32 import java.awt.Window ; 33 import java.awt.Component ; 34 import java.awt.Dimension ; 35 import java.awt.Point ; 36 import java.awt.Toolkit ; 37 import java.io.File ; 38 import javax.swing.filechooser.FileFilter ; 39 import java.util.ResourceBundle ; 40 41 46 public class SwingUtil { 47 static ResourceBundle res = 48 ResourceBundle.getBundle("org.enhydra.tool.common.Res"); 50 53 private SwingUtil() {} 54 55 72 public static File getDirectoryChoice(Component owner, String defaultDir, 73 String title) { 74 return getDirectoryChoice(owner, new File (defaultDir), title); 75 } 76 77 94 public static File getDirectoryChoice(Component owner, File defaultDir, 95 String title) { 96 SecurityManager sm = null; 104 JFileChooser chooser = null; 105 File choice = null; 106 107 sm = System.getSecurityManager(); 108 System.setSecurityManager(null); 109 chooser = new JFileChooser (); 110 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 111 chooser.setFileFilter((javax.swing.filechooser.FileFilter ) new DirectoryFilter()); 112 if ((defaultDir != null) && defaultDir.exists() 113 && defaultDir.isDirectory()) { 114 chooser.setCurrentDirectory(defaultDir); 115 chooser.setSelectedFile(defaultDir); 116 } 117 chooser.setDialogTitle(title); 118 chooser.setApproveButtonText(res.getString("OK")); 119 int v = chooser.showOpenDialog(owner); 120 121 owner.requestFocus(); 122 switch (v) { 123 case JFileChooser.APPROVE_OPTION: 124 if (chooser.getSelectedFile() != null) { 125 if (chooser.getSelectedFile().exists()) { 126 choice = chooser.getSelectedFile(); 127 } else { 128 File parentFile = 129 new File (chooser.getSelectedFile().getParent()); 130 131 choice = parentFile; 132 } 133 } 134 break; 135 case JFileChooser.CANCEL_OPTION: 136 case JFileChooser.ERROR_OPTION: 137 } 138 chooser.removeAll(); 139 chooser = null; 140 System.setSecurityManager(sm); 141 return choice; 142 } 143 144 159 public static File getFileChoice(Component owner, 160 String defaultSelection, 161 FileFilter filter, String title) { 162 return SwingUtil.getFileChoice(owner, new File (defaultSelection), 163 filter, title); 164 } 165 166 181 public static File getFileChoice(Component owner, File defaultSelection, 182 FileFilter filter, String title) { 183 SecurityManager sm = null; 191 File choice = null; 192 JFileChooser chooser = null; 193 194 sm = System.getSecurityManager(); 195 System.setSecurityManager(null); 196 197 chooser = new JFileChooser (); 198 if (defaultSelection.isDirectory()) { 199 chooser.setCurrentDirectory(defaultSelection); 200 } else { 201 chooser.setSelectedFile(defaultSelection); 202 } 203 chooser.setFileFilter(filter); 204 chooser.setDialogTitle(title); 205 chooser.setApproveButtonText(res.getString("OK")); 206 int v = chooser.showOpenDialog(owner); 207 208 owner.requestFocus(); 209 switch (v) { 210 case JFileChooser.APPROVE_OPTION: 211 if (chooser.getSelectedFile() != null) { 212 choice = chooser.getSelectedFile(); 213 } 214 break; 215 case JFileChooser.CANCEL_OPTION: 216 case JFileChooser.ERROR_OPTION: 217 } 218 chooser.removeAll(); 219 chooser = null; 220 System.setSecurityManager(sm); 221 return choice; 222 } 223 224 238 public static Point getCenteringPoint(Dimension size) { 239 Point centeringPoint = new Point (); 240 Dimension screenSize; 241 242 screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 243 if (size.height > screenSize.height) { 244 size.height = screenSize.height; 245 } 246 if (size.width > screenSize.width) { 247 size.width = screenSize.width; 248 } 249 centeringPoint.x = (screenSize.width - size.width) / 2; 250 centeringPoint.y = (screenSize.height - size.height) / 2; 251 return centeringPoint; 252 } 253 254 260 public static void setLookAndFeelToSystem() { 261 try { 262 String systemLook = UIManager.getSystemLookAndFeelClassName(); 263 264 UIManager.setLookAndFeel(systemLook); 265 } catch (javax.swing.UnsupportedLookAndFeelException e) {} 266 catch (IllegalAccessException e) {} 267 catch (InstantiationException e) {} 268 catch (ClassNotFoundException e) {} 269 ; 270 } 271 272 } 273 | Popular Tags |