KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > wizard > Utilities


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
20 package org.netbeans.modules.websvc.wsdl.wizard;
21
22 import java.awt.Component JavaDoc;
23 import java.io.*;
24 import java.util.ArrayList JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JFileChooser JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.filechooser.FileFilter JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.modules.j2ee.common.Util;
32 import org.openide.DialogDescriptor;
33 import org.openide.WizardDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.NotifyDescriptor;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.loaders.DataObject;
39 import org.openide.nodes.Node;
40 import org.openide.util.NbBundle;
41 /**
42  *
43  * @author mkuchtiak
44  */

45 public class Utilities {
46     
47     public static String JavaDoc[] createSteps(String JavaDoc[] before, WizardDescriptor.Panel[] panels) {
48         //assert panels != null;
49
// hack to use the steps set before this panel processed
50
int diff = 0;
51         if (before == null) {
52             before = new String JavaDoc[0];
53         } else if (before.length > 0) {
54             diff = ("...".equals(before[before.length - 1])) ? 1 : 0; // NOI18N
55
}
56         String JavaDoc[] res = new String JavaDoc[ (before.length - diff) + panels.length];
57         for (int i = 0; i < res.length; i++) {
58             if (i < (before.length - diff)) {
59                 res[i] = before[i];
60             } else {
61                 res[i] = panels[i - before.length + diff].getComponent().getName();
62             }
63         }
64         return res;
65     }
66     
67     public static void replaceInDocument(javax.swing.text.Document JavaDoc document, String JavaDoc replaceFrom, String JavaDoc replaceTo) {
68         javax.swing.text.AbstractDocument JavaDoc doc = (javax.swing.text.AbstractDocument JavaDoc)document;
69         int len = replaceFrom.length();
70         try {
71             String JavaDoc content = doc.getText(0,doc.getLength());
72             int index = content.lastIndexOf(replaceFrom);
73             while (index>=0) {
74                 doc.replace(index,len,replaceTo,null);
75                 content=content.substring(0,index);
76                 index = content.lastIndexOf(replaceFrom);
77             }
78         } catch (javax.swing.text.BadLocationException JavaDoc ex){}
79     }
80     
81     // last selected directory
82
private static File lastDirectory;
83     
84     /**
85      * Allows the user to select multiple files
86      * @param extensions takes a list of file extensions
87      * @param dialogTitle dialog title
88      * @param maskTitle title for filter mask
89      * @return array of selected files
90      */

91     public static File[] selectFiles(final String JavaDoc extensions, String JavaDoc dialogTitle, final String JavaDoc maskTitle,
92             Project project){
93         ArrayList JavaDoc<File> fileList = new ArrayList JavaDoc<File>();
94         SelectSchemaPanel explorerPanel = new SelectSchemaPanel(project);
95         DialogDescriptor descriptor = new DialogDescriptor(explorerPanel,
96                 NbBundle.getMessage(Utilities.class,"TTL_SelectSchemas")); //NOI18N
97
/*
98         JFileChooser chooser = new JFileChooser();
99         chooser.setMultiSelectionEnabled(true);
100          
101         chooser.setFileFilter(new FileFilter() {
102             public boolean accept(File f) {
103                 if (f.isDirectory()) return true;
104                 java.util.StringTokenizer token = new java.util.StringTokenizer(extensions, " "); // NOI18N
105                 while (token.hasMoreElements()) {
106                     if (f.getName().endsWith(token.nextToken())) return true;
107                 }
108                 return false;
109             }
110             public String getDescription() {
111                 return maskTitle; // NOI18N
112             }
113         });
114          
115         if (lastDirectory != null) {
116             chooser.setCurrentDirectory(lastDirectory);
117         }
118          
119         chooser.setDialogTitle(dialogTitle);
120         if (chooser.showDialog(org.openide.windows.WindowManager.getDefault().getMainWindow(),
121                 NbBundle.getMessage(Utilities.class,"PROP_select_button"))
122                 == JFileChooser.APPROVE_OPTION) {
123          
124             File[] files = chooser.getSelectedFiles();
125             lastDirectory = chooser.getCurrentDirectory();
126             for(int i = 0; i < files.length; i++){
127                 File f = files[i];
128                 if (f != null && f.isFile()) {
129                     java.util.StringTokenizer token = new java.util.StringTokenizer(extensions, " "); // NOI18N
130                     boolean validFile = false;
131                     while (token.hasMoreElements()) {
132                         if (f.getName().endsWith(token.nextToken())){
133                             fileList.add(f);
134                             validFile = true;
135                         }
136                     }
137                     if(!validFile){
138                         DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
139                                 NbBundle.getMessage(Utilities.class,"MSG_inValidFile", f.getName()), NotifyDescriptor.WARNING_MESSAGE));
140                     }
141                 }
142             }
143         }
144          */

145         if(DialogDisplayer.getDefault().notify(descriptor).equals(NotifyDescriptor.OK_OPTION)) {
146             boolean accepted = true;
147             String JavaDoc errMsg = null;
148             Node[] selectedNodes = explorerPanel.getSelectedNodes();
149             for(int i = 0; i < selectedNodes.length; i++){
150                 Node node = selectedNodes[i];
151                 DataObject dobj = (DataObject)node.getCookie(DataObject.class);
152                 if(dobj != null){
153                     FileObject fo = dobj.getPrimaryFile();
154                     if(fo != null){
155                         File file = FileUtil.toFile(fo);
156                         if (file != null && file.isFile()) {
157                             java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(extensions, " "); // NOI18N
158
boolean validFile = false;
159                             while (token.hasMoreElements()) {
160                                 if (file.getName().endsWith(token.nextToken())){
161                                     fileList.add(file);
162                                     validFile = true;
163                                 }
164                             }
165                             if(!validFile){
166                                 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
167                                         NbBundle.getMessage(Utilities.class,"MSG_inValidFile", file.getName()), NotifyDescriptor.WARNING_MESSAGE));
168                             }
169                         }
170                     }
171                 }
172             }
173             if (!accepted) {
174                 NotifyDescriptor.Message notifyDescr =
175                         new NotifyDescriptor.Message(errMsg,
176                         NotifyDescriptor.ERROR_MESSAGE );
177                 DialogDisplayer.getDefault().notify(notifyDescr);
178                 descriptor.setClosingOptions(closingOptionsWithoutOK);
179             } else {
180                 // Everything was fine so allow OK
181
descriptor.setClosingOptions(closingOptionsWithOK);
182             }
183         }
184         File[] selectedFiles = new File[fileList.size()];
185         return fileList.toArray(selectedFiles);
186     }
187     
188     private static Object JavaDoc[] closingOptionsWithoutOK = {DialogDescriptor.CANCEL_OPTION,
189     DialogDescriptor.CLOSED_OPTION};
190     private static Object JavaDoc[] closingOptionsWithOK = {DialogDescriptor.CANCEL_OPTION,
191     DialogDescriptor.CLOSED_OPTION, DialogDescriptor.OK_OPTION};
192     
193     /**
194      * Prompts user for a file.
195      * @param extensions takes a list of file extensions
196      * @param dialogTitle dialog title
197      * @param maskTitle title for filter mask
198      * @return filename or null if operation was cancelled
199      */

