Résolution du ticket #5

This commit is contained in:
2020-11-29 22:58:19 +01:00
parent 9f99d30069
commit f091f236cb
3 changed files with 73 additions and 16 deletions

View File

@@ -1,3 +1,5 @@
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -41,6 +43,7 @@ public class Class {
this._shape = shape; this._shape = shape;
this._attributes = new ArrayList<Attribute>(); this._attributes = new ArrayList<Attribute>();
this._methods = new ArrayList<Method>(); this._methods = new ArrayList<Method>();
this.computeMinSize();
} }
/** /**
@@ -65,6 +68,7 @@ public class Class {
this._shape = shape; this._shape = shape;
this._attributes = attributes; this._attributes = attributes;
this._methods = methods; this._methods = methods;
this.computeMinSize();
} }
/** /**
@@ -187,6 +191,42 @@ public class Class {
this._minSizeY = minSizeY; this._minSizeY = minSizeY;
} }
/**
* Paramètre les tailles minimum de la classe sur les axes X et Y en fonction de son contenu
*/
public void computeMinSize(){
Font font = new Font("Arial",Font.PLAIN,14);
Canvas c = new Canvas();
int space = 5;
//Calcul de la taille en X
String maxChain = this.getName();
//Parcours des attributs
for(Attribute a : this.getAttributes()){
String chain = a.getAccess() + " " + a.getName() + " : " + a.getType();
if(chain.length() > maxChain.length()) maxChain = chain;
}
//Parcours des méthodes
for(Method m : this.getMethods()){
String chain = m.getAccess() + " " + m.getName() + "(";
for(Argument a : m.getArguments()){
chain += a.getName() + " : " + a.getType() + ", ";
}
chain.substring(0, chain.length() - 2);
chain += ") : " + m.getType();
if(chain.length() > maxChain.length()) maxChain = chain;
}
FontMetrics fm = c.getFontMetrics(font);
this.setMinSizeX(fm.stringWidth(maxChain));
//Calcul de la taille en Y
int attributes = this.getAttributes().size();
int methods = this.getMethods().size();
this.setMinSizeY((attributes + methods + 2) * font.getSize());
//this.setMinSizeY((attributes + methods + 1) * fontSize + (attributes + methods + 4) * space);
//Réadaptation éventuelle de la taille de la classe
if(this.getSizeX() < this.getMinSizeX()) this.setSizeX(this.getMinSizeX());
if(this.getSizeY() < this.getMinSizeY()) this.setSizeY(this.getMinSizeY());
}
/** /**
* Récupère la forme graphique de la classe. * Récupère la forme graphique de la classe.
* @return La forme graphique de la classe (Class.RECTANGLE ou Class.CIRCLE). * @return La forme graphique de la classe (Class.RECTANGLE ou Class.CIRCLE).
@@ -217,6 +257,7 @@ public class Class {
*/ */
public void addAttribute(Attribute a){ public void addAttribute(Attribute a){
this._attributes.add(a); this._attributes.add(a);
this.computeMinSize();
} }
/** /**
@@ -225,6 +266,7 @@ public class Class {
*/ */
public void removeAttribute(Attribute a){ public void removeAttribute(Attribute a){
this._attributes.remove(a); this._attributes.remove(a);
this.computeMinSize();
} }
/** /**
@@ -233,6 +275,7 @@ public class Class {
*/ */
public void removeAttribute(int index){ public void removeAttribute(int index){
this._attributes.remove(index); this._attributes.remove(index);
this.computeMinSize();
} }
/** /**
@@ -240,6 +283,7 @@ public class Class {
*/ */
public void clearAttributes(){ public void clearAttributes(){
this._attributes.clear(); this._attributes.clear();
this.computeMinSize();
} }
/** /**
@@ -256,6 +300,7 @@ public class Class {
*/ */
public void addMethod(Method m){ public void addMethod(Method m){
this._methods.add(m); this._methods.add(m);
this.computeMinSize();
} }
/** /**
@@ -264,6 +309,7 @@ public class Class {
*/ */
public void removeMethod(Method m){ public void removeMethod(Method m){
this._methods.remove(m); this._methods.remove(m);
this.computeMinSize();
} }
/** /**
@@ -272,6 +318,7 @@ public class Class {
*/ */
public void removeMethod(int index){ public void removeMethod(int index){
this._methods.remove(index); this._methods.remove(index);
this.computeMinSize();
} }
/** /**
@@ -279,5 +326,6 @@ public class Class {
*/ */
public void clearMethods(){ public void clearMethods(){
this._methods.clear(); this._methods.clear();
this.computeMinSize();
} }
} }

View File

