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.dnd.DragGestureEvent;
032: import java.awt.dnd.DragGestureListener;
033: import java.awt.dnd.DragGestureRecognizer;
034: import java.awt.dnd.DragSource;
035: import java.awt.dnd.DragSourceDragEvent;
036: import java.awt.dnd.DragSourceDropEvent;
037: import java.awt.dnd.DragSourceEvent;
038: import java.awt.dnd.DragSourceListener;
039: import java.awt.image.BufferedImage;
040:
041: import javax.swing.JTree;
042: import javax.swing.tree.TreeCellRenderer;
043: import javax.swing.tree.TreePath;
044:
045: import org.isqlviewer.model.SchemaNode;
046: import org.isqlviewer.util.LoggableObject;
047:
048: /**
049: * TODO Add SchemaTreeDragListener JavaDoc inforamation
050: * <p>
051: *
052: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
053: * @version 1.0
054: */
055: public class SchemaTreeDragListener extends LoggableObject implements
056: DragGestureListener, DragSourceListener {
057:
058: private static final Point DRAG_OFFSET = new Point(2, 2);
059: private JTree schemaTree = null;
060:
061: public SchemaTreeDragListener(JTree treBookmarks) {
062:
063: this .schemaTree = treBookmarks;
064: }
065:
066: public void dragGestureRecognized(DragGestureEvent event) {
067:
068: DragSource dndDragSource = DragSource.getDefaultDragSource();
069: DragGestureRecognizer dndRecognizer = event
070: .getSourceAsDragGestureRecognizer();
071: schemaTree.cancelEditing();
072: int x = event.getDragOrigin().x;
073: int y = event.getDragOrigin().y;
074: boolean leaf = true;
075: int row = schemaTree.getRowForLocation(x, y);
076: TreePath tp = schemaTree.getPathForLocation(x, y);
077: if (tp == null) {
078: dndRecognizer.resetRecognizer();
079: return;
080: }
081: SchemaNode selected = (SchemaNode) tp.getLastPathComponent();
082: if (selected == null || !isDragSupported(selected)) {
083: dndRecognizer.resetRecognizer();
084: return;
085: }
086:
087: leaf = selected.getChildCount() > 0;
088: GraphicsConfiguration gConfig = schemaTree
089: .getGraphicsConfiguration();
090: TreeCellRenderer tcr = schemaTree.getCellRenderer();
091: Component comp = tcr.getTreeCellRendererComponent(schemaTree,
092: selected, false, false, leaf, row, false);
093: Dimension dim = comp.getPreferredSize();
094: comp.setSize(dim);
095: BufferedImage bi = gConfig.createCompatibleImage(dim.width,
096: dim.height);
097: Graphics2D g2 = bi.createGraphics();
098: comp.paint(g2);
099: g2.setColor(new Color(255, 255, 255, 112));
100: g2.fillRect(0, 0, dim.width, dim.height);
101: try {
102: dndDragSource.startDrag(event, null, bi, DRAG_OFFSET,
103: selected, this );
104: } catch (Exception error) {
105: dndRecognizer.resetRecognizer();
106: }
107: }
108:
109: /**
110: * @param selected
111: * @return
112: */
113: private boolean isDragSupported(SchemaNode selected) {
114:
115: switch (selected.getNodeType()) {
116: case DATA_TYPE:
117: case UDT:
118: return false;
119: default:
120: return true;
121: }
122: }
123:
124: public void dragDropEnd(DragSourceDropEvent dsde) {
125:
126: }
127:
128: public void dragEnter(DragSourceDragEvent dsde) {
129:
130: }
131:
132: public void dragExit(DragSourceEvent dse) {
133:
134: }
135:
136: public void dragOver(DragSourceDragEvent dsde) {
137:
138: }
139:
140: public void dropActionChanged(DragSourceDragEvent dsde) {
141:
142: }
143: }
|