001/*
002 *  $Source: v:/cvsroot/open/projects/WebARTS/ca/bc/webarts/widgets/dnd/FileDropBean.java,v $
003 *  $Name:  $
004 *  $Revision: 1.1 $
005 *  $Date: 2005-04-10 11:53:16 -0700 (Sun, 10 Apr 2005) $
006 *  $Locker:  $
007 */
008/*
009 *  Copyright (C) 2001 WebARTS Design, North Vancouver Canada
010 *  http://www..webarts.bc.ca
011 *
012 *  This program is free software; you can redistribute it and/or modify
013 *  it under the terms of the GNU General Public License as published by
014 *  the Free Software Foundation; either version 2 of the License, or
015 *  (at your option) any later version.
016 *
017 *  This program is distributed in the hope that it will be useful,
018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
020 *  GNU General Public License for more details.
021 *
022 *  You should have received a copy of the GNU General Public License
023 *  along with this program; if not, write to the Free Software
024 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
025 */
026package ca.bc.webarts.widgets.dnd;
027
028import java.beans.*;
029
030/**
031 * If you wish to use the FileDrop package as a JavaBean, this class will act as
032 * an interface to the {@link FileDrop} class that handles all the dirty work.
033 * After instantiating the bean, add components as drop targets using the
034 * {@link #addFileDropTarget addFileDropTarget(...)} method. If the component
035 * is a {@link java.awt.Container}, then all elements contained within will be
036 * marked as a drop target as well.
037 * Using the {@link FileDrop} technique manually in your code will give you more options.
038 *
039 * <p>I'm releasing this code into the Public Domain. Enjoy.
040 * </p>
041 * <p><em>Original author: Robert Harder, rharder@usa.net</em></p>
042 *
043 * @author  Robert Harder
044 * @author  rharder@usa.net
045 * @version 1.1
046 */
047public class FileDropBean
048implements java.io.Serializable
049{
050
051    private javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList();
052
053    /** Creates new FileDropBean */
054    public FileDropBean()
055    {
056    }
057
058    /**
059     * Registers a component as a drop target.
060     * If the component is a container, then all elements contained
061     * within will also be registered as drop targets, though only
062     * the outer container will change borders during a drag and drop
063     * operation (and even then, only if the container is a Swing component).
064     *
065     * @param comp The component to register as a drop target
066     * @since 1.1
067     */
068    public void addFileDropTarget( java.awt.Component comp )
069    {
070        FileDrop.Listener listener = new FileDrop.Listener()
071        {   public void filesDropped( java.io.File[] files )
072            {   fireFileDropHappened( files );
073            }   // end filesDropped
074        };  // end listener
075        boolean recursive = true;
076        new FileDrop( comp, recursive, listener );
077    }   // end newDropTarget
078
079
080    /**
081     * Unregisters a component as a drop target.
082     *
083     * @param comp The component to unregister
084     * @since 1.1
085     */
086    public boolean removeFileDropTarget( java.awt.Component comp )
087    {   return FileDrop.remove( comp );
088    }   // end removeFileDropTarget
089
090
091    /**
092     * Register a listener for {@link FileDropEvent}s.
093     *
094     * @param listener The listener to register
095     * @since 1.1
096     */
097    public void addFileDropListener (FileDropListener listener)
098    {   listenerList.add( FileDropListener.class, listener );
099    }   // end addFileDropListener
100
101
102
103    /**
104     * Unregister a listener for {@link FileDropEvent}s.
105     *
106     * @param listener The listener to unregister
107     * @since 1.1
108     */
109    public void removeFileDropListener (FileDropListener listener)
110    {   listenerList.remove( FileDropListener.class, listener );
111    }   // end addFileDropListener
112
113
114    /**
115     * Fires a {@link FileDropEvent} with the given non-null
116     * list of dropped files.
117     *
118     * @param files The files that were dropped
119     * @since 1.1
120     */
121    protected void fireFileDropHappened( java.io.File[] files )
122    {
123        FileDropEvent evt = new FileDropEvent( files, this );
124
125        // Guaranteed to return a non-null array
126        Object[] listeners = listenerList.getListenerList();
127
128        // Process the listeners last to first, notifying
129        // those that are interested in this event
130        for (int i = listeners.length-2; i>=0; i-=2)
131        {   if (listeners[i]==FileDropListener.class)
132            {   ((FileDropListener)listeners[i+1]).filesDropped(evt);
133            }   // end if: correct listener type
134        }   // end for: each listener
135    }   // end fireFileDropHappened
136
137
138}   // end clas FileDropBean
139