1 19 27 28 package org.netbeans.modules.websvc.customization.multiview; 29 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.io.File ; 33 import java.net.URI ; 34 import java.util.ArrayList ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.HashSet ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.Set ; 41 import javax.swing.JFileChooser ; 42 import javax.swing.filechooser.FileFilter ; 43 import javax.swing.table.AbstractTableModel ; 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 64 public class ExternalBindingTablePanel extends DefaultTablePanel{ 65 private static final String [] columnName = {NbBundle.getMessage(ExternalBindingTablePanel.class, 66 "TITLE_CUSTOMIZATION_FILES")}; 67 private EBTableModel model; 68 private String previousDirectory = ""; 69 private static final FileFilter XML_FILE_FILTER = new XmlFileFilter(); 70 private Node node; 71 private JaxWsModel jmodel; 72 private Map <String , FileObject> addedBindings; 73 private RemoveActionListener removeActionListener; 74 private AddActionListener addActionListener; 75 76 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); addedBindings = new HashMap <String , FileObject>(); 85 86 addActionListener = new AddActionListener(); 87 ActionListener addListener = (ActionListener )WeakListeners.create(ActionListener .class, 88 addActionListener, addButton); 89 addButton.addActionListener(addListener); 90 91 removeActionListener = new RemoveActionListener(); 92 ActionListener removeListener = (ActionListener )WeakListeners.create(ActionListener .class, 93 removeActionListener, removeButton); 94 removeButton.addActionListener(removeListener); 95 } 96 97 public String getRelativePathToWsdl(){ 98 String 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 (localWsdlFile.getURL().toExternalForm())); 121 }catch(Exception e){ 122 return "Unable to obtain relative path"; 123 } 124 125 return "../" + relativePath; 126 } 127 128 public Map <String , FileObject> getAddedBindings(){ 129 return addedBindings; 130 } 131 132 public Set <String > getRemovedBindings(){ 133 Set <String > bindingsRemoved = new HashSet <String >(); 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){ return Collections.emptySet(); 146 } 147 @SuppressWarnings ("unchecked") 148 List <String > bindingsInTable = model.getChildren(); 149 for(int i = 0; i < bindingsInModel.length; i++){ 150 String bindingInModel = bindingsInModel[i].getFileName(); 151 boolean found = false; 152 for(String 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 getChildren(){ 166 return model.getChildren(); 167 } 168 169 class RemoveActionListener implements ActionListener { 170 public void actionPerformed(ActionEvent e){ 171 int row = getTable().getSelectedRow(); 172 if(row == -1) return; 173 String fileName = (String )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 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 { 191 public void actionPerformed(ActionEvent e) { 192 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 chooser = new JFileChooser (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 bindingFile = chooser.getSelectedFile(); 208 if(bindingFile.exists()){ 209 FileObject bindingFO = FileUtil.toFileObject(bindingFile); 210 String 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 { 243 244 List <String > children; 245 public Object 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 value){ 266 children.add(value); 267 fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1); 268 } 269 270 public void setData(Node node){ 271 children = new ArrayList <String >(); 272 List <String > list = new ArrayList <String >(); 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(); } 291 292 public String getColumnName(int column) { 293 return columnName[column]; 294 } 295 296 public List getChildren(){ 297 return children; 298 } 299 300 } 301 302 private static class XmlFileFilter extends FileFilter { 303 public boolean accept(File f) { 304 boolean result; 305 if(f.isDirectory() || "xml".equalsIgnoreCase(FileUtil.getExtension(f.getName()))) { result = true; 307 } else { 308 result = false; 309 } 310 return result; 311 } 312 313 public String getDescription() { 314 return NbBundle.getMessage(ExternalBindingTablePanel.class, "DESC_CUSTOMIZATION_FILE_FILTER"); 315 } 316 } 317 } 318 | Popular Tags |