KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > Utils


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.j2ee.ddloaders.multiview;
20
21 import org.netbeans.api.java.project.JavaProjectConstants;
22 import org.netbeans.api.project.FileOwnerQuery;
23 import org.netbeans.api.project.ProjectUtils;
24 import org.netbeans.api.project.SourceGroup;
25 import org.netbeans.api.project.Sources;
26 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
27 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
28 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
29 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
30 import org.netbeans.modules.j2ee.ddloaders.multiview.ui.BrowseFolders;
31 import org.netbeans.modules.j2ee.spi.ejbjar.support.J2eeProjectView;
32 //import org.netbeans.modules.java.JavaDataObject;
33
//import org.netbeans.modules.java.ui.nodes.SourceNodes;
34
//import org.netbeans.modules.javacore.api.JavaModel;
35
//import org.netbeans.modules.refactoring.api.ui.RefactoringActionsFactory;
36
//import org.netbeans.spi.java.classpath.ClassPathFactory;
37
//import org.netbeans.spi.java.classpath.ClassPathImplementation;
38
//import org.netbeans.spi.java.classpath.PathResourceImplementation;
39
//import org.netbeans.spi.java.classpath.support.ClassPathSupport;
40
import org.openide.DialogDisplayer;
41 import org.openide.NotifyDescriptor;
42 import org.openide.cookies.OpenCookie;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileStateInvalidException;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.nodes.Node;
49 import org.openide.util.Lookup;
50 import org.openide.util.NbBundle;
51 import org.openide.util.Utilities;
52 import org.openide.util.lookup.AbstractLookup;
53 import org.openide.util.lookup.InstanceContent;
54
55 import javax.swing.*;
56 import java.awt.*;
57 import java.beans.PropertyChangeListener JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.net.URL JavaDoc;
60 import java.rmi.RemoteException JavaDoc;
61 import java.util.LinkedList JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import org.netbeans.api.java.classpath.ClassPath;
65
66 /**
67  * @author pfiala
68  */

