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.dnd;
024:
025: import java.awt.Component;
026: import java.awt.datatransfer.DataFlavor;
027: import java.awt.datatransfer.Transferable;
028: import java.awt.datatransfer.UnsupportedFlavorException;
029: import java.awt.dnd.DnDConstants;
030: import java.awt.dnd.DropTargetDragEvent;
031: import java.awt.dnd.DropTargetDropEvent;
032: import java.awt.dnd.DropTargetEvent;
033: import java.awt.dnd.DropTargetListener;
034: import java.awt.event.ActionEvent;
035: import java.io.IOException;
036: import java.text.MessageFormat;
037:
038: import org.isqlviewer.bookmarks.Bookmark;
039: import org.isqlviewer.history.HistoricalCommand;
040: import org.isqlviewer.model.JdbcSchemaTreeModel;
041: import org.isqlviewer.model.SchemaNode;
042: import org.isqlviewer.swing.action.SharedActions;
043: import org.isqlviewer.swing.action.SwingEventManager;
044: import org.isqlviewer.ui.TabbedResultsetRenderer;
045:
046: /**
047: * Adapter object for handling drop events for things the generate result sets from the connected service.
048: * <p>
049: *
050: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
051: * @version 1.0
052: */
053: public class ResultSetRendererDropTarget implements DropTargetListener {
054:
055: private static final long serialVersionUID = 1941372297753703481L;
056: private static DataFlavor[] supportedFlavors = new DataFlavor[] {
057: SchemaNode.DATA_FLAVOR, Bookmark.BOOKMARK_FLAVOR,
058: HistoricalCommand.FLAVOR, DataFlavor.stringFlavor };
059: private TabbedResultsetRenderer nodeRenderer = null;
060: private JdbcSchemaTreeModel treeModel = null;
061: private SwingEventManager eventManager = null;
062:
063: public void setEventManager(SwingEventManager eventManager) {
064:
065: this .eventManager = eventManager;
066: }
067:
068: public void setNodeRenderer(TabbedResultsetRenderer nodeRenderer) {
069:
070: this .nodeRenderer = nodeRenderer;
071: }
072:
073: public void setTreeModel(JdbcSchemaTreeModel treeModel) {
074:
075: this .treeModel = treeModel;
076: }
077:
078: public synchronized void dragEnter(DropTargetDragEvent event) {
079:
080: Transferable transferrable = event.getTransferable();
081: if (!isDataFlavorSupported(transferrable)) {
082: event.rejectDrag();
083: }
084: }
085:
086: public synchronized void dragExit(DropTargetEvent event) {
087:
088: }
089:
090: public void dragOver(DropTargetDragEvent event) {
091:
092: }
093:
094: public void dropActionChanged(DropTargetDragEvent dtde) {
095:
096: }
097:
098: public void drop(DropTargetDropEvent event) {
099:
100: Transferable transferrable = event.getTransferable();
101: int dropAction = event.getDropAction();
102:
103: if (transferrable
104: .isDataFlavorSupported(Bookmark.BOOKMARK_FLAVOR)) {
105: Bookmark command;
106: try {
107: command = (Bookmark) transferrable
108: .getTransferData(Bookmark.BOOKMARK_FLAVOR);
109: String sql = command.getCommandText();
110: int eventId = SharedActions.EXECUTE_SQL_COMMAND;
111: ActionEvent executeAction = new ActionEvent(this ,
112: eventId, sql);
113: eventManager.actionPerformed(executeAction);
114: event.acceptDrop(dropAction);
115: } catch (UnsupportedFlavorException ignored) {
116: // we call isDataFlavorSupported() so we should be good//
117: event.rejectDrop();
118: return;
119: } catch (IOException e) {
120: event.rejectDrop();
121: return;
122: }
123: } else if (transferrable
124: .isDataFlavorSupported(HistoricalCommand.FLAVOR)) {
125: HistoricalCommand command;
126: try {
127: command = (HistoricalCommand) transferrable
128: .getTransferData(HistoricalCommand.FLAVOR);
129: String sql = command.getCommandText();
130: int eventId = SharedActions.EXECUTE_SQL_COMMAND;
131: ActionEvent executeAction = new ActionEvent(this ,
132: eventId, sql);
133: eventManager.actionPerformed(executeAction);
134: event.acceptDrop(dropAction);
135: } catch (UnsupportedFlavorException ignored) {
136: // we call isDataFlavorSupported() so we should be good//
137: event.rejectDrop();
138: return;
139: } catch (IOException e) {
140: event.rejectDrop();
141: return;
142: }
143: } else if (transferrable
144: .isDataFlavorSupported(SchemaNode.DATA_FLAVOR)) {
145: SchemaNode schemaNode;
146: try {
147: schemaNode = (SchemaNode) transferrable
148: .getTransferData(SchemaNode.DATA_FLAVOR);
149: event.acceptDrop(dropAction);
150: } catch (UnsupportedFlavorException ignored) {
151: // we call isDataFlavorSupported() so we should be good//
152: event.rejectDrop();
153: return;
154: } catch (IOException e) {
155: event.rejectDrop();
156: return;
157: }
158: switch (dropAction) {
159: case DnDConstants.ACTION_MOVE:
160: renderSchemNode(schemaNode);
161: break;
162: case DnDConstants.ACTION_COPY:
163: String sql = constructQueryForNode(schemaNode);
164: if (sql != null) {
165: int eventId = SharedActions.EXECUTE_SQL_COMMAND;
166: ActionEvent executeAction = new ActionEvent(this ,
167: eventId, sql);
168: eventManager.actionPerformed(executeAction);
169: }
170: break;
171: default:
172: break;
173: }
174: }
175: }
176:
177: public Component getComponent() {
178:
179: return nodeRenderer.getTabbedPane();
180: }
181:
182: private void renderSchemNode(SchemaNode schemaNode) {
183:
184: Runnable renderer = new SchemaNodeRenderer(nodeRenderer,
185: treeModel, getComponent(), schemaNode);
186: eventManager.enqueueRunnable(renderer);
187: }
188:
189: private boolean isDataFlavorSupported(Transferable flavor) {
190:
191: for (int i = 0; i < supportedFlavors.length; i++) {
192: if (flavor.isDataFlavorSupported(supportedFlavors[i])) {
193: return true;
194: }
195: }
196: return false;
197: }
198:
199: private String constructQueryForNode(SchemaNode schemaNode) {
200:
201: Object[] arguments = null;
202: switch (schemaNode.getNodeType()) {
203: case PRIMARY_KEYS:
204: break;
205: case TABLE:
206: arguments = new Object[1];
207: arguments[0] = schemaNode.getUserObject();
208: return MessageFormat.format("SELECT * FROM {0}", arguments);
209: case COLUMN_SET:
210: arguments = new Object[1];
211: arguments[0] = ((SchemaNode) schemaNode.getParent())
212: .getUserObject();
213: return MessageFormat.format("SELECT * FROM {0}", arguments);
214: case COLUMN:
215: arguments = new Object[2];
216: arguments[0] = schemaNode.getUserObject();
217: arguments[1] = ((SchemaNode) schemaNode.getParent()
218: .getParent()).getUserObject();
219: return MessageFormat.format("SELECT {0} FROM {1}",
220: arguments);
221: default:
222: break;
223:
224: }
225: return null;
226: }
227:
228: }
|