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.support.components;
    14
    15  /***
    16   * This is an example of a component, which serves as a DragSource as
    17   * well as Drop Target.
    18   * To illustrate the concept, JList has been used as a droppable target
    19   * and a draggable source.
    20   * Any component can be used instead of a JList.
    21   * The code also contains debugging messages which can be used for
    22   * diagnostics and understanding the flow of events.
    23   *
    24   * @version 1.0
    25   */
    26
    27  import java.awt.datatransfer.DataFlavor;
    28  import java.awt.datatransfer.StringSelection;
    29  import java.awt.datatransfer.Transferable;
    30  import java.awt.dnd.DnDConstants;
    31  import java.awt.dnd.DragGestureEvent;
    32  import java.awt.dnd.DragGestureListener;
    33  import java.awt.dnd.DragSource;
    34  import java.awt.dnd.DragSourceDragEvent;
    35  import java.awt.dnd.DragSourceDropEvent;
    36  import java.awt.dnd.DragSourceEvent;
    37  import java.awt.dnd.DragSourceListener;
    38  import java.awt.dnd.DropTarget;
    39  import java.awt.dnd.DropTargetDragEvent;
    40  import java.awt.dnd.DropTargetDropEvent;
    41  import java.awt.dnd.DropTargetEvent;
    42  import java.awt.dnd.DropTargetListener;
    43
    44  import javax.swing.DefaultListModel;
    45  import javax.swing.JList;
    46  import javax.swing.ListModel;
    47
    48  import com.eviware.soapui.SoapUI;
    49
    50
    51
    52
    53  public class DNDList extends JList
    54      implements DropTargetListener,DragSourceListener, DragGestureListener    {
    55
    56    /***
    57     * enables this component to be a dropTarget
    58     */
    59
    60    DropTarget dropTarget = null;
    61
    62    /***
    63     * enables this component to be a Drag Source
    64     */
    65    DragSource dragSource = null;
    66
    67
    68    /***
    69     * constructor - initializes the DropTarget and DragSource.
    70     */
    71
    72    public DNDList( ListModel dataModel ) {
    73      super( dataModel );
    74      dropTarget = new DropTarget (this, this);
    75      dragSource = new DragSource();
    76      dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this);
    77    }
    78
    79    /***
    80     * is invoked when you are dragging over the DropSite
    81     *
    82     */
    83
    84    public void dragEnter (DropTargetDragEvent event) {
    85
    86      // debug messages for diagnostics
    87      System.out.println( "dragEnter");
    88      event.acceptDrag (DnDConstants.ACTION_MOVE);
    89    }
    90
    91    /***
    92     * is invoked when you are exit the DropSite without dropping
    93     *
    94     */
    95
    96    public void dragExit (DropTargetEvent event) {
    97      System.out.println( "dragExit");
    98
    99    }
    100
    101   /***
    102    * is invoked when a drag operation is going on
    103    *
    104    */
    105
    106   public void dragOver (DropTargetDragEvent event) {
    107     System.out.println( "dragOver");
    108   }
    109
    110   /***
    111    * a drop has occurred
    112    *
    113    */
    114
    115
    116   public void drop (DropTargetDropEvent event) {
    117
    118     try {
    119         Transferable transferable = event.getTransferable();
    120
    121         // we accept only Strings
    122         if (transferable.isDataFlavorSupported (DataFlavor.stringFlavor)){
    123
    124             event.acceptDrop(DnDConstants.ACTION_MOVE);
    125             String s = (String)transferable.getTransferData ( DataFlavor.stringFlavor);
    126             addElement( s );
    127             event.getDropTargetContext().dropComplete(true);
    128         }
    129         else{
    130             event.rejectDrop();
    131         }
    132     }
    133     catch (Exception exception) {
    134    	 SoapUI.logError( exception );
    135         System.err.println( "Exception" + exception.getMessage());
    136         event.rejectDrop();
    137     }
    138   }
    139
    140   /***
    141    * is invoked if the use modifies the current drop gesture
    142    *
    143    */
    144
    145
    146   public void dropActionChanged ( DropTargetDragEvent event ) {
    147   }
    148
    149   /***
    150    * a drag gesture has been initiated
    151    *
    152    */
    153
    154   public void dragGestureRecognized( DragGestureEvent event) {
    155
    156     Object selected = getSelectedValue();
    157     if ( selected != null ){
    158         StringSelection text = new StringSelection( selected.toString());
    159
    160         // as the name suggests, starts the dragging
    161         dragSource.startDrag (event, DragSource.DefaultMoveDrop, text, this);
    162     } else {
    163         System.out.println( "nothing was selected");
    164     }
    165   }
    166
    167   /***
    168    * this message goes to DragSourceListener, informing it that the dragging
    169    * has ended
    170    *
    171    */
    172
    173   public void dragDropEnd (DragSourceDropEvent event) {
    174     if ( event.getDropSuccess()){
    175         removeElement();
    176     }
    177   }
    178
    179   /***
    180    * this message goes to DragSourceListener, informing it that the dragging
    181    * has entered the DropSite
    182    *
    183    */
    184
    185   public void dragEnter (DragSourceDragEvent event) {
    186     System.out.println( " dragEnter");
    187   }
    188
    189   /***
    190    * this message goes to DragSourceListener, informing it that the dragging
    191    * has exited the DropSite
    192    *
    193    */
    194
    195   public void dragExit (DragSourceEvent event) {
    196     System.out.println( "dragExit");
    197
    198   }
    199
    200   /***
    201    * this message goes to DragSourceListener, informing it that the dragging is currently
    202    * ocurring over the DropSite
    203    *
    204    */
    205
    206   public void dragOver (DragSourceDragEvent event) {
    207     System.out.println( "dragExit");
    208
    209   }
    210
    211   /***
    212    * is invoked when the user changes the dropAction
    213    *
    214    */
    215
    216   public void dropActionChanged ( DragSourceDragEvent event) {
    217     System.out.println( "dropActionChanged");
    218   }
    219
    220   /***
    221    * adds elements to itself
    222    *
    223    */
    224
    225    public void addElement( Object s ){
    226         (( DefaultListModel )getModel()).addElement (s.toString());
    227   }
    228
    229   /***
    230    * removes an element from itself
    231    */
    232
    233   public void removeElement(){
    234     (( DefaultListModel)getModel()).removeElement( getSelectedValue());
    235   }
    236
    237 }