@Override public void propertyChange(java.beans.PropertyChangeEvent e) { String property = e.getPropertyName(); if (property.equals("length")) { // a NamedBean added or deleted init(); fireTableDataChanged(); } else if (e.getSource() instanceof Warrant) { // a value changed. Find it, to avoid complete redraw Warrant bean = (Warrant) e.getSource(); for (int i = 0; i < _warList.size(); i++) { if (bean.equals(_warList.get(i))) { if (_warNX.contains(bean) && ((property.equals("runMode") && ((Integer) e.getNewValue()).intValue() == Warrant.MODE_NONE) || (property.equals("controlChange") && ((Integer) e.getNewValue()).intValue() == Warrant.ABORT))) { fireTableRowsDeleted(i, i); removeNXWarrant(bean); } else { fireTableRowsUpdated(i, i); } break; } } if (e.getPropertyName().equals("blockChange")) { OBlock oldBlock = (OBlock) e.getOldValue(); OBlock newBlock = (OBlock) e.getNewValue(); if (newBlock == null) { _frame.setStatusText( Bundle.getMessage( "ChangedRoute", bean.getDisplayName(), oldBlock.getDisplayName(), bean.getTrainName()), Color.red, true); } else { _frame.setStatusText( Bundle.getMessage( "TrackerBlockEnter", bean.getTrainName(), newBlock.getDisplayName()), myGreen, true); } } else if (e.getPropertyName().equals("blockRelease")) { OBlock block = (OBlock) e.getNewValue(); long et = (System.currentTimeMillis() - block._entryTime) / 1000; _frame.setStatusText( Bundle.getMessage( "TrackerBlockLeave", bean.getTrainName(), block.getDisplayName(), et / 60, et % 60), myGreen, true); } else if (e.getPropertyName().equals("SpeedRestriction")) { _frame.setStatusText( Bundle.getMessage( "speedChange", bean.getTrainName(), bean.getCurrentBlockOrder().getBlock().getDisplayName(), e.getNewValue()), myGold, true); } else if (e.getPropertyName().equals("SpeedChange")) { int row = getRow(bean); if (row >= 0) { fireTableRowsUpdated(row, row); // _frame.setStatusText(bean.getRunningMessage(), myGreen, true); } } else if (e.getPropertyName().equals("runMode")) { int oldMode = ((Integer) e.getOldValue()).intValue(); int newMode = ((Integer) e.getNewValue()).intValue(); if (oldMode == Warrant.MODE_NONE) { if (newMode != Warrant.MODE_NONE) { _frame.setStatusText( Bundle.getMessage( "warrantStart", bean.getTrainName(), bean.getDisplayName(), bean.getCurrentBlockOrder().getBlock().getDisplayName(), Bundle.getMessage(Warrant.MODES[newMode])), myGreen, true); } } else if (newMode == Warrant.MODE_NONE) { OBlock block = bean.getCurrentBlockOrder().getBlock(); int state = block.getState(); if ((state & OBlock.OCCUPIED) != 0 || (state & OBlock.DARK) != 0) { _frame.setStatusText( Bundle.getMessage( "warrantEnd", bean.getTrainName(), bean.getDisplayName(), block.getDisplayName()), myGreen, true); } else { _frame.setStatusText( Bundle.getMessage("warrantAbort", bean.getTrainName(), bean.getDisplayName()), myGreen, true); } } else { _frame.setStatusText( Bundle.getMessage( "modeChange", bean.getTrainName(), bean.getDisplayName(), Bundle.getMessage(Warrant.MODES[oldMode]), Bundle.getMessage(Warrant.MODES[newMode])), myGold, true); } } else if (e.getPropertyName().equals("controlChange")) { int runState = ((Integer) e.getOldValue()).intValue(); int newCntrl = ((Integer) e.getNewValue()).intValue(); String stateStr = null; if (runState < 0) { stateStr = Bundle.getMessage(Warrant.MODES[-runState]); } else { stateStr = Bundle.getMessage( Warrant.RUN_STATE[runState], bean.getCurrentBlockOrder().getBlock().getDisplayName()); } _frame.setStatusText( Bundle.getMessage( "controlChange", bean.getTrainName(), stateStr, Bundle.getMessage(Warrant.CNTRL_CMDS[newCntrl])), myGold, true); } else if (e.getPropertyName().equals("throttleFail")) { _frame.setStatusText( Bundle.getMessage("ThrottleFail", bean.getTrainName(), e.getNewValue()), Color.red, true); } } if (log.isDebugEnabled()) log.debug( "propertyChange of \"" + e.getPropertyName() + "\" for " + e.getSource().getClass().getName()); }
/** * Called by {@link jmri.jmrit.logix.RouteFinder#run()}. If all goes well, * WarrantTableFrame.runTrain(warrant) will run the warrant * * @param orders list of block orders */ @Override public void selectedRoute(ArrayList<BlockOrder> orders) { if (log.isDebugEnabled()) { log.debug("NXFrame selectedRoute()"); } String msg = null; String name = getTrainName(); if (name == null || name.trim().length() == 0) { name = getAddress(); } String s = ("" + Math.random()).substring(2); Warrant warrant = new Warrant("IW" + s, "NX(" + getAddress() + ")"); if (!warrant.setDccAddress(getTrainId())) { msg = Bundle.getMessage("BadDccAddress", getTrainId()); } if (msg == null) { warrant.setTrainName(name); warrant.setBlockOrders(getOrders()); warrant.setOrders(getOrders()); } int mode; if (msg == null && !_runManual.isSelected()) { mode = Warrant.MODE_RUN; warrant.setNoRamp(_noRampBox.isSelected()); msg = getBoxData(); if (msg == null) { msg = makeCommands(warrant); } if (msg == null) { Calibrater calib = null; if (_calibrateBox.isSelected()) { warrant.setViaOrder(getViaBlockOrder()); calib = new Calibrater(warrant, _forward.isSelected(), getLocation()); msg = calib.verifyCalibrate(); if (msg != null) { calib = null; } } warrant.setCalibrater(calib); } } else { mode = Warrant.MODE_MANUAL; } WarrantTableFrame tableFrame = WarrantTableFrame.getInstance(); if (msg == null) { tableFrame.getModel().addNXWarrant(warrant); // need to catch propertyChange at start if (log.isDebugEnabled()) { log.debug("NXWarrant added to table"); } msg = tableFrame.runTrain(warrant, mode); tableFrame.scrollTable(); } if (msg != null) { if (log.isDebugEnabled()) { log.debug( "WarrantTableFrame run warrant. msg= " + msg + " Remove warrant " + warrant.getDisplayName()); } tableFrame.getModel().removeWarrant(warrant); } if (msg == null && mode == Warrant.MODE_RUN) { // if (log.isDebugEnabled()) log.debug("Warrant "+warrant.getDisplayName()+" // running."); if (_haltStartBox.isSelected()) { _haltStart = true; class Halter implements Runnable { Warrant war; Halter(Warrant w) { war = w; } @Override public void run() { int limit = 0; try { // wait until _engineer is assigned so HALT can take effect while (!war.controlRunTrain(Warrant.HALT) && limit < 3000) { Thread.sleep(200); limit += 200; } } catch (InterruptedException e) { war.controlRunTrain(Warrant.HALT); } } } Halter h = new Halter(warrant); new Thread(h).start(); } else { _haltStart = false; } } if (msg != null) { JOptionPane.showMessageDialog( this, msg, Bundle.getMessage("WarningTitle"), JOptionPane.WARNING_MESSAGE); } else { closeFrame(); if (log.isDebugEnabled()) { log.debug("Close Frame."); } } }
@Override public void setValueAt(Object value, int row, int col) { if (log.isDebugEnabled()) log.debug( "setValueAt: row= " + row + ", column= " + col + ", value= " + value.getClass().getName()); Warrant w = getWarrantAt(row); if (w == null) { log.warn("setValueAt row= " + row + " Warrant is null!"); return; } String msg = null; switch (col) { case WARRANT_COLUMN: case ROUTE_COLUMN: return; case TRAIN_NAME_COLUMN: w.setTrainName((String) value); break; case ADDRESS_COLUMN: String addr = (String) value; if (!w.setDccAddress(addr)) { msg = Bundle.getMessage("BadDccAddress", addr); } break; case ALLOCATE_COLUMN: msg = w.allocateRoute(null); if (msg == null) { _frame.setStatusText( Bundle.getMessage("completeAllocate", w.getDisplayName()), myGreen, false); } else { _frame.setStatusText(msg, myGold, false); msg = null; } break; case DEALLOC_COLUMN: if (w.getRunMode() == Warrant.MODE_NONE) { w.deAllocate(); _frame.setStatusText("", myGreen, false); } else { _frame.setStatusText(w.getRunModeMessage(), myGold, false); } break; case SET_COLUMN: msg = w.setRoute(0, null); if (msg == null) { _frame.setStatusText(Bundle.getMessage("pathsSet", w.getDisplayName()), myGreen, false); } else { w.deAllocate(); _frame.setStatusText(msg, myGold, false); msg = null; } break; case AUTO_RUN_COLUMN: msg = _frame.runTrain(w); break; case MANUAL_RUN_COLUMN: if (w.getRunMode() == Warrant.MODE_NONE) { if (w.getBlockOrders().size() == 0) { msg = Bundle.getMessage("EmptyRoute"); break; } msg = w.setRoute(0, null); if (msg == null) { msg = w.setRunMode(Warrant.MODE_MANUAL, null, null, null, false); } if (msg != null) { w.deAllocate(); } else { msg = w.checkStartBlock(Warrant.MODE_RUN); // notify first block occupied by // this train if (msg != null) { _frame.setStatusText(msg, WarrantTableModel.myGold, false); } } if (log.isDebugEnabled()) log.debug("w.runManualTrain= " + msg); } else { msg = w.getRunModeMessage(); } break; case CONTROL_COLUMN: // Message is set when propertyChangeEvent (below) is received from // a warrant // change. fireTableRows then causes getValueAt() which calls // getRunningMessage() int mode = w.getRunMode(); if (mode == Warrant.MODE_LEARN) { Bundle.getMessage("Learning", w.getCurrentBlockOrder().getBlock().getDisplayName()); } else { String setting = (String) value; if (mode == Warrant.MODE_RUN || mode == Warrant.MODE_MANUAL) { int s = -1; if (setting.equals(WarrantTableFrame.halt)) { s = Warrant.HALT; } else if (setting.equals(WarrantTableFrame.resume)) { s = Warrant.RESUME; } else if (setting.equals(WarrantTableFrame.retry)) { s = Warrant.RETRY; } else if (setting.equals(WarrantTableFrame.abort)) { s = Warrant.ABORT; } w.controlRunTrain(s); } else if (setting.equals(WarrantTableFrame.abort)) { w.deAllocate(); } else if (mode == Warrant.MODE_NONE) { msg = Bundle.getMessage("NotRunning", w.getDisplayName()); } else { getValueAt(row, col); } } break; case EDIT_COLUMN: openWarrantFrame(w); break; case DELETE_COLUMN: if (w.getRunMode() == Warrant.MODE_NONE) { removeNXWarrant(w); // removes any warrant } else { w.controlRunTrain(Warrant.ABORT); if (_warNX.contains(w)) { // don't remove regular warrants removeNXWarrant(w); } } break; default: log.error("Invalid Column " + col + " requested."); throw new java.lang.IllegalArgumentException("Invalid Column " + col + " requested."); } if (msg != null) { JOptionPane.showMessageDialog( null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.WARNING_MESSAGE); _frame.setStatusText(msg, Color.red, true); } fireTableRowsUpdated(row, row); }