@@ -7,6 +7,9 @@ import java.util.List;
public class Method { public class Method {
public static int _methodId = 0; public static int _methodId = 0;
public static final String PRIVATE = "-";
public static final String PROTECTED = "#";
public static final String PUBLIC = "+";
private int _id; private int _id;
private String _access; private String _access;
@@ -20,8 +23,8 @@ public class Method {
public Method(){ public Method(){
_methodId++; _methodId++;
this._id = _methodId; this._id = _methodId;
this._access = "public"; this._access = Method.PUBLIC;
this._type = null; this._type = "void";
this._name = "methode" + this._id; this._name = "methode" + this._id;
this._arguments = new ArrayList<Argument>(); this._arguments = new ArrayList<Argument>();
} }

View File

@@ -31,32 +31,42 @@ public class UmlDiagram extends JPanel implements MouseListener, MouseMotionList
*/ */
@Override @Override
public void paintComponent(Graphics g){ public void paintComponent(Graphics g){
int fontSize = 14; Font font = new Font("Arial", Font.PLAIN, 14);
Font font = new Font("Arial", Font.PLAIN, fontSize);
for(Class a : _classes){ for(Class a : _classes){
int posX = a.getPosX()-(a.getSizeX()/2); int posX = a.getPosX() - (a.getSizeX()/2);
int posY = a.getPosY()-(a.getSizeY()/2); int posY = a.getPosY() - (a.getSizeY()/2);
//Dessin du rectangle //Dessin du rectangle
g.setColor(Color.GRAY); g.setColor(Color.GRAY);
g.fillRoundRect(posX, posY, a.getSizeX(), a.getSizeY(), 10, 10); g.fillRoundRect(posX, posY, a.getSizeX(), a.getSizeY(), 10, 10);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
//Dessin du nom de la classe //Dessin du nom de la classe
int posCounter = posY + fontSize; int posCounter = posY + font.getSize();
FontMetrics metrics = g.getFontMetrics(font);
FontMetrics metrics = this.getFontMetrics(font);
g.setFont(font); g.setFont(font);
g.drawString(a.getName(), posX + metrics.stringWidth(a.getName())/4, posCounter); g.drawString(a.getName(), posX + a.getSizeX()/2 - metrics.stringWidth(a.getName())/2, posCounter);
posCounter += 5; posCounter += 5;
//Ligne de séparation //Ligne de séparation
g.drawLine(posX, posY + fontSize + 5, posX + a.getSizeX()-1, posY + fontSize + 5); g.drawLine(posX, posY + font.getSize() + 5, posX + a.getSizeX()-1, posY + font.getSize() + 5);
//Affichage des attributs //Affichage des attributs
for(Attribute b : a.getAttributes()){ for(Attribute b : a.getAttributes()){
posCounter += fontSize; posCounter += font.getSize();
g.drawString(b.getAccess() + " " + b.getName() + " : " + b.getType(), posX, posCounter); g.drawString(b.getAccess() + " " + b.getName() + " : " + b.getType(), posX, posCounter);
} }
posCounter += 5; posCounter += 5;
//Ligne de séparation //Ligne de séparation
g.drawLine(posX, posCounter, posX + a.getSizeX()-1, posCounter); g.drawLine(posX, posCounter, posX + a.getSizeX()-1, posCounter);
g.setColor(new Color(69, 69, 72)); //Dessin des méthodes
for(Method m : a.getMethods()){
posCounter += font.getSize();
String chain = m.getAccess() + " " + m.getName() + "(";
for(Argument ar : m.getArguments()){
chain += ar.getName() + " : " + ar.getType() + ", ";
}
chain = chain.substring(0, chain.length()-2);
chain += ") : " + m.getType();
g.drawString(chain, posX, posCounter);
}
//Si la classe est sélectionnée //Si la classe est sélectionnée
if((Class)_selected == a){ if((Class)_selected == a){
AppThinker.getWindow().getStatusbar().setSizeLabel(a.getSizeX(), a.getSizeY()); AppThinker.getWindow().getStatusbar().setSizeLabel(a.getSizeX(), a.getSizeY());
@@ -126,7 +136,6 @@ public class UmlDiagram extends JPanel implements MouseListener, MouseMotionList
} }
//Actions de la souris sur le diagramme UML //Actions de la souris sur le diagramme UML
/** /**
* Action du clic de la souris sur le diagramme. * Action du clic de la souris sur le diagramme.
* @param e L'événement souris. * @param e L'événement souris.
@@ -144,7 +153,6 @@ public class UmlDiagram extends JPanel implements MouseListener, MouseMotionList
*/ */
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
System.out.println("ok");
int tool = AppThinker.getWindow().getToolbar().getCurrentTool(); int tool = AppThinker.getWindow().getToolbar().getCurrentTool();
switch(tool){ switch(tool){
//On essaie de sélectionner un élément //On essaie de sélectionner un élément
@@ -169,8 +177,6 @@ public class UmlDiagram extends JPanel implements MouseListener, MouseMotionList
//On essaie d'ajouter une classe //On essaie d'ajouter une classe
case AppThinkerToolbar.CLASS_TOOL: case AppThinkerToolbar.CLASS_TOOL:
Class newClass = new Class(e.getX(), e.getY(), Class.RECTANGLE); Class newClass = new Class(e.getX(), e.getY(), Class.RECTANGLE);
newClass.addAttribute(new Attribute("testAttribut", Attribute.PROTECTED, "String"));
newClass.addAttribute(new Attribute("bonjour", Attribute.PUBLIC, "Date"));
AppThinker.getProject().addClass(newClass); AppThinker.getProject().addClass(newClass);
break; break;
case AppThinkerToolbar.ASSOCIATION_TOOL: case AppThinkerToolbar.ASSOCIATION_TOOL: