use of jmri.RouteManager in project JMRI by JMRI.
the class SensorGroupFrame method viewPressed.
void viewPressed() {
for (int i = 0; i < _sensorModel.getRowCount(); i++) {
_sensorModel.setValueAt(Boolean.FALSE, i, BeanTableModel.INCLUDE_COLUMN);
}
// look for name in List panel
String group = _sensorGroupList.getSelectedValue();
if (group == null) {
// not there, look in text field
group = _nameField.getText().toUpperCase().trim();
}
_nameField.setText(group);
// Look for Sensor group in Route table
RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
List<String> l = rm.getSystemNameList();
String prefix = (namePrefix + group + nameDivider).toUpperCase();
boolean isRoute = false;
int setRow = 0;
for (int i = 0; i < l.size(); i++) {
String name = l.get(i);
if (name.startsWith(prefix)) {
isRoute = true;
String sensor = name.substring(prefix.length());
// find and check that sensor
for (int j = _sensorModel.getRowCount() - 1; j >= 0; j--) {
if (_sensorModel.getValueAt(j, BeanTableModel.SNAME_COLUMN).equals(sensor)) {
_sensorModel.setValueAt(Boolean.TRUE, j, BeanTableModel.INCLUDE_COLUMN);
setRow = j;
}
}
}
}
// look for Sensor group in SYSTEM Logix
if (!isRoute) {
Logix logix = getSystemLogix();
String cSystemName = (ConditionalSystemPrefix + group).toUpperCase();
String cUserName = ConditionalUserPrefix + group;
for (int i = 0; i < logix.getNumConditionals(); i++) {
String name = logix.getConditionalByNumberOrder(i);
if (cSystemName.equals(name) || cUserName.equals(name)) {
Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(name);
if (c == null) {
log.error("Conditional \"" + name + "\" expected but NOT found in Logix " + logix.getSystemName());
} else {
ArrayList<ConditionalVariable> variableList = c.getCopyOfStateVariables();
for (int k = 0; k < variableList.size(); k++) {
String sensor = variableList.get(k).getName();
for (int j = _sensorModel.getRowCount() - 1; j >= 0; j--) {
if (_sensorModel.getValueAt(j, BeanTableModel.SNAME_COLUMN).equals(sensor)) {
_sensorModel.setValueAt(Boolean.TRUE, j, BeanTableModel.INCLUDE_COLUMN);
setRow = j;
}
}
}
}
}
}
}
_sensorModel.fireTableDataChanged();
setRow -= 9;
if (setRow < 0) {
setRow = 0;
}
_sensorScrollPane.getVerticalScrollBar().setValue(setRow * rowHeight);
}
use of jmri.RouteManager in project JMRI by JMRI.
the class SensorGroupFrame method initComponents.
@Override
public void initComponents() {
addHelpMenu("package.jmri.jmrit.sensorgroup.SensorGroupFrame", true);
setTitle("Define Sensor Group");
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
// add the sensor table
JPanel p2xs = new JPanel();
JPanel p21s = new JPanel();
p21s.setLayout(new BoxLayout(p21s, BoxLayout.Y_AXIS));
p21s.add(new JLabel("Please select"));
p21s.add(new JLabel("Sensors to "));
p21s.add(new JLabel("be included "));
p21s.add(new JLabel("in this group."));
p2xs.add(p21s);
_sensorModel = new SensorTableModel();
JTable sensorTable = new JTable(_sensorModel);
/*
JTable sensorTable = jmri.util.JTableUtil.sortableDataModel(sensorModel);
try {
jmri.util.com.sun.TableSorter tmodel = ((jmri.util.com.sun.TableSorter)sensorTable.getModel());
tmodel.setColumnComparator(String.class, new jmri.util.SystemNameComparator());
tmodel.setSortingStatus(SensorTableModel.SNAME_COLUMN, jmri.util.com.sun.TableSorter.ASCENDING);
} catch (ClassCastException e3) {} // if not a sortable table model
*/
sensorTable.setRowSelectionAllowed(false);
sensorTable.setPreferredScrollableViewportSize(new java.awt.Dimension(450, 200));
TableColumnModel sensorColumnModel = sensorTable.getColumnModel();
TableColumn includeColumnS = sensorColumnModel.getColumn(SensorTableModel.INCLUDE_COLUMN);
includeColumnS.setResizable(false);
includeColumnS.setMinWidth(50);
includeColumnS.setMaxWidth(60);
TableColumn sNameColumnS = sensorColumnModel.getColumn(SensorTableModel.SNAME_COLUMN);
sNameColumnS.setResizable(true);
sNameColumnS.setMinWidth(75);
sNameColumnS.setPreferredWidth(95);
TableColumn uNameColumnS = sensorColumnModel.getColumn(SensorTableModel.UNAME_COLUMN);
uNameColumnS.setResizable(true);
uNameColumnS.setMinWidth(210);
uNameColumnS.setPreferredWidth(260);
rowHeight = sensorTable.getRowHeight();
_sensorScrollPane = new JScrollPane(sensorTable);
p2xs.add(_sensorScrollPane, BorderLayout.CENTER);
getContentPane().add(p2xs);
p2xs.setVisible(true);
// add name field
JPanel p3 = new JPanel();
p3.add(new JLabel("Group Name:"));
_nameField = new JTextField(20);
p3.add(_nameField);
getContentPane().add(p3);
// button
JPanel p4 = new JPanel();
JButton viewButton = new JButton(" View ");
viewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
viewPressed();
}
});
p4.add(viewButton);
JButton addButton = new JButton("Make Group");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addPressed();
}
});
p4.add(addButton);
JButton undoButton = new JButton("Undo Group");
undoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
undoGroupPressed();
}
});
p4.add(undoButton);
getContentPane().add(p4);
JPanel p5 = new JPanel();
DefaultListModel<String> groupModel = new DefaultListModel<String>();
// Look for Sensor group in Route table
RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
List<String> routeList = rm.getSystemNameList();
int i = 0;
while (i < routeList.size()) {
String name = routeList.get(i);
if (name.startsWith(namePrefix)) {
name = name.substring(namePrefix.length());
String group = name.substring(0, name.indexOf(nameDivider));
String prefix = namePrefix + group + nameDivider;
do {
i++;
if (i >= routeList.size()) {
break;
}
name = routeList.get(i);
} while (name.startsWith(prefix));
groupModel.addElement(group);
}
i++;
}
// Look for Sensor group in Logix
Logix logix = getSystemLogix();
for (i = 0; i < logix.getNumConditionals(); i++) {
String name = logix.getConditionalByNumberOrder(i);
Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(name);
String uname = c.getUserName();
if (uname != null) {
groupModel.addElement(uname.substring(ConditionalUserPrefix.length()));
}
}
_sensorGroupList = new JList<String>(groupModel);
_sensorGroupList.setPrototypeCellValue(ConditionalUserPrefix + "XXXXXXXXXX");
_sensorGroupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
_sensorGroupList.setVisibleRowCount(5);
JScrollPane scrollPane = new JScrollPane(_sensorGroupList);
p5.add(scrollPane);
p5.add(Box.createHorizontalStrut(10));
JButton doneButton = new JButton(" Done ");
doneButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
donePressed(e);
}
});
p5.add(doneButton);
getContentPane().add(p5);
// pack to cause display
pack();
}
use of jmri.RouteManager in project JMRI by JMRI.
the class Follower method instantiate.
/**
* Create the underlying objects that implement this
*/
public void instantiate() {
String nameT = namePrefix + "T" + nameDivider + output;
String nameC = namePrefix + "C" + nameDivider + output;
RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
Route rt = rm.getBySystemName(nameT);
// if an old one exists, remove it
if (rt != null) {
rt.deActivateRoute();
rm.deleteRoute(rt);
}
Route rc = rm.getBySystemName(nameC);
// if an old one exists, remove it
if (rc != null) {
rc.deActivateRoute();
rm.deleteRoute(rc);
}
// create a new one
rt = new DefaultRoute(nameT);
rc = new DefaultRoute(nameC);
// add trigger Sensor
rt.addSensorToRoute(sensor, invert ? Route.ONINACTIVE : Route.ONACTIVE);
rc.addSensorToRoute(sensor, !invert ? Route.ONINACTIVE : Route.ONACTIVE);
// optionally, add veto
if (!veto.equals("")) {
rt.addSensorToRoute(veto, Route.VETOACTIVE);
rc.addSensorToRoute(veto, Route.VETOACTIVE);
}
// add output
rt.addOutputTurnout(output, Turnout.THROWN);
rc.addOutputTurnout(output, Turnout.CLOSED);
// and put Route into operation
rt.activateRoute();
rc.activateRoute();
rm.register(rt);
rm.register(rc);
}
use of jmri.RouteManager in project JMRI by JMRI.
the class DefaultRouteManagerXml method store.
/**
* Default implementation for storing the contents of a RouteManager
*
* @param o Object to store, of type RouteManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element routes = new Element("routes");
setStoreElementClass(routes);
RouteManager tm = (RouteManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not routes to include
if (!iter.hasNext()) {
return null;
}
// store the routes
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
break;
}
log.debug("system name is " + sname);
Route r = tm.getBySystemName(sname);
String cTurnout = r.getControlTurnout();
int addedDelay = r.getRouteCommandDelay();
boolean routeLocked = r.getLocked();
String cLockTurnout = r.getLockControlTurnout();
Element elem = new Element("route");
elem.addContent(new Element("systemName").addContent(sname));
// store common parts
storeCommon(r, elem);
if (cTurnout != null && !cTurnout.equals("")) {
elem.setAttribute("controlTurnout", cTurnout);
int state = r.getControlTurnoutState();
if (state == Route.ONTHROWN) {
elem.setAttribute("controlTurnoutState", "THROWN");
} else if (state == Route.ONCHANGE) {
elem.setAttribute("controlTurnoutState", "CHANGE");
} else if (state == Route.VETOCLOSED) {
elem.setAttribute("controlTurnoutState", "VETOCLOSED");
} else if (state == Route.VETOTHROWN) {
elem.setAttribute("controlTurnoutState", "VETOTHROWN");
} else {
elem.setAttribute("controlTurnoutState", "CLOSED");
}
}
if (cLockTurnout != null && !cLockTurnout.equals("")) {
elem.setAttribute("controlLockTurnout", cLockTurnout);
int state = r.getLockControlTurnoutState();
if (state == Route.ONTHROWN) {
elem.setAttribute("controlLockTurnoutState", "THROWN");
} else if (state == Route.ONCHANGE) {
elem.setAttribute("controlLockTurnoutState", "CHANGE");
} else {
elem.setAttribute("controlLockTurnoutState", "CLOSED");
}
}
if (addedDelay > 0) {
elem.setAttribute("addedDelay", Integer.toString(addedDelay));
}
if (routeLocked) {
elem.setAttribute("routeLocked", "True");
}
// add route output Turnouts, if any
int index = 0;
String rTurnout = null;
while ((rTurnout = r.getOutputTurnoutByIndex(index)) != null) {
Element rElem = new Element("routeOutputTurnout").setAttribute("systemName", rTurnout);
String sState = "CLOSED";
if (r.getOutputTurnoutSetState(rTurnout) == Turnout.THROWN) {
sState = "THROWN";
} else if (r.getOutputTurnoutSetState(rTurnout) == Route.TOGGLE) {
sState = "TOGGLE";
}
rElem.setAttribute("state", sState);
elem.addContent(rElem);
index++;
}
// add route output Sensors, if any
index = 0;
String rSensor = null;
while ((rSensor = r.getOutputSensorByIndex(index)) != null) {
Element rElem = new Element("routeOutputSensor").setAttribute("systemName", rSensor);
String sState = "INACTIVE";
if (r.getOutputSensorSetState(rSensor) == Sensor.ACTIVE) {
sState = "ACTIVE";
} else if (r.getOutputSensorSetState(rSensor) == Route.TOGGLE) {
sState = "TOGGLE";
}
rElem.setAttribute("state", sState);
elem.addContent(rElem);
index++;
}
// add route control Sensors, if any
index = 0;
//rSensor = null; // previous while forces rSensor to null
while ((rSensor = r.getRouteSensorName(index)) != null) {
Element rsElem = new Element("routeSensor").setAttribute("systemName", rSensor);
int mode = r.getRouteSensorMode(index);
String modeName;
switch(mode) {
case Route.ONACTIVE:
modeName = "onActive";
break;
case Route.ONINACTIVE:
modeName = "onInactive";
break;
case Route.ONCHANGE:
modeName = "onChange";
break;
case Route.VETOACTIVE:
modeName = "vetoActive";
break;
case Route.VETOINACTIVE:
modeName = "vetoInactive";
break;
default:
modeName = null;
}
if (modeName != null) {
rsElem.setAttribute("mode", modeName);
}
elem.addContent(rsElem);
index++;
}
// add sound and script file elements if needed
if (r.getOutputSoundName() != null && !r.getOutputSoundName().equals("")) {
Element rsElem = new Element("routeSoundFile").setAttribute("name", jmri.util.FileUtil.getPortableFilename(new java.io.File(r.getOutputSoundName())));
elem.addContent(rsElem);
}
if (r.getOutputScriptName() != null && !r.getOutputScriptName().equals("")) {
Element rsElem = new Element("routeScriptFile").setAttribute("name", jmri.util.FileUtil.getPortableFilename(new java.io.File(r.getOutputScriptName())));
elem.addContent(rsElem);
}
// add turnouts aligned sensor if there is one
if (!r.getTurnoutsAlignedSensor().equals("")) {
Element rsElem = new Element("turnoutsAlignedSensor").setAttribute("name", r.getTurnoutsAlignedSensor());
elem.addContent(rsElem);
}
log.debug("store route " + sname);
routes.addContent(elem);
}
}
return routes;
}
use of jmri.RouteManager in project JMRI by JMRI.
the class DefaultRouteManagerXml method loadRoutes.
/**
* Utility method to load the individual Route objects. If there's no
* additional info needed for a specific route type, invoke this with the
* parent of the set of Route elements.
*
* @param routes Element containing the Route elements to load.
*/
public void loadRoutes(Element routes) {
List<Element> routeList = routes.getChildren("route");
if (log.isDebugEnabled()) {
log.debug("Found " + routeList.size() + " routes");
}
RouteManager tm = InstanceManager.getDefault(jmri.RouteManager.class);
for (int i = 0; i < routeList.size(); i++) {
String sysName = getSystemName(routeList.get(i));
if (sysName == null) {
log.warn("unexpected null in systemName " + routeList.get(i));
break;
}
String userName = getUserName(routeList.get(i));
String cTurnout = null;
String cTurnoutState = null;
String addedDelayTxt = null;
String routeLockedTxt = null;
String cLockTurnout = null;
String cLockTurnoutState = null;
int addedDelay = 0;
if (routeList.get(i).getAttribute("controlTurnout") != null) {
cTurnout = routeList.get(i).getAttribute("controlTurnout").getValue();
}
if (routeList.get(i).getAttribute("controlTurnoutState") != null) {
cTurnoutState = routeList.get(i).getAttribute("controlTurnoutState").getValue();
}
if (routeList.get(i).getAttribute("controlLockTurnout") != null) {
cLockTurnout = routeList.get(i).getAttribute("controlLockTurnout").getValue();
}
if (routeList.get(i).getAttribute("controlLockTurnoutState") != null) {
cLockTurnoutState = routeList.get(i).getAttribute("controlLockTurnoutState").getValue();
}
if (routeList.get(i).getAttribute("addedDelay") != null) {
addedDelayTxt = routeList.get(i).getAttribute("addedDelay").getValue();
if (addedDelayTxt != null) {
addedDelay = Integer.parseInt(addedDelayTxt);
}
}
if (routeList.get(i).getAttribute("routeLocked") != null) {
routeLockedTxt = routeList.get(i).getAttribute("routeLocked").getValue();
}
if (log.isDebugEnabled()) {
log.debug("create route: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
}
Route r;
try {
r = tm.provideRoute(sysName, userName);
} catch (IllegalArgumentException ex) {
log.error("failed to create Route: " + sysName);
return;
}
// load common parts
loadCommon(r, routeList.get(i));
// add control turnout if there is one
if (cTurnout != null) {
r.setControlTurnout(cTurnout);
if (cTurnoutState != null) {
if (cTurnoutState.equals("THROWN")) {
r.setControlTurnoutState(Route.ONTHROWN);
} else if (cTurnoutState.equals("CHANGE")) {
r.setControlTurnoutState(Route.ONCHANGE);
} else if (cTurnoutState.equals("VETOCLOSED")) {
r.setControlTurnoutState(Route.VETOCLOSED);
} else if (cTurnoutState.equals("VETOTHROWN")) {
r.setControlTurnoutState(Route.VETOTHROWN);
} else {
r.setControlTurnoutState(Route.ONCLOSED);
}
} else {
log.error("cTurnoutState was null!");
}
}
// set added delay
r.setRouteCommandDelay(addedDelay);
// determine if route locked
if (routeLockedTxt != null && routeLockedTxt.equals("True")) {
r.setLocked(true);
}
//add lock control turout if there is one
if (cLockTurnout != null) {
r.setLockControlTurnout(cLockTurnout);
if (cLockTurnoutState != null) {
if (cLockTurnoutState.equals("THROWN")) {
r.setLockControlTurnoutState(Route.ONTHROWN);
} else if (cLockTurnoutState.equals("CHANGE")) {
r.setLockControlTurnoutState(Route.ONCHANGE);
} else {
r.setLockControlTurnoutState(Route.ONCLOSED);
}
} else {
log.error("cLockTurnoutState was null!");
}
}
// load output turnouts if there are any - old format first (1.7.6 and before)
List<Element> routeTurnoutList = routeList.get(i).getChildren("routeTurnout");
if (routeTurnoutList.size() > 0) {
// This route has turnouts
for (int k = 0; k < routeTurnoutList.size(); k++) {
if (((routeTurnoutList.get(k))).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + ((routeTurnoutList.get(k))) + " " + ((routeTurnoutList.get(k))).getAttributes());
break;
}
String tSysName = ((routeTurnoutList.get(k))).getAttribute("systemName").getValue();
String rState = ((routeTurnoutList.get(k))).getAttribute("state").getValue();
int tSetState = Turnout.CLOSED;
if (rState.equals("THROWN")) {
tSetState = Turnout.THROWN;
} else if (rState.equals("TOGGLE")) {
tSetState = Route.TOGGLE;
}
// Add turnout to route
r.addOutputTurnout(tSysName, tSetState);
}
}
// load output turnouts if there are any - new format
routeTurnoutList = routeList.get(i).getChildren("routeOutputTurnout");
if (routeTurnoutList.size() > 0) {
// This route has turnouts
for (int k = 0; k < routeTurnoutList.size(); k++) {
if (routeTurnoutList.get(k).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + routeTurnoutList.get(k) + " " + routeTurnoutList.get(k).getAttributes());
break;
}
String tSysName = routeTurnoutList.get(k).getAttribute("systemName").getValue();
String rState = routeTurnoutList.get(k).getAttribute("state").getValue();
int tSetState = Turnout.CLOSED;
if (rState.equals("THROWN")) {
tSetState = Turnout.THROWN;
} else if (rState.equals("TOGGLE")) {
tSetState = Route.TOGGLE;
}
// we will not re add the turnout.
if (!r.isOutputTurnoutIncluded(tSysName)) {
// Add turnout to route
r.addOutputTurnout(tSysName, tSetState);
// determine if turnout should be locked
Turnout t = r.getOutputTurnout(k);
if (r.getLocked()) {
t.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
}
}
}
}
// load output sensors if there are any - new format
routeTurnoutList = routeList.get(i).getChildren("routeOutputSensor");
if (routeTurnoutList.size() > 0) {
// This route has turnouts
for (int k = 0; k < routeTurnoutList.size(); k++) {
if (routeTurnoutList.get(k).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + routeTurnoutList.get(k) + " " + routeTurnoutList.get(k).getAttributes());
break;
}
String tSysName = routeTurnoutList.get(k).getAttribute("systemName").getValue();
String rState = routeTurnoutList.get(k).getAttribute("state").getValue();
int tSetState = Sensor.INACTIVE;
if (rState.equals("ACTIVE")) {
tSetState = Sensor.ACTIVE;
} else if (rState.equals("TOGGLE")) {
tSetState = Route.TOGGLE;
}
// we will not re add the turnout.
if (r.isOutputSensorIncluded(tSysName)) {
break;
}
// Add turnout to route
r.addOutputSensor(tSysName, tSetState);
}
}
// load sound, script files if present
Element fileElement = routeList.get(i).getChild("routeSoundFile");
if (fileElement != null) {
// convert to absolute path name
r.setOutputSoundName(jmri.util.FileUtil.getExternalFilename(fileElement.getAttribute("name").getValue()));
}
fileElement = routeList.get(i).getChild("routeScriptFile");
if (fileElement != null) {
r.setOutputScriptName(jmri.util.FileUtil.getExternalFilename(fileElement.getAttribute("name").getValue()));
}
// load turnouts aligned sensor if there is one
fileElement = routeList.get(i).getChild("turnoutsAlignedSensor");
if (fileElement != null) {
r.setTurnoutsAlignedSensor(fileElement.getAttribute("name").getValue());
}
// load route control sensors, if there are any
List<Element> routeSensorList = routeList.get(i).getChildren("routeSensor");
if (routeSensorList.size() > 0) {
// This route has sensors
for (int k = 0; k < routeSensorList.size(); k++) {
if (routeSensorList.get(k).getAttribute("systemName") == null) {
log.warn("unexpected null in systemName " + routeSensorList.get(k) + " " + routeSensorList.get(k).getAttributes());
break;
}
// default mode
int mode = Route.ONACTIVE;
if (routeSensorList.get(k).getAttribute("mode") != null) {
String sm = routeSensorList.get(k).getAttribute("mode").getValue();
if (sm.equals("onActive")) {
mode = Route.ONACTIVE;
} else if (sm.equals("onInactive")) {
mode = Route.ONINACTIVE;
} else if (sm.equals("onChange")) {
mode = Route.ONCHANGE;
} else if (sm.equals("vetoActive")) {
mode = Route.VETOACTIVE;
} else if (sm.equals("vetoInactive")) {
mode = Route.VETOINACTIVE;
} else {
log.warn("unexpected sensor mode in route " + sysName + " was " + sm);
}
}
// Add Sensor to route
r.addSensorToRoute(routeSensorList.get(k).getAttribute("systemName").getValue(), mode);
}
}
// and start it working
r.activateRoute();
}
}
Aggregations