69 public class Utils {
70
71     public static final String JavaDoc ICON_BASE_DD_VALID =
72             "org/netbeans/modules/j2ee/ddloaders/resources/DDValidIcon"; // NOI18N
73
public static final String JavaDoc ICON_BASE_DD_INVALID =
74             "org/netbeans/modules/j2ee/ddloaders/resources/DDInvalidIcon"; // NOI18N
75
public static final String JavaDoc ICON_BASE_EJB_MODULE_NODE =
76             "org/netbeans/modules/j2ee/ddloaders/resources/EjbModuleNodeIcon"; // NOI18N
77
public static final String JavaDoc ICON_BASE_ENTERPRISE_JAVA_BEANS_NODE =
78             "org/netbeans/modules/j2ee/ddloaders/resources/EjbContainerNodeIcon"; // NOI18N
79
public static final String JavaDoc ICON_BASE_SESSION_NODE =
80             "org/netbeans/modules/j2ee/ddloaders/resources/SessionNodeIcon"; // NOI18N
81
public static final String JavaDoc ICON_BASE_ENTITY_NODE =
82             "org/netbeans/modules/j2ee/ddloaders/resources/EntityNodeIcon"; // NOI18N
83
public static final String JavaDoc ICON_BASE_MESSAGE_DRIVEN_NODE =
84             "org/netbeans/modules/j2ee/ddloaders/resources/MessageNodeIcon"; // NOI18N
85
public static final String JavaDoc ICON_BASE_MISC_NODE =
86             "org/netbeans/modules/j2ee/ddloaders/resources/MiscNodeIcon"; // NOI18N
87

88     private static BrowseFolders.FileObjectFilter imageFileFilter = new BrowseFolders.FileObjectFilter() {
89         public boolean accept(FileObject fileObject) {
90             return fileObject.getMIMEType().startsWith("image/"); // NOI18N
91
}
92     };
93
94     public static String JavaDoc browseIcon(EjbJarMultiViewDataObject dataObject) {
95         FileObject fileObject = org.netbeans.modules.j2ee.ddloaders.multiview.ui.BrowseFolders.showDialog(
96                 dataObject.getSourceGroups(), imageFileFilter);
97         String JavaDoc relativePath;
98         if (fileObject != null) {
99             FileObject projectDirectory = dataObject.getProjectDirectory();
100             relativePath = FileUtil.getRelativePath(projectDirectory, fileObject);
101         } else {
102             relativePath = null;
103         }
104         return relativePath;
105     }
106
107     public static Color getErrorColor() {
108         // inspired by org.openide.WizardDescriptor
109
Color c = UIManager.getColor("nb.errorForeground"); //NOI18N
110
return c == null ? new Color(89, 79, 191) : c;
111     }
112
113     public static JTree findTreeComponent(Component component) {
114         if (component instanceof JTree) {
115             return (JTree) component;
116         }
117         if (component instanceof Container) {
118             Component[] components = ((Container) component).getComponents();
119             for (int i = 0; i < components.length; i++) {
120                 JTree tree = findTreeComponent(components[i]);
121                 if (tree != null) {
122                     return tree;
123                 }
124             }
125         }
126         return null;
127     }
128
129     public static void scrollToVisible(JComponent component) {
130         org.netbeans.modules.xml.multiview.Utils.scrollToVisible(component);
131     }
132
133     public static String JavaDoc getBundleMessage(String JavaDoc messageId) {
134         return NbBundle.getMessage(Utils.class, messageId);
135     }
136
137     public static String JavaDoc getBundleMessage(String JavaDoc messageId, Object JavaDoc param1) {
138         return NbBundle.getMessage(Utils.class, messageId, param1);
139     }
140
141     public static String JavaDoc getBundleMessage(String JavaDoc messageId, Object JavaDoc param1, Object JavaDoc param2) {
142         return NbBundle.getMessage(Utils.class, messageId, param1, param2);
143     }
144
145     public static String JavaDoc getBundleMessage(String JavaDoc messageId, Object JavaDoc param1, Object JavaDoc param2, Object JavaDoc param3) {
146         return NbBundle.getMessage(Utils.class, messageId, param1, param2, param3);
147     }
148
149     public static boolean isJavaIdentifier(String JavaDoc id) {
150         return Utilities.isJavaIdentifier(id);
151     }
152
153     /**
154      * Returns true, if the passed string can be used as a qualified identifier.
155      * it does not check for semantic, only for syntax.
156      * The function returns true for any sequence of identifiers separated by
157      * dots.
158      */

159     public static boolean isValidPackageName(String JavaDoc packageName) {
160         String JavaDoc[] strings = packageName.split("[.]"); // NOI18N
161
if (strings.length == 0) {
162             return false;
163         }
164         for (int i = 0; i < strings.length; i++) {
165             if (!isJavaIdentifier(strings[i])) {
166                 return false;
167             }
168         }
169         return packageName.charAt(packageName.length() - 1) != '.';
170     }
171
172     public static void removeClass(ClassPath classPath, String JavaDoc className) {
173         FileObject sourceFile = getSourceFile(classPath, className);
174         if (sourceFile != null) {
175 // try {
176
//// JavaDataObject.find(sourceFile).delete();
177
// } catch (DataObjectNotFoundException e) {
178
// notifyError(e);
179
// } catch (IOException e) {
180
// notifyError(e);
181
// }
182
}
183     }
184
185     public static FileObject getPackageFile(ClassPath classPath, String JavaDoc packageName) {
186         return classPath.findResource(packageToPath(packageName));
187     }
188
189     private static String JavaDoc packageToPath(String JavaDoc packageName) {
190         return packageName.replace('.', '/');
191     }
192
193     public static String JavaDoc getPackage(String JavaDoc ejbClass) {
194         final int i = ejbClass.lastIndexOf('.');
195         if (i < 0) {
196             return "";
197         } else {
198             return ejbClass.substring(0, i);
199         }
200
201     }
202
203     public static void notifyError(Exception JavaDoc ex) {
204         NotifyDescriptor ndd = new NotifyDescriptor.Message(ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE);
205         DialogDisplayer.getDefault().notify(ndd);
206     }
207
208     public static FileObject getSourceFile(ClassPath classPath, String JavaDoc className) {
209         return classPath.findResource(packageToPath(className) + ".java");
210     }
211
212 // public static Node createEntityNode(FileObject ejbJarFile, ClassPath classPath, Entity entity) {
213
// //todo:
214
// //classPath = getSourceClassPath(ejbJarFile);
215
// EjbJar ejbJar;
216
// try {
217
// ejbJar = DDProvider.getDefault().getDDRoot(ejbJarFile);
218
// } catch (IOException e) {
219
// notifyError(e);
220
// return null;
221
// }
222
// return J2eeProjectView.getEjbNodesFactory().createEntityNode (entity, ejbJar, classPath, ejbJarFile);
223
// }
224

225 // public static ClassPath getSourceClassPath(FileObject ejbJarFile) {
226
// Sources sources = ProjectUtils.getSources(FileOwnerQuery.getOwner(ejbJarFile));
227
// SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
228
// ClassPath srcClassPath = ClassPathFactory.createClassPath(new ClassPathImpl(groups));
229
// ClassPath bootClassPath = ClassPath.getClassPath(ejbJarFile, ClassPath.BOOT);
230
// return ClassPathSupport.createProxyClassPath(new ClassPath[]{srcClassPath, bootClassPath});
231
// }
232

233 // public static Method getMethod(JavaClass javaClass, Method method) {
234
// if (javaClass == null || method == null) {
235
// return null;
236
// } else {
237
// List parameters = new LinkedList();
238
// for (Iterator it = method.getParameters().iterator(); it.hasNext();) {
239
// parameters.add(((Parameter) it.next()).getType());
240
// }
241
// return javaClass.getMethod(method.getName(), parameters, false);
242
// }
243
// }
244

245 // public static void addMethod(JavaClass javaClass, Method prototype) {
246
// addMethod(javaClass, prototype, false);
247
// }
248

249 // public static void addMethod(JavaClass javaClass, Method prototype, boolean remote) {
250
// if (prototype != null) {
251
// addMethod(javaClass, prototype, remote, prototype.getModifiers());
252
// }
253
// }
254
//
255
// public static void addMethod(JavaClass interfaceClass, Method prototype, boolean remote, int modifiers) {
256
// if (interfaceClass == null || prototype == null) {
257
// return;
258
// }
259
// if (getMethod(interfaceClass, prototype) != null) {
260
// return;
261
// }
262
// beginJmiTransaction(true);
263
// boolean rollback = true;
264
// try {
265
// Method method = JMIUtils.createMethod(interfaceClass);
266
// method.setName(prototype.getName());
267
// Type type = prototype.getType();
268
// if (type != null) {
269
// method.setType(JMIUtils.resolveType(type.getName()));
270
// }
271
// JMIUtils.replaceParameters(method, prototype.getParameters());
272
// method.setModifiers(modifiers);
273
// if (remote) {
274
// JMIUtils.addException(method, RemoteException.class.getName());
275
// }
276
// for (Iterator it = prototype.getExceptionNames().iterator(); it.hasNext();) {
277
// MultipartId mpId= (MultipartId) it.next();
278
// String exceptionName = mpId.getName();
279
// if (!"RemoteException".equals(exceptionName) && !"java.rmi.RemoteException".equals(exceptionName)) {
280
// JMIUtils.addException(method, exceptionName);
281
// }
282
// }
283
// getContents(interfaceClass).add(method);
284
// rollback = false;
285
// } finally {
286
// endJmiTransaction(rollback);
287
// }
288
// }
289
//
290
// public static List getContents(JavaClass javaClass) {
291
// return ((JavaClass) JMIUtils.resolveType(javaClass.getName())).getContents();
292
// }
293
//
294
// public static void removeMethod(JavaClass javaClass, Method method) {
295
// if (javaClass == null || method == null) {
296
// return;
297
// }
298
// beginJmiTransaction(true);
299
// boolean rollback = true;
300
// try {
301
// getContents(javaClass).remove(getMethod(javaClass, method));
302
// rollback = false;
303
// } finally {
304
// endJmiTransaction(rollback);
305
// }
306
// }
307
//
308
// private static Lookup createClassRefactoringLookup(String fullClassName) {
309
// Node node = SourceNodes.getExplorerFactory().createClassNode((JavaClass) JMIUtils.resolveType(fullClassName));
310
// InstanceContent ic = new InstanceContent();
311
// ic.add(node);
312
// return new AbstractLookup(ic);
313
// }
314
//
315
// public static void activateRenameClassUI(String fullClassName) {
316
// Lookup lookup = createClassRefactoringLookup(fullClassName);
317
// final Action action = RefactoringActionsFactory.renameAction().createContextAwareInstance(lookup);
318
// action.actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT);
319
// }
320
//
321
// public static void activateMoveClassUI(String fullClassName) {
322
// Lookup lookup = createClassRefactoringLookup(fullClassName);
323
// final Action action = RefactoringActionsFactory.moveClassAction().createContextAwareInstance(lookup);
324
// action.actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT);
325
// }
326
//
327
// public static void renameMethod(Method method, String name) {
328
// if (method != null) {
329
// method.setName(name);
330
// }
331
// }
332

333     public static String JavaDoc getEjbDisplayName(Ejb ejb) {
334         String JavaDoc name = ejb.getDefaultDisplayName();
335         if (name == null) {
336             name = ejb.getEjbName();
337             if (name == null) {
338                 name = " "; // NOI18N
339
}
340         }
341         return name;
342     }
343
344 // /**
345
// * Opens source of given class
346
// * @param ejbJarFile
347
// * @param javaClass
348
// */
349
// public static void openEditorFor(FileObject ejbJarFile, JavaClass javaClass) {
350
// if (javaClass == null) {
351
// return;
352
// }
353
// FileObject sourceFile = getSourceFile(getSourceClassPath(ejbJarFile), javaClass.getName());
354
// if (sourceFile != null) {
355
// DataObject javaDo;
356
// try {
357
// javaDo = DataObject.find(sourceFile);
358
// } catch (DataObjectNotFoundException e) {
359
// DialogDisplayer.getDefault().notify(
360
// new NotifyDescriptor.Message(getBundleMessage("MSG_sourceNotFound")));
361
// return;
362
// }
363
// OpenCookie cookie = (OpenCookie) javaDo.getCookie(OpenCookie.class);
364
// if (cookie != null) {
365
// cookie.open();
366
// }
367
// }
368
// }
369

370     /**
371      * Make sure that the code will run in AWT dispatch thread
372      * @param runnable
373      */

374     public static void runInAwtDispatchThread(Runnable JavaDoc runnable) {
375         org.netbeans.modules.xml.multiview.Utils.runInAwtDispatchThread(runnable);
376     }
377
378 // public static void changeParameterType(final Method method, Type type) {
379
// if (method != null) {
380
// Parameter parameter = (Parameter) method.getParameters().get(0);
381
// parameter.setType(type);
382
// }
383
// }
384
//
385
// public static void beginJmiTransaction(boolean writeAccess) {
386
// JavaModel.getJavaRepository().beginTrans(writeAccess);
387
// }
388
//
389
// public static void endJmiTransaction(boolean rollback) {
390
// JavaModel.getJavaRepository().endTrans(rollback);
391
// }
392

393 // private static class ClassPathImpl implements ClassPathImplementation {
394
//
395
// private List resources = new LinkedList();
396
//
397
// private class PathResourceImpl implements PathResourceImplementation {
398
//
399
// URL[] roots;
400
//
401
// public PathResourceImpl(URL root) {
402
// this.roots = new URL[]{root};
403
// }
404
//
405
// public URL[] getRoots() {
406
// return roots;
407
// }
408
//
409
// public ClassPathImplementation getContent() {
410
// return ClassPathImpl.this;
411
// }
412
//
413
// public void addPropertyChangeListener(PropertyChangeListener listener) {
414
// }
415
//
416
// public void removePropertyChangeListener(PropertyChangeListener listener) {
417
// }
418
// }
419
//
420
// public ClassPathImpl(SourceGroup[] groups) {
421
// for (int i = 0; i < groups.length; i++) {
422
// SourceGroup group = groups[i];
423
// try {
424
// resources.add(new PathResourceImpl(group.getRootFolder().getURL()));
425
// } catch (FileStateInvalidException e) {
426
// notifyError(e);
427
// }
428
// }
429
// }
430
//
431
// public java.util.List /*<PathResourceImplementation>*/ getResources() {
432
// return resources;
433
// }
434
//
435
// public void addPropertyChangeListener(PropertyChangeListener listener) {
436
// }
437
//
438
// public void removePropertyChangeListener(PropertyChangeListener listener) {
439
// }
440
// }
441
}
442
Popular Tags