001 /* JAPI - (Yet another (hopefully) useful) Java API
002 *
003 * Copyright (C) 2004-2006 Christian Hujer
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License as
007 * published by the Free Software Foundation; either version 2 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but
011 * WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018 * 02111-1307, USA.
019 */
020
021
022 // $Header: /cvsroot/japi/japi/src/app/net/sf/japi/swing/bookmarks/BookmarkDropTargetAdapter.java,v 1.1 2006/03/26 01:26:25 christianhujer Exp $
023
024 package net.sf.japi.swing.bookmarks;
025
026 import java.awt.Point;
027 import java.awt.datatransfer.Transferable;
028 import java.awt.datatransfer.UnsupportedFlavorException;
029 import java.awt.dnd.DropTargetAdapter;
030 import java.awt.dnd.DropTargetDropEvent;
031 import java.io.IOException;
032 import javax.swing.JTree;
033 import javax.swing.tree.TreePath;
034 import org.jetbrains.annotations.Nullable;
035 import static net.sf.japi.swing.bookmarks.BookmarkTransferable.getBookmarkDataFlavor;
036
037 /** Class for dropping a bookmark over a JTree managing bookmarks.
038 * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
039 */
040 public class BookmarkDropTargetAdapter extends DropTargetAdapter {
041
042 /** {@inheritDoc} */
043 public void drop(final DropTargetDropEvent dtde) {
044 System.err.println("drop");
045 final JTree tree = (JTree) dtde.getDropTargetContext().getComponent();
046 BookmarkManager.Bookmark dest = getDestBookmark(dtde, tree);
047 final BookmarkManager.Bookmark src = getSourceBookmark(dtde);
048 int pos = -1;
049 if (!(dest instanceof BookmarkManager.BookmarkFolder)) {
050 pos = dest.getFolder().getIndex(dest);
051 dest = dest.getFolder();
052 }
053 assert dest instanceof BookmarkManager.BookmarkFolder;
054 ((BookmarkManager.BookmarkFolder)dest).insert(src, pos);
055 tree.treeDidChange();
056 }
057
058 /** Find the destination bookmark for a drop event.
059 * @param dtde drop event
060 * @param tree JTree to find bookmark in
061 * @return destination bookmark
062 */
063 private static BookmarkManager.Bookmark getDestBookmark(final DropTargetDropEvent dtde, final JTree tree) {
064 final Point p = dtde.getLocation();
065 final TreePath tp = tree.getClosestPathForLocation(p.x, p.y);
066 return (BookmarkManager.Bookmark) tp.getLastPathComponent();
067 }
068
069 /** Find source bookmark for a drop event.
070 * @param dtde drop event
071 * @return source bookmark
072 */
073 @Nullable private static BookmarkManager.Bookmark getSourceBookmark(final DropTargetDropEvent dtde) {
074 final Transferable source = dtde.getTransferable();
075 try {
076 return (BookmarkManager.Bookmark) source.getTransferData(getBookmarkDataFlavor());
077 } catch (final UnsupportedFlavorException e) {
078 return null;
079 } catch (final IOException e) {
080 return null;
081 }
082 }
083
084 } // class BookmarkDropTargetAdapter