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.HeadlessException;
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.sql.SQLException;
037:
038: import javax.swing.JTree;
039: import javax.swing.tree.TreePath;
040:
041: import org.isqlviewer.bookmarks.Bookmark;
042: import org.isqlviewer.bookmarks.BookmarkFolder;
043: import org.isqlviewer.bookmarks.BookmarkReference;
044: import org.isqlviewer.history.HistoricalCommand;
045: import org.isqlviewer.model.BookmarkTreeModel;
046: import org.isqlviewer.sql.embedded.EmbeddedDatabase;
047: import org.isqlviewer.swing.action.SharedActions;
048: import org.isqlviewer.swing.action.SwingEventManager;
049:
050: /**
051: * Adapter object for handling drop events for bookmarks, folders, and text within a bookmark tree view.
052: * <p>
053: *
054: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
055: * @version 1.0
056: */
057: public class BookmarkTreeDropTarget implements DropTargetListener {
058:
059: private static final long serialVersionUID = 1L;
060: private SwingEventManager eventManager = null;
061:
062: public BookmarkTreeDropTarget(SwingEventManager eventManager)
063: throws HeadlessException {
064:
065: this .eventManager = eventManager;
066: }
067:
068: public void drop(DropTargetDropEvent event) {
069:
070: Transferable transferable = event.getTransferable();
071: int dropX = event.getLocation().x;
072: int dropY = event.getLocation().y;
073:
074: JTree bookmarkView = (JTree) event.getDropTargetContext()
075: .getComponent();
076: TreePath dropTreePath = bookmarkView.getPathForLocation(dropX,
077: dropY);
078: BookmarkTreeModel bookmarkModel = (BookmarkTreeModel) bookmarkView
079: .getModel();
080: Object dropTarget = (dropTreePath != null ? dropTreePath
081: .getLastPathComponent() : "/");
082: BookmarkFolder dropFolder = null;
083:
084: if (dropTarget instanceof BookmarkReference) {
085: BookmarkReference reference = (BookmarkReference) dropTarget;
086: dropFolder = reference.getFolder();
087: } else if (dropTarget instanceof BookmarkFolder) {
088: dropFolder = (BookmarkFolder) dropTarget;
089: }
090:
091: if (dropFolder == null) {
092: dropFolder = (BookmarkFolder) bookmarkModel.getRoot();
093: }
094:
095: if (transferable
096: .isDataFlavorSupported(Bookmark.BOOKMARK_FLAVOR)) {
097: event.acceptDrop(event.getDropAction());
098: dropBookmark(event, transferable, bookmarkModel, dropFolder);
099: } else if (transferable
100: .isDataFlavorSupported(HistoricalCommand.FLAVOR)) {
101: event.acceptDrop(event.getDropAction());
102: HistoricalCommand source = null;
103: try {
104: source = (HistoricalCommand) transferable
105: .getTransferData(HistoricalCommand.FLAVOR);
106: } catch (UnsupportedFlavorException e) {
107: event.rejectDrop();
108: return;
109: } catch (IOException e) {
110: event.rejectDrop();
111: return;
112: }
113: String commandText = dropFolder.getPath();
114: int eventId = SharedActions.CONVERT_HISTORY_TO_BOOKMARK;
115: ActionEvent convertAction = new ActionEvent(source,
116: eventId, commandText);
117: eventManager.actionPerformed(convertAction);
118: event.dropComplete(true);
119: } else if (transferable
120: .isDataFlavorSupported(DataFlavor.stringFlavor)) {
121: event.acceptDrop(event.getDropAction());
122: String source = null;
123: try {
124: source = (String) transferable
125: .getTransferData(DataFlavor.stringFlavor);
126: } catch (UnsupportedFlavorException e) {
127: event.rejectDrop();
128: return;
129: } catch (IOException e) {
130: event.rejectDrop();
131: return;
132: }
133: String commandText = dropFolder.getPath();
134: int eventId = SharedActions.CONVERT_TEXT_TO_BOOKMARK;
135: ActionEvent convertAction = new ActionEvent(source,
136: eventId, commandText);
137: eventManager.actionPerformed(convertAction);
138: event.dropComplete(true);
139: } else {
140: // unsupported data flavor //
141: event.rejectDrop();
142: }
143: }
144:
145: public void dragEnter(DropTargetDragEvent dtde) {
146:
147: }
148:
149: public void dragExit(DropTargetEvent dte) {
150:
151: }
152:
153: public void dragOver(DropTargetDragEvent dtde) {
154:
155: }
156:
157: public void dropActionChanged(DropTargetDragEvent dtde) {
158:
159: }
160:
161: private void dropBookmark(DropTargetDropEvent event,
162: Transferable transferable, BookmarkTreeModel bookmarkModel,
163: BookmarkFolder dropFolder) {
164:
165: Bookmark source = null;
166: try {
167: source = (Bookmark) transferable
168: .getTransferData(Bookmark.BOOKMARK_FLAVOR);
169: } catch (UnsupportedFlavorException e) {
170: event.rejectDrop();
171: return;
172: } catch (IOException e) {
173: event.rejectDrop();
174: return;
175: }
176:
177: BookmarkFolder sourceFolder = source.getFolder();
178: Bookmark bookmark = null;
179: boolean doUpdate = false;
180: int dndOperation = event.getDropAction();
181:
182: switch (dndOperation) {
183: case DnDConstants.ACTION_MOVE:
184: bookmark = source;
185: doUpdate = true;
186: break;
187: case DnDConstants.ACTION_COPY:
188: Bookmark copy = source.clone();
189: bookmark = copy;
190: break;
191: default:
192: event.rejectDrop();
193: return;
194: }
195:
196: EmbeddedDatabase database = EmbeddedDatabase
197: .getSharedInstance();
198: try {
199: if (doUpdate) {
200: bookmarkModel.removeBookmark(bookmark);
201: bookmarkModel.addBookmark(bookmark, dropFolder);
202: database.updateBookmark(bookmark);
203: } else {
204: bookmarkModel.addBookmark(bookmark, dropFolder);
205: database.addBookmark(bookmark);
206: }
207: event.dropComplete(true);
208: } catch (SQLException sourceError) {
209: // rollback and reject the drop //
210: if (doUpdate) {
211: bookmarkModel.removeBookmark(bookmark);
212: bookmarkModel.addBookmark(bookmark, sourceFolder);
213: } else {
214: bookmarkModel.removeBookmark(bookmark);
215: }
216: bookmarkModel.reload(sourceFolder, bookmark);
217: event.dropComplete(false);
218: }
219: }
220: }
|