瀏覽代碼

some more code cleaning

I. Dix 7 年之前
父節點
當前提交
1aa2e534b9
共有 4 個文件被更改,包括 253 次插入266 次删除
  1. 242 257
      src/classes/CpsEdge.java
  2. 7 5
      src/ui/view/AbstractCanvas.java
  3. 2 2
      src/ui/view/MyCanvas.java
  4. 2 2
      src/ui/view/UpperNodeCanvas.java

+ 242 - 257
src/classes/CpsEdge.java

@@ -1,288 +1,273 @@
 package classes;
 
-import java.util.ArrayList;
-
 import com.google.gson.annotations.Expose;
 
+import java.util.ArrayList;
+
 /**
  * The class "CpsEdge" represents the connections on the GUI. Each connection
  * contains a max. capacity, a flow, a status (isWorking), tags (for internal
  * use of electricity flow), source and destination
- * 
- * @author Gruppe14
  *
+ * @author Gruppe14
  */
 public class CpsEdge {
 
-	// Max. capacity of the Edge, if flow is greater than the status --> is
-	// Working would be false
-	@Expose
-	float maxCapacity;
-	// Current flow of electricity (through the edge)
-	@Expose
-	float flow;
-	/*
-	 * Represents the actual status of the Edge (true = flows electricity; false
-	 * = no flow of electricity) State represents the real status of the edge If
-	 * it breaks --> stay ruin no matter the actual scenario The only way to
-	 * repair it is through manual change (setStateEdge)
-	 */
-	@Expose
-	boolean isWorking;
-	
-	/*
-	 * 0 = not connected to an upper node
-	 * 1 = connected to an uppder node & connected inside
-	 * 2 = connected to an upper node but not inside 
-	 * Is false when not
-	 */
-	@Expose
-	int isConnected;
-	// for internal use --> flow of electricity (simulation state)
-	
-	ArrayList<Integer> tags;
-	// for internal use --> flow of electricity (simulation state)
-	ArrayList<Integer> pseudoTags;
-	// Source
-	AbstractCpsObject a;
-	// Destination
-	AbstractCpsObject b;
+    public static final int CON_UPPER_NODE = 0;
+    public static final int CON_UPPER_NODE_AND_INSIDE = 1;
+    public static final int CON_UPPER_NODE_NOT_INSIDE = 2;
+    // Max. capacity of the Edge, if flow is greater than the status --> is
+    // Working would be false
+    @Expose
+    float maxCapacity;
+    // for internal use --> flow of electricity (simulation state)
+    // Current flow of electricity (through the edge)
+    @Expose
+    float flow;
+    /*
+     * Represents the actual status of the Edge (true = flows electricity; false
+     * = no flow of electricity) State represents the real status of the edge If
+     * it breaks --> stay ruin no matter the actual scenario The only way to
+     * repair it is through manual change (setStateEdge)
+     */
+    @Expose
+    boolean isWorking;
+    /*
+     * 0 = not connected to an upper node
+     * 1 = connected to an uppder node & connected inside
+     * 2 = connected to an upper node but not inside
+     * Is false when not
+     */
+    @Expose
+    int isConnected;
+    ArrayList<Integer> tags;
+    // for internal use --> flow of electricity (simulation state)
+    ArrayList<Integer> pseudoTags;
+    // Source
+    AbstractCpsObject a;
+    // Destination
+    AbstractCpsObject b;
+
+    /**
+     * Constructor without max. capacity (by default as 100)
+     *
+     * @param a Source
+     * @param b Destination
+     */
+    public CpsEdge(AbstractCpsObject a, AbstractCpsObject b) {
+        setA(a);
+        setB(b);
+        this.a.addConnection(this);
+        this.b.addConnection(this);
+        this.maxCapacity = 100;
+        flow = 0;
+        isWorking = true;
+        pseudoTags = new ArrayList<Integer>();
+    }
+
+    /**
+     * Constructor with a user-defined max. capacity
+     *
+     * @param a      Source
+     * @param b      Destination
+     * @param maxCap Maximum Capacity
+     */
+    public CpsEdge(AbstractCpsObject a, AbstractCpsObject b, float maxCap) {
+        setA(a);
+        setB(b);
+        this.a.addConnection(this);
+        this.b.addConnection(this);
+        this.maxCapacity = maxCap;
+        flow = 0;
+        isWorking = true;
+        isConnected = 0;
+        pseudoTags = new ArrayList<Integer>();
+    }
+
+    /**
+     * Getter for the max. capacity
+     *
+     * @return the capacity
+     */
+    public float getCapacity() {
+        return maxCapacity;
+    }
+
+    /**
+     * Setter for the max. capacity
+     *
+     * @param cap the Capacity to set
+     */
+    public void setCapacity(float cap) {
+        this.maxCapacity = cap;
+    }
+
+    /**
+     * Getter fot the current flow.
+     *
+     * @return the flow
+     */
+    public float getFlow() {
+        return flow;
+    }
+
+    /**
+     * Set the flow into a new one.
+     *
+     * @param flow the flow to set
+     */
+    public void setFlow(float flow) {
+        this.flow = flow;
+        /**
+         * if (flow <= maxCapacity || flow == -1) { isWorking = true; } else {
+         * isWorking = false; state = false;
+         **/
+    }
+
+    /**
+     * Calculates the state of the edge (see description of isWorking).
+     */
 
-	/**
-	 * Constructor without max. capacity (by default as 100)
-	 * 
-	 * @param a
-	 *            Source
-	 * @param b
-	 *            Destination
-	 */
-	public CpsEdge(AbstractCpsObject a, AbstractCpsObject b) {
-		setA(a);
-		setB(b);
-		this.a.addConnection(this);
-		this.b.addConnection(this);
-		this.maxCapacity = 100;
-		flow = 0;
-		isWorking = true;
-		pseudoTags = new ArrayList<Integer>();
-	}
+    public void calculateState() {
+        if (flow > maxCapacity && maxCapacity != -1 && maxCapacity != -2) {
+            isWorking = false;
+            flow = 0;
+        }
+    }
 
-	/**
-	 * Constructor with a user-defined max. capacity
-	 * 
-	 * @param a
-	 *            Source
-	 * @param b
-	 *            Destination
-	 * @param maxCap
-	 *            Maximum Capacity
-	 */
-	public CpsEdge(AbstractCpsObject a, AbstractCpsObject b, float maxCap) {
-		setA(a);
-		setB(b);
-		this.a.addConnection(this);
-		this.b.addConnection(this);
-		this.maxCapacity = maxCap;
-		flow = 0;
-		isWorking = true;
-		isConnected = 0;
-		pseudoTags = new ArrayList<Integer>();
-	}
+    /**
+     * Getter for the Source.
+     *
+     * @return the a
+     */
+    public AbstractCpsObject getA() {
+        return a;
+    }
 
-	/**
-	 * Getter for the max. capacity
-	 * 
-	 * @return the capacity
-	 */
-	public float getCapacity() {
-		return maxCapacity;
-	}
+    /**
+     * Set the Source to a new one.
+     *
+     * @param a the a to set
+     */
+    public void setA(AbstractCpsObject a) {
+        this.a = a;
+    }
 
-	/**
-	 * Setter for the max. capacity
-	 * 
-	 * @param cap
-	 *            the Capacity to set
-	 */
-	public void setCapacity(float cap) {
-		this.maxCapacity = cap;
-	}
+    /**
+     * Getter for the destination.
+     *
+     * @return the b
+     */
+    public AbstractCpsObject getB() {
+        return b;
+    }
 
-	/**
-	 * Getter fot the current flow.
-	 * 
-	 * @return the flow
-	 */
-	public float getFlow() {
-		return flow;
-	}
+    /**
+     * Set the Destination to a new one.
+     *
+     * @param b the b to set
+     */
+    public void setB(AbstractCpsObject b) {
+        this.b = b;
+    }
 
-	/**
-	 * Set the flow into a new one.
-	 * 
-	 * @param flow
-	 *            the flow to set
-	 */
-	public void setFlow(float flow) {
-		this.flow = flow;
-		/**
-		 * if (flow <= maxCapacity || flow == -1) { isWorking = true; } else {
-		 * isWorking = false; state = false;
-		 **/
-	}
+    /**
+     * Getter for the status.
+     *
+     * @return the state
+     */
+    public boolean getState() {
+        return isWorking;
+    }
 
-	/**
-	 * Calculates the state of the edge (see description of isWorking).
-	 * 
-	 * @param simMode
-	 *            Simulation Mode
-	 */
+    /**
+     * Set the state manually to a new one.
+     *
+     * @param state the state
+     */
+    public void setState(boolean state) {
+        isWorking = state;
+    }
 
-	public void calculateState() {
-		if (flow > maxCapacity && maxCapacity != -1 && maxCapacity != -2) {
-			isWorking = false;
-			flow = 0;
-		}
-	}
+    /**
+     * Getter for the ArrayList of tags.
+     *
+     * @return tags tags for this edge
+     */
+    public ArrayList<Integer> getTags() {
+        return tags;
+    }
 
-	/**
-	 * Getter for the Source.
-	 * 
-	 * @return the a
-	 */
-	public AbstractCpsObject getA() {
-		return a;
-	}
+    /**
+     * set the tags into a new set of tags.
+     *
+     * @param tags tags for this edge
+     */
+    public void setTags(ArrayList<Integer> tags) {
+        this.tags = tags;
+    }
 
-	/**
-	 * Set the Source to a new one.
-	 * 
-	 * @param a
-	 *            the a to set
-	 */
-	public void setA(AbstractCpsObject a) {
-		this.a = a;
-	}
+    /**
+     * Add a new tag to the ArrayList.
+     *
+     * @param tag tag for the ArrayList
+     */
+    public void addTag(int tag) {
+        if (!tags.contains(tag)) {
+            tags.add(tag);
+        }
+    }
 
-	/**
-	 * Getter for the destination.
-	 * 
-	 * @return the b
-	 */
-	public AbstractCpsObject getB() {
-		return b;
-	}
+    /**
+     * checks wether tags contains all given tags
+     *
+     * @param toCheck tags that are checked
+     * @return true if all are contained in tags, false otherwise
+     */
+    public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck) {
+        if (toCheck.size() == 0) {
+            return true;
+        } else {
+            for (Integer i : toCheck) {
+                if (!(list.contains(i))) {
+                    return false;
+                }
+            }
+            return true;
+        }
+    }
 
