View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.panels.attachments;
14  
15  import java.awt.Component;
16  import java.awt.Toolkit;
17  import java.awt.datatransfer.DataFlavor;
18  import java.awt.datatransfer.Transferable;
19  import java.awt.dnd.DnDConstants;
20  import java.awt.dnd.DropTarget;
21  import java.awt.dnd.DropTargetDragEvent;
22  import java.awt.dnd.DropTargetDropEvent;
23  import java.awt.dnd.DropTargetEvent;
24  import java.awt.dnd.DropTargetListener;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.MouseAdapter;
27  import java.awt.event.MouseEvent;
28  import java.beans.PropertyChangeEvent;
29  import java.beans.PropertyChangeListener;
30  import java.io.File;
31  import java.io.FileOutputStream;
32  import java.io.IOException;
33  import java.util.List;
34  
35  import javax.swing.AbstractListModel;
36  import javax.swing.ComboBoxModel;
37  import javax.swing.DefaultCellEditor;
38  import javax.swing.JButton;
39  import javax.swing.JComboBox;
40  import javax.swing.JFileChooser;
41  import javax.swing.JTable;
42  import javax.swing.event.ListSelectionEvent;
43  import javax.swing.event.ListSelectionListener;
44  
45  import com.eviware.soapui.impl.wsdl.WsdlAttachmentPart;
46  import com.eviware.soapui.impl.wsdl.WsdlRequest;
47  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
48  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
49  import com.eviware.soapui.model.iface.Attachment;
50  import com.eviware.soapui.support.Tools;
51  import com.eviware.soapui.support.UISupport;
52  
53  /***
54   * 
55   * @author emibre
56   */
57  public class AttachmentPanel extends javax.swing.JPanel
58  {
59  	private DropTarget dropTarget;
60  	private FileTransferHandler fileTransferHandler;
61  	private RequestAttachmentTableModel tableModel;
62  	private JFileChooser fc;
63  	private final WsdlRequest request;
64  	boolean allowChange = false;
65  	boolean isRequest = false;
66  	private JButton exportBtn;
67  
68  	/*** Creates new form FileTableList */
69  	public AttachmentPanel(WsdlRequest request, boolean isRequest)
70  	{
71  		this.request = request;
72  		this.allowChange = isRequest;
73  		this.isRequest = isRequest;
74  		initComponents();
75  		initFileTransfer();
76  	}
77  	
78  	public void release()
79  	{
80  		tableModel.release();
81  		if( attachmentPartCellEditor != null )
82  			attachmentPartCellEditor.release();
83  	}
84  
85  	private void initFileTransfer()
86  	{
87  		if (allowChange)
88  		{
89  			fileTransferHandler = new FileTransferHandler(tableModel);
90  			fileTable.setDragEnabled(true);
91  			fileTable.setTransferHandler(fileTransferHandler);
92  
93  			dropTarget = new DropTarget();
94  			dropTarget.setActive(true);
95  			try
96  			{
97  				dropTarget.addDropTargetListener(new DropTargetListener()
98  				{
99  					public void dragEnter(DropTargetDragEvent dtde)
100 					{
101 					}
102 
103 					public void dragExit(DropTargetEvent dte)
104 					{
105 					}
106 
107 					public void dragOver(DropTargetDragEvent dtde)
108 					{
109 					}
110 
111 					@SuppressWarnings("unchecked")
112 					public void drop(DropTargetDropEvent dtde)
113 					{
114 						try
115 						{
116 							dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
117 							Transferable trans = dtde.getTransferable();
118 							List<File> files = (List<File>) trans.getTransferData(DataFlavor.javaFileListFlavor);
119 							for (File f : files)
120 							{
121 								System.out.println("Dropping file: " + f.getName());
122 
123 								Boolean retval = UISupport.confirmOrCancel("Cache attachment in request?", "Att Attachment");
124 								if (retval == null)
125 									return;
126 
127 								tableModel.addFile(f, retval);
128 							}
129 
130 						}
131 						catch (Exception e)
132 						{
133 							e.printStackTrace();
134 						}
135 					}
136 
137 					public void dropActionChanged(DropTargetDragEvent dtde)
138 					{
139 					}
140 				});
141 			}
142 			catch (Exception e)
143 			{
144 				e.printStackTrace();
145 			}
146 
147 			jScrollPane1.getViewport().setDropTarget(dropTarget);
148 		}
149 	}
150 
151 	private void initComponents()
152 	{
153 		jScrollPane1 = new javax.swing.JScrollPane();
154 		tableModel = new RequestAttachmentTableModel(request, isRequest);
155 		fileTable = new JTable(tableModel);
156 
157 		if (isRequest)
158 		{
159 			attachmentPartCellEditor = new AttachmentPartCellEditor();
160 			fileTable.getColumnModel().getColumn(3).setCellEditor(attachmentPartCellEditor);
161 		}
162 
163 		setLayout(new java.awt.BorderLayout());
164 		jScrollPane1.setViewportView(fileTable);
165 
166 		add(jScrollPane1, java.awt.BorderLayout.CENTER);
167 
168 		jPanel1 = new javax.swing.JPanel();
169 		
170 		if (allowChange)
171 		{
172 			addFileBtn = new javax.swing.JButton();
173 			removeBtn = new javax.swing.JButton();
174 
175 			addFileBtn.setText("Add file");
176 			addFileBtn.addActionListener(new java.awt.event.ActionListener()
177 			{
178 				public void actionPerformed(java.awt.event.ActionEvent evt)
179 				{
180 					addFileBtnActionPerformed(evt);
181 				}
182 			});
183 
184 			jPanel1.add(addFileBtn);
185 
186 			removeBtn.setText("Remove selected");
187 			removeBtn.setEnabled(false);
188 			removeBtn.addActionListener(new java.awt.event.ActionListener()
189 			{
190 				public void actionPerformed(java.awt.event.ActionEvent evt)
191 				{
192 					removeBtnActionPerformed(evt);
193 				}
194 			});
195 
196 			jPanel1.add(removeBtn);
197 		}
198 
199 		exportBtn = new javax.swing.JButton();
200 		exportBtn.setText("Export selected");
201 		exportBtn.setEnabled(false);
202 		exportBtn.addActionListener(new java.awt.event.ActionListener()
203 		{
204 			public void actionPerformed(java.awt.event.ActionEvent evt)
205 			{
206 				exportBtnActionPerformed(evt);
207 			}
208 		});
209 
210 		jPanel1.add(exportBtn);
211 		jPanel1.add(new JButton(new ShowOnlineHelpAction(HelpUrls.ATTACHMENTS_HELP_URL)));
212 		add(jPanel1, java.awt.BorderLayout.SOUTH);
213 
214 		fileTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
215 		{
216 			public void valueChanged(ListSelectionEvent e)
217 			{
218 				if( removeBtn != null )
219 					removeBtn.setEnabled(fileTable.getSelectedRowCount() > 0);
220 				
221 				exportBtn.setEnabled(fileTable.getSelectedRowCount() > 0);
222 			}
223 		});
224 		
225 		fileTable.addMouseListener(new MouseAdapter()
226 		{
227 			public void mouseClicked(MouseEvent e)
228 			{
229 				if (e.getClickCount() < 2)
230 					return;
231 
232 				int ix = fileTable.getSelectedRow();
233 				if (ix == -1)
234 					return;
235 
236 				Attachment attachment = isRequest ? request.getAttachmentAt(ix)
237 						: request.getResponse().getAttachments()[ix];
238 				String url = attachment.getUrl();
239 				if (url != null)
240 				{
241 					Tools.openURL(url);
242 				}
243 				else
244 				{
245 					Toolkit.getDefaultToolkit().beep();
246 				}
247 			}
248 		});
249 	}
250 
251 	protected void exportBtnActionPerformed( ActionEvent evt )
252 	{
253 		File file = UISupport.getFileDialogs().saveAs( this, "Export Attachment.." );
254 		while( file != null && file.exists() && 
255 				 !UISupport.confirm( "File " + file.getName() + " exists, overwrite?", "Export Attachment" ))
256 		{
257 			file = UISupport.getFileDialogs().saveAs( this, "Export Attachment.." );
258 		}
259 		
260 		if( file != null )
261 		{
262 			Attachment attachment = tableModel.getAttachmentAt(fileTable.getSelectedRow());
263 			try
264 			{
265 				FileOutputStream out = new FileOutputStream( file );
266 				long total = Tools.writeAll( out, attachment.getInputStream() );
267 				out.close();
268 				UISupport.showInfoMessage( "Written [" + total + "] bytes to " + file.getName() );
269 			}
270 			catch( IOException e )
271 			{
272 				UISupport.showErrorMessage( e );
273 			}
274 		}
275 	}
276 
277 	private void addFileBtnActionPerformed(java.awt.event.ActionEvent evt)
278 	{// GEN-FIRST:event_addFileBtnActionPerformed
279 		if (fc == null)
280 			fc = new JFileChooser();
281 
282 		int returnVal = fc.showOpenDialog(this);
283 
284 		if (returnVal == JFileChooser.APPROVE_OPTION)
285 		{
286 			File file = fc.getSelectedFile();
287 			Boolean retval = UISupport.confirmOrCancel("Cache attachment in request?", "Att Attachment");
288 			if (retval == null)
289 				return;
290 			try
291 			{
292 				tableModel.addFile(file, retval);
293 			}
294 			catch (IOException e)
295 			{
296 				UISupport.showErrorMessage(e);
297 			}
298 		}
299 		else
300 		{
301 			System.out.println("Open command cancelled by user.");
302 		}
303 	}// GEN-LAST:event_addFileBtnActionPerformed
304 
305 	private void removeBtnActionPerformed(java.awt.event.ActionEvent evt)
306 	{// GEN-FIRST:event_removeBtnActionPerformed
307 		if (UISupport.confirm("Remove selected attachments?", "Remove Attachments"))
308 			tableModel.removeAttachment(fileTable.getSelectedRows());
309 	}// GEN-LAST:event_removeBtnActionPerformed
310 
311 	// Variables declaration - do not modify//GEN-BEGIN:variables
312 	private javax.swing.JButton addFileBtn;
313 	private JTable fileTable;
314 	private javax.swing.JPanel jPanel1;
315 	private javax.swing.JScrollPane jScrollPane1;
316 	private javax.swing.JButton removeBtn;
317 	private AttachmentPartCellEditor attachmentPartCellEditor;
318 
319 	// End of variables declaration//GEN-END:variables
320 
321 	private class AttachmentPartCellEditor extends DefaultCellEditor
322 	{
323 		public AttachmentPartCellEditor()
324 		{
325 			super(new JComboBox(new PartsComboBoxModel()));
326 		}
327 
328 		public void release()
329 		{
330 			((PartsComboBoxModel) ((JComboBox) editorComponent).getModel()).release();
331 		}
332 
333 		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
334 		{
335 			((PartsComboBoxModel) ((JComboBox) editorComponent).getModel()).init(tableModel.getAttachmentAt(row));
336 			return super.getTableCellEditorComponent(table, value, isSelected, row, column);
337 		}
338 	}
339 
340 	private final class PartsComboBoxModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener
341 	{
342 		private Attachment attachment;
343 		private WsdlAttachmentPart[] parts;
344 		
345 		public PartsComboBoxModel()
346 		{
347 			request.addPropertyChangeListener( this );
348 		}
349 
350 		public void release()
351 		{
352 			request.removePropertyChangeListener( this );
353 		}
354 
355 		public void init(Attachment attachment)
356 		{
357 			System.out.println( "Initializing parts..");
358 			this.attachment = attachment;
359 			parts = request.getDefinedAttachmentParts();
360 		}
361 
362 		public Object getElementAt(int index)
363 		{
364 			return parts == null ? null : parts[index].getName();
365 		}
366 
367 		public int getSize()
368 		{
369 			return parts == null ? 0 : parts.length;
370 		}
371 
372 		public Object getSelectedItem()
373 		{
374 			return attachment == null ? null : attachment.getPart();
375 		}
376 
377 		public void setSelectedItem(Object anItem)
378 		{
379 			if (attachment != null)
380 				attachment.setPart((String) anItem);
381 		}
382 
383 		public void propertyChange( PropertyChangeEvent arg0 )
384 		{
385 			if( arg0.getPropertyName().equals( WsdlRequest.ATTACHMENTS_PROPERTY ))
386 			{
387 				// delete our current one?
388 				if( arg0.getOldValue() == attachment && arg0.getNewValue() == null )
389 				{
390 					attachment = null;
391 					parts = null;
392 				}
393 			}
394 		}
395 	}
396 }