Résolution #8 Modifications classes et méthodes
This commit is contained in:
371
AppThinker/src/ClassPropertiesWindow.java
Normal file
371
AppThinker/src/ClassPropertiesWindow.java
Normal file
@@ -0,0 +1,371 @@
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.Border;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.table.TableCellEditor;
|
||||
import javax.swing.table.TableColumn;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* Classe permettant la création de fenêtres pour la modification des propriétés des classes.
|
||||
* @author V.BOULANGER
|
||||
*/
|
||||
public class ClassPropertiesWindow extends JFrame {
|
||||
|
||||
private UmlDiagram _umlDiagram;
|
||||
private Class _class;
|
||||
private JTextField _nameField;
|
||||
private JTable _attributesTable;
|
||||
private JScrollPane _scrollAttributes;
|
||||
private String[] _attributesColumns = {"Name", "Access modifier", "Type"};
|
||||
private DefaultTableModel _attributeModel;
|
||||
private JTable _methodsTable;
|
||||
private JScrollPane _scrollMethods;
|
||||
private String[] _methodsColumns = {"Name", "Access modifier", "Type", "Arguments"};
|
||||
private DefaultTableModel _methodModel;
|
||||
|
||||
|
||||
/**
|
||||
* Constructeur - Crée une instance de la fenêtre de propriétés de classe à partir d'un diagramme et de la classe à modifier.
|
||||
* @param umlDiagram Le diagramme qui contient la classe.
|
||||
* @param a La classe à modifier.
|
||||
*/
|
||||
public ClassPropertiesWindow(UmlDiagram umlDiagram, Class a){
|
||||
_umlDiagram = umlDiagram;
|
||||
_class = a;
|
||||
|
||||
//Paramétrage de la fenêtre
|
||||
this.setTitle("Edit properties - " + a.getName());
|
||||
this.setSize(new Dimension(800, 300));
|
||||
Image img = null;
|
||||
try { img = ImageIO.read(AppThinker.class.getResource("img/logoAppThinker.png")); } catch (Exception ex) { }
|
||||
this.setIconImage(img);
|
||||
this.setResizable(false);
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setLayout(new BorderLayout());
|
||||
|
||||
//Espace général de la fenêtre
|
||||
JPanel generalPanel = new JPanel();
|
||||
generalPanel.setLayout(new BoxLayout(generalPanel, BoxLayout.Y_AXIS));
|
||||
generalPanel.setAlignmentX(LEFT_ALIGNMENT);
|
||||
|
||||
//Espace de modification du nom
|
||||
JPanel namePan = new JPanel();
|
||||
namePan.setLayout(new BoxLayout(namePan, BoxLayout.X_AXIS));
|
||||
JLabel nameLbl = new JLabel("Name : ");
|
||||
_nameField = new JTextField();
|
||||
_nameField.setPreferredSize(new Dimension(300, 20));
|
||||
_nameField.setText(a.getName());
|
||||
namePan.add(nameLbl);
|
||||
namePan.add(_nameField);
|
||||
generalPanel.add(namePan);
|
||||
|
||||
JLabel attrLbl = new JLabel("Edit attributes");
|
||||
generalPanel.add(attrLbl);
|
||||
|
||||
//Espace de modification des attributs
|
||||
JPanel attributesPan = new JPanel();
|
||||
attributesPan.setLayout(new BoxLayout(attributesPan, BoxLayout.X_AXIS));
|
||||
|
||||
_attributeModel = new DefaultTableModel(_attributesColumns, 0);
|
||||
_attributesTable = new JTable(_attributeModel);
|
||||
|
||||
_scrollAttributes = new JScrollPane(_attributesTable);
|
||||
_scrollAttributes.setPreferredSize(new Dimension(350, 100));
|
||||
attributesPan.add(_scrollAttributes);
|
||||
|
||||
JPanel attributesTableModifier = new JPanel();
|
||||
attributesTableModifier.setLayout(new BoxLayout(attributesTableModifier, BoxLayout.Y_AXIS));
|
||||
JButton addAttribute = new JButton("+");
|
||||
addAttribute.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
addAttribute();
|
||||
}
|
||||
});
|
||||
JButton removeAttribute = new JButton("-");
|
||||
removeAttribute.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
removeAttribute();
|
||||
}
|
||||
});
|
||||
attributesTableModifier.add(addAttribute);
|
||||
attributesTableModifier.add(removeAttribute);
|
||||
|
||||
attributesPan.add(attributesTableModifier);
|
||||
|
||||
generalPanel.add(attributesPan);
|
||||
|
||||
JLabel methLbl = new JLabel("Edit methods");
|
||||
generalPanel.add(methLbl);
|
||||
|
||||
//Espace de modification des méthodes
|
||||
JPanel methodsPan = new JPanel();
|
||||
methodsPan.setLayout(new BoxLayout(methodsPan, BoxLayout.X_AXIS));
|
||||
|
||||
_methodModel = new DefaultTableModel(_methodsColumns, 0);
|
||||
_methodsTable = new JTable(_methodModel);
|
||||
|
||||
_methodsTable.addMouseListener(new MouseAdapter() {
|
||||
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
Point p = e.getPoint();
|
||||
int col = _methodsTable.columnAtPoint(p);
|
||||
int row = _methodsTable.rowAtPoint(p);
|
||||
System.out.println("clic");
|
||||
if (col == 3) {
|
||||
openArgumentsWindow(_class.getMethods().get(row));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
_scrollMethods = new JScrollPane(_methodsTable);
|
||||
_scrollMethods.setPreferredSize(new Dimension(350, 100));
|
||||
methodsPan.add(_scrollMethods);
|
||||
|
||||
JPanel methodsTableModifier = new JPanel();
|
||||
methodsTableModifier.setLayout(new BoxLayout(methodsTableModifier, BoxLayout.Y_AXIS));
|
||||
JButton addMethod = new JButton("+");
|
||||
addMethod.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
addMethod();
|
||||
}
|
||||
});
|
||||
JButton removeMethod = new JButton("-");
|
||||
removeMethod.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
removeMethod();
|
||||
}
|
||||
});
|
||||
methodsTableModifier.add(addMethod);
|
||||
methodsTableModifier.add(removeMethod);
|
||||
|
||||
methodsPan.add(methodsTableModifier);
|
||||
|
||||
generalPanel.add(methodsPan);
|
||||
|
||||
//Bouton de sauvegarde
|
||||
JButton saveBtn = new JButton("Save changes and quit");
|
||||
saveBtn.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
saveAndClose();
|
||||
}
|
||||
});
|
||||
generalPanel.add(saveBtn);
|
||||
|
||||
this.add(generalPanel, BorderLayout.CENTER);
|
||||
|
||||
this.setVisible(true);
|
||||
|
||||
//Import des attributs et méthodes dans les tableaux
|
||||
this.listAttributes();
|
||||
this.listMethods();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ouvre une fenêtre d'édition des arguments pour la méthode sélectionnée
|
||||
*/
|
||||
public void openArgumentsWindow(Method m){
|
||||
ArgumentsPropertiesWindow argsWin = new ArgumentsPropertiesWindow(this, _umlDiagram, m);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère la classe en cours d'édition.
|
||||
* @return La classe en cours d'édition.
|
||||
*/
|
||||
public Class getEditingClass(){
|
||||
return _class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute un attribut à la classe et rafraîchit la liste des attributs.
|
||||
*/
|
||||
public void addAttribute(){
|
||||
_class.addAttribute(new Attribute());
|
||||
this.listAttributes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retire un attribut à la classe et rafraîchit la liste des attributs.
|
||||
*/
|
||||
public void removeAttribute(){
|
||||
int i = _attributesTable.getSelectedRow();
|
||||
if(i != -1){
|
||||
_class.removeAttribute(i);
|
||||
this.listAttributes();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Affiche la liste des attributs de la classe dans le tableau
|
||||
*/
|
||||
public void listAttributes(){
|
||||
//Import des attributs dans la table
|
||||
_attributeModel.setDataVector((Object[][]) null, _attributesColumns);
|
||||
for(Attribute attr : _class.getAttributes()){
|
||||
String access = attr.getAccess();
|
||||
switch(access){
|
||||
case "-":
|
||||
access = "PRIVATE";
|
||||
break;
|
||||
case "+":
|
||||
access = "PUBLIC";
|
||||
break;
|
||||
case "#":
|
||||
access = "PROTECTED";
|
||||
break;
|
||||
}
|
||||
_attributeModel.addRow(new Object[]{attr.getName(), access, attr.getType()});
|
||||
}
|
||||
//On ajoute les contrôles pour chaque colonne
|
||||
JComboBox accessComboBox = new JComboBox();
|
||||
accessComboBox.setEditable(true);
|
||||
accessComboBox.addItem("PRIVATE");
|
||||
accessComboBox.addItem("PUBLIC");
|
||||
accessComboBox.addItem("PROTECTED");
|
||||
TableColumn accessColumn = _attributesTable.getColumn(_attributesColumns[1]);
|
||||
accessColumn.setCellEditor(new DefaultCellEditor(accessComboBox));
|
||||
|
||||
JComboBox typeComboBox = new JComboBox();
|
||||
typeComboBox.setEditable(true);
|
||||
typeComboBox.addItem("boolean");
|
||||
typeComboBox.addItem("char");
|
||||
typeComboBox.addItem("byte");
|
||||
typeComboBox.addItem("short");
|
||||
typeComboBox.addItem("int");
|
||||
typeComboBox.addItem("long");
|
||||
typeComboBox.addItem("float");
|
||||
typeComboBox.addItem("double");
|
||||
typeComboBox.addItem("String");
|
||||
TableColumn typeColumn = _attributesTable.getColumn(_attributesColumns[2]);
|
||||
typeColumn.setCellEditor(new DefaultCellEditor(typeComboBox));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute une méthode à la classe et rafraîchit la liste des méthodes.
|
||||
*/
|
||||
public void addMethod(){
|
||||
_class.addMethod(new Method());
|
||||
this.listMethods();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retire une méthode à la classe et rafraîchit la liste des méthodes.
|
||||
*/
|
||||
public void removeMethod(){
|
||||
int i = _methodsTable.getSelectedRow();
|
||||
if(i != -1){
|
||||
_class.removeMethod(i);
|
||||
this.listMethods();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Affiche la liste des méthodes de la classe dans le tableau
|
||||
*/
|
||||
public void listMethods(){
|
||||
//Import des attributs dans la table
|
||||
_methodModel.setDataVector((Object[][]) null, _methodsColumns);
|
||||
for(Method meth : _class.getMethods()){
|
||||
String access = meth.getAccess();
|
||||
switch(access){
|
||||
case "-":
|
||||
access = "PRIVATE";
|
||||
break;
|
||||
case "+":
|
||||
access = "PUBLIC";
|
||||
break;
|
||||
case "#":
|
||||
access = "PROTECTED";
|
||||
break;
|
||||
}
|
||||
_methodModel.addRow(new Object[]{meth.getName(), access, meth.getType(), "..."});
|
||||
}
|
||||
//On ajoute les contrôles pour chaque colonne
|
||||
JComboBox accessComboBox = new JComboBox();
|
||||
accessComboBox.setEditable(true);
|
||||
accessComboBox.addItem("PRIVATE");
|
||||
accessComboBox.addItem("PUBLIC");
|
||||
accessComboBox.addItem("PROTECTED");
|
||||
TableColumn accessColumn = _methodsTable.getColumn(_methodsColumns[1]);
|
||||
accessColumn.setCellEditor(new DefaultCellEditor(accessComboBox));
|
||||
|
||||
JComboBox typeComboBox = new JComboBox();
|
||||
typeComboBox.setEditable(true);
|
||||
typeComboBox.addItem("boolean");
|
||||
typeComboBox.addItem("char");
|
||||
typeComboBox.addItem("byte");
|
||||
typeComboBox.addItem("short");
|
||||
typeComboBox.addItem("int");
|
||||
typeComboBox.addItem("long");
|
||||
typeComboBox.addItem("float");
|
||||
typeComboBox.addItem("double");
|
||||
typeComboBox.addItem("String");
|
||||
TableColumn typeColumn = _methodsTable.getColumn(_methodsColumns[2]);
|
||||
typeColumn.setCellEditor(new DefaultCellEditor(typeComboBox));
|
||||
|
||||
JTextField argsField = new JTextField();
|
||||
argsField.setEnabled(false);
|
||||
TableColumn argColumn = _methodsTable.getColumn(_methodsColumns[3]);
|
||||
argColumn.setCellEditor(new DefaultCellEditor(argsField));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sauvegarde les modifications pour la classe en cours et ferme la fenêtre.
|
||||
*/
|
||||
public void saveAndClose(){
|
||||
//Termine l'édition en sélectionnant une autre cellule
|
||||
_attributesTable.editCellAt(0,0);
|
||||
//Changement du nom de la classe
|
||||
_class.setName(_nameField.getText());
|
||||
//Enregistrement des attributs
|
||||
for(int i = 0; i <= _attributeModel.getRowCount()-1; i++){
|
||||
Vector vect = (Vector)_attributeModel.getDataVector().elementAt(i);
|
||||
String access = vect.get(1).toString();
|
||||
switch(access){
|
||||
case "PRIVATE" :
|
||||
_class.getAttributes().get(i).setAccess(Attribute.PRIVATE);
|
||||
break;
|
||||
case "PUBLIC" :
|
||||
_class.getAttributes().get(i).setAccess(Attribute.PUBLIC);
|
||||
break;
|
||||
case "PROTECTED" :
|
||||
_class.getAttributes().get(i).setAccess(Attribute.PROTECTED);
|
||||
break;
|
||||
}
|
||||
_class.getAttributes().get(i).setType(vect.get(2).toString());
|
||||
_class.getAttributes().get(i).setName(vect.get(0).toString());
|
||||
}
|
||||
//Enregistrement des méthodes
|
||||
_methodsTable.editCellAt(0,0);
|
||||
for(int i = 0; i <= _methodModel.getRowCount()-1; i++){
|
||||
Vector vect = (Vector)_methodModel.getDataVector().elementAt(i);
|
||||
String access = vect.get(1).toString();
|
||||
switch(access){
|
||||
case "PRIVATE" :
|
||||
_class.getMethods().get(i).setAccess(Method.PRIVATE);
|
||||
break;
|
||||
case "PUBLIC" :
|
||||
_class.getMethods().get(i).setAccess(Method.PUBLIC);
|
||||
break;
|
||||
case "PROTECTED" :
|
||||
_class.getMethods().get(i).setAccess(Method.PROTECTED);
|
||||
break;
|
||||
}
|
||||
_class.getMethods().get(i).setType(vect.get(2).toString());
|
||||
_class.getMethods().get(i).setName(vect.get(0).toString());
|
||||
}
|
||||
|
||||
//Rafraichissement de l'affichage
|
||||
this._class.computeMinSize();
|
||||
_umlDiagram.repaint();
|
||||
this.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user