001/*
002 * Copyright (c) 2004-2007 by Michael Connor. All Rights Reserved.
003 *
004 * Redistribution and use in source and binary forms, with or without
005 * modification, are permitted provided that the following conditions are met:
006 *
007 *  o Redistributions of source code must retain the above copyright notice,
008 *    this list of conditions and the following disclaimer.
009 *
010 *  o Redistributions in binary form must reproduce the above copyright notice,
011 *    this list of conditions and the following disclaimer in the documentation
012 *    and/or other materials provided with the distribution.
013 *
014 *  o Neither the name of FormLayoutBuilder or Michael Connor nor the names of
015 *    its contributors may be used to endorse or promote products derived
016 *    from this software without specific prior written permission.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030
031/**
032 *
033 */
034package org.mlc.swing.layout;
035
036import java.awt.dnd.DnDConstants;
037import java.awt.dnd.DragGestureEvent;
038import java.awt.dnd.DragGestureListener;
039import java.awt.dnd.DragSource;
040import java.awt.dnd.DragSourceListener;
041import java.awt.event.MouseEvent;
042
043import javax.swing.JList;
044import javax.swing.ListModel;
045import javax.swing.ListSelectionModel;
046
047/**
048 * DndList class.
049 *
050 * @author eal
051 * @version $Id$
052 * @since Ptolemy II 8.0
053 * @Pt.ProposedRating Red (cxh)
054 * @Pt.AcceptedRating Red (cxh)
055 */
056public class DndList extends JList
057        implements DragSourceListener, DragGestureListener {
058    /**
059     *
060     */
061    private final FormEditor editor;
062    private static final long serialVersionUID = 1L;
063    protected DragSource fDragSource = null;
064
065    public DndList(FormEditor editor, ListModel listModel) {
066        super(listModel);
067        this.editor = editor;
068        fDragSource = new DragSource();
069        this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
070
071        // it is very important that autoscrolls be set to false. i
072        // spent a lot of time tracking down a bug where there was
073        // very strange behavior involving selections and dragging with
074        // this list. as far as i can tell, the autoscroller was
075        // generating phantom events which i believe is what it is
076        // designed to do. i don't have an explanation for how this
077        // should be done but i can say if this line of code is not
078        // present then bad things will happen.
079        this.editor.setAutoscrolls(false);
080        fDragSource.createDefaultDragGestureRecognizer(this,
081                DnDConstants.ACTION_MOVE, this);
082    }
083
084    @Override
085    public void dragDropEnd(
086            java.awt.dnd.DragSourceDropEvent dragSourceDropEvent) {
087    }
088
089    @Override
090    public void dragEnter(
091            java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
092    }
093
094    @Override
095    public void dragExit(java.awt.dnd.DragSourceEvent dragSourceEvent) {
096    }
097
098    @Override
099    public void dragOver(java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
100    }
101
102    @Override
103    public void dropActionChanged(
104            java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
105    }
106
107    @Override
108    public void dragGestureRecognized(DragGestureEvent event) {
109        int dragIndex = locationToIndex(event.getDragOrigin());
110        if (dragIndex >= 0) {
111            Object draggingComponent = this.getModel().getElementAt(dragIndex);
112            event.startDrag(java.awt.dnd.DragSource.DefaultCopyDrop,
113                    new TransferableWrapper(draggingComponent), this);
114        }
115    }
116
117    @Override
118    public String getToolTipText(MouseEvent evt) {
119        // return a tooltip for the specific entry in the list
120        // Get item index
121        int index = locationToIndex(evt.getPoint());
122        if (index == -1) {
123            return "";
124        }
125
126        // Get item
127        Object o = this.getModel().getElementAt(index);
128        if (o instanceof ComponentDef) {
129            ComponentDef thisItem = (ComponentDef) o;
130            if (thisItem != null) {
131                return thisItem.getDescription();
132            }
133        }
134        return "";
135    }
136
137    public void drop(java.awt.dnd.DropTargetDropEvent e) {
138    }
139}