From f091f236cba1396157c9c07da2488466201231f7 Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sun, 29 Nov 2020 22:58:19 +0100 Subject: [PATCH] =?UTF-8?q?R=C3=A9solution=20du=20ticket=20#5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AppThinker/src/Class.java | 48 ++++++++++++++++++++++++++++++++++ AppThinker/src/Method.java | 7 +++-- AppThinker/src/UmlDiagram.java | 34 ++++++++++++++---------- 3 files changed, 73 insertions(+), 16 deletions(-) diff --git a/AppThinker/src/Class.java b/AppThinker/src/Class.java index 061ea42..418ad43 100644 --- a/AppThinker/src/Class.java +++ b/AppThinker/src/Class.java @@ -1,3 +1,5 @@ +import javax.swing.*; +import java.awt.*; import java.util.ArrayList; import java.util.List; @@ -41,6 +43,7 @@ public class Class { this._shape = shape; this._attributes = new ArrayList(); this._methods = new ArrayList(); + this.computeMinSize(); } /** @@ -65,6 +68,7 @@ public class Class { this._shape = shape; this._attributes = attributes; this._methods = methods; + this.computeMinSize(); } /** @@ -187,6 +191,42 @@ public class Class { 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. * @return La forme graphique de la classe (Class.RECTANGLE ou Class.CIRCLE). @@ -217,6 +257,7 @@ public class Class { */ public void addAttribute(Attribute a){ this._attributes.add(a); + this.computeMinSize(); } /** @@ -225,6 +266,7 @@ public class Class { */ public void removeAttribute(Attribute a){ this._attributes.remove(a); + this.computeMinSize(); } /** @@ -233,6 +275,7 @@ public class Class { */ public void removeAttribute(int index){ this._attributes.remove(index); + this.computeMinSize(); } /** @@ -240,6 +283,7 @@ public class Class { */ public void clearAttributes(){ this._attributes.clear(); + this.computeMinSize(); } /** @@ -256,6 +300,7 @@ public class Class { */ public void addMethod(Method m){ this._methods.add(m); + this.computeMinSize(); } /** @@ -264,6 +309,7 @@ public class Class { */ public void removeMethod(Method m){ this._methods.remove(m); + this.computeMinSize(); } /** @@ -272,6 +318,7 @@ public class Class { */ public void removeMethod(int index){ this._methods.remove(index); + this.computeMinSize(); } /** @@ -279,5 +326,6 @@ public class Class { */ public void clearMethods(){ this._methods.clear(); + this.computeMinSize(); } } diff --git a/AppThinker/src/Method.java b/AppThinker/src/Method.java index 875e23f..5860e4e 100644 --- a/AppThinker/src/Method.java +++ b/AppThinker/src/Method.java @@ -7,6 +7,9 @@ import java.util.List; public class Method { public static int _methodId = 0; + public static final String PRIVATE = "-"; + public static final String PROTECTED = "#"; + public static final String PUBLIC = "+"; private int _id; private String _access; @@ -20,8 +23,8 @@ public class Method { public Method(){ _methodId++; this._id = _methodId; - this._access = "public"; - this._type = null; + this._access = Method.PUBLIC; + this._type = "void"; this._name = "methode" + this._id; this._arguments = new ArrayList(); } diff --git a/AppThinker/src/UmlDiagram.java b/AppThinker/src/UmlDiagram.java index 4611aa4..846ea4b 100644 --- a/AppThinker/src/UmlDiagram.java +++ b/AppThinker/src/UmlDiagram.java @@ -31,32 +31,42 @@ public class UmlDiagram extends JPanel implements MouseListener, MouseMotionList */ @Override public void paintComponent(Graphics g){ - int fontSize = 14; - Font font = new Font("Arial", Font.PLAIN, fontSize); + Font font = new Font("Arial", Font.PLAIN, 14); for(Class a : _classes){ - int posX = a.getPosX()-(a.getSizeX()/2); - int posY = a.getPosY()-(a.getSizeY()/2); + int posX = a.getPosX() - (a.getSizeX()/2); + int posY = a.getPosY() - (a.getSizeY()/2); //Dessin du rectangle g.setColor(Color.GRAY); g.fillRoundRect(posX, posY, a.getSizeX(), a.getSizeY(), 10, 10); g.setColor(Color.BLACK); //Dessin du nom de la classe - int posCounter = posY + fontSize; - FontMetrics metrics = g.getFontMetrics(font); + int posCounter = posY + font.getSize(); + + FontMetrics metrics = this.getFontMetrics(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; //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 for(Attribute b : a.getAttributes()){ - posCounter += fontSize; + posCounter += font.getSize(); g.drawString(b.getAccess() + " " + b.getName() + " : " + b.getType(), posX, posCounter); } posCounter += 5; //Ligne de séparation 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 if((Class)_selected == a){ 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 - /** * Action du clic de la souris sur le diagramme. * @param e L'événement souris. @@ -144,7 +153,6 @@ public class UmlDiagram extends JPanel implements MouseListener, MouseMotionList */ @Override public void mousePressed(MouseEvent e) { - System.out.println("ok"); int tool = AppThinker.getWindow().getToolbar().getCurrentTool(); switch(tool){ //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 case AppThinkerToolbar.CLASS_TOOL: 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); break; case AppThinkerToolbar.ASSOCIATION_TOOL: