import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
public class Results extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel resultPanel = new JPanel();
private JPanel caption = new JPanel();
private JLabel captionA = new JLabel();
private JLabel captionAA = new JLabel();
private JLabel captionB = new JLabel();
private JLabel captionBB = new JLabel();
// private JLabel captionC = new JLabel();
public Results() {
super("DBCompareTool");
caption.setLayout(new FlowLayout());
captionA.setForeground(Color.GREEN);
captionB.setForeground(Color.YELLOW);
captionA.setText("■");
captionAA.setText("NEW");
captionB.setText("■");
captionBB.setText("DIFFERENCE");
caption.add(captionA);
caption.add(captionAA);
caption.add(captionB);
caption.add(captionBB);
resultPanel.setLayout(new FlowLayout(5));
this.setLayout(new BorderLayout());
this.add(new JLabel("Compare Results: "), BorderLayout.NORTH);
this.add(resultPanel, BorderLayout.CENTER);
this.add(caption, BorderLayout.SOUTH);
}
public void generateStruct(String databaseName, ArrayList<String> additionalTables, ArrayList<String> additionalColums) {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Kategorie 1");
node.add(new DefaultMutableTreeNode(new NodeObject("Unterkategorie 1", "test", "new")));
node.add(new DefaultMutableTreeNode(new NodeObject("Unterkategorie 2", "test", "difference")));
node.add(new DefaultMutableTreeNode(new NodeObject("Unterkategorie 3", "test", "")));
rootNode.add(node);
node = new DefaultMutableTreeNode("Ub0r Kategorie 2");
DefaultMutableTreeNode unode = new DefaultMutableTreeNode("Kategorie 2");
DefaultMutableTreeNode uunode = new DefaultMutableTreeNode(new NodeObject("Unterkategorie 4", "test", "new"));
uunode.add(new DefaultMutableTreeNode(new NodeObject("Unterkategorie 5", "test", "difference")));
unode.add(new DefaultMutableTreeNode(new NodeObject("Unterkategorie 5", "test", "")));
unode.add(new DefaultMutableTreeNode(new NodeObject("Unterkategorie 6", "test", "new")));
node.add(unode);
rootNode.add(node);
// DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
// for (int i = 0; i < additionalTables.size(); i++) {
// DefaultMutableTreeNode node = new
// DefaultMutableTreeNode(additionalTables.get(i));
// rootNode.add(node);
// }
// if (!additionalColums.isEmpty()) {
// uniqueRowTables(additionalColums);
// for (int i = 0; i < uniqueRowTables(additionalColums).size(); i++) {
// DefaultMutableTreeNode node = new
// DefaultMutableTreeNode(uniqueRowTables(additionalColums).get(i).substring(0,
// additionalColums.get(i).indexOf(".")));
// for (int j = 0; j < additionalColums.size(); j++) {
// if (additionalColums.get(j).substring(0,
// additionalColums.get(j).indexOf(".")).equals(uniqueRowTables(additionalColums).get(i).substring(0,
// additionalColums.get(0).indexOf(".")))) {
// node.add(new
// DefaultMutableTreeNode(additionalColums.get(j).substring(additionalColums.get(0).indexOf(".")
// + 1)));
// }
// }
// rootNode.add(node);
// }
JTree tree = new JTree(rootNode) {
public void startEditingAtPath(TreePath path) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) path.getLastPathComponent();
if (!treeNode.isLeaf()) {
super.startEditingAtPath(path);
}
}
};
tree.setCellRenderer(new MyCellRenderer());
tree.setEditable(false);
this.add(new JScrollPane(tree), BorderLayout.CENTER);
pack();
}
public ArrayList<String> uniqueRowTables(ArrayList<String> additionalColumns) {
ArrayList<String> uniqueRowTables = new ArrayList<String>();
for (int i = 0; i < additionalColumns.size(); i++) {
if (i == 0) {
uniqueRowTables.add(additionalColumns.get(i).substring(0, additionalColumns.get(i).indexOf(".")));
} else if (!additionalColumns.get(i).substring(0, additionalColumns.get(i).indexOf(".")).equals(additionalColumns.get(i - 1).substring(0, additionalColumns.get(i).indexOf(".")))) {
uniqueRowTables.add(additionalColumns.get(i).substring(0, additionalColumns.get(i).indexOf(".")));
}
}
return uniqueRowTables;
}
class NodeObject {
private String nodeName;
private String nodeDeclaration;
private String nodeType;
public NodeObject(String nodeName, String nodeDeclaration, String nodeType) {
this.nodeName = nodeName;
this.nodeDeclaration = nodeDeclaration;
this.nodeType = nodeType;
}
public String getNodeName() {
return nodeName;
}
public String getNodeDeclaration() {
return nodeDeclaration;
}
public String getNodeType() {
return nodeType;
}
}
class MyCellRenderer extends DefaultTreeCellRenderer {
private DefaultMutableTreeNode treeNode;
private NodeObject nObj;
private JPanel Resultpanel = new JPanel();
private JLabel nodeLabel = new JLabel();
private JLabel nodeDeclaration = new JLabel();
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
treeNode = (DefaultMutableTreeNode) value;
if (treeNode.getUserObject() instanceof NodeObject) {
nObj = (NodeObject) treeNode.getUserObject();
nodeLabel.setText(nObj.getNodeName());
nodeDeclaration.setText(nObj.getNodeDeclaration());
if (nObj.getNodeType().equals("new")) {
Resultpanel.setBackground(Color.green);
} else if (nObj.getNodeType().equals("difference")) {
Resultpanel.setBackground(Color.YELLOW);
} else {
Resultpanel.setBackground(Color.lightGray);
}
Resultpanel.add(nodeLabel);
Resultpanel.add(nodeDeclaration);
return Resultpanel;
}
this.setIcon(null);
return this;
}
}
}