KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > webservices > ui > WSHandlerDialog


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.core.webservices.ui;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.lang.model.element.TypeElement;
29 import javax.lang.model.type.DeclaredType;
30 import javax.lang.model.type.TypeMirror;
31 import javax.lang.model.type.WildcardType;
32 import org.netbeans.api.java.classpath.ClassPath;
33 import org.netbeans.api.java.source.CancellableTask;
34 import org.netbeans.api.java.source.CompilationController;
35 import org.netbeans.api.java.source.JavaSource;
36 import org.netbeans.api.java.source.JavaSource.Phase;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.modules.j2ee.common.source.SourceUtils;
39 import org.netbeans.modules.websvc.core.webservices.ui.panels.SelectHandlerPanel;
40 import org.openide.DialogDescriptor;
41 import org.openide.DialogDisplayer;
42 import org.openide.ErrorManager;
43 import org.openide.NotifyDescriptor;
44 import org.openide.filesystems.FileObject;
45 import org.openide.loaders.DataObject;
46 import org.openide.nodes.Node;
47 import org.openide.util.NbBundle;
48
49 /**
50  *
51  * @author Roderico Cruz, Milan Kuchtiak
52  * Displays a Dialog for selecting web service message handler classes
53  * that are in a project.
54  */

55 public class WSHandlerDialog {
56     private Dialog JavaDoc dialog;
57     private SelectHandlerPanel sPanel;
58     private AddMessageHandlerDialogDesc dlgDesc;
59     private boolean isJaxWS;
60     /**
61      * Creates a new instance of WSHandlerDialog
62      */

63     public WSHandlerDialog(Project project, boolean isJaxWS) {
64         this.isJaxWS = isJaxWS;
65         sPanel = new SelectHandlerPanel(project);
66         dlgDesc = new AddMessageHandlerDialogDesc(sPanel);
67         dialog = DialogDisplayer.getDefault().createDialog(dlgDesc);
68     }
69     
70     public void show(){
71         dialog.setVisible(true);
72     }
73     
74     public boolean okButtonPressed(){
75         return dlgDesc.getValue() == DialogDescriptor.OK_OPTION;
76     }
77     
78     public Set JavaDoc<String JavaDoc> getSelectedClasses(){
79         Set JavaDoc<String JavaDoc> selectedClasses = new HashSet JavaDoc<String JavaDoc>();
80         Node[] nodes = sPanel.getSelectedNodes();
81         for(int i = 0; i < nodes.length; i++) {
82             FileObject fo = getFileObjectFromNode(nodes[i]);
83             if (fo!=null) {
84                 ClassPath classPath = ClassPath.getClassPath(fo, ClassPath.SOURCE);
85                 String JavaDoc handlerClassName = classPath.getResourceName(fo, '.', false);
86                 selectedClasses.add(handlerClassName);
87             }
88         }
89         return selectedClasses;
90     }
91     
92     private FileObject getFileObjectFromNode(Node n) {
93         DataObject dObj = n.getCookie(DataObject.class);
94         if (dObj!=null) return dObj.getPrimaryFile();
95         return null;
96     }
97     
98     class AddMessageHandlerDialogDesc extends DialogDescriptor{
99         Project project;
100         final SelectHandlerPanel sPanel;
101         
102         private Object JavaDoc[] closingOptionsWithoutOK = {DialogDescriptor.CANCEL_OPTION,
103         DialogDescriptor.CLOSED_OPTION};
104         private Object JavaDoc[] closingOptionsWithOK = {DialogDescriptor.CANCEL_OPTION,
105         DialogDescriptor.CLOSED_OPTION, DialogDescriptor.OK_OPTION};
106         
107         /**
108          * Creates a new instance of AddMessageHandlerDialogDesc
109          */

110         public AddMessageHandlerDialogDesc(SelectHandlerPanel sPanel) {
111             super(sPanel, NbBundle.getMessage(WSHandlerDialog.class, "TTL_SelectHandler"));
112             this.sPanel = sPanel;
113             this.setButtonListener(new AddMessageActionListener(sPanel));
114         }
115         
116         class AddMessageActionListener implements ActionListener JavaDoc{
117             SelectHandlerPanel sPanel;
118             public AddMessageActionListener(SelectHandlerPanel sPanel){
119                 this.sPanel = sPanel;
120             }
121             public void actionPerformed(ActionEvent JavaDoc evt) {
122                 if(evt.getSource() == NotifyDescriptor.OK_OPTION){
123                     boolean accepted = true;
124                     String JavaDoc errMsg = null;
125                     Node[] selectedNodes = sPanel.getSelectedNodes();
126                     for(int i = 0; i < selectedNodes.length; i++){
127                         Node node = selectedNodes[i];
128                         FileObject javaClassFo = getFileObjectFromNode(node);
129                         //FIX-ME: Improve this by filtering the Tree View to only include handlers
130
if(javaClassFo == null){
131                             errMsg = NbBundle.getMessage(WSHandlerDialog.class,
132                                     "NotJavaClass_msg");
133                             accepted = false;
134                             break;
135                         }
136                         final boolean[] isHandler = new boolean[]{false};
137                         
138                         JavaSource javaSource = JavaSource.forFileObject(javaClassFo);
139                         if(javaSource == null){
140                             errMsg = NbBundle.getMessage(WSHandlerDialog.class,
141                                     "NotJavaClass_msg");
142                             accepted = false;
143                             break;
144                         }
145                         
146                         CancellableTask<CompilationController> task = new CancellableTask<CompilationController>() {
147                             public void run(CompilationController controller) throws IOException JavaDoc {
148                                 controller.toPhase(Phase.ELEMENTS_RESOLVED);
149                                 if (isHandler(controller)) isHandler[0]=true;
150                             }
151                             public void cancel() {}
152                         };
153                         try {
154                             javaSource.runUserActionTask(task, true);
155                         } catch (IOException JavaDoc ex) {
156                             ErrorManager.getDefault().notify(ex);
157                         }
158                         
159                         if(!isHandler[0]) {
160                             errMsg = NbBundle.getMessage(WSHandlerDialog.class,
161                                     "NotHandlerClass_msg");
162                             accepted = false;
163                             break;
164                         }
165                     }
166                     if (!accepted) {
167                         NotifyDescriptor.Message notifyDescr =
168                                 new NotifyDescriptor.Message(errMsg,
169                                 NotifyDescriptor.ERROR_MESSAGE );
170                         DialogDisplayer.getDefault().notify(notifyDescr);
171                         AddMessageHandlerDialogDesc.this.setClosingOptions(closingOptionsWithoutOK);
172                     } else {
173                         // Everything was fine so allow OK
174
AddMessageHandlerDialogDesc.this.setClosingOptions(closingOptionsWithOK);
175                     }
176                 }
177             }
178         }
179         
180         private boolean isHandler(CompilationController cc) throws IOException JavaDoc {
181             SourceUtils srcUtils = SourceUtils.newInstance(cc);
182             if (srcUtils!=null) {
183                 TypeMirror classMirror = srcUtils.getTypeElement().asType();
184                 
185                 if(isJaxWS) {
186                     // test if class extends "javax.xml.ws.handler.LogicalHandler<C extends javax.xml.ws.handler.LogicalMessageContext>"
187
TypeElement handlerElement = cc.getElements().getTypeElement("javax.xml.ws.handler.LogicalHandler"); //NOI18N
188
DeclaredType handlerType=null;
189                     if (handlerElement!=null) {
190                         TypeElement messageContextElement = cc.getElements().getTypeElement("javax.xml.ws.handler.LogicalMessageContext"); //NOI18N
191
WildcardType wildcardType = cc.getTypes().getWildcardType(messageContextElement.asType(), null);
192                         handlerType = cc.getTypes().getDeclaredType(handlerElement, wildcardType);
193                     }
194                     if (handlerType!=null && cc.getTypes().isSubtype(classMirror, handlerType)) {
195                         return true;
196                     }
197                     // test if class extends "javax.xml.ws.handler.Handler<C extends javax.xml.ws.handler.MessageContext>"
198
handlerElement = cc.getElements().getTypeElement("javax.xml.ws.handler.Handler"); //NOI18N
199
handlerType=null;
200                     if (handlerElement!=null) {
201                         TypeElement messageContextElement = cc.getElements().getTypeElement("javax.xml.ws.handler.MessageContext"); //NOI18N
202
WildcardType wildcardType = cc.getTypes().getWildcardType(messageContextElement.asType(), null);
203                         handlerType = cc.getTypes().getDeclaredType(handlerElement, wildcardType);
204                     }
205                     if (handlerType!=null && cc.getTypes().isSubtype(classMirror, handlerType)) {
206                         return true;
207                     }
208                 }
209                 else {
210                     // test if class extends "javax.xml.rpc.handler.Handler"
211
TypeElement handlerElement = cc.getElements().getTypeElement("javax.xml.rpc.handler.Handler"); //NOI18N
212
if (handlerElement!=null && cc.getTypes().isSubtype(classMirror, handlerElement.asType())) {
213                         return true;
214                     }
215                 }
216             }
217             return false;
218         }
219     }
220 }
221
Popular Tags