use of jmri.NamedBeanHandle in project JMRI by JMRI.
the class SignallingPanel method updatePressed.
/**
* Update changes in SML when Update button is pressed in the Edit Logic - Add Logic pane.
*
* @param e the event heard
*/
void updatePressed(ActionEvent e) {
sourceMast = (SignalMast) sourceMastBox.getSelectedBean();
destMast = (SignalMast) destMastBox.getSelectedBean();
boolean smlPairAdded = false;
destOK = true;
if (sourceMast == destMast || fixedSourceMastLabel.getText() == destMast.getDisplayName()) {
JOptionPane.showMessageDialog(null, rb.getString("ErrorSignalMastIdentical"));
destOK = false;
log.debug("Destination Mast check failed, keep pane open");
return;
}
if ((sml == null) && (useLayoutEditor.isSelected())) {
boolean valid = false;
try {
valid = InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlockConnectivityTools().checkValidDest(sourceMast, destMast, LayoutBlockConnectivityTools.MASTTOMAST);
if (!valid) {
JOptionPane.showMessageDialog(null, rb.getString("ErrorUnReachableDestination"));
return;
}
} catch (jmri.JmriException je) {
JOptionPane.showMessageDialog(null, rb.getString("WarningUnabletoValidate"));
}
}
if (sml == null) {
// a new SML directly from the SML Table
sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).newSignalMastLogic(sourceMast);
// check if a similar SML pair already exists when in Add New session
if (!sml.getDestinationList().contains(destMast)) {
// not yet defined as a pair
smlPairAdded = true;
sml.setDestinationMast(destMast);
} else {
// show replace/update dialog
int mes = JOptionPane.showConfirmDialog(null, rb.getString("WarningExistingPair"), Bundle.getMessage("WarningTitle"), JOptionPane.YES_NO_OPTION);
if (mes == JOptionPane.NO_OPTION) {
return;
}
}
fixedSourceMastLabel.setText(sourceMast.getDisplayName());
fixedDestMastLabel.setText(destMast.getDisplayName());
sourceMastBox.setVisible(false);
destMastBox.setVisible(false);
fixedSourceMastLabel.setVisible(true);
fixedDestMastLabel.setVisible(true);
_autoTurnoutModel.smlValid();
_autoBlockModel.smlValid();
_autoSignalMastModel.smlValid();
}
initializeIncludedList();
sml.allowAutoMaticSignalMastGeneration(allowAutoMastGeneration.isSelected(), destMast);
boolean layouteditorgen = true;
try {
sml.useLayoutEditor(useLayoutEditor.isSelected(), destMast);
} catch (jmri.JmriException je) {
JOptionPane.showMessageDialog(null, je.toString());
layouteditorgen = false;
}
try {
if (useLayoutEditor.isSelected()) {
sml.useLayoutEditorDetails(useLayoutEditorTurnout.isSelected(), useLayoutEditorBlock.isSelected(), destMast);
}
} catch (jmri.JmriException ji) {
if (layouteditorgen) {
JOptionPane.showMessageDialog(null, ji.toString());
}
}
Hashtable<Block, Integer> hashBlocks = new Hashtable<Block, Integer>();
for (int i = 0; i < _includedManualBlockList.size(); i++) {
Block blk = jmri.InstanceManager.getDefault(jmri.BlockManager.class).getBlock(_includedManualBlockList.get(i).getSysName());
hashBlocks.put(blk, _includedManualBlockList.get(i).getState());
}
sml.setBlocks(hashBlocks, destMast);
Hashtable<NamedBeanHandle<Turnout>, Integer> hashTurnouts = new Hashtable<NamedBeanHandle<Turnout>, Integer>();
for (int i = 0; i < _includedManualTurnoutList.size(); i++) {
String turnoutName = _includedManualTurnoutList.get(i).getDisplayName();
Turnout turnout = jmri.InstanceManager.turnoutManagerInstance().getTurnout(_includedManualTurnoutList.get(i).getDisplayName());
NamedBeanHandle<Turnout> namedTurnout = nbhm.getNamedBeanHandle(turnoutName, turnout);
hashTurnouts.put(namedTurnout, _includedManualTurnoutList.get(i).getState());
// no specific value, just show the current turnout state as selection in comboBox.
// for existing SML pair, will be updated to show present setting by editDetails()
}
sml.setTurnouts(hashTurnouts, destMast);
Hashtable<NamedBeanHandle<Sensor>, Integer> hashSensors = new Hashtable<NamedBeanHandle<Sensor>, Integer>();
for (int i = 0; i < _includedManualSensorList.size(); i++) {
String sensorName = _includedManualSensorList.get(i).getDisplayName();
Sensor sensor = jmri.InstanceManager.sensorManagerInstance().getSensor(_includedManualSensorList.get(i).getDisplayName());
NamedBeanHandle<Sensor> namedSensor = nbhm.getNamedBeanHandle(sensorName, sensor);
hashSensors.put(namedSensor, _includedManualSensorList.get(i).getState());
// no specific value, just show the current sensor state as selection in comboBox.
// for existing SML pair, will be updated to show present setting by editDetails()
}
sml.setSensors(hashSensors, destMast);
Hashtable<SignalMast, String> hashSignalMasts = new Hashtable<SignalMast, String>();
for (int i = 0; i < _includedManualSignalMastList.size(); i++) {
if (_includedManualSignalMastList.get(i).getMast() == sourceMast || _includedManualSignalMastList.get(i).getMast() == destMast) {
// warn user that control mast is either source or destination mast of this pair, but allow as a valid choice
int mes = JOptionPane.showConfirmDialog(null, java.text.MessageFormat.format(rb.getString("SignalMastCriteriaOwn"), new Object[] { _includedManualSignalMastList.get(i).getMast().getDisplayName() }), rb.getString("SignalMastCriteriaOwnTitle"), JOptionPane.YES_NO_OPTION);
if (mes == 0) {
// Yes
hashSignalMasts.put(_includedManualSignalMastList.get(i).getMast(), _includedManualSignalMastList.get(i).getSetToState());
} else {
// No
// deselect "Included" checkBox for signal mast in manualSignalList
_includedManualSignalMastList.get(i).setIncluded(false);
initializeIncludedList();
_signalMastModel.fireTableDataChanged();
}
} else {
hashSignalMasts.put(_includedManualSignalMastList.get(i).getMast(), _includedManualSignalMastList.get(i).getSetToState());
}
}
sml.setMasts(hashSignalMasts, destMast);
sml.allowTurnoutLock(lockTurnouts.isSelected(), destMast);
sml.initialise(destMast);
if (smlPairAdded) {
log.debug("New SML");
// to show new SML in underlying table
firePropertyChange("newDestination", null, destMastBox.getSelectedBean());
}
}
use of jmri.NamedBeanHandle in project JMRI by JMRI.
the class DefaultSignalMastLogicManagerXml method loadSignalMastLogic.
public boolean loadSignalMastLogic(Element signalMastLogic) {
List<Element> logicList = signalMastLogic.getChildren("signalmastlogic");
if (log.isDebugEnabled()) {
log.debug("Found " + logicList.size() + " signal mast logics");
}
SignalMastManager sm = InstanceManager.getDefault(jmri.SignalMastManager.class);
SignalMastLogicManager sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
try {
String logicDelay = signalMastLogic.getChild("logicDelay").getText();
sml.setSignalLogicDelay(Long.parseLong(logicDelay));
} catch (java.lang.NullPointerException e) {
//Considered normal if it doesn't exists
}
boolean loadOk = true;
for (Element so : logicList) {
String source = so.getChild("sourceSignalMast").getText();
SignalMast sourceMast = sm.getSignalMast(source);
if (sourceMast != null) {
SignalMastLogic logic = sml.newSignalMastLogic(sourceMast);
List<Element> destList = so.getChildren("destinationMast");
for (Element s : destList) {
String destination = s.getChild("destinationSignalMast").getText();
SignalMast dest = sm.getSignalMast(destination);
if (dest != null) {
logic.setDestinationMast(dest);
if (s.getChild("comment") != null) {
logic.setComment(s.getChild("comment").getText(), dest);
}
if (s.getChild("enabled") != null) {
if (s.getChild("enabled").getText().equals("yes")) {
logic.setEnabled(dest);
} else {
logic.setDisabled(dest);
}
}
if (s.getChild("allowAutoMaticSignalMastGeneration") != null) {
if (s.getChild("allowAutoMaticSignalMastGeneration").getText().equals("no")) {
logic.allowAutoMaticSignalMastGeneration(false, dest);
} else {
logic.allowAutoMaticSignalMastGeneration(true, dest);
}
}
boolean useLayoutEditorTurnout = true;
boolean useLayoutEditorBlock = true;
if (s.getChild("useLayoutEditorTurnouts") != null) {
if (s.getChild("useLayoutEditorTurnouts").getText().equals("no")) {
useLayoutEditorTurnout = false;
}
}
if (s.getChild("useLayoutEditorBlocks") != null) {
if (s.getChild("useLayoutEditorBlocks").getText().equals("no")) {
useLayoutEditorBlock = false;
}
}
try {
logic.useLayoutEditorDetails(useLayoutEditorTurnout, useLayoutEditorBlock, dest);
} catch (jmri.JmriException ex) {
}
if (s.getChild("useLayoutEditor") != null) {
try {
if (s.getChild("useLayoutEditor").getText().equals("yes")) {
logic.useLayoutEditor(true, dest);
} else {
logic.useLayoutEditor(false, dest);
}
} catch (jmri.JmriException e) {
//Considered normal if layout editor hasn't yet been set up.
}
}
if (s.getChild("associatedSection") != null) {
Section sect = InstanceManager.getDefault(jmri.SectionManager.class).getSection(s.getChild("associatedSection").getText());
logic.setAssociatedSection(sect, dest);
}
Element turnoutElem = s.getChild("turnouts");
if (turnoutElem != null) {
List<Element> turnoutList = turnoutElem.getChildren("turnout");
if (turnoutList.size() > 0) {
Hashtable<NamedBeanHandle<Turnout>, Integer> list = new Hashtable<NamedBeanHandle<Turnout>, Integer>();
for (Element t : turnoutList) {
String turnout = t.getChild("turnoutName").getText();
String state = t.getChild("turnoutState").getText();
int value = Turnout.CLOSED;
if (state.equals("thrown")) {
value = Turnout.THROWN;
}
Turnout turn = InstanceManager.turnoutManagerInstance().getTurnout(turnout);
if (turn != null) {
NamedBeanHandle<Turnout> namedTurnout = nbhm.getNamedBeanHandle(turnout, turn);
list.put(namedTurnout, value);
}
log.debug("Unable to add Turnout {} as it does not exist in the panel file", turnout);
}
logic.setTurnouts(list, dest);
}
}
Element sensorElem = s.getChild("sensors");
if (sensorElem != null) {
List<Element> sensorList = sensorElem.getChildren("sensor");
if (sensorList.size() > 0) {
Hashtable<NamedBeanHandle<Sensor>, Integer> list = new Hashtable<NamedBeanHandle<Sensor>, Integer>();
for (Element sl : sensorList) {
String sensorName = sl.getChild("sensorName").getText();
String state = sl.getChild("sensorState").getText();
int value = Sensor.INACTIVE;
if (state.equals("active")) {
value = Sensor.ACTIVE;
}
Sensor sen = InstanceManager.sensorManagerInstance().getSensor(sensorName);
if (sen != null) {
NamedBeanHandle<Sensor> namedSensor = nbhm.getNamedBeanHandle(sensorName, sen);
list.put(namedSensor, value);
}
log.debug("Unable to add sensor {} as it does not exist in the panel file", sensorName);
}
logic.setSensors(list, dest);
}
}
Element blockElem = s.getChild("blocks");
if (blockElem != null) {
List<Element> blockList = blockElem.getChildren("block");
if (blockList.size() > 0) {
Hashtable<Block, Integer> list = new Hashtable<Block, Integer>();
for (Element b : blockList) {
String block = b.getChild("blockName").getText();
String state = b.getChild("blockState").getText();
int value = 0x03;
if (state.equals("occupied")) {
value = Block.OCCUPIED;
} else if (state.equals("unoccupied")) {
value = Block.UNOCCUPIED;
}
Block blk = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(block);
if (blk != null) {
list.put(blk, value);
}
log.debug("Unable to add Block {} as it does not exist in the panel file", block);
}
logic.setBlocks(list, dest);
}
}
Element mastElem = s.getChild("masts");
if (mastElem != null) {
List<Element> mastList = mastElem.getChildren("mast");
if (mastList.size() > 0) {
Hashtable<SignalMast, String> list = new Hashtable<SignalMast, String>();
for (Element m : mastList) {
String mast = m.getChild("mastName").getText();
String state = m.getChild("mastState").getText();
SignalMast mst = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(mast);
if (mst != null) {
list.put(mst, state);
}
log.debug("Unable to add Signal Mast {} as it does not exist in the panel file", mast);
}
logic.setMasts(list, dest);
}
}
} else {
log.error("Destination Mast " + destination + " Not found, logic not loaded");
loadOk = false;
}
}
} else {
log.error("Source Mast " + source + " Not found, logic not loaded");
loadOk = false;
}
}
sml.initialise();
return loadOk;
}
use of jmri.NamedBeanHandle in project JMRI by JMRI.
the class DefaultSignalMastLogicManagerXml method store.
@Override
public Element store(Object o) {
Element signalMastLogic = new Element("signalmastlogics");
setStoreElementClass(signalMastLogic);
SignalMastLogicManager smlm = (SignalMastLogicManager) o;
signalMastLogic.addContent(new Element("logicDelay").addContent(Long.toString(smlm.getSignalLogicDelay())));
ArrayList<SignalMastLogic> sml = smlm.getSignalMastLogicList();
for (int i = 0; i < sml.size(); i++) {
SignalMastLogic sm = sml.get(i);
Element source = new Element("signalmastlogic");
// added purely to make human reading of the xml easier
source.setAttribute("source", sm.getSourceMast().getDisplayName());
source.addContent(new Element("sourceSignalMast").addContent(sm.getSourceMast().getDisplayName()));
ArrayList<SignalMast> destination = sm.getDestinationList();
if (destination.size() != 0) {
for (int k = 0; k < destination.size(); k++) {
SignalMast dest = destination.get(k);
if (sml.get(i).getStoreState(dest) != SignalMastLogic.STORENONE) {
Element elem = new Element("destinationMast");
// added purely to make human reading of the xml easier
elem.setAttribute("destination", dest.getDisplayName());
elem.addContent(new Element("destinationSignalMast").addContent(dest.getDisplayName()));
elem.addContent(new Element("comment").addContent(sm.getComment(dest)));
if (sm.isEnabled(dest)) {
elem.addContent(new Element("enabled").addContent("yes"));
} else {
elem.addContent(new Element("enabled").addContent("no"));
}
if (sm.allowAutoMaticSignalMastGeneration(dest)) {
elem.addContent(new Element("allowAutoMaticSignalMastGeneration").addContent("yes"));
} else {
elem.addContent(new Element("allowAutoMaticSignalMastGeneration").addContent("no"));
}
if (sm.useLayoutEditor(dest)) {
elem.addContent(new Element("useLayoutEditor").addContent("yes"));
} else {
elem.addContent(new Element("useLayoutEditor").addContent("no"));
}
if (sm.useLayoutEditorTurnouts(dest)) {
elem.addContent(new Element("useLayoutEditorTurnouts").addContent("yes"));
} else {
elem.addContent(new Element("useLayoutEditorTurnouts").addContent("no"));
}
if (sm.useLayoutEditorBlocks(dest)) {
elem.addContent(new Element("useLayoutEditorBlocks").addContent("yes"));
} else {
elem.addContent(new Element("useLayoutEditorBlocks").addContent("no"));
}
if (sm.getAssociatedSection(dest) != null) {
elem.addContent(new Element("associatedSection").addContent(sm.getAssociatedSection(dest).getDisplayName()));
}
if (sm.isTurnoutLockAllowed(dest)) {
elem.addContent(new Element("lockTurnouts").addContent("yes"));
} else {
elem.addContent(new Element("lockTurnouts").addContent("no"));
}
if (sml.get(i).getStoreState(dest) == SignalMastLogic.STOREALL) {
ArrayList<Block> blocks = sm.getBlocks(dest);
if (blocks.size() > 0) {
Element blockElement = new Element("blocks");
for (int j = 0; j < blocks.size(); j++) {
Element bloc = new Element("block");
bloc.addContent(new Element("blockName").addContent(blocks.get(j).getDisplayName()));
String blkState = "anyState";
if (sm.getBlockState(blocks.get(j), dest) == Block.OCCUPIED) {
blkState = "occupied";
} else if (sm.getBlockState(blocks.get(j), dest) == Block.UNOCCUPIED) {
blkState = "unoccupied";
}
bloc.addContent(new Element("blockState").addContent(blkState));
blockElement.addContent(bloc);
}
elem.addContent(blockElement);
}
ArrayList<NamedBeanHandle<Turnout>> turnouts = sm.getNamedTurnouts(dest);
if (turnouts.size() > 0) {
Element turnoutElement = new Element("turnouts");
for (int j = 0; j < turnouts.size(); j++) {
Element turn = new Element("turnout");
turn.addContent(new Element("turnoutName").addContent(turnouts.get(j).getName()));
String turnState = "thrown";
if (sm.getTurnoutState(turnouts.get(j).getBean(), dest) == Turnout.CLOSED) {
turnState = "closed";
}
turn.addContent(new Element("turnoutState").addContent(turnState));
turnoutElement.addContent(turn);
}
elem.addContent(turnoutElement);
}
ArrayList<NamedBeanHandle<Sensor>> sensors = sm.getNamedSensors(dest);
if (sensors.size() > 0) {
Element sensorElement = new Element("sensors");
for (int j = 0; j < sensors.size(); j++) {
Element sensor = new Element("sensor");
sensor.addContent(new Element("sensorName").addContent(sensors.get(j).getName()));
String sensorState = "inActive";
if (sm.getSensorState(sensors.get(j).getBean(), dest) == Sensor.ACTIVE) {
sensorState = "active";
}
sensor.addContent(new Element("sensorState").addContent(sensorState));
sensorElement.addContent(sensor);
}
elem.addContent(sensorElement);
}
ArrayList<SignalMast> masts = sm.getSignalMasts(dest);
if (masts.size() > 0) {
Element mastElement = new Element("masts");
for (int j = 0; j < masts.size(); j++) {
Element mast = new Element("mast");
mast.addContent(new Element("mastName").addContent(masts.get(j).getDisplayName()));
mast.addContent(new Element("mastState").addContent(sm.getSignalMastState(masts.get(j), dest)));
mastElement.addContent(mast);
}
elem.addContent(mastElement);
}
}
source.addContent(elem);
}
}
signalMastLogic.addContent(source);
}
}
return signalMastLogic;
}
use of jmri.NamedBeanHandle in project JMRI by JMRI.
the class MergSD2SignalHeadTest method testCTor.
@Test
public void testCTor() {
Turnout it = (InstanceManager.getDefault(TurnoutManager.class)).provideTurnout("IT1");
NamedBeanHandle<Turnout> green = new NamedBeanHandle<>("green handle", it);
Turnout it2 = (InstanceManager.getDefault(TurnoutManager.class)).provideTurnout("IT3");
NamedBeanHandle<Turnout> red = new NamedBeanHandle<>("red handle", it2);
Turnout it3 = (InstanceManager.getDefault(TurnoutManager.class)).provideTurnout("IT2");
NamedBeanHandle<Turnout> yellow = new NamedBeanHandle<>("yellow handle", it3);
MergSD2SignalHead t = new MergSD2SignalHead("testsys", "testuser", 3, green, yellow, red, false, false);
Assert.assertNotNull("exists", t);
}
use of jmri.NamedBeanHandle in project JMRI by JMRI.
the class TripleOutputSignalHeadTest method testCTor.
@Test
public void testCTor() {
Turnout it = (InstanceManager.getDefault(TurnoutManager.class)).provideTurnout("IT1");
NamedBeanHandle<Turnout> green = new NamedBeanHandle<>("green handle", it);
Turnout it2 = (InstanceManager.getDefault(TurnoutManager.class)).provideTurnout("IT3");
NamedBeanHandle<Turnout> red = new NamedBeanHandle<>("red handle", it2);
Turnout it3 = (InstanceManager.getDefault(TurnoutManager.class)).provideTurnout("IT2");
NamedBeanHandle<Turnout> yellow = new NamedBeanHandle<>("yellow handle", it3);
TripleOutputSignalHead t = new TripleOutputSignalHead("Test Head", green, red, yellow);
Assert.assertNotNull("exists", t);
}
Aggregations