1 19 20 package org.netbeans.modules.websvc.core.webservices.ui; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.IOException ; 26 import java.util.HashSet ; 27 import java.util.Set ; 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 55 public class WSHandlerDialog { 56 private Dialog dialog; 57 private SelectHandlerPanel sPanel; 58 private AddMessageHandlerDialogDesc dlgDesc; 59 private boolean isJaxWS; 60 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 <String > getSelectedClasses(){ 79 Set <String > selectedClasses = new HashSet <String >(); 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 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 [] closingOptionsWithoutOK = {DialogDescriptor.CANCEL_OPTION, 103 DialogDescriptor.CLOSED_OPTION}; 104 private Object [] closingOptionsWithOK = {DialogDescriptor.CANCEL_OPTION, 105 DialogDescriptor.CLOSED_OPTION, DialogDescriptor.OK_OPTION}; 106 107 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 { 117 SelectHandlerPanel sPanel; 118 public AddMessageActionListener(SelectHandlerPanel sPanel){ 119 this.sPanel = sPanel; 120 } 121 public void actionPerformed(ActionEvent evt) { 122 if(evt.getSource() == NotifyDescriptor.OK_OPTION){ 123 boolean accepted = true; 124 String 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 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 { 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 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 AddMessageHandlerDialogDesc.this.setClosingOptions(closingOptionsWithOK); 175 } 176 } 177 } 178 } 179 180 private boolean isHandler(CompilationController cc) throws IOException { 181 SourceUtils srcUtils = SourceUtils.newInstance(cc); 182 if (srcUtils!=null) { 183 TypeMirror classMirror = srcUtils.getTypeElement().asType(); 184 185 if(isJaxWS) { 186 TypeElement handlerElement = cc.getElements().getTypeElement("javax.xml.ws.handler.LogicalHandler"); DeclaredType handlerType=null; 189 if (handlerElement!=null) { 190 TypeElement messageContextElement = cc.getElements().getTypeElement("javax.xml.ws.handler.LogicalMessageContext"); 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 handlerElement = cc.getElements().getTypeElement("javax.xml.ws.handler.Handler"); handlerType=null; 200 if (handlerElement!=null) { 201 TypeElement messageContextElement = cc.getElements().getTypeElement("javax.xml.ws.handler.MessageContext"); 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 TypeElement handlerElement = cc.getElements().getTypeElement("javax.xml.rpc.handler.Handler"); if (handlerElement!=null && cc.getTypes().isSubtype(classMirror, handlerElement.asType())) { 213 return true; 214 } 215 } 216 } 217 return false; 218 } 219 } 220 } 221 | Popular Tags |