KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > wizard > 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.core.wizard;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.io.File JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.TreeSet JavaDoc;
27
28 import javax.swing.JFileChooser JavaDoc;
29 import javax.swing.filechooser.FileFilter JavaDoc;
30
31 import org.openide.NotifyDescriptor;
32 import org.openide.DialogDisplayer;
33 import org.openide.actions.ActionManager;
34 import org.openide.windows.WindowManager;
35
36 import org.netbeans.api.xml.services.UserCatalog;
37
38 import org.netbeans.modules.xml.core.lib.AbstractUtil;
39 import org.openide.loaders.DataObject;
40 import org.openide.nodes.Node;
41 import org.openide.util.Lookup;
42 import org.openide.util.actions.SystemAction;
43
44 /**
45  * Collection of static support methods.
46  *
47  * @author Petr Kuzel
48  */

49 class Util extends AbstractUtil {
50
51     // last catalog directory
52
private static File JavaDoc lastDirectory;
53     
54     /** Default and only one instance of this class. */
55     public static final Util THIS = new Util();
56
57     /** Nobody can create instance of it, just me. */
58     private Util () {
59     }
60     
61     /**
62      * Prompts user for a Schema file.
63      * @param extensions a space separated list of file extensions
64      * @return filename or null if operation was cancelled.
65      */

66     public static File JavaDoc selectSchemaFile(final String JavaDoc extensions) {
67         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
68
69         chooser.setFileFilter(new FileFilter JavaDoc() {
70             public boolean accept(File JavaDoc f) {
71                 if (f.isDirectory()) return true;
72                 StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(extensions, " "); // NOI18N
73
while (token.hasMoreElements()) {
74                     if (f.getName().endsWith(token.nextToken())) return true;
75                 }
76                 return false;
77             }
78             public String JavaDoc getDescription() {
79                 return Util.THIS.getString("PROP_schema_mask"); // NOI18N
80
}
81         });
82
83         if (lastDirectory != null) {
84             chooser.setCurrentDirectory(lastDirectory);
85         }
86
87         chooser.setDialogTitle(Util.THIS.getString("PROP_schema_dialog_name"));
88         while (chooser.showDialog(WindowManager.getDefault().getMainWindow(),
89                                Util.THIS.getString("PROP_schema_select_button"))
90                == JFileChooser.APPROVE_OPTION)
91         {
92             File JavaDoc f = chooser.getSelectedFile();
93             lastDirectory = chooser.getCurrentDirectory();
94             if (f != null && f.isFile()) {
95                 StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(extensions, " "); // NOI18N
96
while (token.hasMoreElements()) {
97                     if (f.getName().endsWith(token.nextToken())) return f;
98                 }
99                      }
100
101             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
102                 Util.THIS.getString("MSG_inValidFile"), NotifyDescriptor.WARNING_MESSAGE));
103         }
104         return null;
105     }
106     
107     /**
108      * Obtain all known DTD public IDs.
109      */

110     public static String JavaDoc[] getKnownDTDPublicIDs() {
111         UserCatalog catalog = UserCatalog.getDefault();
112         if (catalog != null) {
113             Set JavaDoc idSet = new TreeSet JavaDoc();
114             for (Iterator JavaDoc it = catalog.getPublicIDs(); it.hasNext(); ) {
115                 String JavaDoc next = (String JavaDoc) it.next();
116                 // exclude schema publicIDs
117
String JavaDoc nextLowerCase = next.toLowerCase();
118                 if (!nextLowerCase.startsWith("schema:") && !nextLowerCase.endsWith(".xsd")) { // NOI18N
119
idSet.add(next);
120                 }
121             }
122             return (String JavaDoc[]) idSet.toArray(new String JavaDoc[idSet.size()]);
123         } else {
124             Util.THIS.debug("Note SourceResolver not found!"); // NOI18N
125
return new String JavaDoc[0];
126         }
127     }
128     
129     /**
130      * Perform default action on specified data object.
131      */

132     public static void performDefaultAction (DataObject dataObject) {
133
134         Node node = dataObject.getNodeDelegate();
135         SystemAction action = node.getDefaultAction();
136
137         if (action != null) {
138             ActionManager manager = (ActionManager) Lookup.getDefault().lookup(ActionManager.class);
139             manager.invokeAction(action, new ActionEvent JavaDoc (node, ActionEvent.ACTION_PERFORMED, "")); // NOI18N
140
}
141     }
142     
143 }
144
Popular Tags