View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.mock.WsdlMockResponse;
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  
54  /***
55   * 
56   * @author emibre
57   */
58  public class MockAttachmentPanel extends javax.swing.JPanel
59  {
60  	private DropTarget dropTarget;
61  	private FileTransferHandler fileTransferHandler;
62  	private MockAttachmentTableModel tableModel;
63  	private JFileChooser fc;
64  	private final WsdlMockResponse mockOperation;
65  	boolean allowChange = false;
66  	boolean isResponse = false;
67  	private JButton exportBtn;
68  
69  	/*** Creates new form FileTableList */
70  	public MockAttachmentPanel(WsdlMockResponse response, boolean isResponse)
71  	{
72  		this.mockOperation = response;
73  		this.allowChange = isResponse;
74  		this.isResponse = isResponse;
75  		initComponents();
76  		initFileTransfer();
77  	}
78  	
79  	public void release()
80  	{
81  		tableModel.release();
82  		if( attachmentPartCellEditor != null )
83  			attachmentPartCellEditor.release();
84  	}
85  
86  	private void initFileTransfer()
87  	{
88  		if (allowChange)
89  		{
90  			fileTransferHandler = new FileTransferHandler(tableModel);
91  			fileTable.setDragEnabled(true);
92  			fileTable.setTransferHandler(fileTransferHandler);
93  
94  			dropTarget = new DropTarget();
95  			dropTarget.setActive(true);
96  			try
97  			{
98  				dropTarget.addDropTargetListener(new DropTargetListener()
99  				{
100 					public void dragEnter(DropTargetDragEvent dtde)
101 					{
102 					}
103 
104 					public void dragExit(DropTargetEvent dte)
105 					{
106 					}
107 
108 					public void dragOver(DropTargetDragEvent dtde)
109 					{
110 					}
111 
112 					@SuppressWarnings("unchecked")
113 					public void drop(DropTargetDropEvent dtde)
114 					{
115 						try
116 						{
117 							dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
118 							Transferable trans = dtde.getTransferable();
119 							List<File> files = (List<File>) trans.getTransferData(DataFlavor.javaFileListFlavor);
120 							for (File f : files)
121 							{
122 								System.out.println("Dropping file: " + f.getName());
123 
124 								Boolean retval = UISupport.confirmOrCancel("Cache attachment in response?", "Att Attachment");
125 								if (retval == null)
126 									return;
127 
128 								tableModel.addFile(f, retval);
129 							}
130 
131 						}
132 						catch (Exception e)
133 						{
134 							e.printStackTrace();
135 						}
136 					}
137 
138 					public void dropActionChanged(DropTargetDragEvent dtde)
139 					{
140 					}
141 				});
142 			}
143 			catch (Exception e)
144 			{
145 				e.printStackTrace();
146 			}
147 
148 			jScrollPane1.getViewport().setDropTarget(dropTarget);
149 		}
150 	}
151 
152 	private void initComponents()
153 	{
154 		jScrollPane1 = new javax.swing.JScrollPane();
155 		tableModel = new MockAttachmentTableModel(mockOperation, isResponse);
156 		fileTable = new JTable(tableModel);
157 
158 		if (isResponse)
159 		{
160 			attachmentPartCellEditor = new AttachmentPartCellEditor();
161 			fileTable.getColumnModel().getColumn(3).setCellEditor(attachmentPartCellEditor);
162 		}
163 
164 		setLayout(new java.awt.BorderLayout());
165 		jScrollPane1.setViewportView(fileTable);
166 
167 		add(jScrollPane1, java.awt.BorderLayout.CENTER);
168 
169 		jPanel1 = new javax.swing.JPanel();
170 		
171 		if (allowChange)
172 		{
173 			addFileBtn = new javax.swing.JButton();
174 			removeBtn = new javax.swing.JButton();
175 
176 			addFileBtn.setText("Add file");
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.add(addFileBtn);
186 
187 			removeBtn.setText("Remove selected");
188 			removeBtn.setEnabled(false);
189 			removeBtn.addActionListener(new java.awt.event.ActionListener()
190 			{
191 				public void actionPerformed(java.awt.event.ActionEvent evt)
192 				{
193 					removeBtnActionPerformed(evt);
194 				}
195 			});
196 
197 			jPanel1.add(removeBtn);
198 		}
199 
200 		exportBtn = new javax.swing.JButton();
201 		exportBtn.setText("Export selected");
202 		exportBtn.setEnabled(false);
203 		exportBtn.addActionListener(new java.awt.event.ActionListener()
204 		{
205 			public void actionPerformed(java.awt.event.ActionEvent evt)
206 			{
207 				exportBtnActionPerformed(evt);
208 			}
209 		});
210 
211 		jPanel1.add(exportBtn);
212 		jPanel1.add(new JButton(new ShowOnlineHelpAction(HelpUrls.ATTACHMENTS_HELP_URL)));
213 		add(jPanel1, java.awt.BorderLayout.SOUTH);
214 
215 		fileTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
216 		{
217 			public void valueChanged(ListSelectionEvent e)
218 			{
219 				if( removeBtn != null )
220 					removeBtn.setEnabled(fileTable.getSelectedRowCount() > 0);
221 				
222 				exportBtn.setEnabled(fileTable.getSelectedRowCount() > 0);
223 			}
224 		});
225 		
226 		fileTable.addMouseListener(new MouseAdapter()
227 		{
228 			public void mouseClicked(MouseEvent e)
229 			{
230 				if (e.getClickCount() < 2)
231 					return;
232 
233 				int ix = fileTable.getSelectedRow();
234 				if (ix == -1)
235 					return;
236 
237 				Attachment attachment = isResponse ? mockOperation.getAttachments()[ix]
238 						: mockOperation.getMockResult().getMockRequest().getRequestAttachments()[ix];
239 				String url = attachment.getUrl();
240 				if (url != null)
241 				{
242 					Tools.openURL(url);
243 				}
244 				else
245 				{
246 					Toolkit.getDefaultToolkit().beep();
247 				}
248 			}
249 		});
250 	}
251 
252 	protected void exportBtnActionPerformed( ActionEvent evt )
253 	{
254 		File file = UISupport.getFileDialogs().saveAs( this, "Export Attachment.." );
255 		while( file != null && file.exists() && 
256 				 !UISupport.confirm( "File " + file.getName() + " exists, overwrite?", "Export Attachment" ))
257 		{
258 			file = UISupport.getFileDialogs().saveAs( this, "Export Attachment.." );
259 		}
260 		
261 		if( file != null )
262 		{
263 			Attachment attachment = tableModel.getAttachmentAt(fileTable.getSelectedRow());
264 			try
265 			{
266 				FileOutputStream out = new FileOutputStream( file );
267 				long total = Tools.writeAll( out, attachment.getInputStream() );
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 			mockOperation.addPropertyChangeListener( this );
348 		}
349 
350 		public void release()
351 		{
352 			mockOperation.removePropertyChangeListener( this );
353 		}
354 
355 		public void init(Attachment attachment)
356 		{
357 			System.out.println( "Initializing parts..");
358 			this.attachment = attachment;
359 			parts = mockOperation.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 }