001/* A palette of objects that can be dropped into a customizable run control panel.
002
003 Copyright (c) 2007-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 This class is based on DndList by Michael Connor, which
028 bears the following copyright:
029
030 * Copyright (c) 2004-2014 by Michael Connor. All Rights Reserved.
031 *
032 * Redistribution and use in source and binary forms, with or without
033 * modification, are permitted provided that the following conditions are met:
034 *
035 *  o Redistributions of source code must retain the above copyright notice,
036 *    this list of conditions and the following disclaimer.
037 *
038 *  o Redistributions in binary form must reproduce the above copyright notice,
039 *    this list of conditions and the following disclaimer in the documentation
040 *    and/or other materials provided with the distribution.
041 *
042 *  o Neither the name of FormLayoutBuilder or Michael Connor nor the names of
043 *    its contributors may be used to endorse or promote products derived
044 *    from this software without specific prior written permission.
045 *
046 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
047 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
048 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
049 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
050 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
051 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
052 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
053 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
054 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
055 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
056 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
057 */
058
059package ptolemy.actor.gui.run;
060
061import java.awt.dnd.DnDConstants;
062import java.awt.dnd.DragGestureEvent;
063import java.awt.dnd.DragGestureListener;
064import java.awt.dnd.DragSource;
065import java.awt.dnd.DragSourceListener;
066import java.awt.event.MouseEvent;
067
068import javax.swing.JList;
069import javax.swing.ListModel;
070import javax.swing.ListSelectionModel;
071
072import org.mlc.swing.layout.ComponentDef;
073import org.mlc.swing.layout.TransferableWrapper;
074
075///////////////////////////////////////////////////////////////////
076//// PaletteList
077
078/**
079A customized version of the DndList class by
080Michael Connor (mlconnor@yahoo.com). The only reason for
081customization is to require a PtolemyFormEditor constructor argument.
082
083@author Michael Connor and Edward A. Lee
084@version $Id$
085@since Ptolemy II 8.0
086@Pt.ProposedRating Yellow (eal)
087@Pt.AcceptedRating Red (cxh)
088 */
089public class PaletteList extends JList
090        implements DragSourceListener, DragGestureListener {
091
092    /** Construct a PaletteList.
093     * @param editor The form editor
094     * @param listModel the list model
095     */
096    public PaletteList(PtolemyFormEditor editor, ListModel listModel) {
097        super(listModel);
098        this.editor = editor;
099        fDragSource = new DragSource();
100        this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
101
102        // it is very important that autoscrolls be set to false. i
103        // spent a lot of time tracking down a bug where there was
104        // very strange behavior involving selections and dragging with
105        // this list. as far as i can tell, the autoscroller was
106        // generating phantom events which i believe is what it is
107        // designed to do. i don't have an explanation for how this
108        // should be done but i can say if this line of code is not
109        // present then bad things will happen.
110        this.editor.setAutoscrolls(false);
111        fDragSource.createDefaultDragGestureRecognizer(this,
112                DnDConstants.ACTION_MOVE, this);
113    }
114
115    ///////////////////////////////////////////////////////////////////
116    ////                         public methods                    ////
117
118    /** In this derived class, do nothing.
119     *  @param dragSourceDropEvent Ignored.
120     */
121    @Override
122    public void dragDropEnd(
123            java.awt.dnd.DragSourceDropEvent dragSourceDropEvent) {
124    }
125
126    /** In this derived class, do nothing.
127     *  @param dragSourceDragEvent Ignored.
128     */
129    @Override
130    public void dragEnter(
131            java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
132    }
133
134    /** In this derived class, do nothing.
135     *  @param dragSourceEvent Ignored.
136     */
137    @Override
138    public void dragExit(java.awt.dnd.DragSourceEvent dragSourceEvent) {
139    }
140
141    /** Start the drag.
142     *  @param event The event that starts the drag if it has an index.
143     */
144    @Override
145    public void dragGestureRecognized(DragGestureEvent event) {
146        int dragIndex = locationToIndex(event.getDragOrigin());
147        if (dragIndex >= 0) {
148            Object draggingComponent = this.getModel().getElementAt(dragIndex);
149            event.startDrag(java.awt.dnd.DragSource.DefaultCopyDrop,
150                    new TransferableWrapper(draggingComponent), this);
151        }
152    }
153
154    /** In this derived class, do nothing.
155     *  @param dragSourceDragEvent Ignored.
156     */
157    @Override
158    public void dragOver(java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
159    }
160
161    /** In this derived class, do nothing.
162     *  @param e Ignored.
163     */
164    public void drop(java.awt.dnd.DropTargetDropEvent e) {
165    }
166
167    /** In this derived class, do nothing.
168     *  @param dragSourceDragEvent Ignored.
169     */
170    @Override
171    public void dropActionChanged(
172            java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
173    }
174
175    /** Return the tool tip text for an event.
176     *  @param evt The event
177     *  @return The tool tip text, if any.
178     */
179    @Override
180    public String getToolTipText(MouseEvent evt) {
181        // return a tooltip for the specific entry in the list
182        // Get item index
183        int index = locationToIndex(evt.getPoint());
184        if (index == -1) {
185            return "";
186        }
187
188        // Get item
189        Object o = this.getModel().getElementAt(index);
190        if (o instanceof ComponentDef) {
191            ComponentDef thisItem = (ComponentDef) o;
192            if (thisItem != null) {
193                return thisItem.getDescription();
194            }
195        }
196        return "";
197    }
198
199    ///////////////////////////////////////////////////////////////////
200    ////                         protected variables               ////
201
202    /** The source of the drag. */
203    protected DragSource fDragSource = null;
204
205    ///////////////////////////////////////////////////////////////////
206    ////                         private variables                 ////
207
208    private final PtolemyFormEditor editor;
209
210    private static final long serialVersionUID = 1L;
211}