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.SoapUI;
    46  import com.eviware.soapui.impl.wsdl.WsdlAttachmentPart;
    47  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    48  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
    49  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
    50  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    51  import com.eviware.soapui.model.iface.Attachment;
    52  import com.eviware.soapui.support.Tools;
    53  import com.eviware.soapui.support.UISupport;
    54
    55  /***
    56   * AttachmentPanel for WsdlMockResponse
    57   *
    58   * @author ole.matzura
    59   */
    60
    61  public class MockAttachmentPanel extends javax.swing.JPanel
    62  {
    63  	private DropTarget dropTarget;
    64  	private FileTransferHandler fileTransferHandler;
    65  	private MockAttachmentTableModel tableModel;
    66  	private JFileChooser fc;
    67  	private final WsdlMockResponse mockOperation;
    68  	boolean allowChange = false;
    69  	boolean isResponse = false;
    70  	private JButton exportBtn;
    71
    72  	/*** Creates new form FileTableList */
    73  	public MockAttachmentPanel(WsdlMockResponse response, boolean isResponse)
    74  	{
    75  		this.mockOperation = response;
    76  		this.allowChange = isResponse;
    77  		this.isResponse = isResponse;
    78  		initComponents();
    79  		initFileTransfer();
    80  	}
    81
    82  	public void release()
    83  	{
    84  		tableModel.release();
    85  		if( attachmentPartCellEditor != null )
    86  			attachmentPartCellEditor.release();
    87  	}
    88
    89  	private void initFileTransfer()
    90  	{
    91  		if (allowChange)
    92  		{
    93  			fileTransferHandler = new FileTransferHandler(tableModel);
    94  			fileTable.setDragEnabled(true);
    95  			fileTable.setTransferHandler(fileTransferHandler);
    96
    97  			dropTarget = new DropTarget();
    98  			dropTarget.setActive(true);
    99  			try
    100 			{
    101 				dropTarget.addDropTargetListener(new DropTargetListener()
    102 				{
    103 					public void dragEnter(DropTargetDragEvent dtde)
    104 					{
    105 					}
    106
    107 					public void dragExit(DropTargetEvent dte)
    108 					{
    109 					}
    110
    111 					public void dragOver(DropTargetDragEvent dtde)
    112 					{
    113 					}
    114
    115 					@SuppressWarnings("unchecked")
    116 					public void drop(DropTargetDropEvent dtde)
    117 					{
    118 						try
    119 						{
    120 							dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    121 							Transferable trans = dtde.getTransferable();
    122 							List<File> files = (List<File>) trans.getTransferData(DataFlavor.javaFileListFlavor);
    123 							for (File f : files)
    124 							{
    125 								System.out.println("Dropping file: " + f.getName());
    126
    127 								Boolean retval = UISupport.confirmOrCancel("Cache attachment in response?", "Att Attachment");
    128 								if (retval == null)
    129 									return;
    130
    131 								tableModel.addFile(f, retval);
    132 							}
    133
    134 						}
    135 						catch (Exception e)
    136 						{
    137 							SoapUI.logError( e );
    138 						}
    139 					}
    140
    141 					public void dropActionChanged(DropTargetDragEvent dtde)
    142 					{
    143 					}
    144 				});
    145 			}
    146 			catch (Exception e)
    147 			{
    148 				SoapUI.logError( e );
    149 			}
    150
    151 			jScrollPane1.getViewport().setDropTarget(dropTarget);
    152 		}
    153 	}
    154
    155 	private void initComponents()
    156 	{
    157 		jScrollPane1 = new javax.swing.JScrollPane();
    158 		tableModel = new MockAttachmentTableModel(mockOperation, isResponse);
    159 		fileTable = new JTable(tableModel);
    160
    161 		if (isResponse)
    162 		{
    163 			attachmentPartCellEditor = new AttachmentPartCellEditor();
    164 			fileTable.getColumnModel().getColumn(3).setCellEditor(attachmentPartCellEditor);
    165 		}
    166
    167 		setLayout(new java.awt.BorderLayout());
    168 		jScrollPane1.setViewportView(fileTable);
    169
    170 		add(jScrollPane1, java.awt.BorderLayout.CENTER);
    171
    172 		jPanel1 = new javax.swing.JPanel();
    173
    174 		if (allowChange)
    175 		{
    176 			addFileBtn = new javax.swing.JButton();
    177 			removeBtn = new javax.swing.JButton();
    178
    179 			addFileBtn.setText("Add file");
    180 			addFileBtn.addActionListener(new java.awt.event.ActionListener()
    181 			{
    182 				public void actionPerformed(java.awt.event.ActionEvent evt)
    183 				{
    184 					addFileBtnActionPerformed(evt);
    185 				}
    186 			});
    187
    188 			jPanel1.add(addFileBtn);
    189
    190 			removeBtn.setText("Remove selected");
    191 			removeBtn.setEnabled(false);
    192 			removeBtn.addActionListener(new java.awt.event.ActionListener()
    193 			{
    194 				public void actionPerformed(java.awt.event.ActionEvent evt)
    195 				{
    196 					removeBtnActionPerformed(evt);
    197 				}
    198 			});
    199
    200 			jPanel1.add(removeBtn);
    201 		}
    202
    203 		exportBtn = new javax.swing.JButton();
    204 		exportBtn.setText("Export selected");
    205 		exportBtn.setEnabled(false);
    206 		exportBtn.addActionListener(new java.awt.event.ActionListener()
    207 		{
    208 			public void actionPerformed(java.awt.event.ActionEvent evt)
    209 			{
    210 				exportBtnActionPerformed(evt);
    211 			}
    212 		});
    213
    214 		jPanel1.add(exportBtn);
    215 		jPanel1.add(new JButton(new ShowOnlineHelpAction(HelpUrls.ATTACHMENTS_HELP_URL)));
    216 		add(jPanel1, java.awt.BorderLayout.SOUTH);
    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 = isResponse ? mockOperation.getAttachments()[ix]
    241 						: mockOperation.getMockResult().getMockRequest().getRequestAttachments()[ix];
    242 				String url = attachment.getUrl();
    243 				if (url != null)
    244 				{
    245 					Tools.openURL(url);
    246 				}
    247 				else
    248 				{
    249 					Toolkit.getDefaultToolkit().beep();
    250 				}
    251 			}
    252 		});
    253 	}
    254
    255 	protected void exportBtnActionPerformed( ActionEvent evt )
    256 	{
    257 		File file = UISupport.getFileDialogs().saveAs( this, "Export Attachment.." );
    258 		while( file != null && file.exists() &&
    259 				 !UISupport.confirm( "File " + file.getName() + " exists, overwrite?", "Export Attachment" ))
    260 		{
    261 			file = UISupport.getFileDialogs().saveAs( this, "Export Attachment.." );
    262 		}
    263
    264 		if( file != null )
    265 		{
    266 			Attachment attachment = tableModel.getAttachmentAt(fileTable.getSelectedRow());
    267 			try
    268 			{
    269 				FileOutputStream out = new FileOutputStream( file );
    270 				long total = Tools.writeAll( out, attachment.getInputStream() );
    271 				UISupport.showInfoMessage( "Written [" + total + "] bytes to " + file.getName() );
    272 			}
    273 			catch( Exception e )
    274 			{
    275 				UISupport.showErrorMessage( e );
    276 			}
    277 		}
    278 	}
    279
    280 	private void addFileBtnActionPerformed(java.awt.event.ActionEvent evt)
    281 	{// GEN-FIRST:event_addFileBtnActionPerformed
    282 		if (fc == null)
    283 			fc = new JFileChooser();
    284
    285 		int returnVal = fc.showOpenDialog(this);
    286
    287 		if (returnVal == JFileChooser.APPROVE_OPTION)
    288 		{
    289 			File file = fc.getSelectedFile();
    290 			Boolean retval = UISupport.confirmOrCancel("Cache attachment in request?", "Att Attachment");
    291 			if (retval == null)
    292 				return;
    293 			try
    294 			{
    295 				tableModel.addFile(file, retval);
    296 			}
    297 			catch (IOException e)
    298 			{
    299 				UISupport.showErrorMessage(e);
    300 			}
    301 		}
    302 		else
    303 		{
    304 			System.out.println("Open command cancelled by user.");
    305 		}
    306 	}// GEN-LAST:event_addFileBtnActionPerformed
    307
    308 	private void removeBtnActionPerformed(java.awt.event.ActionEvent evt)
    309 	{// GEN-FIRST:event_removeBtnActionPerformed
    310 		if (UISupport.confirm("Remove selected attachments?", "Remove Attachments"))
    311 			tableModel.removeAttachment(fileTable.getSelectedRows());
    312 	}// GEN-LAST:event_removeBtnActionPerformed
    313
    314 	// Variables declaration - do not modify//GEN-BEGIN:variables
    315 	private javax.swing.JButton addFileBtn;
    316 	private JTable fileTable;
    317 	private javax.swing.JPanel jPanel1;
    318 	private javax.swing.JScrollPane jScrollPane1;
    319 	private javax.swing.JButton removeBtn;
    320 	private AttachmentPartCellEditor attachmentPartCellEditor;
    321
    322 	// End of variables declaration//GEN-END:variables
    323
    324 	private class AttachmentPartCellEditor extends DefaultCellEditor
    325 	{
    326 		public AttachmentPartCellEditor()
    327 		{
    328 			super(new JComboBox(new PartsComboBoxModel()));
    329 		}
    330
    331 		public void release()
    332 		{
    333 			((PartsComboBoxModel) ((JComboBox) editorComponent).getModel()).release();
    334 		}
    335
    336 		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    337 		{
    338 			((PartsComboBoxModel) ((JComboBox) editorComponent).getModel()).init(tableModel.getAttachmentAt(row));
    339 			return super.getTableCellEditorComponent(table, value, isSelected, row, column);
    340 		}
    341 	}
    342
    343 	private final class PartsComboBoxModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener
    344 	{
    345 		private Attachment attachment;
    346 		private WsdlAttachmentPart[] parts;
    347
    348 		public PartsComboBoxModel()
    349 		{
    350 			mockOperation.addPropertyChangeListener( this );
    351 		}
    352
    353 		public void release()
    354 		{
    355 			mockOperation.removePropertyChangeListener( this );
    356 		}
    357
    358 		public void init(Attachment attachment)
    359 		{
    360 			System.out.println( "Initializing parts..");
    361 			this.attachment = attachment;
    362 			parts = mockOperation.getDefinedAttachmentParts();
    363 		}
    364
    365 		public Object getElementAt(int index)
    366 		{
    367 			return parts == null ? null : parts[index].getName();
    368 		}
    369
    370 		public int getSize()
    371 		{
    372 			return parts == null ? 0 : parts.length;
    373 		}
    374
    375 		public Object getSelectedItem()
    376 		{
    377 			return attachment == null ? null : attachment.getPart();
    378 		}
    379
    380 		public void setSelectedItem(Object anItem)
    381 		{
    382 			if (attachment != null)
    383 				attachment.setPart((String) anItem);
    384 		}
    385
    386 		public void propertyChange( PropertyChangeEvent arg0 )
    387 		{
    388 			if( arg0.getPropertyName().equals( WsdlRequest.ATTACHMENTS_PROPERTY ))
    389 			{
    390 				// delete our current one?
    391 				if( arg0.getOldValue() == attachment && arg0.getNewValue() == null )
    392 				{
    393 					attachment = null;
    394 					parts = null;
    395 				}
    396 			}
    397 		}
    398 	}
    399 }