KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > multiServer > launch > SwingUtil


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

23 package org.enhydra.multiServer.launch;
24
25 // Standard imports
26
import java.awt.Dialog JavaDoc;
27 import java.awt.Frame JavaDoc;
28 import java.awt.Window JavaDoc;
29 import java.awt.Component JavaDoc;
30 import java.awt.Dimension JavaDoc;
31 import java.awt.Point JavaDoc;
32 import java.awt.Toolkit JavaDoc;
33 import java.io.File JavaDoc;
34 import javax.swing.JFileChooser JavaDoc;
35 import javax.swing.UIManager JavaDoc;
36
37 /**
38  * SwingUtil contains static utility methods for working with Swing
39  * classes.
40  *
41  * @author
42  * Paul Mahar
43  *
44  * @version %I%, %G%
45  */

46 public class SwingUtil {
47
48     /**
49      * Open a JFileChooser dialog for selecting a directory and return the
50      * selected directory.
51      *
52      *
53      * @param owner
54      * The frame or dialog that controls the invokation of this dialog.
55      * @param defaultDir
56      * A string representation of the directory to show when the
57      * dialog opens.
58      * @param title
59      * Tile for the dialog.
60      *
61      * @return
62      * The selected directory as a File. Null if user cancels dialog without
63      * a selection.
64      *
65      */

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

88     public static File JavaDoc getDirectoryChoice(Component JavaDoc owner, File JavaDoc defaultDir,
89                                           String JavaDoc title) {
90         File JavaDoc choice = null;
91         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
92
93         chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
94         chooser.setFileFilter((javax.swing.filechooser.FileFilter JavaDoc) 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 JavaDoc parentFile =
111                     new File JavaDoc(chooser.getSelectedFile().getParent());
112
113                 choice = parentFile;
114             }
115         }
116         chooser.removeAll();
117         chooser = null;
118         return choice;
119     }
120
121     public static File JavaDoc getFileChoice(Component JavaDoc owner, File JavaDoc defaultSelection,
122                                      ExtensionFilter filter, String JavaDoc title) {
123         File JavaDoc choice = null;
124         JFileChooser JavaDoc chooser;
125
126         chooser = new JFileChooser JavaDoc();
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     /**
148      * Get the point on point on the screen at which to open a dialog
149      * or window for it to appear centered. This point is the top right hand
150      * corner of the container you want to position.
151      *
152      *
153      * @param size
154      * The demensions of the dialog or window to position.
155      *
156      * @return
157      * The top left hand point at which to position the container
158      * for it to appear centered.
159      *
160      */

161     public static Point JavaDoc getCenteringPoint(Dimension JavaDoc size) {
162         Point JavaDoc centeringPoint = new Point JavaDoc();
163         Dimension JavaDoc 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     /**
178      * Sets the look and feel to the native operating system look and feel.
179      * This method ignores any exceptions that may occur when you set
180      * the look and feel.
181      *
182      */

183     public static void setLookAndFeelToSystem() {
184         try {
185             String JavaDoc systemLook = UIManager.getSystemLookAndFeelClassName();
186
187             UIManager.setLookAndFeel(systemLook);
188         } catch (javax.swing.UnsupportedLookAndFeelException JavaDoc e) {}
189         catch (IllegalAccessException JavaDoc e) {}
190         catch (InstantiationException JavaDoc e) {}
191         catch (ClassNotFoundException JavaDoc e) {}
192         ;
193     }
194
195 }
196
Popular Tags