//Copyright (c)2005 Holobloc Inc.
//Contributed to the OOONEIDA FBench project under the Common Public License.
package fbench.graph;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;
import fbench.graph.model.GraphModel;
import fbench.tree.LibraryTreeNode;
/**
* An importer for LibraryTreeNodes into GraphViews.
*
* @author JHC
* @version 20050903/JHC
*/
public class GraphViewTransfer extends TransferHandler {
/** The JTree of Files containing LibraryElements. */
private JTree libraryTree;
/** The prefix for acceptable String data. */
public GraphViewTransfer(JTree libraryTree) {
super();
this.libraryTree = libraryTree;
}
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
if (!(comp instanceof GraphView))
return false;
LibraryTreeNode ltn = getSelectedNode();
if (ltn == null)
return false;
File fl = ltn.getFile();
if (fl.isDirectory())
return false;
GraphModel mdl = ((GraphView) comp).getModel();
//System.out.println("canImport: " + mdl);
if (!(mdl instanceof GraphModel))
return false;
return mdl.canAccept(fl.getName());
}
public boolean importData(JComponent c, Transferable t) {
String elname = ((LibraryTreeNode) libraryTree.getSelectionPath()
.getLastPathComponent()).getText();
System.out.println("importData: " + elname);
return false;
}
/**
* Returns the selected node from the Library tree, or <TT>null</TT> if
* none is selected.
*/
protected LibraryTreeNode getSelectedNode() {
TreePath tp = libraryTree.getSelectionPath();
if (tp == null)
return null;
Object lastcomp = tp.getLastPathComponent();
return (lastcomp instanceof LibraryTreeNode) ? (LibraryTreeNode) lastcomp
: null;
}
}