use of jmri.jmrit.roster.RosterEntry in project JMRI by JMRI.
the class ActivateTrainFrame method addNewTrain.
/**
* Handles press of "Add New Train" button by edit-checking populated values
* then (if no errors) creating an ActiveTrain and (optionally) an AutoActiveTrain
*/
private void addNewTrain(ActionEvent e) {
// get information
if (selectedTransit == null) {
// no transits available
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error15"), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
cancelInitiateTrain(null);
return;
}
String transitName = selectedTransit.getSystemName();
String trainName = "";
int index = startingBlockBox.getSelectedIndex();
if (index < 0) {
return;
}
String startBlockName = startingBlockBoxList.get(index).getSystemName();
int startBlockSeq = startingBlockSeqList.get(index).intValue();
index = destinationBlockBox.getSelectedIndex();
if (index < 0) {
return;
}
String endBlockName = destinationBlockBoxList.get(index).getSystemName();
int endBlockSeq = destinationBlockSeqList.get(index).intValue();
boolean autoRun = autoRunBox.isSelected();
if (!checkResetWhenDone()) {
return;
}
boolean resetWhenDone = resetWhenDoneBox.isSelected();
boolean reverseAtEnd = reverseAtEndBox.isSelected();
boolean allocateAllTheWay = allocateAllTheWayBox.isSelected();
int delayedStart = delayModeFromBox(delayedStartBox);
int delayedReStart = delayModeFromBox(delayedReStartBox);
int departureTimeHours = 8;
try {
departureTimeHours = Integer.parseInt(departureHrField.getText());
if ((departureTimeHours < 0) || (departureTimeHours > 23)) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("BadEntry3", departureHrField.getText()), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
log.warn("Range error in Departure Time Hours field");
return;
}
} catch (NumberFormatException ehr) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("BadEntry2", departureHrField.getText()), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
log.warn("Conversion exception in departure time hours field");
return;
}
int departureTimeMinutes = 8;
try {
departureTimeMinutes = Integer.parseInt(departureMinField.getText());
if ((departureTimeMinutes < 0) || (departureTimeMinutes > 59)) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("BadEntry3", departureMinField.getText()), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
log.warn("Range error in Departure Time Minutes field");
return;
}
} catch (NumberFormatException emn) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("BadEntry2", departureMinField.getText()), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
log.warn("Conversion exception in departure time minutes field");
return;
}
int delayRestartMinutes = 0;
try {
delayRestartMinutes = Integer.parseInt(delayMinField.getText());
if ((delayRestartMinutes < 0)) {
JOptionPane.showMessageDialog(initiateFrame, delayMinField.getText(), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
log.warn("Range error in Delay Restart Time Minutes field");
return;
}
} catch (NumberFormatException emn) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("BadEntry2", delayMinField.getText()), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
log.warn("Conversion exception in restart delay minutes field");
return;
}
int tSource = 0;
String dccAddress = "unknown";
if (_TrainsFromRoster) {
index = trainSelectBox.getSelectedIndex();
if (index < 0) {
// no trains available
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error14"), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
cancelInitiateTrain(null);
return;
}
trainName = (String) trainSelectBox.getSelectedItem();
RosterEntry r = trainBoxList.get(index);
dccAddress = r.getDccAddress();
if (!isAddressFree(r.getDccLocoAddress().getNumber())) {
// DCC address is already in use by an Active Train
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error40", dccAddress), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
tSource = ActiveTrain.ROSTER;
if (trainTypeBox.getSelectedIndex() != 0 && (r.getAttribute("DisptacherTrainType") == null || !r.getAttribute("DispatcherTrainType").equals("" + trainTypeBox.getSelectedItem()))) {
r.putAttribute("DispatcherTrainType", "" + trainTypeBox.getSelectedItem());
r.updateFile();
Roster.getDefault().writeRoster();
}
} else if (_TrainsFromTrains) {
tSource = ActiveTrain.OPERATIONS;
index = trainSelectBox.getSelectedIndex();
if (index < 0) {
// no trains available
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error14"), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
cancelInitiateTrain(null);
return;
}
trainName = (String) trainSelectBox.getSelectedItem();
} else if (_TrainsFromUser) {
trainName = trainNameField.getText();
if ((trainName == null) || trainName.equals("")) {
// no train name entered
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error14"), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
if (!isTrainFree(trainName)) {
// train name is already in use by an Active Train
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error24", trainName), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
dccAddress = dccAddressField.getText();
int address = -1;
try {
address = Integer.parseInt(dccAddress);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error23"), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
log.error("Conversion exception in dccAddress field");
return;
}
if ((address < 1) || (address > 9999)) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error23"), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
if (!isAddressFree(address)) {
// DCC address is already in use by an Active Train
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error40", address), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
tSource = ActiveTrain.USER;
}
int priority = 5;
try {
priority = Integer.parseInt(priorityField.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("BadEntry", priorityField.getText()), Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
log.error("Conversion exception in priority field");
return;
}
int trainType = trainTypeBox.getSelectedIndex();
if (autoRunBox.isSelected()) {
if (!readAutoRunItems()) {
return;
}
}
// create a new Active Train
ActiveTrain at = _dispatcher.createActiveTrain(transitName, trainName, tSource, startBlockName, startBlockSeq, endBlockName, endBlockSeq, autoRun, dccAddress, priority, resetWhenDone, reverseAtEnd, allocateAllTheWay, true, initiateFrame);
if (at == null) {
// error message sent by createActiveTrain
return;
}
if (tSource == ActiveTrain.ROSTER) {
at.setRosterEntry(trainBoxList.get(trainSelectBox.getSelectedIndex()));
}
at.setDelayedStart(delayedStart);
at.setDelayedReStart(delayedReStart);
at.setDepartureTimeHr(departureTimeHours);
at.setDepartureTimeMin(departureTimeMinutes);
at.setRestartDelay(delayRestartMinutes);
at.setDelaySensor((jmri.Sensor) delaySensor.getSelectedBean());
if ((_dispatcher.isFastClockTimeGE(departureTimeHours, departureTimeMinutes) && delayedStart != ActiveTrain.SENSORDELAY) || delayedStart == ActiveTrain.NODELAY) {
at.setStarted();
}
at.setRestartSensor((jmri.Sensor) delayReStartSensor.getSelectedBean());
at.setTrainType(trainType);
at.setTerminateWhenDone(terminateWhenDoneBox.isSelected());
if (autoRunBox.isSelected()) {
AutoActiveTrain aat = new AutoActiveTrain(at);
setAutoRunItems(aat);
if (!aat.initialize()) {
JOptionPane.showMessageDialog(initiateFrame, Bundle.getMessage("Error27", at.getTrainName()), Bundle.getMessage("InformationTitle"), JOptionPane.INFORMATION_MESSAGE);
}
_dispatcher.getAutoTrainsFrame().addAutoActiveTrain(aat);
}
_dispatcher.allocateNewActiveTrain(at);
initiateFrame.setVisible(false);
// prevent this window from being listed in the Window menu.
initiateFrame.dispose();
initiateFrame = null;
_dispatcher.newTrainDone(at);
}
use of jmri.jmrit.roster.RosterEntry in project JMRI by JMRI.
the class ActivateTrainFrame method initializeFreeTrainsCombo.
private void initializeFreeTrainsCombo() {
trainSelectBox.removeActionListener(trainSelectBoxListener);
trainSelectBox.removeAllItems();
trainBoxList.clear();
if (_TrainsFromRoster) {
// initialize free trains from roster
List<RosterEntry> l = Roster.getDefault().matchingList(null, null, null, null, null, null, null);
if (l.size() > 0) {
for (int i = 0; i < l.size(); i++) {
RosterEntry r = l.get(i);
String rName = r.titleString();
int rAddr = r.getDccLocoAddress().getNumber();
if (isTrainFree(rName) && isAddressFree(rAddr)) {
trainBoxList.add(r);
trainSelectBox.addItem(rName);
}
}
}
if (trainSelectBoxListener == null) {
trainSelectBoxListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
RosterEntry r = trainBoxList.get(trainSelectBox.getSelectedIndex());
if (transitsFromSpecificBlock) {
//resets the transit box if required
transitsFromSpecificBlock = false;
initializeFreeTransitsCombo(new ArrayList<Transit>());
}
if (r.getAttribute("DispatcherTrainType") != null && !r.getAttribute("DispatcherTrainType").equals("")) {
trainTypeBox.setSelectedItem(r.getAttribute("DispatcherTrainType"));
}
}
};
}
trainSelectBox.addActionListener(trainSelectBoxListener);
} else if (_TrainsFromTrains) {
// initialize free trains from operations
List<Train> trains = TrainManager.instance().getTrainsByNameList();
if (trains.size() > 0) {
for (int i = 0; i < trains.size(); i++) {
Train t = trains.get(i);
if (t != null) {
String tName = t.getName();
if (isTrainFree(tName)) {
trainSelectBox.addItem(tName);
}
}
}
}
}
if (trainBoxList.size() > 0) {
trainSelectBox.setSelectedIndex(0);
}
}
use of jmri.jmrit.roster.RosterEntry in project JMRI by JMRI.
the class LocoIconXml method load.
/**
* Create a PositionableLabel, then add to a target JLayeredPane
*
* @param element Top level Element to unpack.
* @param o an Editor as an Object
*/
@Override
public void load(Element element, Object o) {
Editor ed = (Editor) o;
LocoIcon l = new LocoIcon(ed);
// create the objects
String textName = "error";
try {
textName = element.getAttribute("text").getValue();
} catch (Exception e) {
log.error("failed to get loco text attribute ex= " + e);
}
String name = "error";
NamedIcon icon;
try {
name = element.getAttribute("icon").getValue();
} catch (Exception e) {
log.error("failed to get icon attribute ex= " + e);
}
if (name.equals("yes")) {
icon = loadIcon(l, "icon", element, "LocoIcon", ed);
} else {
icon = NamedIcon.getIconByName(name);
if (icon == null) {
icon = ed.loadFailed("LocoIcon", name);
if (icon == null) {
log.info("LocoIcon icon removed for url= " + name);
return;
}
}
}
l.updateIcon(icon);
try {
int x = element.getAttribute("dockX").getIntValue();
int y = element.getAttribute("dockY").getIntValue();
l.setDockingLocation(x, y);
// l.dock();
} catch (Exception e) {
log.warn("failed to get docking location= " + e);
}
String rosterId = null;
try {
rosterId = element.getAttribute("rosterentry").getValue();
RosterEntry entry = Roster.getDefault().entryFromTitle(rosterId);
l.setRosterEntry(entry);
} catch (Exception e) {
log.debug("no roster entry for " + rosterId + ", ex= " + e);
}
ed.putLocoIcon(l, textName);
// load individual item's option settings after editor has set its global settings
loadCommonAttributes(l, Editor.MARKERS, element);
loadTextInfo(l, element);
// to detect "background" color for use in Tracker, examine icon file
l.init();
}
use of jmri.jmrit.roster.RosterEntry in project JMRI by JMRI.
the class RosterEntryToGroupAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent event) {
roster = Roster.getDefault();
selections = new RosterGroupComboBox();
selections.setAllEntriesEnabled(false);
if (lastGroupSelect != null) {
selections.setSelectedItem(lastGroupSelect);
}
rosterEntryUpdate();
selections.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rosterEntryUpdate();
}
});
int retval = JOptionPane.showOptionDialog(_who, Bundle.getMessage("AddEntryToGroupDialog"), Bundle.getMessage("AddEntryToGroupTitle"), 0, JOptionPane.INFORMATION_MESSAGE, null, new Object[] { Bundle.getMessage("ButtonDone"), Bundle.getMessage("ButtonOK"), selections, rosterEntry }, null);
log.debug("Dialog value " + retval + " selected " + selections.getSelectedIndex() + ":" + selections.getSelectedItem() + ", " + rosterEntry.getSelectedIndex() + ":" + rosterEntry.getSelectedItem());
if (retval != 1) {
return;
}
String selEntry = (String) rosterEntry.getSelectedItem();
lastGroupSelect = (String) selections.getSelectedItem();
RosterEntry re = roster.entryFromTitle(selEntry);
String selGroup = Roster.getRosterGroupProperty((String) selections.getSelectedItem());
re.putAttribute(selGroup, "yes");
Roster.getDefault().writeRoster();
re.updateFile();
actionPerformed(event);
}
use of jmri.jmrit.roster.RosterEntry in project JMRI by JMRI.
the class RosterEntryListCellRenderer method getListCellRendererComponent.
// FIXME: JList needs typed
@Override
public Component getListCellRendererComponent(@SuppressWarnings("rawtypes") JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (value != null) {
if (value instanceof RosterEntry) {
String rosterEntryTitle = ((RosterEntry) value).titleString();
ImageIcon icon = InstanceManager.rosterIconFactoryInstance().getIcon(rosterEntryTitle);
if (icon != null) {
icon.setImageObserver(list);
}
setIcon(icon);
setText(rosterEntryTitle);
} else {
setText(value.toString());
setIcon(null);
}
}
return this;
}
Aggregations