KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxrpc > nodes > ConfigureHandlerCookieImpl


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.jaxrpc.nodes;
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.ArrayList JavaDoc;
27 import javax.swing.DefaultListModel JavaDoc;
28 import javax.swing.ListModel JavaDoc;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
31 import org.netbeans.modules.j2ee.dd.api.common.ServiceRef;
32 import org.netbeans.modules.j2ee.dd.api.common.ServiceRefHandler;
33 import org.netbeans.modules.j2ee.dd.api.common.RootInterface;
34 import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
35 import org.netbeans.modules.websvc.core.webservices.action.ConfigureHandlerCookie;
36 import org.netbeans.modules.websvc.core.webservices.ui.panels.MessageHandlerPanel;
37 import org.openide.DialogDescriptor;
38 import org.openide.DialogDisplayer;
39 import org.openide.NotifyDescriptor;
40 import org.openide.filesystems.FileObject;
41 import org.openide.util.NbBundle;
42
43 public class ConfigureHandlerCookieImpl implements ConfigureHandlerCookie {
44     private String JavaDoc serviceName;
45     private Project project;
46     private FileObject srcRoot;
47     private WebServicesClientSupport clientSupport;
48     
49     public ConfigureHandlerCookieImpl(String JavaDoc serviceName, Project project, WebServicesClientSupport clientSupport, FileObject srcRoot) {
50         this.serviceName = serviceName;
51         this.project = project;
52         this.clientSupport = clientSupport;
53         this.srcRoot = srcRoot;
54     }
55     
56     
57     
58     public void configureHandler() {
59       try{
60         final FileObject ddFO = clientSupport.getDeploymentDescriptor();
61         if(ddFO == null) return;
62         final RootInterface rootDD = DDProvider.getDefault().getDDRoot(ddFO);
63         String JavaDoc ddServiceName = "service/" + serviceName;
64         final ServiceRef serviceRef = (ServiceRef) rootDD.findBeanByName("ServiceRef", "ServiceRefName", ddServiceName); // NOI18N
65
if (serviceRef==null) {
66             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
67                     NbBundle.getMessage(ConfigureHandlerCookieImpl.class, "ERR_NotJSR109Client"),NotifyDescriptor.WARNING_MESSAGE));
68             return;
69         }
70         ServiceRefHandler[] handlers = serviceRef.getHandler();
71         ArrayList JavaDoc handlerList = new ArrayList JavaDoc();
72         for(int j = 0; j < handlers.length; j++) {
73             handlerList.add(handlers[j].getHandlerClass());
74         }
75         
76         final MessageHandlerPanel panel = new MessageHandlerPanel(project,
77         (String JavaDoc[])handlerList.toArray(new String JavaDoc[handlerList.size()]), false, serviceName);
78         String JavaDoc title = NbBundle.getMessage(ConfigureHandlerCookieImpl.class,"TTL_MessageHandlerPanel");
79         DialogDescriptor dialogDesc = new DialogDescriptor(panel, title, true,
80         new ActionListener JavaDoc(){
81             public void actionPerformed(ActionEvent JavaDoc evt) {
82                 if(evt.getSource() == NotifyDescriptor.OK_OPTION) {
83                     DefaultListModel JavaDoc listModel = panel.getListModel();
84                     //DefaultTableModel tblModel = panel.getTableModel();
85
try{
86                         //add new handlers
87
for (int i = 0 ; i < listModel.getSize(); i++){
88                             String JavaDoc className = (String JavaDoc)listModel.getElementAt(i);
89                             if(isNewHandler(className, serviceRef)){
90                                 ServiceRefHandler handler = (ServiceRefHandler)rootDD.createBean("ServiceRefHandler");
91                                 handler.setHandlerName(className);
92                                 handler.setHandlerClass(className);
93                                 serviceRef.addHandler(handler);
94                             }
95                         }
96                         //remove handlers that have been deleted
97
ServiceRefHandler[] handlers = serviceRef.getHandler();
98                         for(int j = 0; j < handlers.length; j++){
99                             ServiceRefHandler handler = handlers[j];
100                             String JavaDoc clsName = handler.getHandlerClass();
101                             if(!isInModel(clsName, listModel)){
102                                 serviceRef.removeHandler(handler);
103                             }
104                         }
105                         rootDD.write(ddFO);
106                     }catch(ClassNotFoundException JavaDoc e){
107                         throw new RuntimeException JavaDoc(e.getMessage());
108                     }
109                     catch(IOException JavaDoc e){
110                         throw new RuntimeException JavaDoc(e.getMessage());
111                     }
112                 }
113             }
114         });
115         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDesc);
116         dialog.setVisible(true);
117       }catch(IOException JavaDoc e){
118            throw new RuntimeException JavaDoc(e.getMessage());
119       }
120         
121     }
122     
123     private boolean isNewHandler(String JavaDoc className, ServiceRef serviceRef){
124         ServiceRefHandler[] handlers = serviceRef.getHandler();
125         for(int i = 0; i < handlers.length; i++){
126             if(handlers[i].getHandlerClass().equals(className)){
127                 return false;
128             }
129         }
130         return true;
131     }
132     
133     private boolean isInModel(String JavaDoc className, ListModel JavaDoc model){
134         for(int i = 0; i < model.getSize(); i++){
135             String JavaDoc cls = (String JavaDoc)model.getElementAt(i);
136             if(className.equals(cls)){
137                 return true;
138             }
139         }
140         return false;
141     }
142     
143 }
144
Popular Tags