KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > ui > 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 /*
20  * Util.java
21  *
22  * Created on February 12, 2004, 10:52 AM
23  */

24 package org.netbeans.modules.j2ee.sun.ide.j2ee.ui;
25
26 import java.io.File JavaDoc;
27 import javax.swing.filechooser.FileFilter JavaDoc;
28 import javax.swing.text.PlainDocument JavaDoc;
29 import javax.swing.text.AttributeSet JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import java.awt.Toolkit JavaDoc;
32 import javax.swing.JFileChooser JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import org.netbeans.modules.j2ee.sun.api.Asenv;
35 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager;
36 import org.netbeans.modules.j2ee.sun.api.SunURIManager;
37 import org.openide.DialogDisplayer;
38 import org.openide.NotifyDescriptor;
39 import org.openide.WizardDescriptor;
40 import org.openide.awt.StatusDisplayer;
41 import org.openide.util.NbBundle;
42 import org.openide.util.RequestProcessor;
43
44 /**
45  *
46  * @author nityad
47  */

48 public class Util {
49     
50     /** Creates a new instance of Util */
51     public Util() {
52     }
53     
54     ///Numeric Document
55
public static NumericDocument getNumericDocument(){
56         return new NumericDocument();
57     }
58     public static class NumericDocument extends PlainDocument JavaDoc {
59         private Toolkit JavaDoc toolkit = Toolkit.getDefaultToolkit();
60         
61         public void insertString(int offs, String JavaDoc str, AttributeSet JavaDoc a)
62         throws BadLocationException JavaDoc {
63             char[] s = str.toCharArray();
64             char[] r = new char[s.length];
65             int j = 0;
66             for (int i = 0; i < r.length; i++) {
67                 if (Character.isDigit(s[i])) {
68                     r[j++] = s[i];
69                 } else {
70                     toolkit.beep();
71                 }
72             }
73             super.insertString(offs, new String JavaDoc(r, 0, j), a);
74         }
75     } // class NumericDocument
76

77     public static void showInformation(final String JavaDoc msg, final String JavaDoc title){
78         SwingUtilities.invokeLater(new Runnable JavaDoc() {
79             public void run() {
80                 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
81                 d.setTitle(title);
82                 DialogDisplayer.getDefault().notify(d);
83             }
84         });
85         
86     }
87     
88     public static void showInformationWhenHolding(final String JavaDoc msg, final String JavaDoc title){
89         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
90             public void run() {
91                 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
92                 d.setTitle(title);
93                 DialogDisplayer.getDefault().notify(d);
94             }
95         });
96         
97     }
98
99     public static void showInformation(final String JavaDoc msg){
100         SwingUtilities.invokeLater(new Runnable JavaDoc() {
101             public void run() {
102                 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
103                 DialogDisplayer.getDefault().notify(d);
104             }
105         });
106         
107     }
108     
109     //Fix bug# 5005127 - deletion of cp resource should inform user about dependent resources
110
public static Object JavaDoc showWarning(final String JavaDoc msg){
111         NotifyDescriptor d = new NotifyDescriptor.Confirmation(msg, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.WARNING_MESSAGE);
112         return DialogDisplayer.getDefault().notify(d);
113         
114         
115     }
116     
117     public static Object JavaDoc showWarning(final String JavaDoc msg, final String JavaDoc title){
118         NotifyDescriptor d = new NotifyDescriptor.Confirmation(msg, title, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.WARNING_MESSAGE);
119         return DialogDisplayer.getDefault().notify(d);
120         
121     }
122     
123     public static void showError(final String JavaDoc msg){
124         SwingUtilities.invokeLater(new Runnable JavaDoc() {
125             public void run() {
126                 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
127                 DialogDisplayer.getDefault().notify(d);
128             }
129         });
130         
131     }
132     
133     public static void showError(final String JavaDoc msg, final String JavaDoc title){
134         SwingUtilities.invokeLater(new Runnable JavaDoc() {
135             public void run() {
136                 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
137                 d.setTitle(title);
138                 DialogDisplayer.getDefault().notify(d);
139             }
140         });
141         
142     }
143     
144     public static void setStatusBar(final String JavaDoc msg){
145         SwingUtilities.invokeLater(new Runnable JavaDoc() {
146             public void run() {
147                 StatusDisplayer.getDefault().setStatusText(msg);
148             }
149         });
150         
151     }
152     
153     
154     
155     static File JavaDoc[] getRegisterableDefaultDomains(File JavaDoc location) {
156         File JavaDoc[] noneRegisterable = new File JavaDoc[0];
157         //File[] retVal = noneRegisterable;
158
String JavaDoc ext = (File.separatorChar == '/' ? "conf" : "bat"); // NOI18N
159
File JavaDoc asenv = new File JavaDoc(location,"config/asenv."+ext); // NOI18N
160
Asenv asenvContent = new Asenv(asenv);
161         String JavaDoc defDomainsDirName = asenvContent.get(Asenv.AS_DEF_DOMAINS_PATH);
162         File JavaDoc domainsDir = new File JavaDoc(defDomainsDirName);//NOI18N
163
// File domainsDir = new File(location,"domains");
164
if (!domainsDir.exists() && location.getAbsolutePath().startsWith("/opt/SUNWappserver")) {
165             domainsDir = new File JavaDoc("/var/opt/SUNWappserver/domains");
166         }
167         if (!domainsDir.exists())
168             return noneRegisterable;
169         
170         File JavaDoc[] possibles = domainsDir.listFiles(new java.io.FileFilter JavaDoc() {
171             public boolean accept(File JavaDoc pathname) {
172                 if (pathname.isDirectory() && pathname.canWrite())
173                     return true;
174                 return false;
175             }
176         });
177         if (null == possibles)
178             return noneRegisterable;
179
180         // prune out unusable entries...
181
int realCount = 0;
182         for (int i = 0; i < possibles.length; i++) {
183             if (rootOfUsableDomain(possibles[i])) {
184                 realCount++;
185             } else {
186                 possibles[i] = null;
187             }
188         }
189         File JavaDoc[] retVal = new File JavaDoc[realCount];
190         int nextSlot = 0;
191         for (int i = 0; i < possibles.length; i++) {
192             if (possibles[i] != null) {
193                 retVal[nextSlot] = possibles[i];
194                 nextSlot++;
195             }
196         }
197         return retVal;
198     }
199     
200     /**
201      */

