1 23 package org.enhydra.multiServer.launch; 24 25 import java.awt.Dialog ; 27 import java.awt.Frame ; 28 import java.awt.Window ; 29 import java.awt.Component ; 30 import java.awt.Dimension ; 31 import java.awt.Point ; 32 import java.awt.Toolkit ; 33 import java.io.File ; 34 import javax.swing.JFileChooser ; 35 import javax.swing.UIManager ; 36 37 46 public class SwingUtil { 47 48 66 public static File getDirectoryChoice(Component owner, String defaultDir, 67 String title) { 68 return getDirectoryChoice(owner, new File (defaultDir), title); 69 } 70 71 88 public static File getDirectoryChoice(Component owner, File defaultDir, 89 String title) { 90 File choice = null; 91 JFileChooser chooser = new JFileChooser (); 92 93 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 94 chooser.setFileFilter((javax.swing.filechooser.FileFilter ) new DirectoryFilter()); 95 if ((defaultDir != null) && defaultDir.exists() 96 && defaultDir.isDirectory()) { 97 chooser.setCurrentDirectory(defaultDir); 98 chooser.setSelectedFile(defaultDir); 99 } 100 chooser.setDialogTitle(title); 101 chooser.setApproveButtonText("OK"); 102 int v = chooser.showOpenDialog(owner); 103 104 owner.requestFocus(); 105 if (v == JFileChooser.APPROVE_OPTION 106 && chooser.getSelectedFile() != null) { 107 if (chooser.getSelectedFile().exists()) { 108 choice = chooser.getSelectedFile(); 109 } else { 110 File parentFile = 111 new File (chooser.getSelectedFile().getParent()); 112 113 choice = parentFile; 114 } 115 } 116 chooser.removeAll(); 117 chooser = null; 118 return choice; 119 } 120 121 public static File getFileChoice(Component owner, File defaultSelection, 122 ExtensionFilter filter, String title) { 123 File choice = null; 124 JFileChooser chooser; 125 126 chooser = new JFileChooser (); 127 if (defaultSelection.isDirectory()) { 128 chooser.setCurrentDirectory(defaultSelection); 129 } else { 130 chooser.setSelectedFile(defaultSelection); 131 } 132 chooser.setFileFilter(filter); 133 chooser.setDialogTitle(title); 134 chooser.setApproveButtonText("OK"); 135 int v = chooser.showOpenDialog(owner); 136 137 owner.requestFocus(); 138 if (v == JFileChooser.APPROVE_OPTION 139 && chooser.getSelectedFile() != null) { 140 choice = chooser.getSelectedFile(); 141 } 142 chooser.removeAll(); 143 chooser = null; 144 return choice; 145 } 146 147 161 public static Point getCenteringPoint(Dimension size) { 162 Point centeringPoint = new Point (); 163 Dimension screenSize; 164 165 screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 166 if (size.height > screenSize.height) { 167 size.height = screenSize.height; 168 } 169 if (size.width > screenSize.width) { 170 size.width = screenSize.width; 171 } 172 centeringPoint.x = (screenSize.width - size.width) / 2; 173 centeringPoint.y = (screenSize.height - size.height) / 2; 174 return centeringPoint; 175 } 176 177 183 public static void setLookAndFeelToSystem() { 184 try { 185 String systemLook = UIManager.getSystemLookAndFeelClassName(); 186 187 UIManager.setLookAndFeel(systemLook); 188 } catch (javax.swing.UnsupportedLookAndFeelException e) {} 189 catch (IllegalAccessException e) {} 190 catch (InstantiationException e) {} 191 catch (ClassNotFoundException e) {} 192 ; 193 } 194 195 } 196 | Popular Tags |