1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
public Fenetre(String nom){
super(nom);
panel = new JPanel();
panel.setLayout(new BorderLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.centre = new Centre();
this.connexion = new Connexion();
this.cardLayout = new CardLayout();
buildTree();
// Application de l'afficheur à l'arbre.
tree.setCellRenderer(new DefaultTreeCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
if ( value instanceof TypedDefaultMutableTreeNode ) {
TypedDefaultMutableTreeNode node = (TypedDefaultMutableTreeNode) value;
value = node.getType()+": " + node.getName();
}
return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
}
});
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
panel.add(centre,BorderLayout.CENTER);
tree.setPreferredSize(new Dimension(300,750));
panel.add(new JScrollPane(tree),BorderLayout.WEST);
menuBar.add(file);
menuBar.add(param);
menuBar.add(jeu);
this.menuItem1.setBackground(new Color(253,147,198));
this.menuItem2.setBackground(new Color(253,147,198));
this.menuItem3.setBackground(new Color(253,147,198));
this.menuItem4.setBackground(new Color(253,147,198));
this.menuItem5.setBackground(new Color(253,147,198));
this.menuItem6.setBackground(new Color(253,147,198));
this.menuItem7.setBackground(new Color(253,147,198));
this.menuItem8.setBackground(new Color(253,147,198));
this.menuItem9.setBackground(new Color(253,147,198));
file.add(menuItem1);
file.add(menuItem2);
file.add(menuItem3);
file.add(menuItem4);
file.add(menuItem5);
param.add(menuItem6);
param.add(menuItem7);
param.add(menuItem8);
param.add(menuItem9);
this.menuBar.setBackground(new Color(252,73,158));
setJMenuBar(menuBar);
main = new JPanel (cardLayout);
main.add("connexion",connexion);
main.add("panel",panel);
this.add(main);
this.setResizable(false);
this.pack();
this.setSize(400,250);
this.setVisible(true);
this.myActionListener = new MyActionListener(centre,connexion,this,tree);
this.control = new Control(centre,connexion,myActionListener,this,myDocumentListener);
myActionListener.addControl(control);
myDocumentListener.addControl(control);
}
private void buildTree(){
//Nous créons, avec notre hiérarchie, un arbre
tree = new JTree(racine);
//Que nous plaçons sur le ContentPane de notre JFrame à l'aide d'un scroll
this.getContentPane().add(new JScrollPane(tree));
}
public void createTable(JTree tree) {
String nomDeLaTable = JOptionPane.showInputDialog(tree,"Nom de la table");
if ( nomDeLaTable!=null ) {
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
TypedDefaultMutableTreeNode node = lookFor(root, NodeType.TABLE, nomDeLaTable);
if ( node!=null ) {
JOptionPane.showMessageDialog(tree, "Cette table existe déjà");
}
else {
TypedDefaultMutableTreeNode newNode = new TypedDefaultMutableTreeNode(NodeType.TABLE, nomDeLaTable);
model.insertNodeInto(newNode, root, root.getChildCount());
model.reload(newNode);
Object[] path = {root, newNode};
tree.setSelectionPath(new TreePath(path));
}
}
}
public void addAttribute(JTree tree) {
TreePath path = tree.getSelectionPath();
TypedDefaultMutableTreeNode node = (TypedDefaultMutableTreeNode) path.getLastPathComponent();
if (node.getType()==NodeType.TABLE ) {
addAttribute(tree, node);
}
}
private void addAttribute(JTree tree, TypedDefaultMutableTreeNode tableNode) {
System.out.println("2");
JTextField nameField = new JTextField();
JTextField valueField = new JTextField();
Object[] message = {
"Nom de l'attribut:", nameField,
"Valeur de l'attribut:", valueField
};
int option = JOptionPane.showConfirmDialog(tree, message, "Ajouter un attribut", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
if ( nameField.getText().trim().isEmpty() ) {
JOptionPane.showMessageDialog(tree, "Le nom de l'attribut est obligatoire");
} else {
String name = nameField.getText().trim();
String value = valueField.getText();
TypedDefaultMutableTreeNode attributeNode = new TypedDefaultMutableTreeNode(NodeType.ATTRIBUTE, name);
TypedDefaultMutableTreeNode valueNode = new TypedDefaultMutableTreeNode(NodeType.VALUE, value);
attributeNode.add(valueNode);
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.insertNodeInto(attributeNode, tableNode, tableNode.getChildCount());
model.reload(attributeNode);
Object[] path = {model.getRoot(), tableNode, attributeNode};
TreePath treePath = new TreePath(path);
tree.setSelectionPath(treePath);
tree.expandPath(treePath);
}
}
}
private TypedDefaultMutableTreeNode lookFor(TreeNode parent, NodeType nodeType, String name) {
for(int i=0; i<parent.getChildCount(); i++) {
TreeNode child = parent.getChildAt(i);
if ( child instanceof TypedDefaultMutableTreeNode ) {
TypedDefaultMutableTreeNode node = (TypedDefaultMutableTreeNode) child;
if ( nodeType!=null && node.getType()!=nodeType ) {
continue;
}
if ( node.getName().equals(name) ) {
return node;
}
}
if ( child.getAllowsChildren() && !child.isLeaf() ) {
TypedDefaultMutableTreeNode found = lookFor(child, nodeType, name);
if ( found!=null ) {
return found;
}
}
}
return null;
}
public enum NodeType {
TABLE(true),
ATTRIBUTE(true),
VALUE(false);
private final boolean allowsChildren;
private NodeType(boolean allowsChildren) {
this.allowsChildren=allowsChildren;
}
public boolean allowsChildren() {
return allowsChildren;
}
}
public class TypedDefaultMutableTreeNode extends DefaultMutableTreeNode {
private static final long serialVersionUID = 1L;
private final NodeType type;
public TypedDefaultMutableTreeNode(NodeType type, String name) {
super(name, type.allowsChildren());
this.type=type;
}
public NodeType getType() {
return type;
}
public String getName() {
return (String) getUserObject();
}
} |