-	/**
-	 * Set the Destination to a new one.
-	 * 
-	 * @param b
-	 *            the b to set
-	 */
-	public void setB(AbstractCpsObject b) {
-		this.b = b;
-	}
+    public void addPseudoTag(int tag) {
+        if (!pseudoTags.contains(tag)) {
+            pseudoTags.add(tag);
+        }
+    }
 
-	/**
-	 * Set the state manually to a new one.
-	 * 
-	 * @param state
-	 *            the state
-	 */
-	public void setState(boolean state) {
-		isWorking = state;
-	}
+    public void setPseudoTag(ArrayList<Integer> list) {
+        pseudoTags = list;
+    }
 
-	/**
-	 * Getter for the status.
-	 * 
-	 * @return the state
-	 */
-	public boolean getState() {
-		return isWorking;
-	}
+    public ArrayList<Integer> getPseudoTags() {
+        return pseudoTags;
+    }
 
-	/**
-	 * set the tags into a new set of tags.
-	 * 
-	 * @param tags
-	 *            tags for this edge
-	 */
-	public void setTags(ArrayList<Integer> tags) {
-		this.tags = tags;
-	}
+    public void recalculateTags() {
+        for (Integer i : pseudoTags) {
+            if (!tags.contains(i)) {
+                tags.add(i);
+            }
+        }
+    }
 
