001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.ui.listeners;
024:
025: import java.awt.Color;
026: import java.awt.Component;
027: import java.awt.Dimension;
028: import java.awt.Graphics2D;
029: import java.awt.GraphicsConfiguration;
030: import java.awt.Point;
031: import java.awt.datatransfer.StringSelection;
032: import java.awt.datatransfer.Transferable;
033: import java.awt.dnd.DragGestureEvent;
034: import java.awt.dnd.DragGestureListener;
035: import java.awt.dnd.DragGestureRecognizer;
036: import java.awt.dnd.DragSource;
037: import java.awt.dnd.DragSourceDragEvent;
038: import java.awt.dnd.DragSourceDropEvent;
039: import java.awt.dnd.DragSourceEvent;
040: import java.awt.dnd.DragSourceListener;
041: import java.awt.image.BufferedImage;
042:
043: import javax.swing.JTree;
044: import javax.swing.tree.TreeCellRenderer;
045: import javax.swing.tree.TreePath;
046:
047: import org.isqlviewer.bookmarks.BookmarkReference;
048: import org.isqlviewer.util.LoggableObject;
049:
050: /**
051: * Listener class for getting bookmark reference objects from the tree model for drag gestures.
052: * <p>
053: *
054: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
055: * @version 1.0
056: */
057: public class BookmarkTreeDragListener extends LoggableObject implements
058: DragGestureListener, DragSourceListener {
059:
060: private static final Point DRAG_OFFSET = new Point(2, 2);
061: private JTree bookmarkTree = null;
062:
063: public BookmarkTreeDragListener(JTree treBookmarks) {
064:
065: this .bookmarkTree = treBookmarks;
066: }
067:
068: public void dragGestureRecognized(DragGestureEvent event) {
069:
070: DragSource dndDragSource = DragSource.getDefaultDragSource();
071: DragGestureRecognizer dndRecognizer = event
072: .getSourceAsDragGestureRecognizer();
073: bookmarkTree.cancelEditing();
074: int x = event.getDragOrigin().x;
075: int y = event.getDragOrigin().y;
076: boolean leaf = true;
077: int row = bookmarkTree.getRowForLocation(x, y);
078: TreePath tp = bookmarkTree.getPathForLocation(x, y);
079: if (tp == null) {
080: dndRecognizer.resetRecognizer();
081: return;
082: }
083: Object selected = tp.getLastPathComponent();
084: if (selected == null) {
085: dndRecognizer.resetRecognizer();
086: return;
087: }
088: Transferable data = null;
089: if (selected instanceof BookmarkReference) {
090: data = (BookmarkReference) selected;
091: leaf = true;
092: } else {
093: data = new StringSelection(selected.toString());
094: leaf = false;
095: }
096: BufferedImage bi = null;
097: if (DragSource.isDragImageSupported()) {
098: GraphicsConfiguration gConfig = bookmarkTree
099: .getGraphicsConfiguration();
100: TreeCellRenderer tcr = bookmarkTree.getCellRenderer();
101: Component comp = tcr.getTreeCellRendererComponent(
102: bookmarkTree, selected, false, false, leaf, row,
103: false);
104: Dimension dim = comp.getPreferredSize();
105: comp.setSize(dim);
106: bi = gConfig.createCompatibleImage(dim.width, dim.height);
107: Graphics2D g2 = bi.createGraphics();
108: comp.paint(g2);
109: g2.setColor(new Color(255, 255, 255, 112));
110: g2.fillRect(0, 0, dim.width, dim.height);
111: }
112: try {
113: dndDragSource.startDrag(event, null, bi, DRAG_OFFSET, data,
114: this );
115: } catch (Exception error) {
116: dndRecognizer.resetRecognizer();
117: }
118: }
119:
120: public void dragDropEnd(DragSourceDropEvent dsde) {
121:
122: }
123:
124: public void dragEnter(DragSourceDragEvent dsde) {
125:
126: }
127:
128: public void dragExit(DragSourceEvent dse) {
129:
130: }
131:
132: public void dragOver(DragSourceDragEvent dsde) {
133:
134: }
135:
136: public void dropActionChanged(DragSourceDragEvent dsde) {
137:
138: }
139:
140: }
|