001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.client.gui.dnd;
020:
021: import java.awt.datatransfer.DataFlavor;
022: import java.awt.datatransfer.Transferable;
023: import java.awt.datatransfer.UnsupportedFlavorException;
024: import java.awt.dnd.DnDConstants;
025: import java.awt.dnd.DropTargetAdapter;
026: import java.awt.dnd.DropTargetContext;
027: import java.awt.dnd.DropTargetDropEvent;
028: import java.awt.dnd.DropTargetListener;
029: import java.io.File;
030: import java.io.IOException;
031: import java.net.URI;
032: import java.net.URISyntaxException;
033: import java.util.List;
034: import java.util.StringTokenizer;
035:
036: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
037: import net.sourceforge.squirrel_sql.client.session.ISession;
038: import net.sourceforge.squirrel_sql.fw.util.StringManager;
039: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
040: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
041: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
042:
043: /**
044: * A utility class that can be used to add the ability to drag a file from the
045: * desktop to a session sql editor panel.
046: *
047: * @author manningr
048: */
049: public class FileEditorDropTargetListener extends DropTargetAdapter
050: implements DropTargetListener {
051:
052: /** Logger for this class. */
053: private static final ILogger s_log = LoggerController
054: .createLogger(FileEditorDropTargetListener.class);
055:
056: /** Internationalized strings for this class. */
057: private static final StringManager s_stringMgr = StringManagerFactory
058: .getStringManager(FileEditorDropTargetListener.class);
059:
060: private static interface i18n {
061: //i18n[FileEditorDropTargetListener.oneFileDropMessage=Only one file
062: //may be dropped onto the editor at a time.]
063: String ONE_FILE_DROP_MESSAGE = s_stringMgr
064: .getString("FileEditorDropTargetListener.oneFileDropMessage");
065: }
066:
067: /** the session we are listening for drops into */
068: private ISession _session;
069:
070: public FileEditorDropTargetListener(ISession session) {
071: this ._session = session;
072: }
073:
074: /**
075: * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
076: */
077: @SuppressWarnings("unchecked")
078: public void drop(DropTargetDropEvent dtde) {
079: try {
080: DropTargetContext context = dtde.getDropTargetContext();
081: dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
082: Transferable t = dtde.getTransferable();
083: File fileToOpen = null;
084:
085: if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
086: fileToOpen = handleJavaFileList(t);
087: } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
088: fileToOpen = handleUriListString(t);
089: } else {
090: s_log.error("drop: flavors fileList and UriListString "
091: + "are not supported");
092: }
093: if (fileToOpen != null) {
094: if (s_log.isInfoEnabled()) {
095: s_log.info("drop: path="
096: + fileToOpen.getAbsolutePath());
097: }
098: ISQLPanelAPI api = _session
099: .getSQLPanelAPIOfActiveSessionWindow();
100: api.fileOpen(fileToOpen);
101: }
102: context.dropComplete(true);
103: } catch (Exception e) {
104: s_log.error("drop: Unexpected exception " + e.getMessage(),
105: e);
106: }
107:
108: }
109:
110: /**
111: * Handles the String data flavor which returns the data as a list of
112: * java.io.File objects.
113: *
114: * @param t
115: * the transferable to get the list from
116:
117: * @return the only file in the list
118: * @throws UnsupportedFlavorException
119: * @throws IOException
120: */
121: private File handleUriListString(Transferable t)
122: throws UnsupportedFlavorException, IOException {
123: File result = null;
124:
125: @SuppressWarnings("unchecked")
126: String transferData = (String) t
127: .getTransferData(DataFlavor.stringFlavor);
128:
129: if (transferData != null) {
130: // Check to see if the string is a file uri.
131: if (transferData.startsWith("file://")) {
132: try {
133: // we can have more than one file in the string so tokenize
134: // on whitespace. Let the user know if we find multiple
135: // tokens that they cannot place drop than one file at a
136: // time
137: StringTokenizer st = new StringTokenizer(
138: transferData);
139: if (st.countTokens() > 1) {
140: _session
141: .showErrorMessage(i18n.ONE_FILE_DROP_MESSAGE);
142: } else {
143: if (st.hasMoreTokens()) {
144: String fileUrlStr = st.nextToken();
145: URI uri = new URI(fileUrlStr);
146: result = new File(uri);
147: }
148: }
149: } catch (URISyntaxException e) {
150: s_log.error("handleUriListString: encountered an "
151: + "invalid URI: " + transferData, e);
152: }
153: } else {
154: // Not a uri - assume it is a string filename.
155: result = new File(transferData);
156: }
157: }
158: return result;
159: }
160:
161: /**
162: * Handles the JavaFileList data flavor which returns the data as a list of
163: * java.io.File objects.
164: *
165: * @param t
166: * the transferable to get the list from
167:
168: * @return the only file in the list
169: * @throws UnsupportedFlavorException
170: * @throws IOException
171: */
172: private File handleJavaFileList(Transferable t)
173: throws UnsupportedFlavorException, IOException {
174: File result = null;
175:
176: @SuppressWarnings("unchecked")
177: List<File> transferData = (List<File>) t
178: .getTransferData(DataFlavor.javaFileListFlavor);
179:
180: if (transferData.size() > 1) {
181: _session.showErrorMessage(i18n.ONE_FILE_DROP_MESSAGE);
182: } else {
183: result = transferData.get(0);
184: if (s_log.isInfoEnabled()) {
185: s_log.info("drop: path=" + result.getAbsolutePath());
186: }
187:
188: }
189: return result;
190: }
191: }
|