-	/**
-	 * Getter for the ArrayList of tags.
-	 * 
-	 * @return tags tags for this edge
-	 */
-	public ArrayList<Integer> getTags() {
-		return tags;
-	}
+    public int getConnected() {
+        return isConnected;
+    }
 
-	/**
-	 * Add a new tag to the ArrayList.
-	 * 
-	 * @param tag tag for the ArrayList
-	 */
-	public void addTag(int tag) {
-		if(!tags.contains(tag)){
-			tags.add(tag);
-		}
-	}
-	
-	/**
-	 * checks wether tags contains all given tags
-	 * @param toCheck 
-	 * 				tags that are checked
-	 * @return
-	 * 		true if all are contained in tags, false otherwise
-	 */
-	public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck){
-		if(toCheck.size() == 0){
-			return true;
-		}else{
-			for(Integer i: toCheck){
-				if(!(list.contains(i))){
-					return false;
-				}
-			}
-			return true;
-		}
-	}
-	
-	public void addPseudoTag(int tag){
-		if(!pseudoTags.contains(tag)){
-			pseudoTags.add(tag);
-		}
-	}
-	
-	public void setPseudoTag(ArrayList<Integer> list){
-		pseudoTags = list;
-	}
-	
-	public ArrayList<Integer> getPseudoTags(){
-		return pseudoTags;
-	}
-	
-	public void recalculateTags(){
-		for(Integer i: pseudoTags){
-			if(!tags.contains(i)){
-				tags.add(i);
-			}
-		}
-	}
-	
-	public int getConnected(){
-		return isConnected;
-	}
-	
-	public void setConnected(int state){
-		isConnected = state;
-		if(state == 1){
-			maxCapacity = -2;
-		}
-	}
+    public void setConnected(int state) {
+        isConnected = state;
+        if (state == 1) {
+            maxCapacity = -2;
+        }
+    }
 
 }

