View Javadoc

1   /*
2    * Copyright (c) 2004-2005 by Michael Connor. All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    *  o Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    *
10   *  o Redistributions in binary form must reproduce the above copyright notice,
11   *    this list of conditions and the following disclaimer in the documentation
12   *    and/or other materials provided with the distribution.
13   *
14   *  o Neither the name of FormLayoutBuilder or Michael Connor nor the names of
15   *    its contributors may be used to endorse or promote products derived
16   *    from this software without specific prior written permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25   * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  
31  /***
32   *
33   */
34  package org.mlc.swing.layout;
35  
36  import java.awt.dnd.DnDConstants;
37  import java.awt.dnd.DragGestureEvent;
38  import java.awt.dnd.DragGestureListener;
39  import java.awt.dnd.DragSource;
40  import java.awt.dnd.DragSourceListener;
41  import java.awt.event.MouseEvent;
42  
43  import javax.swing.JList;
44  import javax.swing.ListModel;
45  import javax.swing.ListSelectionModel;
46  
47  class DndList extends JList implements DragSourceListener,
48      DragGestureListener
49  {
50    /***
51     *
52     */
53    private final FormEditor editor;
54    private static final long serialVersionUID = 1L;
55    protected DragSource fDragSource = null;
56  
57    public DndList(FormEditor editor, ListModel listModel)
58    {
59      super(listModel);
60      this.editor = editor;
61      fDragSource = new DragSource();
62      this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
63  
64      // it is very important that autoscrolls be set to false. i
65      // spent a lot of time tracking down a bug where there was
66      // very strange behavior involving selections and dragging with
67      // this list. as far as i can tell, the autoscroller was
68      // generating phantom events which i believe is what it is
69      // designed to do. i don't have an explanation for how this
70      // should be done but i can say if this line of code is not
71      // present then bad things will happen.
72      this.editor.setAutoscrolls(false);
73      fDragSource.createDefaultDragGestureRecognizer(this,
74          DnDConstants.ACTION_MOVE, this);
75    }
76  
77    public void dragDropEnd(java.awt.dnd.DragSourceDropEvent dragSourceDropEvent)
78    {
79    }
80  
81    public void dragEnter(java.awt.dnd.DragSourceDragEvent dragSourceDragEvent)
82    {
83    }
84  
85    public void dragExit(java.awt.dnd.DragSourceEvent dragSourceEvent)
86    {
87    }
88  
89    public void dragOver(java.awt.dnd.DragSourceDragEvent dragSourceDragEvent)
90    {
91    }
92  
93    public void dropActionChanged(
94        java.awt.dnd.DragSourceDragEvent dragSourceDragEvent)
95    {
96    }
97  
98    public void dragGestureRecognized(DragGestureEvent event)
99    {
100     int dragIndex = locationToIndex(event.getDragOrigin());
101     if (dragIndex >= 0)
102     {
103       Object draggingComponent = this.getModel().getElementAt(
104           dragIndex);
105       event.startDrag(java.awt.dnd.DragSource.DefaultCopyDrop,
106           new TransferableWrapper(draggingComponent), this);
107     }
108   }
109 
110   public String getToolTipText(MouseEvent evt)
111   {
112     // return a tooltip for the specific entry in the list
113       // Get item index
114       int index = locationToIndex(evt.getPoint());
115       if ( index == -1 )
116         return "";
117 
118       // Get item
119       Object o = this.getModel().getElementAt(index);
120       if ( o instanceof ComponentDef )
121       {
122         ComponentDef thisItem = (ComponentDef) o;
123         return thisItem.getDescription();
124       }
125       return "";
126   }
127 
128   public void drop(java.awt.dnd.DropTargetDropEvent e)
129   {
130   }
131 }