001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.panels.attachments;
014:
015: import java.awt.Component;
016: import java.awt.Toolkit;
017: import java.awt.datatransfer.DataFlavor;
018: import java.awt.datatransfer.Transferable;
019: import java.awt.dnd.DnDConstants;
020: import java.awt.dnd.DropTarget;
021: import java.awt.dnd.DropTargetDragEvent;
022: import java.awt.dnd.DropTargetDropEvent;
023: import java.awt.dnd.DropTargetEvent;
024: import java.awt.dnd.DropTargetListener;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.MouseAdapter;
027: import java.awt.event.MouseEvent;
028: import java.beans.PropertyChangeEvent;
029: import java.beans.PropertyChangeListener;
030: import java.io.File;
031: import java.io.FileOutputStream;
032: import java.io.IOException;
033: import java.util.List;
034:
035: import javax.swing.AbstractListModel;
036: import javax.swing.ComboBoxModel;
037: import javax.swing.DefaultCellEditor;
038: import javax.swing.JButton;
039: import javax.swing.JComboBox;
040: import javax.swing.JFileChooser;
041: import javax.swing.JTable;
042: import javax.swing.event.ListSelectionEvent;
043: import javax.swing.event.ListSelectionListener;
044:
045: import com.eviware.soapui.SoapUI;
046: import com.eviware.soapui.impl.wsdl.WsdlAttachmentPart;
047: import com.eviware.soapui.impl.wsdl.WsdlRequest;
048: import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
049: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
050: import com.eviware.soapui.model.iface.Attachment;
051: import com.eviware.soapui.support.Tools;
052: import com.eviware.soapui.support.UISupport;
053: import com.eviware.soapui.support.components.JXToolBar;
054:
055: /**
056: * Utility Panel for displaying a table of attachments
057: *
058: * @author emibre
059: */
060:
061: public class AttachmentPanel extends javax.swing.JPanel {
062: private DropTarget dropTarget;
063: private FileTransferHandler fileTransferHandler;
064: private RequestAttachmentTableModel tableModel;
065: private JFileChooser fc;
066: private final WsdlRequest request;
067: boolean allowChange = false;
068: boolean isRequest = false;
069: private JButton exportBtn;
070:
071: /** Creates new form FileTableList */
072: public AttachmentPanel(WsdlRequest request, boolean isRequest) {
073: this .request = request;
074: this .allowChange = isRequest;
075: this .isRequest = isRequest;
076: initComponents();
077: initFileTransfer();
078: }
079:
080: public void release() {
081: tableModel.release();
082: if (attachmentPartCellEditor != null)
083: attachmentPartCellEditor.release();
084: }
085:
086: private void initFileTransfer() {
087: if (allowChange) {
088: fileTransferHandler = new FileTransferHandler(tableModel);
089: fileTable.setDragEnabled(true);
090: fileTable.setTransferHandler(fileTransferHandler);
091:
092: dropTarget = new DropTarget();
093: dropTarget.setActive(true);
094: try {
095: dropTarget
096: .addDropTargetListener(new DropTargetListener() {
097: public void dragEnter(
098: DropTargetDragEvent dtde) {
099: }
100:
101: public void dragExit(DropTargetEvent dte) {
102: }
103:
104: public void dragOver(
105: DropTargetDragEvent dtde) {
106: }
107:
108: @SuppressWarnings("unchecked")
109: public void drop(DropTargetDropEvent dtde) {
110: try {
111: dtde
112: .acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
113: Transferable trans = dtde
114: .getTransferable();
115: List<File> files = (List<File>) trans
116: .getTransferData(DataFlavor.javaFileListFlavor);
117: for (File f : files) {
118: System.out
119: .println("Dropping file: "
120: + f.getName());
121:
122: Boolean retval = UISupport
123: .confirmOrCancel(
124: "Cache attachment in request?",
125: "Att Attachment");
126: if (retval == null)
127: return;
128:
129: tableModel.addFile(f, retval);
130: }
131:
132: } catch (Exception e) {
133: SoapUI.logError(e);
134: }
135: }
136:
137: public void dropActionChanged(
138: DropTargetDragEvent dtde) {
139: }
140: });
141: } catch (Exception e) {
142: SoapUI.logError(e);
143: }
144:
145: jScrollPane1.getViewport().setDropTarget(dropTarget);
146: }
147: }
148:
149: private void initComponents() {
150: jScrollPane1 = new javax.swing.JScrollPane();
151: tableModel = new RequestAttachmentTableModel(request, isRequest);
152: fileTable = new JTable(tableModel);
153:
154: if (isRequest) {
155: attachmentPartCellEditor = new AttachmentPartCellEditor();
156: fileTable.getColumnModel().getColumn(3).setCellEditor(
157: attachmentPartCellEditor);
158: }
159:
160: setLayout(new java.awt.BorderLayout());
161: jScrollPane1.setViewportView(fileTable);
162:
163: add(jScrollPane1, java.awt.BorderLayout.CENTER);
164:
165: jPanel1 = UISupport.createToolbar();
166:
167: if (allowChange) {
168: addFileBtn = new javax.swing.JButton();
169: removeBtn = new javax.swing.JButton();
170:
171: addFileBtn.setText("Add file");
172: addFileBtn
173: .addActionListener(new java.awt.event.ActionListener() {
174: public void actionPerformed(
175: java.awt.event.ActionEvent evt) {
176: addFileBtnActionPerformed(evt);
177: }
178: });
179:
180: jPanel1.addFixed(addFileBtn);
181:
182: removeBtn.setText("Remove selected");
183: removeBtn.setEnabled(false);
184: removeBtn
185: .addActionListener(new java.awt.event.ActionListener() {
186: public void actionPerformed(
187: java.awt.event.ActionEvent evt) {
188: removeBtnActionPerformed(evt);
189: }
190: });
191:
192: jPanel1.addFixed(removeBtn);
193: }
194:
195: exportBtn = new javax.swing.JButton();
196: exportBtn.setText("Export selected");
197: exportBtn.setEnabled(false);
198: exportBtn
199: .addActionListener(new java.awt.event.ActionListener() {
200: public void actionPerformed(
201: java.awt.event.ActionEvent evt) {
202: exportBtnActionPerformed(evt);
203: }
204: });
205:
206: jPanel1.addFixed(exportBtn);
207: jPanel1.addGlue();
208: jPanel1.addFixed(UISupport
209: .createToolbarButton(new ShowOnlineHelpAction(
210: HelpUrls.ATTACHMENTS_HELP_URL)));
211: add(jPanel1, java.awt.BorderLayout.SOUTH);
212:
213: fileTable.getSelectionModel().addListSelectionListener(
214: new ListSelectionListener() {
215: public void valueChanged(ListSelectionEvent e) {
216: if (removeBtn != null)
217: removeBtn.setEnabled(fileTable
218: .getSelectedRowCount() > 0);
219:
220: exportBtn.setEnabled(fileTable
221: .getSelectedRowCount() > 0);
222: }
223: });
224:
225: fileTable.addMouseListener(new MouseAdapter() {
226: public void mouseClicked(MouseEvent e) {
227: if (e.getClickCount() < 2)
228: return;
229:
230: int ix = fileTable.getSelectedRow();
231: if (ix == -1)
232: return;
233:
234: Attachment attachment = isRequest ? request
235: .getAttachmentAt(ix) : request.getResponse()
236: .getAttachments()[ix];
237: String url = attachment.getUrl();
238: if (url != null) {
239: Tools.openURL(url);
240: } else {
241: Toolkit.getDefaultToolkit().beep();
242: }
243: }
244: });
245: }
246:
247: protected void exportBtnActionPerformed(ActionEvent evt) {
248: File file = UISupport.getFileDialogs().saveAs(this ,
249: "Export Attachment..");
250: while (file != null
251: && file.exists()
252: && !UISupport.confirm("File " + file.getName()
253: + " exists, overwrite?", "Export Attachment")) {
254: file = UISupport.getFileDialogs().saveAs(this ,
255: "Export Attachment..");
256: }
257:
258: if (file != null) {
259: Attachment attachment = tableModel
260: .getAttachmentAt(fileTable.getSelectedRow());
261: try {
262: FileOutputStream out = new FileOutputStream(file);
263:
264: long total = Tools.writeAll(out, attachment
265: .getInputStream());
266: out.close();
267: UISupport.showInfoMessage("Written [" + total
268: + "] bytes to " + file.getName());
269: } catch (Exception e) {
270: UISupport.showErrorMessage(e);
271: }
272: }
273: }
274:
275: private void addFileBtnActionPerformed(
276: java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addFileBtnActionPerformed
277: if (fc == null)
278: fc = new JFileChooser();
279:
280: int returnVal = fc.showOpenDialog(this );
281:
282: if (returnVal == JFileChooser.APPROVE_OPTION) {
283: File file = fc.getSelectedFile();
284: Boolean retval = UISupport.confirmOrCancel(
285: "Cache attachment in request?", "Att Attachment");
286: if (retval == null)
287: return;
288: try {
289: tableModel.addFile(file, retval);
290: } catch (IOException e) {
291: UISupport.showErrorMessage(e);
292: }
293: } else {
294: System.out.println("Open command cancelled by user.");
295: }
296: }// GEN-LAST:event_addFileBtnActionPerformed
297:
298: private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_removeBtnActionPerformed
299: if (UISupport.confirm("Remove selected attachments?",
300: "Remove Attachments"))
301: tableModel.removeAttachment(fileTable.getSelectedRows());
302: }// GEN-LAST:event_removeBtnActionPerformed
303:
304: // Variables declaration - do not modify//GEN-BEGIN:variables
305: private javax.swing.JButton addFileBtn;
306: private JTable fileTable;
307: private JXToolBar jPanel1;
308: private javax.swing.JScrollPane jScrollPane1;
309: private javax.swing.JButton removeBtn;
310: private AttachmentPartCellEditor attachmentPartCellEditor;
311:
312: // End of variables declaration//GEN-END:variables
313:
314: private class AttachmentPartCellEditor extends DefaultCellEditor {
315: public AttachmentPartCellEditor() {
316: super (new JComboBox(new PartsComboBoxModel()));
317: }
318:
319: public void release() {
320: ((PartsComboBoxModel) ((JComboBox) editorComponent)
321: .getModel()).release();
322: }
323:
324: public Component getTableCellEditorComponent(JTable table,
325: Object value, boolean isSelected, int row, int column) {
326: ((PartsComboBoxModel) ((JComboBox) editorComponent)
327: .getModel()).init(tableModel.getAttachmentAt(row));
328: return super .getTableCellEditorComponent(table, value,
329: isSelected, row, column);
330: }
331: }
332:
333: private final class PartsComboBoxModel extends AbstractListModel
334: implements ComboBoxModel, PropertyChangeListener {
335: private Attachment attachment;
336: private WsdlAttachmentPart[] parts;
337:
338: public PartsComboBoxModel() {
339: request.addPropertyChangeListener(this );
340: }
341:
342: public void release() {
343: request.removePropertyChangeListener(this );
344: }
345:
346: public void init(Attachment attachment) {
347: System.out.println("Initializing parts..");
348: this .attachment = attachment;
349: parts = request.getDefinedAttachmentParts();
350: }
351:
352: public Object getElementAt(int index) {
353: return parts == null ? null : parts[index].getName();
354: }
355:
356: public int getSize() {
357: return parts == null ? 0 : parts.length;
358: }
359:
360: public Object getSelectedItem() {
361: return attachment == null ? null : attachment.getPart();
362: }
363:
364: public void setSelectedItem(Object anItem) {
365: if (attachment != null)
366: attachment.setPart((String) anItem);
367: }
368:
369: public void propertyChange(PropertyChangeEvent arg0) {
370: if (arg0.getPropertyName().equals(
371: WsdlRequest.ATTACHMENTS_PROPERTY)) {
372: // delete our current one?
373: if (arg0.getOldValue() == attachment
374: && arg0.getNewValue() == null) {
375: attachment = null;
376: parts = null;
377: }
378: }
379: }
380: }
381: }
|