+ 7 - 5
src/ui/view/AbstractCanvas.java

@@ -76,7 +76,7 @@ public abstract class AbstractCanvas extends JPanel {
     protected String paintEdge(CpsEdge con, String maxCap) {
         if (con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
                 && con != edgeHighlight) {
-            if (con.getConnected() == 0) {
+            if (con.getConnected() == CpsEdge.CON_UPPER_NODE) {
                 setEdgeState(con);
             } else {
                 g2.setColor(Color.DARK_GRAY);
@@ -88,7 +88,8 @@ public abstract class AbstractCanvas extends JPanel {
             maxCap = setCapacityString(con, maxCap);
 
             if (showedInformation[0]) {
-                if (con.getConnected() == 0 || con.getConnected() == 1) {
+                if (con.getConnected() == CpsEdge.CON_UPPER_NODE
+                        || con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
                     g2.drawString(con.getFlow() + "/" + maxCap,
                             (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
                             (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
@@ -128,7 +129,7 @@ public abstract class AbstractCanvas extends JPanel {
     }
 
 
-    protected String nameSthSensible(CpsEdge con, String maxCap) {
+    protected String drawEdgeLine(CpsEdge con, String maxCap) {
         if (con.getA().getId() == model.getSelectedObjectID()
                 || model.getSelectedCpsObjects().contains(con.getA()) || tempSelected.contains(con.getA())
                 || con.getB().getId() == model.getSelectedObjectID()
@@ -140,7 +141,8 @@ public abstract class AbstractCanvas extends JPanel {
             maxCap = setCapacityString(con, maxCap);
 
             if (showedInformation[0]) {
-                if (con.getConnected() == 0 || con.getConnected() == 1) {
+                if (con.getConnected() == CpsEdge.CON_UPPER_NODE
+                        || con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
                     g2.drawString(con.getFlow() + "/" + maxCap,
                             (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
                             (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
@@ -156,7 +158,7 @@ public abstract class AbstractCanvas extends JPanel {
     }
 
 
-    protected void nameSthSensible2(AbstractCpsObject cps) {
+    protected void setEdgePictureAndHighlighting(AbstractCpsObject cps) {
         // node image
         if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
                 || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {

+ 2 - 2
src/ui/view/MyCanvas.java

@@ -372,7 +372,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener, MouseMoti
                     g2.setStroke(new BasicStroke(2));
                 }
 
-                maxCap = nameSthSensible(con, maxCap);
+                maxCap = drawEdgeLine(con, maxCap);
             }
         } else if (edgeHighlight != null) {
             g2.setColor(Color.BLUE);
@@ -407,7 +407,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener, MouseMoti
                 }
             }
 
-            nameSthSensible2(cps);
+            setEdgePictureAndHighlighting(cps);
 
             g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
                     cps.getPosition().y - controller.getScaleDiv2(), controller.getScale(), controller.getScale(),

+ 2 - 2
src/ui/view/UpperNodeCanvas.java

@@ -509,7 +509,7 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
                     g2.setStroke(new BasicStroke(2));
                 }
 
-                maxCap = nameSthSensible(con, maxCap);
+                maxCap = drawEdgeLine(con, maxCap);
             }
         } else if (edgeHighlight != null) {
             g2.setColor(Color.BLUE);
@@ -546,7 +546,7 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
                 }
             }
 
-            nameSthSensible2(cps);
+            setEdgePictureAndHighlighting(cps);
 
             g2.drawImage(img, cps.getPosition().x - model.getScaleDiv2(), cps.getPosition().y - model.getScaleDiv2(),
                     controller.getScale(), controller.getScale(), null);