package pkg;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.Action;
import javax.swing.DropMode;
import javax.swing.JTree;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.TransferHandler;
import javax.swing.tree.TreeSelectionModel;
public class App {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new App();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* The number of nodes that will be randomly constructed. Must be smaller then NODE_NAMES.length.
*/
private static final int NODE_COUNT = 10;
/**
* An array containing random names that will be used for constructing the tree.
*/
private static final String[] NODE_NAMES = new String[] {
"Albert", "Annabell", "Benjamin", "Bella", "Cedric", "Cecile",
"David", "Danielle", "Emanuel", "Elisabeth", "Frederick", "Felicita",
"Georg", "Giselle", "Hans", "Henriette", "Ismael", "Irene",
"Joshua", "Joceline", "Kyle", "Kaithlin", "Lyod", "Lisa",
"Michael", "Michelle", "Norbert", "Nele", "Olaf", "Ophelia",
"Robert", "Renate", "Stuart", "Sabrina", "Theo", "Tania",
"Ulric", "Ursula", "Victor", "Veronica", "William", "Wilma"
};
/*
* If the static final variables have illegal values we will throw an exception at class initialization.
*/
static {
if (NODE_NAMES.length < NODE_COUNT) {
throw new RuntimeException("Node count must be no bigger then: "+NODE_NAMES.length);
}
}
public App() {
// Setup the frame
JFrame frmTreeModelTest = new JFrame();
frmTreeModelTest.setTitle("JTree Transfer Handler Test");
frmTreeModelTest.setSize(600, 480);
frmTreeModelTest.setLocationRelativeTo(null);
frmTreeModelTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Scroll panel for the tree
JScrollPane scrollPane = new JScrollPane();
frmTreeModelTest.getContentPane().add(scrollPane, BorderLayout.CENTER);
/*
* Construct our initial nodes.
* This will create a random tree which contains all kinds of names.
*/
Node rootNode = new Node("Root");
List<String> possibleNames = new ArrayList<>(Arrays.asList(NODE_NAMES));
List<Node> existingNodes = new ArrayList<>();
existingNodes.add(rootNode);
Random random = new Random();
for (int i = 0; i < NODE_COUNT; i++) {
int nameID = random.nextInt(possibleNames.size());
Node node = new Node(possibleNames.remove(nameID));
int parentID = random.nextInt(existingNodes.size());
Node parent = existingNodes.get(parentID);
parent.addChild(node);
existingNodes.add(node);
}
// The JTree that will be used for this test
JTree tree = new JTree();
tree.setModel(new MyModel(rootNode));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
tree.setTransferHandler(new JTreeTransferHandler());
tree.setDragEnabled(true);
tree.setDropMode(DropMode.ON_OR_INSERT);
/*
* This code was taken from the oracle tutorial website for Copy-Cut-Paste support.
* http://docs.oracle.com/javase/tutorial/uiswing/dnd/listpaste.html
*/
tree.getActionMap().put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
tree.getActionMap().put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
tree.getActionMap().put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
scrollPane.setViewportView(tree);
// Construct the menu bar with CCP functionality.
JMenuBar menuBar = new JMenuBar();
frmTreeModelTest.setJMenuBar(menuBar);
JMenu mnEdit = new JMenu("Edit");
menuBar.add(mnEdit);
JMenuItem mntmCopy = new JMenuItem("Copy");
mntmCopy.addActionListener(TransferActionListener.INSTANCE);
mntmCopy.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME));
mntmCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
mnEdit.add(mntmCopy);
JMenuItem mntmCut = new JMenuItem("Cut");
mntmCut.addActionListener(TransferActionListener.INSTANCE);
mntmCut.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME));
mntmCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
mnEdit.add(mntmCut);
JMenuItem mntmPaste = new JMenuItem("Paste");
mntmPaste.addActionListener(TransferActionListener.INSTANCE);
mntmPaste.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME));
mntmPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
mnEdit.add(mntmPaste);
// Show the frame
frmTreeModelTest.setVisible(true);
}
}