* @author boldrini
*/
public class CheckboxTreeApplet extends JApplet {
private void createGUI() {
final CheckboxTree tree = new CheckboxTree();
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);// SINGLE_TREE_SELECTION);
// Events panel
JPanel eventsPanel = new JPanel();
Border eventsBorder = BorderFactory.createTitledBorder("Events");
eventsPanel.setBorder(eventsBorder);
final JTextArea textArea = new JTextArea(7, 20);
textArea.setLineWrap(true);
JScrollPane textPane = new JScrollPane(textArea);
tree.addTreeCheckingListener(new TreeCheckingListener() {
public void valueChanged(TreeCheckingEvent e) {
// convert(e.getLeadingPath());
textArea.append("Checking event source: " + (e.getPath().getLastPathComponent()) + "\n");
}
// private void convert(TreePath changedPath) {
// Object[] path = changedPath.getPath();
// TreeModel tm = tree.getModel();
// for (int i = 0; i < path.length - 1; i++) {
// System.out.println(tm.getIndexOfChild(path[i], path[i + 1]));
// }
// System.out.println();
// }
});
eventsPanel.add(textPane);
// Modes panel
JPanel modesPanel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Checking Mode");
modesPanel.setBorder(border);
ButtonGroup group = new ButtonGroup();
JRadioButton aRadioButton = null;
final CheckingMode modes[] = { CheckingMode.SIMPLE, CheckingMode.PROPAGATE, CheckingMode.PROPAGATE_PRESERVING_UNCHECK,
CheckingMode.PROPAGATE_PRESERVING_CHECK };
for (int i = 0, n = modes.length; i < n; i++) {
final int g = i;
ActionListener modeListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
tree.getCheckingModel().clearChecking();
tree.getCheckingModel().setCheckingMode(modes[g]);
}
};
aRadioButton = new JRadioButton(modes[i].toString());
modesPanel.add(aRadioButton);
group.add(aRadioButton);
aRadioButton.addActionListener(modeListener);
}
aRadioButton.setSelected(true);
tree.getCheckingModel().setCheckingMode(CheckingMode.PROPAGATE_PRESERVING_CHECK);
// Actions Panel
JPanel commandsPanel = new JPanel(new GridLayout(0, 1));
Border commandsBorder = BorderFactory.createTitledBorder("Actions");
commandsPanel.setBorder(commandsBorder);
// expand all action
JButton actionButton = new JButton("Expand all");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
tree.expandAll();
}
};
actionButton.addActionListener(actionListener);
commandsPanel.add(actionButton);
// clear all action
JButton clearButton = new JButton("Clear checking");
ActionListener clearListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
tree.clearChecking();
}
};
clearButton.addActionListener(clearListener);
commandsPanel.add(clearButton);
// add a children action
JButton addButton = new JButton("Add child");
ActionListener addListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
TreePath selected = tree.getSelectionPath();
if (selected != null) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getLastPathComponent();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("child " + (parent.getChildCount() + 1));
parent.add(child);
DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel();
dtm.nodesWereInserted(parent, new int[] { parent.getIndex(child) });
tree.expandPath(selected);
}
}
};
addButton.addActionListener(addListener);
commandsPanel.add(addButton);
// remove a children action
JButton removeButton = new JButton("Remove selected node");
ActionListener removeListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
TreePath selected = tree.getSelectionPath();
if (selected != null) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) selected.getLastPathComponent();
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) child.getParent();
if (parent != null) {
int index = parent.getIndex(child);
child.removeFromParent();
DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel();
dtm.nodesWereRemoved(parent, new int[] { index }, new TreeNode[] { child });
}
}
}
};