KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > customization > multiview > ExternalBindingTablePanel


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  * ExternalBindingTablePanel.java
21  *
22  * Created on March 8, 2006, 9:42 AM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.websvc.customization.multiview;
29
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.io.File JavaDoc;
33 import java.net.URI JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
41 import javax.swing.JFileChooser JavaDoc;
42 import javax.swing.filechooser.FileFilter JavaDoc;
43 import javax.swing.table.AbstractTableModel JavaDoc;
44 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
45 import org.netbeans.modules.websvc.api.jaxws.project.config.Binding;
46 import org.netbeans.modules.websvc.api.jaxws.project.config.Client;
47 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
48 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
49 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport;
50 import org.netbeans.modules.xml.multiview.ui.DefaultTablePanel;
51 import org.netbeans.modules.xml.retriever.catalog.Utilities;
52 import org.openide.DialogDisplayer;
53 import org.openide.NotifyDescriptor;
54 import org.openide.filesystems.FileObject;
55 import org.openide.filesystems.FileUtil;
56 import org.openide.nodes.Node;
57 import org.openide.util.NbBundle;
58 import org.openide.util.WeakListeners;
59
60 /**
61  *
62  * @author Roderico Cruz
63  */

64 public class ExternalBindingTablePanel extends DefaultTablePanel{
65     private static final String JavaDoc[] columnName = {NbBundle.getMessage(ExternalBindingTablePanel.class,
66             "TITLE_CUSTOMIZATION_FILES")};
67     private EBTableModel model;
68     private String JavaDoc previousDirectory = "";
69     private static final FileFilter JavaDoc XML_FILE_FILTER = new XmlFileFilter();
70     private Node node;
71     private JaxWsModel jmodel;
72     private Map JavaDoc<String JavaDoc, FileObject> addedBindings;
73     private RemoveActionListener removeActionListener;
74     private AddActionListener addActionListener;
75     
76     /** Creates a new instance of ExternalBindingTablePanel */
77     public ExternalBindingTablePanel(EBTableModel model,
78             Node node, JaxWsModel jmodel) {
79         super(model);
80         this.model = model;
81         this.node = node;
82         this.jmodel = jmodel;
83         this.editButton.setVisible(false); //can't edit an entry
84
addedBindings = new HashMap JavaDoc<String JavaDoc, FileObject>();
85         
86         addActionListener = new AddActionListener();
87         ActionListener JavaDoc addListener = (ActionListener JavaDoc)WeakListeners.create(ActionListener JavaDoc.class,
88                 addActionListener, addButton);
89         addButton.addActionListener(addListener);
90         
91         removeActionListener = new RemoveActionListener();
92         ActionListener JavaDoc removeListener = (ActionListener JavaDoc)WeakListeners.create(ActionListener JavaDoc.class,
93                 removeActionListener, removeButton);
94         removeButton.addActionListener(removeListener);
95     }
96     
97     public String JavaDoc getRelativePathToWsdl(){
98         String JavaDoc relativePath = "";
99         FileObject srcRoot = (FileObject)node.getLookup().lookup(FileObject.class);
100         FileObject localWsdlFile = null;
101         FileObject wsdlFolder = null;
102         Client client = (Client)node.getLookup().lookup(Client.class);
103         if(client != null){
104             JAXWSClientSupport support = JAXWSClientSupport.getJaxWsClientSupport(srcRoot);
105             wsdlFolder = support.getLocalWsdlFolderForClient(client.getName(),false);
106             localWsdlFile =
107                     wsdlFolder.getFileObject(client.getLocalWsdlFile());
108             
109         }
110         else{
111             JAXWSSupport support = JAXWSSupport.getJAXWSSupport(srcRoot);
112             Service service = (Service)node.getLookup().lookup(Service.class);
113             wsdlFolder = support.getLocalWsdlFolderForService(service.getName(), false);
114             localWsdlFile =
115                     wsdlFolder.getFileObject(service.getLocalWsdlFile());
116             
117         }
118         try{
119             relativePath = Utilities.relativize(FileUtil.toFile(wsdlFolder).toURI(),
120                     new URI JavaDoc(localWsdlFile.getURL().toExternalForm()));
121         }catch(Exception JavaDoc e){
122             return "Unable to obtain relative path";
123         }
124         
125         return "../" + relativePath;
126     }
127     
128     public Map JavaDoc<String JavaDoc, FileObject> getAddedBindings(){
129         return addedBindings;
130     }
131     
132     public Set JavaDoc<String JavaDoc> getRemovedBindings(){
133         Set JavaDoc<String JavaDoc> bindingsRemoved = new HashSet JavaDoc<String JavaDoc>();
134         Binding[] bindingsInModel = null;
135         Client client = (Client)node.getLookup().lookup(Client.class);
136         if(client != null){
137             bindingsInModel = client.getBindings();
138         } else{
139             Service service = (Service)node.getLookup().lookup(Service.class);
140             if(service != null){
141                 bindingsInModel= service.getBindings();
142             }
143         }
144         if(bindingsInModel == null){ //this can't happen
145
return Collections.emptySet();
146         }
147         @SuppressWarnings JavaDoc("unchecked")
148         List JavaDoc<String JavaDoc> bindingsInTable = model.getChildren();
149         for(int i = 0; i < bindingsInModel.length; i++){
150             String JavaDoc bindingInModel = bindingsInModel[i].getFileName();
151             boolean found = false;
152             for(String JavaDoc bindingInTable: bindingsInTable){
153                 if(bindingInTable.equals(bindingInModel)){
154                     found = true;
155                     break;
156                 }
157             }
158             if(!found){
159                 bindingsRemoved.add(bindingInModel);
160             }
161         }
162         return bindingsRemoved;
163     }
164     
165     public List JavaDoc getChildren(){
166         return model.getChildren();
167     }
168     
169     class RemoveActionListener implements ActionListener JavaDoc{
170         public void actionPerformed(ActionEvent JavaDoc e){
171             int row = getTable().getSelectedRow();
172             if(row == -1) return;
173             String JavaDoc fileName = (String JavaDoc)getTable().getValueAt(row, 0);
174             if(confirmDeletion(fileName)){
175                 addedBindings.remove(fileName);
176                 ExternalBindingTablePanel.this.model.removeRow(row);
177             }
178         }
179         
180         private boolean confirmDeletion(String JavaDoc fileName) {
181             NotifyDescriptor.Confirmation notifyDesc =
182                     new NotifyDescriptor.Confirmation(NbBundle.getMessage
183                     (ExternalBindingTablePanel.class, "MSG_CONFIRM_DELETE", fileName),
184                     NotifyDescriptor.YES_NO_OPTION);
185             DialogDisplayer.getDefault().notify(notifyDesc);
186             return (notifyDesc.getValue() == NotifyDescriptor.YES_OPTION);
187         }
188     }
189     
190     class AddActionListener implements ActionListener JavaDoc{
191         public void actionPerformed(ActionEvent JavaDoc e) {
192             //Display information about wsdlLocation
193
NotifyDescriptor.Confirmation notifyDesc =
194                     new NotifyDescriptor.Confirmation(NbBundle.getMessage
195                     (ExternalBindingTablePanel.class, "MSG_EXTERNAL_BINDING", getRelativePathToWsdl()),
196                     NotifyDescriptor.YES_NO_OPTION);
197             DialogDisplayer.getDefault().notify(notifyDesc);
198             if(notifyDesc.getValue() == NotifyDescriptor.NO_OPTION) return;
199             
200             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(previousDirectory);
201             chooser.setMultiSelectionEnabled(false);
202             chooser.setAcceptAllFileFilterUsed(false);
203             chooser.addChoosableFileFilter(XML_FILE_FILTER);
204             chooser.setFileFilter(XML_FILE_FILTER);
205             
206             if(chooser.showOpenDialog(ExternalBindingTablePanel.this) == JFileChooser.APPROVE_OPTION) {
207                 File JavaDoc bindingFile = chooser.getSelectedFile();
208                 if(bindingFile.exists()){
209                     FileObject bindingFO = FileUtil.toFileObject(bindingFile);
210                     String JavaDoc bindingName = bindingFO.getName();
211                     bindingName = FileUtil.findFreeFileName(getBindingsFolder(node), bindingName, bindingFO.getExt());
212                     bindingName = bindingFO.getExt().equals("") ? bindingName : bindingName + "." + bindingFO.getExt();
213                     addedBindings.put(bindingName, bindingFO);
214                     ExternalBindingTablePanel.this.model.addRow(bindingName);
215                     previousDirectory = bindingFile.getPath();
216                 }
217             }
218         }
219         private FileObject getBindingsFolder(Node node){
220             FileObject srcRoot = (FileObject)node.getLookup().lookup(FileObject.class);
221             assert srcRoot != null : "Cannot find srcRoot";
222             FileObject bindingsFolder = null;
223             Client client = (Client)node.getLookup().lookup(Client.class);
224             if(client != null){
225                 JAXWSClientSupport support = JAXWSClientSupport.getJaxWsClientSupport(srcRoot);
226                 bindingsFolder = support.getBindingsFolderForClient(node.getName(), true);
227             } else{
228                 Service service = (Service)node.getLookup().lookup(Service.class);
229                 if(service != null){
230                     JAXWSSupport support = JAXWSSupport.getJAXWSSupport(srcRoot);
231                     bindingsFolder = support.getBindingsFolderForService(node.getName(), true);
232                 }
233             }
234             return bindingsFolder;
235         }
236     }
237     
238     void populateModel(){
239         model.setData(node);
240     }
241     
242     public static class EBTableModel extends AbstractTableModel JavaDoc{
243         
244         List JavaDoc<String JavaDoc> children;
245         public Object JavaDoc getValueAt(int row, int column) {
246             return children.get(row);
247         }
248         
249         public int getRowCount() {
250             if(children != null){
251                 return children.size();
252             }
253             return 0;
254         }
255         
256         public int getColumnCount() {
257             return columnName.length;
258         }
259         
260         public void removeRow(int row){
261             children.remove(row);
262             fireTableRowsDeleted(row, row);
263         }
264         
265         public void addRow(String JavaDoc value){
266             children.add(value);
267             fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
268         }
269         
270         public void setData(Node node){
271             children = new ArrayList JavaDoc<String JavaDoc>();
272             List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
273             Client client = (Client)node.getLookup().lookup(Client.class);
274             if(client != null){
275                 Binding[] bindings = client.getBindings();
276                 for(int i = 0; i < bindings.length; i++){
277                     list.add(bindings[i].getFileName());
278                 }
279             } else{
280                 Service service = (Service)node.getLookup().lookup(Service.class);
281                 if(service != null){
282                     Binding[] bindings = service.getBindings();
283                     for(int i = 0; i < bindings.length; i++){
284                         list.add(bindings[i].getFileName());
285                     }
286                 }
287             }
288             children.addAll(list);
289             this.fireTableDataChanged(); //do we need to do this?
290
}
291         
292         public String JavaDoc getColumnName(int column) {
293             return columnName[column];
294         }
295         
296         public List JavaDoc getChildren(){
297             return children;
298         }
299         
300     }
301     
302     private static class XmlFileFilter extends FileFilter JavaDoc {
303         public boolean accept(File JavaDoc f) {
304             boolean result;
305             if(f.isDirectory() || "xml".equalsIgnoreCase(FileUtil.getExtension(f.getName()))) { // NOI18N
306
result = true;
307             } else {
308                 result = false;
309             }
310             return result;
311         }
312         
313         public String JavaDoc getDescription() {
314            return NbBundle.getMessage(ExternalBindingTablePanel.class, "DESC_CUSTOMIZATION_FILE_FILTER");
315         }
316     }
317 }
318
Popular Tags