import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import util.StartTree;
import util.pathMethod;
import util.pathMethodSaveInFile;
public class TreeProgram extends JFrame {
private JPanel contentPane;
private JTextField txtEingabeEinesSystems;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TreeProgram frame = new TreeProgram();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TreeProgram() {
setResizable(false);
setTitle("Bebauungsplan in K-SIE-4");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 483);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
final DefaultTreeModel model;
final JButton btnUnterpunktAnlegen = new JButton("Unterpunkt anlegen");
final JButton btnLokalisieren = new JButton("Lokalisieren");
final JButton btnSysAnlegen = new JButton("System anlegen");
final JButton btnEditieren = new JButton("Editieren");
final JButton btnLoeschen = new JButton("Loeschen");
final JButton btnSuchen = new JButton("Suchen");
final JButton btnSpeichern = new JButton("Speichern");
final JButton btnSchliessen = new JButton("Schliessen");
txtEingabeEinesSystems = new JTextField();
txtEingabeEinesSystems.setText("...");
txtEingabeEinesSystems.setColumns(10);
// Tree konfig
final StartTree startTree = new StartTree();
TreeNode root = startTree.makeSampleTree();
model = new DefaultTreeModel(root);
final JTree tree = new JTree(model);
// **************************
// SCHLIESSEN -!!-
// **************************
ActionListener back = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// ***********
// ZURÜCK -!!-
// ***********
setVisible(false);
final MainFRM mainFRM = new MainFRM();
mainFRM.setVisible(true);
mainFRM.setLocationRelativeTo(null);
};
};
// **************************
// LÖSCHEN -!!-
// **************************
ActionListener delete = new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (event.getSource().equals(btnLoeschen)) {
if (selectedNode.getParent() != null)
model.removeNodeFromParent(selectedNode);
return;
}
} catch (Exception e) {
JOptionPane.showConfirmDialog(null, "Nichts selektiert!",
"Löschen nicht möglich!", JOptionPane.DEFAULT_OPTION);
}
};
};
// **************************
// LOKALISIEREN-!!-
// **************************
ActionListener path = new ActionListener() {
public void actionPerformed(ActionEvent event) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (event.getSource().equals(btnLokalisieren)) {
TreePath path = tree.getSelectionPath();
final pathMethod pathMethod = new pathMethod();
pathMethod.ausgabe(path);
if (selectedNode.getParent() != null)
// JOptionPane.showConfirmDialog((Component) null, "" +
// tree.getSelectionPath(),
// "Pfadangabe", JOptionPane.DEFAULT_OPTION);
return;
}
};
};
// **************************
// SPEICHERN*(Baum) -!!-
// **************************
ActionListener save = new ActionListener() {
public void actionPerformed(ActionEvent event) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (event.getSource().equals(btnSpeichern)) {
TreePath path = tree.getSelectionPath();
System.out.println(tree.getSelectionPath());
final pathMethodSaveInFile pathMethodSaveInFile = new pathMethodSaveInFile();
try {
pathMethodSaveInFile.ausgabe(path);
System.out.println("fertig");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (selectedNode.getParent() != null)
// JOptionPane.showConfirmDialog((Component) null, "" +
// tree.getSelectionPath(),
// "Pfadangabe", JOptionPane.DEFAULT_OPTION);
return;
}
};
};
// **************************
// EDITIEREN -!!-
// **************************
ActionListener edit = new ActionListener() {
public void actionPerformed(ActionEvent event) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (event.getSource().equals(btnEditieren)) {
String renamePunkt = JOptionPane.showInputDialog("Umbenennen in...");
DefaultMutableTreeNode renameNode = new DefaultMutableTreeNode(renamePunkt);
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode
.getParent();
if (parent != null) {
int selectedIndex = parent.getIndex(selectedNode);
model.removeNodeFromParent(selectedNode);
model.insertNodeInto(renameNode, parent, selectedIndex);
}
return;
}
};
};
// **************************
// SYSTEM HINZUFÜGEN -!!-
// **************************
ActionListener addSys = new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String name = JOptionPane.showInputDialog("Bitte Namen eingeben...");
// String beschreibung =
// JOptionPane.showInputDialog("Beschreibung eingeben...");
if ((name != null) || (name == "")) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(name);
if (event.getSource().equals(btnSysAnlegen)) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode
.getParent();
if (parent != null) {
int selectedIndex = parent.getIndex(selectedNode);
model.insertNodeInto(newNode, parent, selectedIndex + 1);
}
}
if (event.getSource().equals(btnUnterpunktAnlegen)) {
model.insertNodeInto(newNode, selectedNode,
selectedNode.getChildCount());
}
// ******************
// AKTUALISIEREN -!!-
// ******************
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
// **************************
// SYSTEMICON SETZEN!
//
// FUNZT NOCH NICHT -!!-
// **************************
final ImageIcon icon = new ImageIcon("Y:/iconsmall.jpg");
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setLeafIcon(icon);
tree.setCellRenderer(renderer);
}
} catch (NullPointerException e) {
JOptionPane.showConfirmDialog(null, "Keine Bezeichnung eingegeben",
"Hinzufügen fehlgeschlagen!", JOptionPane.DEFAULT_OPTION);
}
}
};
// **************************
// UNTERPUNKT HINZUFÜGEN -!!-
// **************************
ActionListener addFolder = new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String name = JOptionPane.showInputDialog("Bitte Namen eingeben...");
// String beschreibung =
// JOptionPane.showInputDialog("Beschreibung eingeben...");
if ((name != null) || (name == "")) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(name);
if (event.getSource().equals(btnUnterpunktAnlegen)) {
model.insertNodeInto(newNode, selectedNode,
selectedNode.getChildCount());
}
// ******************
// AKTUALISIEREN -!!-
// ******************
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}
} catch (NullPointerException e) {
JOptionPane.showConfirmDialog(null, "Keine Bezeichnung eingegeben",
"Hinzufügen fehlgeschlagen!", JOptionPane.DEFAULT_OPTION);
}
};
};
btnEditieren.addActionListener(edit);
btnLoeschen.addActionListener(delete);
btnLokalisieren.addActionListener(path);
btnSysAnlegen.addActionListener(addSys);
btnUnterpunktAnlegen.addActionListener(addFolder);
btnSpeichern.addActionListener(save);
btnSchliessen.addActionListener(back);
JButton btnDetails = new JButton("Details");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane
.setHorizontalGroup(gl_contentPane
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_contentPane
.createSequentialGroup()
.addGroup(
gl_contentPane
.createParallelGroup(Alignment.LEADING)
.addComponent(tree,
GroupLayout.PREFERRED_SIZE, 433,
GroupLayout.PREFERRED_SIZE)
.addComponent(btnDetails))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(
gl_contentPane
.createParallelGroup(Alignment.TRAILING)
.addGroup(
gl_contentPane
.createSequentialGroup()
.addGroup(
gl_contentPane
.createParallelGroup(
Alignment.LEADING)
.addComponent(
btnSchliessen,
GroupLayout.DEFAULT_SIZE,
133,
Short.MAX_VALUE)
.addComponent(
btnSpeichern,
GroupLayout.DEFAULT_SIZE,
133,
Short.MAX_VALUE)
.addComponent(
btnUnterpunktAnlegen,
GroupLayout.PREFERRED_SIZE,
133,
Short.MAX_VALUE)
.addComponent(
btnEditieren,
Alignment.TRAILING,
GroupLayout.DEFAULT_SIZE,
133,
Short.MAX_VALUE)
.addComponent(
btnSysAnlegen,
Alignment.TRAILING,
GroupLayout.PREFERRED_SIZE,
133,
Short.MAX_VALUE)
.addComponent(
btnLoeschen,
GroupLayout.DEFAULT_SIZE,
133,
Short.MAX_VALUE)
.addComponent(
btnSuchen,
GroupLayout.DEFAULT_SIZE,
133,
Short.MAX_VALUE)
.addComponent(
txtEingabeEinesSystems,
GroupLayout.DEFAULT_SIZE,
133,
Short.MAX_VALUE))
.addGap(6))
.addGroup(
gl_contentPane
.createSequentialGroup()
.addComponent(
btnLokalisieren)
.addGap(29)))));
gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(
Alignment.TRAILING,
gl_contentPane
.createSequentialGroup()
.addGroup(
gl_contentPane
.createParallelGroup(Alignment.TRAILING)
.addComponent(tree, Alignment.LEADING,
GroupLayout.DEFAULT_SIZE, 409,
Short.MAX_VALUE)
.addGroup(
gl_contentPane
.createSequentialGroup()
.addComponent(btnUnterpunktAnlegen)
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(btnSysAnlegen)
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(btnEditieren)
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(btnLoeschen)
.addGap(20)
.addComponent(
txtEingabeEinesSystems,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(btnSuchen)
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(btnLokalisieren)
.addPreferredGap(
ComponentPlacement.RELATED,
176, Short.MAX_VALUE)
.addComponent(btnSpeichern)))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(
gl_contentPane
.createParallelGroup(Alignment.BASELINE)
.addComponent(btnSchliessen,
GroupLayout.PREFERRED_SIZE, 30,
GroupLayout.PREFERRED_SIZE)
.addComponent(btnDetails))));
contentPane.setLayout(gl_contentPane);
}
}