/*
* Copyright (C) 2011 Alasdair C. Hamilton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package ketUI;
import java.io.*;
import java.awt.dnd.*;
import java.awt.Point;
import javax.swing.JComponent;
import java.awt.datatransfer.*;
import javax.swing.TransferHandler;
import javax.swing.TransferHandler.DropLocation;
import geom.Position;
import ket.Selection;
import ketUI.Clipboard;
import ket.math.Argument;
import ket.math.Equation;
import ket.MathCollection;
import ketUI.panel.KetPanel;
import ket.math.KnownArguments;
import ket.math.convert.ArgumentParser;
public class KetTransfer extends TransferHandler {
Clipboard clipboard;
KnownArguments knownArguments;
MathCollection mathCollection;
KetPanel ketPanel; // TODO: Can this change during full screen?
public KetTransfer(KetPanel ketPanel, KnownArguments knownArguments, Clipboard clipboard, MathCollection mathCollection) {
super();
this.ketPanel = ketPanel;
this.knownArguments = knownArguments;
this.clipboard = clipboard;
this.mathCollection = mathCollection;
}
private boolean supported(int value, int flag) {
return (flag & value)==flag;
}
@Override
public boolean canImport(TransferHandler.TransferSupport transferSupport) {
if ( ! transferSupport.isDataFlavorSupported(DataFlavor.stringFlavor) ) {
return false;
}
// WARNING: This risks data loss on the part of the transfer source.
if ( ! supported(transferSupport.getSourceDropActions(), TransferHandler.COPY_OR_MOVE) ) {
return false;
}
transferSupport.setDropAction(TransferHandler.COPY);
return true;
}
@Override
public boolean importData(TransferHandler.TransferSupport transferSupport) {
// Select current mouse location.
// TODO: Move this to a separate selectCurrent() method where appropriate.
DropLocation dl = transferSupport.getDropLocation();
Point p = dl.getDropPoint();
Position release = new Position(p.getX(), p.getY());
Argument finishArgument = ketPanel.findDeepestArgument(release);
if (finishArgument==null) {
Equation finishEquation = finishArgument!=null ?
finishArgument.getEquation() :
ketPanel.pointToEquation(release);
if (finishEquation==null) {
Ket.out.println(" !!! No argument at mouse release location !!! ");
return false;
}
finishArgument = finishEquation.getRoot();
}
mathCollection.getSelection().setOnly(finishArgument);
if ( ! canImport(transferSupport) ) {
return false;
}
Transferable t = transferSupport.getTransferable();
String string = null;
try {
string = (String) t.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
Ket.out.println(" !!! A string-flavor transfer exception occurred while reading (drag and drop) transfer data !!! ");
return false;
} catch (IOException ioe) {
Ket.out.println(" !!! IO Exception while reading (drag and drop) transfer data !!! ");
return false;
}
String[] lines = string.split("\n");
// TODO: Re-join wrapped lines, 'text... \'?
if (lines.length==1) {
// Single line.
Argument arg = ArgumentParser.parseArgument(string, knownArguments, clipboard, mathCollection);
if (arg!=null) {
Selection selection = mathCollection.getSelection();
Equation appendedEquation = new Equation(arg);
selection.appendEquation(appendedEquation);
}
} else {
// Multiple lines.
for (String next : lines) {
Equation e = mathCollection.processLine(next);
if (e!=null) {
Selection selection = mathCollection.getSelection();
Equation appendedEquation = new Equation(e.getRoot());
selection.appendEquation(appendedEquation);
}
}
}
ketPanel.updateAndRepaint();
return true;
}
//TransferHandler transferHandler =
//jFrame.setTransferHandler(transferHandler);
//TransferHandler:-
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY_OR_MOVE;
}
@Override
public Transferable createTransferable(JComponent c) {
return new StringSelection("THIS IS WORKING");
}
@Override
public void exportDone(JComponent c, Transferable t, int action) {
/*-
if (action == MOVE) {
c.removeSelection();
}
*/
// Do nothing.
}
}