200     public static File selectFile(final String JavaDoc extensions, String JavaDoc dialogTitle, final String JavaDoc maskTitle) {
201         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
202         
203         chooser.setFileFilter(new FileFilter JavaDoc() {
204             public boolean accept(File f) {
205                 if (f.isDirectory()) return true;
206                 java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(extensions, " "); // NOI18N
207
while (token.hasMoreElements()) {
208                     if (f.getName().endsWith(token.nextToken())) return true;
209                 }
210                 return false;
211             }
212             public String JavaDoc getDescription() {
213                 return maskTitle; // NOI18N
214
}
215         });
216         
217         if (lastDirectory != null) {
218             chooser.setCurrentDirectory(lastDirectory);
219         }
220         
221         chooser.setDialogTitle(dialogTitle);
222         while (chooser.showDialog(org.openide.windows.WindowManager.getDefault().getMainWindow(),
223                 NbBundle.getMessage(Utilities.class,"PROP_select_button"))
224                 == JFileChooser.APPROVE_OPTION) {
225             File f = chooser.getSelectedFile();
226             lastDirectory = chooser.getCurrentDirectory();
227             if (f != null && f.isFile()) {
228                 java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(extensions, " "); // NOI18N
229
while (token.hasMoreElements()) {
230                     if (f.getName().endsWith(token.nextToken())) return f;
231                 }
232             }
233             
234             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
235                     NbBundle.getMessage(Utilities.class,"MSG_inValidFile"), NotifyDescriptor.WARNING_MESSAGE));
236         }
237         return null;
238     }
239     
240     /*
241      * Changes the text of a JLabel in component from oldLabel to newLabel
242      */

243     public static JTextComponent JavaDoc findTextFieldForLabel(JComponent JavaDoc component, String JavaDoc text) {
244         JLabel JavaDoc label = Util.findLabel(component, text);
245         if(label != null) {
246             Component JavaDoc comp = label.getLabelFor();
247             if (comp!=null && (comp instanceof JTextComponent JavaDoc)) return (JTextComponent JavaDoc)comp;
248         }
249         return null;
250     }
251 }
252
Popular Tags