1
2
3
4
5
6
7
8
9
10
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 AttachmentsPanel 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 AttachmentsPanel(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.createSmallToolbar();
169
170 if (!container.isReadOnly())
171 {
172 addFileBtn = UISupport.createToolbarButton(UISupport.createImageIcon( "/add_property.gif" ));
173 removeBtn = UISupport.createToolbarButton(UISupport.createImageIcon( "/remove_property.gif" ));
174
175 addFileBtn.setToolTipText( "Adds an attachment to this request" );
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.addFixed(addFileBtn);
185
186 removeBtn.setToolTipText( "Removes the selected attachment from this request" );
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.addFixed(removeBtn);
197 }
198
199 exportBtn = UISupport.createToolbarButton(UISupport.createImageIcon( "/export.gif" ));
200 exportBtn.setToolTipText( "Exports the selected attachment to a file" );
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.addFixed(exportBtn);
211 jPanel1.addGlue();
212 jPanel1.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction(HelpUrls.ATTACHMENTS_HELP_URL)));
213 add(jPanel1, java.awt.BorderLayout.NORTH);
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 = container.getAttachmentAt(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
267 long total = Tools.writeAll( out, attachment.getInputStream() );
268 out.close();
269 if( UISupport.confirm( "Written [" + total + "] bytes to " + file.getName() + ", open in browser?", "Saved File" ))
270 {
271 Tools.openURL( file.toURI().toURL().toString() );
272 }
273 }
274 catch( Exception e )
275 {
276 UISupport.showErrorMessage( e );
277 }
278 }
279 }
280
281 private void addFileBtnActionPerformed(java.awt.event.ActionEvent evt)
282 {
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 }
308
309 private void removeBtnActionPerformed(java.awt.event.ActionEvent evt)
310 {
311 if (UISupport.confirm("Remove selected attachments?", "Remove Attachments"))
312 tableModel.removeAttachment(fileTable.getSelectedRows());
313 }
314
315
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
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 model = ((PartsComboBoxModel) ((JComboBox) editorComponent).getModel());
340 ((JComboBox) editorComponent).setModel( model.init(tableModel.getAttachmentAt(row)) );
341
342
343 return super.getTableCellEditorComponent(table, value, isSelected, row, column);
344 }
345 }
346
347 private final class PartsComboBoxModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener
348 {
349 private Attachment attachment;
350 private WsdlAttachmentPart[] parts;
351
352 public PartsComboBoxModel()
353 {
354 container.addAttachmentsChangeListener( this );
355 }
356
357 public void release()
358 {
359 container.removeAttachmentsChangeListener( this );
360 }
361
362 public PartsComboBoxModel init(Attachment attachment)
363 {
364 System.out.println( "Initializing parts..");
365 this.attachment = attachment;
366
367 int previousPartsCount = parts == null ? 0 : parts.length;
368
369 parts = container.getDefinedAttachmentParts();
370 if( previousPartsCount < parts.length )
371 {
372 fireIntervalAdded( this, previousPartsCount, parts.length );
373 }
374 else if( previousPartsCount > parts.length )
375 {
376 fireIntervalRemoved( this, parts.length-1, previousPartsCount );
377 }
378
379 fireContentsChanged( this, 0, parts.length-1 );
380
381 return this;
382 }
383
384 public Object getElementAt(int index)
385 {
386 return parts == null ? null : parts[index].getName();
387 }
388
389 public int getSize()
390 {
391 return parts == null ? 0 : parts.length;
392 }
393
394 public Object getSelectedItem()
395 {
396 return attachment == null ? null : attachment.getPart();
397 }
398
399 public void setSelectedItem(Object anItem)
400 {
401 if (attachment != null)
402 attachment.setPart((String) anItem);
403 }
404
405 public void propertyChange( PropertyChangeEvent arg0 )
406 {
407
408 if( arg0.getOldValue() == attachment && arg0.getNewValue() == null )
409 {
410 attachment = null;
411 parts = null;
412 }
413 }
414 }
415 }