KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > spi > ejbjar > support > J2eeProjectView


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.j2ee.spi.ejbjar.support;
21
22 import java.awt.Image JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import javax.swing.event.EventListenerList JavaDoc;
27 import org.netbeans.api.java.classpath.ClassPath;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.queries.VisibilityQuery;
30 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
31 import org.netbeans.modules.j2ee.spi.ejbjar.EjbNodesFactory;
32 import org.netbeans.modules.j2ee.ejbjar.project.ui.EjbContainerNode;
33 import org.netbeans.modules.j2ee.ejbjar.project.ui.ServerResourceNode;
34 import org.openide.ErrorManager;
35 import org.openide.actions.FindAction;
36 import org.openide.filesystems.FileObject;
37 import org.openide.loaders.ChangeableDataFilter;
38 import org.openide.loaders.DataFilter;
39 import org.openide.loaders.DataFolder;
40 import org.openide.loaders.DataObject;
41 import org.openide.nodes.FilterNode;
42 import org.openide.nodes.Node;
43 import org.openide.util.Lookup;
44 import org.openide.util.NbBundle;
45 import org.openide.util.Utilities;
46 import org.openide.util.actions.SystemAction;
47
48 /** Utility class for creating J2EE project nodes.
49  *
50  * @author Pavel Buzek
51  */

52 public final class J2eeProjectView {
53     
54     /**
55      * The programmatic name (returned by {@link org.openide.nodes.Node.getName})
56      * of the node created by the {@link createConfigFilesView} method.
57      *
58      * @since 1.6
59      */

60     public static final String JavaDoc CONFIG_FILES_VIEW_NAME = "configurationFiles"; // NOI18N
61

62     private static EjbNodesFactory factoryInstance = null;
63     
64     private J2eeProjectView() {
65     }
66
67     /** Returns an instance of EjbNodesFactory if there is any
68      * available in default lookup. Otherwise returns null.
69      */

70     public static EjbNodesFactory getEjbNodesFactory () {
71         if (factoryInstance == null) {
72             factoryInstance = (EjbNodesFactory) Lookup.getDefault().lookup(EjbNodesFactory.class);
73         }
74         if (factoryInstance == null) {
75             ErrorManager.getDefault().log("No EjbNodesFactory instance available: Enterprise Beans nodes cannot be creater");
76         }
77         return factoryInstance;
78     }
79     
80     public static Node createServerResourcesNode (Project p) {
81         return new ServerResourceNode (p);
82     }
83     
84     public static Node createEjbsView (EjbJar model, ClassPath srcPath, FileObject ddFile, Project p) {
85         return new EjbContainerNode(model, srcPath, ddFile, p, getEjbNodesFactory());
86     }
87     
88     public static Node createConfigFilesView (FileObject folder) {
89         return new DocBaseNode(DataFolder.findFolder(folder));
90     }
91     
92     private static final class DocBaseNode extends FilterNode {
93         private static final DataFilter VISIBILITY_QUERY_FILTER = new VisibilityQueryDataFilter();
94
95         private static Image JavaDoc CONFIGURATION_FILES_BADGE =
96                 Utilities.loadImage( "org/netbeans/modules/j2ee/ejbjar/project/ui/ejbjar.gif", true ); // NOI18N
97

98         public DocBaseNode(DataFolder folder) {
99             super(folder.getNodeDelegate(), folder.createNodeChildren(VISIBILITY_QUERY_FILTER));
100         }
101
102         public Image JavaDoc getIcon( int type ) {
103             return computeIcon( false, type );
104         }
105
106         public Image JavaDoc getOpenedIcon( int type ) {
107             return computeIcon( true, type );
108         }
109
110         private Image JavaDoc computeIcon( boolean opened, int type ) {
111             Node folderNode = getOriginal();
112             Image JavaDoc image = opened ? folderNode.getOpenedIcon( type ) : folderNode.getIcon( type );
113             return Utilities.mergeImages( image, CONFIGURATION_FILES_BADGE, 7, 7 );
114         }
115         
116         public String JavaDoc getName() {
117             return CONFIG_FILES_VIEW_NAME;
118         }
119
120         public String JavaDoc getDisplayName() {
121             return NbBundle.getMessage(J2eeProjectView.class, "LBL_Node_ConfigFiles"); //NOI18N
122
}
123
124         public boolean canCopy() {
125             return false;
126         }
127
128         public boolean canCut() {
129             return false;
130         }
131
132         public boolean canRename() {
133             return false;
134         }
135
136         public boolean canDestroy() {
137             return false;
138         }
139
140         public Action JavaDoc[] getActions( boolean context ) {
141             return new Action JavaDoc[] {
142                 SystemAction.get(FindAction.class),
143             };
144         }
145     }
146     
147     private static final class VisibilityQueryDataFilter implements ChangeListener JavaDoc, ChangeableDataFilter {
148         
149         EventListenerList JavaDoc ell = new EventListenerList JavaDoc();
150         
151         public VisibilityQueryDataFilter() {
152             VisibilityQuery.getDefault().addChangeListener( this );
153         }
154                 
155         public boolean acceptDataObject(DataObject obj) {
156             FileObject fo = obj.getPrimaryFile();
157             return VisibilityQuery.getDefault().isVisible( fo );
158         }
159         
160         public void stateChanged( ChangeEvent JavaDoc e) {
161             Object JavaDoc[] listeners = ell.getListenerList();
162             ChangeEvent JavaDoc event = null;
163             for (int i = listeners.length-2; i>=0; i-=2) {
164                 if (listeners[i] == ChangeListener JavaDoc.class) {
165                     if ( event == null) {
166                         event = new ChangeEvent JavaDoc( this );
167                     }
168                     ((ChangeListener JavaDoc)listeners[i+1]).stateChanged( event );
169                 }
170             }
171         }
172     
173         public void addChangeListener( ChangeListener JavaDoc listener ) {
174             ell.add( ChangeListener JavaDoc.class, listener );
175         }
176                         
177         public void removeChangeListener( ChangeListener JavaDoc listener ) {
178             ell.remove( ChangeListener JavaDoc.class, listener );
179         }
180         
181     }
182
183     
184 }
185
Popular Tags