001    /* BEE - Book Editing Environment for PAVLOV
002     * Copyright (C) 2004 T.J. Willis
003     *
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *
018     * $Header: /cvsroot/pavlov/net/sourceforge/bee/StringTransferHandler.java,v 1.3 2004/06/03 03:18:23 tj_willis Exp $
019     */
020    
021    /*
022     * StringTransferHandler.java is used by the 1.4
023     * ExtendedDnDDemo.java example.
024     */
025    package net.sourceforge.bee;
026    
027    import java.awt.datatransfer.*;
028    import javax.swing.*;
029    import java.io.IOException;
030    
031    public abstract class StringTransferHandler extends TransferHandler {
032        
033        protected abstract String exportString(JComponent comp);
034        protected abstract void importString(JComponent comp, String str);
035        protected abstract void cleanup(JComponent comp, boolean remove);
036        
037        protected Transferable createTransferable(JComponent comp) {
038            return new StringSelection(exportString(comp));
039        }
040        
041        public int getSourceActions(JComponent comp) {
042            return COPY_OR_MOVE;
043        }
044        
045        public boolean importData(JComponent comp, Transferable trans) {
046            boolean ret = false;
047            if (canImport(comp, trans.getTransferDataFlavors())) {
048                try {
049                    String str = (String)trans.getTransferData(DataFlavor.stringFlavor);
050                    importString(comp, str);
051                    ret = true;
052                } catch (UnsupportedFlavorException ufe) {
053                } catch (IOException ioe) {
054                }
055            }
056    
057            return ret;
058        }
059        
060        protected void exportDone(JComponent comp, Transferable data, int action) {
061            cleanup(comp, action == MOVE);
062        }
063        
064        public boolean canImport(JComponent comp, DataFlavor[] flavors) {
065            boolean ret = false;
066            for (int i = 0; i < flavors.length; i++) {
067                if (DataFlavor.stringFlavor.equals(flavors[i])) {
068                    ret = true;
069                } else if (DataFlavor.plainTextFlavor.equals(flavors[i])) {
070                     ret = true;
071                }
072            }
073            return ret;
074        }
075    }