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: import java.text.DateFormat;
043: import java.util.Date;
044:
045: import javax.swing.tree.TreePath;
046:
047: import org.isqlviewer.history.HistoricalCommand;
048: import org.isqlviewer.swing.outline.JOutline;
049: import org.isqlviewer.util.LoggableObject;
050:
051: /**
052: * Listener class for getting historical reference objects from the tree model for drag gestures.
053: * <p>
054: *
055: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
056: * @version 1.0
057: */
058: public class HistoryTreeDragListener extends LoggableObject implements
059: DragGestureListener, DragSourceListener {
060:
061: private static final Point DRAG_OFFSET = new Point(2, 2);
062: private JOutline historyTree = null;
063:
064: public HistoryTreeDragListener(JOutline treBookmarks) {
065:
066: this .historyTree = treBookmarks;
067: }
068:
069: public void dragGestureRecognized(DragGestureEvent event) {
070:
071: DragSource dndDragSource = DragSource.getDefaultDragSource();
072: DragGestureRecognizer dndRecognizer = event
073: .getSourceAsDragGestureRecognizer();
074: historyTree.cancelEditing();
075: int x = event.getDragOrigin().x;
076: int y = event.getDragOrigin().y;
077: boolean leaf = true;
078: int row = historyTree.getRowForLocation(x, y);
079: TreePath tp = historyTree.getPathForLocation(x, y);
080: if (tp == null) {
081: dndRecognizer.resetRecognizer();
082: return;
083: }
084: Object userSelection = tp.getLastPathComponent();
085: Transferable data = null;
086: if (userSelection instanceof HistoricalCommand) {
087: data = (Transferable) tp.getLastPathComponent();
088: } else if (userSelection instanceof Date) {
089: Date date = (Date) userSelection;
090: DateFormat format = DateFormat
091: .getDateInstance(DateFormat.FULL);
092: data = new StringSelection(format.format(date));
093: }
094:
095: if (data == null) {
096: dndRecognizer.resetRecognizer();
097: return;
098: }
099:
100: BufferedImage bi = null;
101: if (DragSource.isDragImageSupported()) {
102: GraphicsConfiguration gConfig = historyTree
103: .getGraphicsConfiguration();
104: Component comp = historyTree.getTreeCellRendererComponent(
105: userSelection, false, false, leaf, row, false);
106: Dimension dim = comp.getPreferredSize();
107: comp.setSize(dim);
108: bi = gConfig.createCompatibleImage(dim.width, dim.height);
109: Graphics2D g2 = bi.createGraphics();
110: comp.paint(g2);
111: g2.setColor(new Color(255, 255, 255, 112));
112: g2.fillRect(0, 0, dim.width, dim.height);
113: }
114: try {
115: dndDragSource.startDrag(event, null, bi, DRAG_OFFSET, data,
116: this );
117: } catch (Exception error) {
118: dndRecognizer.resetRecognizer();
119: }
120: }
121:
122: public void dragDropEnd(DragSourceDropEvent dsde) {
123:
124: }
125:
126: public void dragEnter(DragSourceDragEvent dsde) {
127:
128: }
129:
130: public void dragExit(DragSourceEvent dse) {
131:
132: }
133:
134: public void dragOver(DragSourceDragEvent dsde) {
135:
136: }
137:
138: public void dropActionChanged(DragSourceDragEvent dsde) {
139:
140: }
141:
142: }
|