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.io.PrintWriter;
043: import java.io.StringWriter;
044:
045: import javax.swing.tree.TreePath;
046:
047: import org.isqlviewer.swing.outline.JOutline;
048: import org.isqlviewer.util.LoggableObject;
049:
050: /**
051: * Drag listener for transferring Throwables using Drag and Drop.
052: * <p>
053: *
054: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
055: * @version 1.0
056: */
057: public class ThrowableTreeDragListener extends LoggableObject implements
058: DragGestureListener, DragSourceListener {
059:
060: private static final Point DRAG_OFFSET = new Point(2, 2);
061: private JOutline errorTree = null;
062:
063: public ThrowableTreeDragListener(JOutline treBookmarks) {
064:
065: this .errorTree = treBookmarks;
066: }
067:
068: public void dragGestureRecognized(DragGestureEvent event) {
069:
070: DragSource dndDragSource = DragSource.getDefaultDragSource();
071: DragGestureRecognizer dndRecognizer = event
072: .getSourceAsDragGestureRecognizer();
073: errorTree.cancelEditing();
074: int x = event.getDragOrigin().x;
075: int y = event.getDragOrigin().y;
076: boolean leaf = true;
077: int row = errorTree.getRowForLocation(x, y);
078: TreePath tp = errorTree.getPathForLocation(x, y);
079: if (tp == null) {
080: dndRecognizer.resetRecognizer();
081: return;
082: }
083:
084: Object userSelection = tp.getLastPathComponent();
085: Transferable data = null;
086: if (userSelection instanceof Throwable) {
087: StringWriter memoryOut = new StringWriter();
088: Throwable error = (Throwable) userSelection;
089: error.printStackTrace(new PrintWriter(memoryOut));
090: data = new StringSelection(memoryOut.toString());
091: } else if (userSelection instanceof StackTraceElement) {
092: data = new StringSelection(userSelection.toString());
093: }
094:
095: if (data == null) {
096: dndRecognizer.resetRecognizer();
097: return;
098: }
099:
100: BufferedImage bi = null;
101: if (DragSource.isDragImageSupported()) {
102: GraphicsConfiguration gConfig = errorTree
103: .getGraphicsConfiguration();
104: Component comp = errorTree.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: }
|