package classes; import java.awt.Point; import java.awt.geom.Point2D.Double; import java.util.LinkedList; import com.google.gson.annotations.Expose; import interfaces.GraphEditable; import interfaces.IGraphedElement; import ui.controller.SingletonControl; import ui.view.IndexTranslator; import ui.view.UnitGraph; /** * The class HolonSwitch represents a Switch, which can be turned on and off. * * @author Gruppe14 * */ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, GraphEditable { /** * The class HolonSwitch represents an Object in the system, that has the * capacity of manipulate the electricity flow. The switch can be manage * automatically through a graph or direct manually. * * @author Gruppe14 * */ /* * manual state True, if this wire is working (capable of carrying * electricity), else false */ @Expose boolean manualActive; /* * active state True, if this wire is working (capable of carrying * electricity), else false */ @Expose private boolean autoActive; /* * true if switch has to be used manually */ @Expose boolean manualMode; @Expose int localPeriod; @Expose boolean stretch; /* * Energy at each point of the graph with 50 predefined points. At the * beginning, it starts with all values at energy */ boolean[] activeAt; // Points on the UnitGraph LinkedList graphPoints = new LinkedList<>(); /** * Create a new HolonSwitch with the default name ("Switch"), a default * value of automatic handle and active status. * * @param objName * String */ public HolonSwitch(String objName) { super(objName); setStretching(IGraphedElement.STRETCH_BY_DEFAULT); activeAt=new boolean[IGraphedElement.STANDARD_GRAPH_ACCURACY]; setLocalPeriod(SingletonControl.getInstance().getControl()==null? IGraphedElement.STANDARD_GRAPH_ACCURACY: SingletonControl.getInstance().getControl().getModel().getGraphIterations() ); setManualState(true); setAutoState(true); setActiveAt(true); setManualMode(false); setGraphPoints(new LinkedList()); } /** * Create a copy of an existing HolonSwitch. * * @param obj * the Object to copy */ public HolonSwitch(AbstractCpsObject obj) { super(obj); HolonSwitch copyObj = (HolonSwitch)obj; setLocalPeriod(copyObj.getLocalPeriod()); setStretching(copyObj.isStretching()); activeAt=new boolean[IGraphedElement.STANDARD_GRAPH_ACCURACY]; super.setName(obj.getName()); setManualState(copyObj.getActiveManual()); setAutoState(true); setLocalPeriod(((IGraphedElement)obj).getLocalPeriod()); setActiveAt(true); for (int i = 0; i < activeAt.length; i++) { activeAt[i] = copyObj.getState(i); } setGraphPoints(new LinkedList()); for (Point p : copyObj.getGraphPoints()) { this.graphPoints.add(new Point((int) p.getX(), (int) p.getY())); } setManualMode(copyObj.getManualMode()); } /** * Calculates the state of the Switch. */ public void switchState() { if (!manualMode) { setManualMode(true); } if (this.manualActive == true) { setImage("/Images/switch-off.png"); } else { setImage("/Images/switch-on.png"); } this.manualActive = !manualActive; } /** * Getter for the status of the Switch at a given timestep. * * @param timeStep state at given iteration. * @return state value */ public boolean getState(int timeStep) { if (manualMode) { return this.manualActive; } else { return activeAt[IndexTranslator.getEffectiveIndex(this, timeStep)]; } } /** * Overall status of the switch (manual or automatic mode). * * @return boolean the State */ public boolean getState() {//TODO: not really necessary if (manualMode) { return this.manualActive; } else { return this.autoActive; } } /** * Change the state of the Switch to manual. * * @param state the State */ public void setManualState(boolean state) { this.manualActive = state; setImage(); } /** * Set the state of the Switch to automatic. * * @param state the State */ public void setAutoState(boolean state) {//TODO: This should probably not be public this.autoActive = state; setImage(); } /** * Set Image of the Switch. */ private void setImage() { if (manualMode) { if (!this.manualActive) { setImage("/Images/switch-off.png"); } else { setImage("/Images/switch-on.png"); } } else { if (!this.autoActive) { setImage("/Images/switch-off.png"); } else { setImage("/Images/switch-on.png"); } } } /** * For automatic use only (through the graph). * * @return the Graph Points */ public LinkedList getGraphPoints() { return graphPoints; } /** * Set the values of the switch in the graph (auto. mode only). * * @param points the Graph points */ public void setGraphPoints(LinkedList points) { this.graphPoints = points; } /** * Returns the ManualState. * * @return boolean Manual State */ public boolean getActiveManual() { return this.manualActive; } /** * Set the value of the Switch. * * @param active * the default value */ public void setActiveAt(boolean active) { activeAt = new boolean[100];//TODO This is necessary because of thisgson rubbish. for (int i = 0; i < activeAt.length; i++) { this.activeAt[i] = active; } } public void setActiveAt(int pos, boolean active) { //activeAt = new boolean[100]; this.activeAt[pos] = active; } /** * Set the overall value of the Switch (manual mode). * * @param mode * the mode (boolean) */ public void setManualMode(boolean mode) { manualMode = mode; } /** * Get manualmode state. * * @return boolean manual mode state */ public boolean getManualMode() { return manualMode; } @Override public void setLocalPeriod(int period) { localPeriod=period; } @Override public int getLocalPeriod() { return localPeriod; } @Override public boolean isStretching() { return stretch; } @Override public void setStretching(boolean stretch) { this.stretch=stretch; } @Override public Graphtype getGraphType() { return Graphtype.boolGraph; } @Override public LinkedList getStateGraph() { // TODO no Double List return null; } @Override public void sampleGraph() { // TODO Auto-generated method stub } }