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.request.components.editor.inspectors.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.SoapUI;
46  import com.eviware.soapui.impl.wsdl.AttachmentContainer;
47  import com.eviware.soapui.impl.wsdl.WsdlAttachmentPart;
48  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
49  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
50  import com.eviware.soapui.model.iface.Attachment;
51  import com.eviware.soapui.support.Tools;
52  import com.eviware.soapui.support.UISupport;
53  import com.eviware.soapui.support.components.JXToolBar;
54  
55  /***
56   * Utility Panel for displaying a table of attachments
57   * 
58   * @author emibre
59   */
60  
61  public class AttachmentPanel extends javax.swing.JPanel
62  {
63  	private DropTarget dropTarget;
64  	private FileTransferHandler fileTransferHandler;
65  	private AttachmentsTableModel tableModel;
66  	private JFileChooser fc;
67  	private final AttachmentContainer container;
68  	private JButton exportBtn;
69  
70  	/*** Creates new form FileTableList */
71  	public AttachmentPanel(AttachmentContainer container)
72  	{
73  		this.container = container;
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 (!container.isReadOnly())
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 							SoapUI.logError( e );
134 						}
135 					}
136 
137 					public void dropActionChanged(DropTargetDragEvent dtde)
138 					{
139 					}
140 				});
141 			}
142 			catch (Exception e)
143 			{
144 				SoapUI.logError( e );
145 			}
146 
147 			jScrollPane1.getViewport().setDropTarget(dropTarget);
148 		}
149 	}
150 
151 	private void initComponents()
152 	{
153 		jScrollPane1 = new javax.swing.JScrollPane();
154 		tableModel = new AttachmentsTableModel(container);
155 		fileTable = new JTable(tableModel);
156 
157 		if (!container.isReadOnly())
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 = UISupport.createToolbar();
169 		
170 		if (!container.isReadOnly())
171 		{
172 			addFileBtn = new javax.swing.JButton();
173 			removeBtn = new javax.swing.JButton();
174 
175 			addFileBtn.setIcon(UISupport.createImageIcon( "/add_property.gif" ));
176 			addFileBtn.setToolTipText( "Adds an attachment to this request" );
177 			addFileBtn.addActionListener(new java.awt.event.ActionListener()
178 			{
179 				public void actionPerformed(java.awt.event.ActionEvent evt)
180 				{
181 					addFileBtnActionPerformed(evt);
182 				}
183 			});
184 
185 			jPanel1.addFixed(addFileBtn);
186 
187 			removeBtn.setIcon(UISupport.createImageIcon( "/remove_property.gif" ));
188 			removeBtn.setToolTipText( "Removes the selected attachment from this request" );
189 			removeBtn.setEnabled(false);
190 			removeBtn.addActionListener(new java.awt.event.ActionListener()
191 			{
192 				public void actionPerformed(java.awt.event.ActionEvent evt)
193 				{
194 					removeBtnActionPerformed(evt);
195 				}
196 			});
197 
198 			jPanel1.addFixed(removeBtn);
199 		}
200 
201 		exportBtn = new javax.swing.JButton();
202 		exportBtn.setIcon(UISupport.createImageIcon( "/export.gif" ));
203 		exportBtn.setToolTipText( "Exports the selected attachment to a file" );
204 		exportBtn.setEnabled(false);
205 		exportBtn.addActionListener(new java.awt.event.ActionListener()
206 		{
207 			public void actionPerformed(java.awt.event.ActionEvent evt)
208 			{
209 				exportBtnActionPerformed(evt);
210 			}
211 		});
212 
213 		jPanel1.addFixed(exportBtn);
214 		jPanel1.addGlue();
215 		jPanel1.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction(HelpUrls.ATTACHMENTS_HELP_URL)));
216 		add(jPanel1, java.awt.BorderLayout.NORTH);
217 
218 		fileTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
219 		{
220 			public void valueChanged(ListSelectionEvent e)
221 			{
222 				if( removeBtn != null )
223 					removeBtn.setEnabled(fileTable.getSelectedRowCount() > 0);
224 				
225 				exportBtn.setEnabled(fileTable.getSelectedRowCount() > 0);
226 			}
227 		});
228 		
229 		fileTable.addMouseListener(new MouseAdapter()
230 		{
231 			public void mouseClicked(MouseEvent e)
232 			{
233 				if (e.getClickCount() < 2)
234 					return;
235 
236 				int ix = fileTable.getSelectedRow();
237 				if (ix == -1)
238 					return;
239 
240 				Attachment attachment = container.getAttachmentAt(ix);
241 				String url = attachment.getUrl();
242 				if (url != null)
243 				{
244 					Tools.openURL(url);
245 				}
246 				else
247 				{
248 					Toolkit.getDefaultToolkit().beep();
249 				}
250 			}
251 		});
252 	}
253 
254 	protected void exportBtnActionPerformed( ActionEvent evt )
255 	{
256 		File file = UISupport.getFileDialogs().saveAs( this, "Export Attachment.." );
257 		while( file != null && file.exists() && 
258 				 !UISupport.confirm( "File " + file.getName() + " exists, overwrite?", "Export Attachment" ))
259 		{
260 			file = UISupport.getFileDialogs().saveAs( this, "Export Attachment.." );
261 		}
262 		
263 		if( file != null )
264 		{
265 			Attachment attachment = tableModel.getAttachmentAt(fileTable.getSelectedRow());
266 			try
267 			{
268 				FileOutputStream out = new FileOutputStream( file );
269 				
270 				long total = Tools.writeAll( out, attachment.getInputStream() );
271 				out.close();
272 				UISupport.showInfoMessage( "Written [" + total + "] bytes to " + file.getName() );
273 			}
274 			catch( Exception e )
275 			{
276 				UISupport.showErrorMessage( e );
277 			}
278 		}
279 	}
280 
281 	private void addFileBtnActionPerformed(java.awt.event.ActionEvent evt)
282 	{// GEN-FIRST:event_addFileBtnActionPerformed
283 		if (fc == null)
284 			fc = new JFileChooser();
285 
286 		int returnVal = fc.showOpenDialog(this);
287 
288 		if (returnVal == JFileChooser.APPROVE_OPTION)
289 		{
290 			File file = fc.getSelectedFile();
291 			Boolean retval = UISupport.confirmOrCancel("Cache attachment in request?", "Att Attachment");
292 			if (retval == null)
293 				return;
294 			try
295 			{
296 				tableModel.addFile(file, retval);
297 			}
298 			catch (IOException e)
299 			{
300 				UISupport.showErrorMessage(e);
301 			}
302 		}
303 		else
304 		{
305 			System.out.println("Open command cancelled by user.");
306 		}
307 	}// GEN-LAST:event_addFileBtnActionPerformed
308 
309 	private void removeBtnActionPerformed(java.awt.event.ActionEvent evt)
310 	{// GEN-FIRST:event_removeBtnActionPerformed
311 		if (UISupport.confirm("Remove selected attachments?", "Remove Attachments"))
312 			tableModel.removeAttachment(fileTable.getSelectedRows());
313 	}// GEN-LAST:event_removeBtnActionPerformed
314 
315 	// Variables declaration - do not modify//GEN-BEGIN:variables
316 	private javax.swing.JButton addFileBtn;
317 	private JTable fileTable;
318 	private JXToolBar jPanel1;
319 	private javax.swing.JScrollPane jScrollPane1;
320 	private javax.swing.JButton removeBtn;
321 	private AttachmentPartCellEditor attachmentPartCellEditor;
322 
323 	// End of variables declaration//GEN-END:variables
324 
325 	private class AttachmentPartCellEditor extends DefaultCellEditor
326 	{
327 		public AttachmentPartCellEditor()
328 		{
329 			super(new JComboBox(new PartsComboBoxModel()));
330 		}
331 
332 		public void release()
333 		{
334 			((PartsComboBoxModel) ((JComboBox) editorComponent).getModel()).release();
335 		}
336 
337 		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
338 		{
339 			((PartsComboBoxModel) ((JComboBox) editorComponent).getModel()).init(tableModel.getAttachmentAt(row));
340 			return super.getTableCellEditorComponent(table, value, isSelected, row, column);
341 		}
342 	}
343 
344 	private final class PartsComboBoxModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener
345 	{
346 		private Attachment attachment;
347 		private WsdlAttachmentPart[] parts;
348 		
349 		public PartsComboBoxModel()
350 		{
351 			container.addAttachmentsChangeListener( this );
352 		}
353 
354 		public void release()
355 		{
356 			container.removeAttachmentsChangeListener( this );
357 		}
358 
359 		public void init(Attachment attachment)
360 		{
361 			System.out.println( "Initializing parts..");
362 			this.attachment = attachment;
363 			parts = container.getDefinedAttachmentParts();
364 		}
365 
366 		public Object getElementAt(int index)
367 		{
368 			return parts == null ? null : parts[index].getName();
369 		}
370 
371 		public int getSize()
372 		{
373 			return parts == null ? 0 : parts.length;
374 		}
375 
376 		public Object getSelectedItem()
377 		{
378 			return attachment == null ? null : attachment.getPart();
379 		}
380 
381 		public void setSelectedItem(Object anItem)
382 		{
383 			if (attachment != null)
384 				attachment.setPart((String) anItem);
385 		}
386 
387 		public void propertyChange( PropertyChangeEvent arg0 )
388 		{
389 			// delete our current one?
390 			if( arg0.getOldValue() == attachment && arg0.getNewValue() == null )
391 			{
392 				attachment = null;
393 				parts = null;
394 			}
395 		}
396 	}
397 }