ingenias.editor.widget.DnDJTree Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sociaalmled Show documentation
Show all versions of sociaalmled Show documentation
A editor for modelling scenarios for PHAT simulator
/*
This code has been extracted and modified from an original
work from Rob Kenworthy and an example from Sheetal Gupta
(http://java.sun.com/docs/books/tutorial/dnd/sheetal.html)
*/
package ingenias.editor.widget;
import javax.swing.JTree;
import java.awt.dnd.*;
import java.awt.Point;
import java.awt.datatransfer.*;
import java.awt.Cursor;
import java.io.*;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.*;
import java.awt.Component;
import org.jgraph.JGraph;
import javax.swing.JViewport;
import java.awt.*;
import java.awt.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import java.util.*;
public class DnDJTree extends JTree implements java.io.Serializable,
DropTargetListener,DragSourceListener,DragGestureListener{
public JScrollPane getContainer() {
return container;
}
public void setContainer(JScrollPane container) {
this.container = container;
}
DropTarget dropTarget=null;
DragSource dragSource=null;
JScrollPane container=null;
Vector expansionPaths=new Vector();
boolean dragOn=false;
DefaultMutableTreeNode nodeInTransfer=null;
TreeNode root=null;
public DnDJTree(JScrollPane jsp,TreeNode tn) {
super(tn);
this.setAutoscrolls(true);
this.container=jsp;
final JTree jt=this;
this.root=tn;
dropTarget=new DropTarget(this,this);
dragSource=new DragSource();
dragSource.createDefaultDragGestureRecognizer(this,
DnDConstants.ACTION_MOVE,this);
this.setEditable(true);
this.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e) {
}
// Code added to make nodes go up and down among siblings
public void keyTyped(final KeyEvent e) {
if (e.getKeyChar()=='q' && jt.getSelectionPath()!=null){
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)jt.getSelectionPath().getLastPathComponent();
if (node.getParent()!=null){
int index=node.getParent().getIndex(node);
if (index>0){
DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent();
node.removeFromParent();
parent.insert(node, index-1);
node.setParent(parent);
storeTreeExpansionPaths();
((DefaultTreeModel)getModel()).reload();
validate();
restoreTreeExpansionPath();
jt.addSelectionPath(new TreePath(node.getPath()));
}
}
}
if (e.getKeyChar()=='z' && jt.getSelectionPath()!=null){
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)jt.getSelectionPath().getLastPathComponent();
if (node.getParent()!=null){
int index=node.getParent().getIndex(node);
if (index10)){
container.validate();
container.getVerticalScrollBar().setValue(container.getVerticalScrollBar().getValue()-10);
}
// this.getLocation()
if (dmtn!=null){
this.setSelectionPath(new TreePath(dmtn.getPath()));
} else {
}
}
public void dragOver (DropTargetDragEvent event) {
this.moveMouseWithDrag(event.getLocation());
}
/**
* is invoked when the user changes the dropAction
*
*/
public void dropActionChanged ( DragSourceDragEvent event) {
}
}