import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
/**
* <p>
* TODO This is what TreeExample does.
* </p>
*
* @author Darimont
* @since 10.08.2004
* @version $Rev:$
*/
public class TreeExample extends JFrame {
private JScrollPane scrollPane;
private JTree jtree;
private TreeModel model;
private JButton btnLoad, btnStore;
private JFileChooser chooser = new JFileChooser();
public TreeExample() {
super("TreeExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
model = new DefaultTreeModel(new DefaultMutableTreeNode("ROOT"));
/*
* Diese Methode hier nach dem Speichern des ersten Models auskommentieren.. danach
* gespeichertes Model mit Load neu reinladen ...
*/
appendChildren(model);
jtree = new JTree(model);
scrollPane = new JScrollPane(jtree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
ActionListener handler = new ActionHandler();
btnLoad = new JButton("Load");
btnLoad.addActionListener(handler);
btnStore = new JButton("Store");
btnStore.addActionListener(handler);
panel.add(btnLoad, BorderLayout.NORTH);
panel.add(btnStore, BorderLayout.SOUTH);
getContentPane().add(panel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
/**
* @param model2
*/
private void appendChildren(TreeModel mod) {
MutableTreeNode root = (MutableTreeNode) mod.getRoot();
for (int i = 0; i < 10; i++) {
DefaultMutableTreeNode child = new DefaultMutableTreeNode("child" + i);
root.insert(child, i);
}
}
public static void main(String[] args) {
new TreeExample();
}
class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == btnLoad) {
int result = chooser.showOpenDialog(((JButton) btnLoad).getParent());
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Object o = ois.readObject();
model = (TreeModel) o;
jtree.setModel(model);
jtree.updateUI();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
}
}
} else if (src == btnStore) { //store
int result = chooser.showSaveDialog(((JButton) btnStore).getParent());
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(model);
oos.flush();
oos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
}
}
}
}