KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > SwingUtil


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the gEnhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  * Thomas Wessell
23  *
24  */

25 package org.enhydra.tool.common;
26
27 //
28
import javax.swing.JFileChooser JavaDoc;
29 import javax.swing.UIManager JavaDoc;
30 import java.awt.Dialog JavaDoc;
31 import java.awt.Frame JavaDoc;
32 import java.awt.Window JavaDoc;
33 import java.awt.Component JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.Point JavaDoc;
36 import java.awt.Toolkit JavaDoc;
37 import java.io.File JavaDoc;
38 import javax.swing.filechooser.FileFilter JavaDoc;
39 import java.util.ResourceBundle JavaDoc;
40
41 /**
42  * SwingUtil contains static utility methods for working with Swing
43  * classes.
44  *
45  */

46 public class SwingUtil {
47     static ResourceBundle JavaDoc res =
48         ResourceBundle.getBundle("org.enhydra.tool.common.Res"); // nores
49

50     /**
51      * Hidden default constructor
52      */

53     private SwingUtil() {}
54
55     /**
56      * Open a JFileChooser dialog for selecting a directory and return the
57      * selected directory.
58      *
59      * @param owner
60      * The frame or dialog that controls the invokation of this dialog.
61      * @param defaultDir
62      * A string representation of the directory to show when the
63      * dialog opens.
64      * @param title
65      * Tile for the dialog.
66      *
67      * @return
68      * The selected directory as a File. Null if user cancels dialog without
69      * a selection.
70      *
71      */

72     public static File JavaDoc getDirectoryChoice(Component JavaDoc owner, String JavaDoc defaultDir,
73                                           String JavaDoc title) {
74         return getDirectoryChoice(owner, new File JavaDoc(defaultDir), title);
75     }
76
77     /**
78      * Open a JFileChooser dialog for selecting a directory and return the
79      * selected directory.
80      *
81      *
82      * @param owner
83      * The frame or dialog that controls the invokation of this dialog.
84      * @param defaultDir
85      * The directory to show when the dialog opens.
86      * @param title
87      * Tile for the dialog.
88      *
89      * @return
90      * The selected directory as a File. Null if user cancels dialog without
91      * a selection.
92      *
93      */

94     public static File JavaDoc getDirectoryChoice(Component JavaDoc owner, File JavaDoc defaultDir,
95                                           String JavaDoc title) {
96         //
97
// There is apparently a bug in the native Windows FileSystem class that
98
// occurs when you use a file chooser and there is a security manager
99
// active. An error dialog is displayed indicating there is no disk in
100
// Drive A:. To avoid this, the security manager is temporarily set to
101
// null and then reset after the file chooser is closed.
102
//
103
SecurityManager JavaDoc sm = null;
104         JFileChooser JavaDoc chooser = null;
105         File JavaDoc choice = null;
106
107         sm = System.getSecurityManager();
108         System.setSecurityManager(null);
109         chooser = new JFileChooser JavaDoc();
110         chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
111         chooser.setFileFilter((javax.swing.filechooser.FileFilter JavaDoc) 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 JavaDoc parentFile =
129                         new File JavaDoc(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     /**
145      * Get a file selection using the FileChooser dialog.
146      *
147      * @param owner
148      * The parent of this modal dialog.
149      * @param defaultSelection
150      * The default file selection as a string.
151      * @param filter
152      * An extension filter
153      * @param title
154      * The caption for the dialog.
155      *
156      * @return
157      * A selected file or null if no selection is made.
158      */

159     public static File JavaDoc getFileChoice(Component JavaDoc owner,
160                                      String JavaDoc defaultSelection,
161                                      FileFilter JavaDoc filter, String JavaDoc title) {
162         return SwingUtil.getFileChoice(owner, new File JavaDoc(defaultSelection),
163                                        filter, title);
164     }
165
166     /**
167      * Get a file selection using the FileChooser dialog.
168      *
169      * @param owner
170      * The parent of this modal dialog.
171      * @param defaultSelection
172      * The default file selection as a file.
173      * @param filter
174      * An extension filter
175      * @param title
176      * The caption for the dialog.
177      *
178      * @return
179      * A selected file or null if no selection is made.
180      */

181     public static File JavaDoc getFileChoice(Component JavaDoc owner, File JavaDoc defaultSelection,
182                                      FileFilter JavaDoc filter, String JavaDoc title) {
183         //
184
// There is apparently a bug in the native Windows FileSystem class that
185
// occurs when you use a file chooser and there is a security manager
186
// active. An error dialog is displayed indicating there is no disk in
187
// Drive A:. To avoid this, the security manager is temporarily set to
188
// null and then reset after the file chooser is closed.
189
//
190
SecurityManager JavaDoc sm = null;
191         File JavaDoc choice = null;
192         JFileChooser JavaDoc chooser = null;
193
194         sm = System.getSecurityManager();
195         System.setSecurityManager(null);
196
197         chooser = new JFileChooser JavaDoc();
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     /**
225      * Get the point on point on the screen at which to open a dialog
226      * or window for it to appear centered. This point is the top right hand
227      * corner of the container you want to position.
228      *
229      *
230      * @param size
231      * The demensions of the dialog or window to position.
232      *
233      * @return
234      * The top left hand point at which to position the container
235      * for it to appear centered.
236      *
237      */

238     public static Point JavaDoc getCenteringPoint(Dimension JavaDoc size) {
239         Point JavaDoc centeringPoint = new Point JavaDoc();
240         Dimension JavaDoc 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     /**
255      * Sets the look and feel to the native operating system look and feel.
256      * This method ignores any exceptions that may occur when you set
257      * the look and feel.
258      *
259      */

260     public static void setLookAndFeelToSystem() {
261         try {
262             String JavaDoc systemLook = UIManager.getSystemLookAndFeelClassName();
263
264             UIManager.setLookAndFeel(systemLook);
265         } catch (javax.swing.UnsupportedLookAndFeelException JavaDoc e) {}
266         catch (IllegalAccessException JavaDoc e) {}
267         catch (InstantiationException JavaDoc e) {}
268         catch (ClassNotFoundException JavaDoc e) {}
269         ;
270     }
271
272 }
273
Popular Tags