KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalog > lib > Util


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.catalog.lib;
20
21 import java.beans.*;
22 import java.io.File JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import javax.swing.JFileChooser JavaDoc;
26 import javax.swing.filechooser.FileFilter JavaDoc;
27
28 import org.openide.NotifyDescriptor;
29 import org.openide.DialogDisplayer;
30 import org.openide.windows.WindowManager;
31 import org.openide.util.NbBundle;
32
33 import org.netbeans.modules.xml.core.lib.AbstractUtil;
34
35 /**
36  * Utility methods.
37  *
38  * @author Petr Kuzel
39  * @author Libor Kramolis
40  * @version 0.2
41  */

42 public class Util extends AbstractUtil {
43
44
45     /** Default and only one instance of this class. */
46     public static final Util THIS = new Util();
47
48     /** Nobody can create instance of it, just me. */
49     private Util () {
50     }
51
52          
53
54     /**
55      * Should be rewritten for fallback Properties customizer
56      * @return customizer of given Class
57      */

58     public static Customizer getProviderCustomizer(Class JavaDoc clazz) {
59         try {
60             Class JavaDoc customizer =
61                 Introspector.getBeanInfo(clazz).getBeanDescriptor().getCustomizerClass();
62             
63             return (Customizer) customizer.newInstance();
64             
65         } catch (InstantiationException JavaDoc ex) {
66             return null;
67         } catch (IntrospectionException ex) {
68             return null;
69         } catch (IllegalAccessException JavaDoc ex) {
70             return null;
71         }
72     }
73     
74
75     /**
76      * Create new instance of given provider.
77      */

78     public static Object JavaDoc createProvider(Class JavaDoc clazz) {
79         try {
80             return clazz.newInstance();
81         } catch (InstantiationException JavaDoc ex) {
82             return null;
83         } catch (IllegalAccessException JavaDoc ex) {
84             return null;
85         }
86     }
87
88     // last catalog directory
89
private static File JavaDoc lastDirectory;
90     
91     /**
92      * Prompts user for a catalog file.
93      * @param extensions takes a list of file extensions
94      * @return filename or null if operation was cancelled.
95      */

96     public static File JavaDoc selectCatalogFile(final String JavaDoc extensions) {
97         return selectFile(extensions, Util.THIS.getString("TITLE_select_catalog"), Util.THIS.getString("PROP_catalog_mask"));
98     }
99     
100     /**
101      * Prompts user for a file.
102      * @param extensions takes a list of file extensions
103      * @param dialogTitle dialog title
104      * @param maskTitle title for filter mask
105      * @return filename or null if operation was cancelled
106      */

107     public static File JavaDoc selectFile(final String JavaDoc extensions, String JavaDoc dialogTitle, final String JavaDoc maskTitle) {
108         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
109
110         chooser.setFileFilter(new FileFilter JavaDoc() {
111             public boolean accept(File JavaDoc f) {
112                 if (f.isDirectory()) return true;
113                 StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(extensions, " "); // NOI18N
114
while (token.hasMoreElements()) {
115                     if (f.getName().endsWith(token.nextToken())) return true;
116                 }
117                 return false;
118             }
119             public String JavaDoc getDescription() {
120                 return maskTitle; // NOI18N
121
}
122         });
123
124         if (lastDirectory != null) {
125             chooser.setCurrentDirectory(lastDirectory);
126         }
127
128         chooser.setDialogTitle(dialogTitle);
129         while (chooser.showDialog(WindowManager.getDefault().getMainWindow(),
130                                Util.THIS.getString("PROP_select_button"))
131                == JFileChooser.APPROVE_OPTION)
132         {
133             File JavaDoc f = chooser.getSelectedFile();
134             lastDirectory = chooser.getCurrentDirectory();
135             if (f != null && f.isFile()) {
136                 StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(extensions, " "); // NOI18N
137
while (token.hasMoreElements()) {
138                     if (f.getName().endsWith(token.nextToken())) return f;
139                 }
140             }
141
142             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
143                 Util.THIS.getString("MSG_inValidFile"), NotifyDescriptor.WARNING_MESSAGE));
144         }
145         return null;
146     }
147     
148 }
149
Popular Tags