diff --git a/src/main/java/ch/psi/mxsc/Controller.java b/src/main/java/ch/psi/mxsc/Controller.java index 1192152..fc9e26b 100644 --- a/src/main/java/ch/psi/mxsc/Controller.java +++ b/src/main/java/ch/psi/mxsc/Controller.java @@ -358,7 +358,7 @@ public class Controller { } else { Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, "No puck_detection detected."); } - checkEnablePuckDetection(); + checkPuckLoading(); } PuckState[] puckState; @@ -959,12 +959,13 @@ public class Controller { } } - - public void checkEnablePuckDetection(){ + boolean showingPuckLoadingDialog; + + public void checkPuckLoading(){ try{ - if ("loading".equals(Context.getSetting("puck_detection"))){ - boolean loading = (dialogPuckLoading != null) && (dialogPuckLoading.isShowing()); - puck_detection.setEnabled(loading); + showingPuckLoadingDialog = (dialogPuckLoading != null) && (dialogPuckLoading.isShowing()); + if ("loading".equals(Context.getSetting("puck_detection"))){ + puck_detection.setEnabled(showingPuckLoadingDialog); } } catch (Exception ex) { Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex); @@ -979,7 +980,7 @@ public class Controller { dialogPuckLoading = new PuckLoadingDialog(mainFrame.getTopLevel(), load, false); dialogPuckLoading.setLocationRelativeTo(mainFrame); dialogPuckLoading.setVisible(true); - checkEnablePuckDetection(); + checkPuckLoading(); dialogPuckLoading.addWindowListener(new WindowAdapter() { @Override @@ -992,7 +993,7 @@ public class Controller { } @Override public void windowClosed(WindowEvent e) { - checkEnablePuckDetection(); + checkPuckLoading(); } }); diff --git a/src/main/java/ch/psi/mxsc/PuckState.java b/src/main/java/ch/psi/mxsc/PuckState.java index 06cfed0..8b14972 100644 --- a/src/main/java/ch/psi/mxsc/PuckState.java +++ b/src/main/java/ch/psi/mxsc/PuckState.java @@ -1,102 +1,106 @@ -/* - * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved. - */ -package ch.psi.mxsc; - -/** - * - */ -public class PuckState { - - public final int id; - public boolean online; - public SwitchState mecSwitch; - public SwitchState indSwitch; - - public enum SwitchState{ - On, - Off, - Error; - static SwitchState fromInt(int value){ - switch (value){ - case 1: return SwitchState.On; - case 0: return SwitchState.Off; - default: return SwitchState.Error; - } - } - } - - PuckState(int id) { - this.id = id; - } - - BasePlate getBasePlate() { - if (Controller.getInstance() == null) { - return null; - } - return Controller.getInstance().basePlate; - } - - void clear() { - online = false; - mecSwitch = SwitchState.Off; - indSwitch = SwitchState.Off; - BasePlate basePlate = getBasePlate(); - if (basePlate != null) { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty; - } - } - - boolean set(int mecSwitch, int indSwitch) { - online = true; - //TODO: Handle -1 value: error - SwitchState mec = SwitchState.fromInt(mecSwitch); - SwitchState ind = SwitchState.fromInt(indSwitch); - boolean changed = (mec != this.mecSwitch) || (mec != this.mecSwitch); - this.mecSwitch = mec; - this.indSwitch = ind; - - BasePlate basePlate = getBasePlate(); - if (basePlate != null) { - Puck puck = basePlate.getPucks()[id - 1]; - switch(puck.getDetectionMode()){ - case Mechanical: - if (this.mecSwitch ==SwitchState.On ) { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Present; - } else if (this.mecSwitch ==SwitchState.Off ) { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty; - } else { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Error; - } - break; - - case Inductive: - if (this.indSwitch ==SwitchState.On ) { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Present; - } else if (this.indSwitch ==SwitchState.Off ) { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty; - } else { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Error; - } - break; - - case Both: - if ((this.mecSwitch != this.indSwitch) ||(this.mecSwitch == SwitchState.Error) ||(this.mecSwitch == SwitchState.Error)) { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Error; - } else if (this.mecSwitch ==SwitchState.On ) { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Present; - } else { - basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty; - } - break; - } - } - return changed; - } - - @Override - public String toString() { - return "Online = " + online + "\ns1 = " + mecSwitch + "\ns2 = " + indSwitch; - } - -} +/* + * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved. + */ +package ch.psi.mxsc; + +/** + * + */ +public class PuckState { + + public final int id; + public boolean online; + public SwitchState mecSwitch; + public SwitchState indSwitch; + + public enum SwitchState{ + On, + Off, + Error; + static SwitchState fromInt(int value){ + switch (value){ + case 1: return SwitchState.On; + case 0: return SwitchState.Off; + default: return SwitchState.Error; + } + } + } + + PuckState(int id) { + this.id = id; + } + + BasePlate getBasePlate() { + if (Controller.getInstance() == null) { + return null; + } + return Controller.getInstance().basePlate; + } + + void clear() { + online = false; + mecSwitch = SwitchState.Off; + indSwitch = SwitchState.Off; + BasePlate basePlate = getBasePlate(); + if (basePlate != null) { + basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty; + } + } + + boolean set(int mecSwitch, int indSwitch) { + online = true; + //TODO: Handle -1 value: error + SwitchState mec = SwitchState.fromInt(mecSwitch); + SwitchState ind = SwitchState.fromInt(indSwitch); + boolean changed = (mec != this.mecSwitch) || (mec != this.mecSwitch); + this.mecSwitch = mec; + this.indSwitch = ind; + + BasePlate basePlate = getBasePlate(); + if (basePlate != null) { + Puck puck = basePlate.getPucks()[id - 1]; + Puck.Detection detection = null; + switch(puck.getDetectionMode()){ + case Mechanical: + if (this.mecSwitch ==SwitchState.On ) { + detection = Puck.Detection.Present; + } else if (this.mecSwitch ==SwitchState.Off ) { + detection = Puck.Detection.Empty; + } else { + detection = Puck.Detection.Error; + } + break; + + case Inductive: + if (this.indSwitch ==SwitchState.On ) { + detection = Puck.Detection.Present; + } else if (this.indSwitch ==SwitchState.Off ) { + detection = Puck.Detection.Empty; + } else { + detection = Puck.Detection.Error; + } + break; + + case Both: + if ((this.mecSwitch != this.indSwitch) ||(this.mecSwitch == SwitchState.Error) ||(this.mecSwitch == SwitchState.Error)) { + detection = Puck.Detection.Error; + } else if (this.mecSwitch ==SwitchState.On ) { + detection = Puck.Detection.Present; + } else { + detection = Puck.Detection.Empty; + } + break; + } + if (detection!=null){ + puck.detection = detection; + } + } + return changed; + } + + @Override + public String toString() { + return "Online = " + online + "\ns1 = " + mecSwitch + "\ns2 = " + indSwitch; + } + +}