202     public static boolean rootOfUsableDomain(File JavaDoc f) {
203         File JavaDoc testFile = new File JavaDoc(f,"logs");
204         if (!testFile.exists() || !testFile.isDirectory() || !testFile.canWrite())
205             return false;
206         testFile = new File JavaDoc(f,"config");
207         if (!testFile.exists() || !testFile.isDirectory() || !testFile.canWrite())
208             return false;
209         testFile = new File JavaDoc(testFile,"domain.xml");
210         if (!testFile.exists() || !testFile.canWrite())
211             return false;
212         return true;
213     }
214     
215     public static String JavaDoc getHostPort(File JavaDoc domainDir, File JavaDoc platformDir){
216         File JavaDoc xmlRoot;
217         if(File.pathSeparatorChar == ':')
218             xmlRoot = new File JavaDoc(domainDir.getAbsolutePath() + "/config/domain.xml");//NOI18N
219
else
220             xmlRoot = new File JavaDoc(domainDir.getAbsolutePath() + "\\config\\domain.xml");//NOI18N
221
String JavaDoc adminHostPort = null;
222         try{
223             Class JavaDoc[] argClass = new Class JavaDoc[2];
224             argClass[0] = File JavaDoc.class;
225             argClass[1] = File JavaDoc.class;
226             Object JavaDoc[] argObject = new Object JavaDoc[2];
227             argObject[0] = xmlRoot;
228             argObject[1] = platformDir;
229             
230             ClassLoader JavaDoc loader = ServerLocationManager.getServerOnlyClassLoader(platformDir);
231             if(loader != null){
232                 Class JavaDoc cc = loader.loadClass("org.netbeans.modules.j2ee.sun.bridge.AppServerBridge");
233                 java.lang.reflect.Method JavaDoc getHostPort = cc.getMethod("getHostPort", argClass);//NOI18N
234
adminHostPort = (String JavaDoc)getHostPort.invoke(null, argObject);
235             }
236         }catch(Exception JavaDoc ex){
237             //Suppressing exception while trying to obtain admin host port value
238
}
239         return adminHostPort;
240     }
241     
242     static String JavaDoc getDeploymentUri(File JavaDoc domainDir, File JavaDoc platformDir) {
243         return SunURIManager.SUNSERVERSURI+getHostPort(domainDir,platformDir);
244     }
245
246     static File JavaDoc domainFile(File JavaDoc domainDir) {
247         if (File.pathSeparatorChar == ';')
248             return new File JavaDoc(domainDir+"\\config\\domain.xml");
249         else
250             return new File JavaDoc(domainDir+"/config/domain.xml");
251     }
252     
253     static void fillDescriptorFromDomainXml(final WizardDescriptor wiz, final File JavaDoc domainDir) {
254         String JavaDoc hp = Util.getHostPort(domainDir,
255                 (File JavaDoc) wiz.getProperty(AddDomainWizardIterator.PLATFORM_LOCATION));
256         if (null != hp) {
257             wiz.putProperty(AddDomainWizardIterator.DOMAIN_FILE,
258                     Util.domainFile(domainDir));
259             int sepDex = hp.indexOf(':');
260             wiz.putProperty(AddDomainWizardIterator.HOST,hp.substring(0,sepDex));
261             wiz.putProperty(AddDomainWizardIterator.PORT,hp.substring(sepDex+1));
262             wiz.putProperty(AddDomainWizardIterator.DOMAIN,domainDir.getName());
263             wiz.putProperty(AddDomainWizardIterator.INSTALL_LOCATION,
264                     domainDir.getParentFile().getAbsolutePath());
265             wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,null);
266         } else {
267             wiz.putProperty(AddDomainWizardIterator.HOST,"");
268             wiz.putProperty(AddDomainWizardIterator.PORT,"");
269             wiz.putProperty(AddDomainWizardIterator.DOMAIN,"");
270             wiz.putProperty(AddDomainWizardIterator.INSTALL_LOCATION,
271                     "");
272             wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,null);
273         }
274     }
275     
276     static JFileChooser JavaDoc getJFileChooser(final FileFilter JavaDoc f){
277         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
278         decorateChooser(chooser,null,NbBundle.getMessage(Util.class,
279                 "LBL_Chooser_Name"));
280         return chooser;
281     }
282     
283     static void decorateChooser(JFileChooser JavaDoc chooser,String JavaDoc fname,String JavaDoc title) {
284         chooser.setDialogTitle(title); //NOI18N
285
chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
286         
287         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
288         chooser.setApproveButtonMnemonic(NbBundle.getMessage(Util.class,
289                 "Choose_Button_Mnemonic").charAt(0)); //NOI18N
290
chooser.setMultiSelectionEnabled(false);
291         chooser.setAcceptAllFileFilterUsed(false);
292         chooser.setApproveButtonToolTipText(NbBundle.getMessage(Util.class,
293                 "LBL_Chooser_Name")); //NOI18N
294

295         chooser.getAccessibleContext().
296                 setAccessibleName(NbBundle.getMessage(Util.class,
297                 "LBL_Chooser_Name")); //NOI18N
298
chooser.getAccessibleContext().
299                 setAccessibleDescription(NbBundle.getMessage(Util.class,
300                 "LBL_Chooser_Name")); //NOI18N
301
if (null != fname && fname.length() > 0) {
302             File JavaDoc sel = new File JavaDoc(fname);
303             if (sel.isDirectory())
304                 chooser.setCurrentDirectory(sel);
305             else
306                 chooser.setSelectedFile(sel);
307         }
308     }
309     
310     
311
312 }
313
Popular Tags