use of jmri.SignalHead in project JMRI by JMRI.
the class SignalGroupTableAction method addPressed.
/**
* Respond to click on Add... button below Signal Group Table.
* <p>
* Create JPanel with options for configuration.
* @param e Event from origin; null when called from Edit button in Signal Group Table row
*/
@Override
protected void addPressed(ActionEvent e) {
if (inEditMode) {
log.debug("Can not open another editing session for Signal Groups.");
// add user warning that a 2nd session not allowed (cf. Logix)
// Already editing a Signal Group, ask for completion of that edit first
String workingTitle = _systemName.getText();
if (workingTitle.isEmpty() || workingTitle == null) {
workingTitle = Bundle.getMessage("NONE");
_systemName.setText(workingTitle);
}
javax.swing.JOptionPane.showMessageDialog(addFrame, Bundle.getMessage("SigGroupEditBusyWarning", workingTitle), Bundle.getMessage("ErrorTitle"), javax.swing.JOptionPane.ERROR_MESSAGE);
return;
}
inEditMode = true;
_mastAspectsList = null;
jmri.SignalHeadManager shm = InstanceManager.getDefault(jmri.SignalHeadManager.class);
List<String> systemNameList = shm.getSystemNameList();
_signalHeadsList = new ArrayList<SignalGroupSignalHead>(systemNameList.size());
// create list of all available Single Output Signal Heads to choose from
Iterator<String> iter = systemNameList.iterator();
// int i = 1; // for debug of iter next
while (iter.hasNext()) {
String systemName = iter.next();
SignalHead sh = shm.getBySystemName(systemName);
// debug using i & sysnamelist.size
if (sh != null) {
if (sh.getClass().getName().contains("SingleTurnoutSignalHead")) {
String userName = sh.getUserName();
// add every single output signal head item to the list
_signalHeadsList.add(new SignalGroupSignalHead(systemName, userName));
} else {
log.debug("Signal Head " + systemName + " is not a Single Output Controlled Signal Head");
}
} else {
// this is not an error and the value of systemName mentioned is actually from the last head that was indeed loaded
log.error("Failed to get signal head {} (SGTA)", systemName);
}
}
// Set up Add/Edit Signal Group window
if (addFrame == null) {
// if it's not yet present, create addFrame
mainSignalComboBox = new JmriBeanComboBox(jmri.InstanceManager.getDefault(jmri.SignalMastManager.class), null, JmriBeanComboBox.DisplayOptions.DISPLAYNAME);
// causes NPE when user selects that 1st line, so do not respond to result null
mainSignalComboBox.setFirstItemBlank(true);
addFrame = new JmriJFrame(Bundle.getMessage("AddSignalGroup"), false, true);
addFrame.addHelpMenu("package.jmri.jmrit.beantable.SignalGroupAddEdit", true);
addFrame.setLocation(100, 30);
addFrame.getContentPane().setLayout(new BoxLayout(addFrame.getContentPane(), BoxLayout.Y_AXIS));
Container contentPane = addFrame.getContentPane();
// add system name
JPanel ps = new JPanel();
ps.setLayout(new FlowLayout());
ps.add(nameLabel);
ps.add(_systemName);
_systemName.setToolTipText(Bundle.getMessage("SignalGroupSysNameTooltip"));
ps.add(fixedSystemName);
fixedSystemName.setVisible(false);
contentPane.add(ps);
// add user name
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(userLabel);
p.add(_userName);
_userName.setToolTipText(Bundle.getMessage("SignalGroupUserNameTooltip"));
contentPane.add(p);
// add Signal Masts/Heads Display Choice
JPanel py = new JPanel();
py.add(new JLabel(Bundle.getMessage("Show")));
selGroup = new ButtonGroup();
allButton = new JRadioButton(Bundle.getMessage("All"), true);
selGroup.add(allButton);
py.add(allButton);
allButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Setup for display of all Signal Masts & SingleTO Heads, if needed
if (!showAll) {
showAll = true;
_SignalGroupHeadModel.fireTableDataChanged();
_AspectModel.fireTableDataChanged();
}
}
});
includedButton = new JRadioButton(Bundle.getMessage("Included"), false);
selGroup.add(includedButton);
py.add(includedButton);
includedButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Setup for display of included Turnouts only, if needed
if (showAll) {
showAll = false;
initializeIncludedList();
_SignalGroupHeadModel.fireTableDataChanged();
_AspectModel.fireTableDataChanged();
}
}
});
py.add(new JLabel(" " + Bundle.getMessage("_and_", Bundle.getMessage("LabelAspects"), Bundle.getMessage("SignalHeads"))));
contentPane.add(py);
// add main signal mast table
JPanel p3 = new JPanel();
p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
JPanel p31 = new JPanel();
p31.add(new JLabel(Bundle.getMessage("EnterMastAttached", Bundle.getMessage("BeanNameSignalMast"))));
p3.add(p31);
JPanel p32 = new JPanel();
p32.add(new JLabel(Bundle.getMessage("MakeLabel", Bundle.getMessage("BeanNameSignalMast"))));
// comboBox to pick a main Signal Mast
p32.add(mainSignalComboBox);
p3.add(p32);
p3xsi = new JPanel();
JPanel p3xsiSpace = new JPanel();
p3xsiSpace.setLayout(new BoxLayout(p3xsiSpace, BoxLayout.Y_AXIS));
p3xsiSpace.add(new JLabel(" "));
p3xsi.add(p3xsiSpace);
JPanel p31si = new JPanel();
p31si.setLayout(new BoxLayout(p31si, BoxLayout.Y_AXIS));
p31si.add(new JLabel(Bundle.getMessage("SelectAppearanceTrigger")));
p3xsi.add(p31si);
_AspectModel = new SignalMastAspectModel();
JTable SignalMastAspectTable = new JTable(_AspectModel);
TableRowSorter<SignalMastAspectModel> smaSorter = new TableRowSorter<>(_AspectModel);
smaSorter.setComparator(SignalMastAspectModel.ASPECT_COLUMN, new SystemNameComparator());
RowSorterUtil.setSortOrder(smaSorter, SignalMastAspectModel.ASPECT_COLUMN, SortOrder.ASCENDING);
SignalMastAspectTable.setRowSorter(smaSorter);
SignalMastAspectTable.setRowSelectionAllowed(false);
SignalMastAspectTable.setPreferredScrollableViewportSize(new java.awt.Dimension(200, 80));
TableColumnModel SignalMastAspectColumnModel = SignalMastAspectTable.getColumnModel();
TableColumn includeColumnA = SignalMastAspectColumnModel.getColumn(SignalGroupTableAction.SignalMastAspectModel.INCLUDE_COLUMN);
includeColumnA.setResizable(false);
includeColumnA.setMinWidth(30);
includeColumnA.setMaxWidth(60);
@SuppressWarnings("static-access") TableColumn sNameColumnA = SignalMastAspectColumnModel.getColumn(_AspectModel.ASPECT_COLUMN);
sNameColumnA.setResizable(true);
sNameColumnA.setMinWidth(75);
sNameColumnA.setMaxWidth(140);
_SignalAppearanceScrollPane = new JScrollPane(SignalMastAspectTable);
p3xsi.add(_SignalAppearanceScrollPane, BorderLayout.CENTER);
p3.add(p3xsi);
p3xsi.setVisible(true);
// respond to comboBox selection
mainSignalComboBox.addActionListener(new ActionListener() {
//public void focusGained(FocusEvent e) {
//}
@Override
public void actionPerformed(ActionEvent event) {
if (mainSignalComboBox.getSelectedBean() == null) {
// ie. empty first row was selected or set
log.debug("Empty line in mainSignal comboBox");
//setValidSignalMastAspects(); // clears the Aspect table
} else {
if (curSignalGroup == null || mainSignalComboBox.getSelectedBean() != curSignalGroup.getSignalMast()) {
log.debug("comboBox closed, choice: {}", mainSignalComboBox.getSelectedItem());
// refresh table with signal mast aspects
setValidSignalMastAspects();
} else {
log.debug("Mast {} picked in mainSignal comboBox", mainSignalComboBox.getSelectedItem());
}
}
}
});
// complete this panel
Border p3Border = BorderFactory.createEtchedBorder();
p3.setBorder(p3Border);
contentPane.add(p3);
p2xsi = new JPanel();
JPanel p2xsiSpace = new JPanel();
p2xsiSpace.setLayout(new BoxLayout(p2xsiSpace, BoxLayout.Y_AXIS));
p2xsiSpace.add(new JLabel("XXX"));
p2xsi.add(p2xsiSpace);
JPanel p21si = new JPanel();
p21si.setLayout(new BoxLayout(p21si, BoxLayout.Y_AXIS));
p21si.add(new JLabel(Bundle.getMessage("SelectInGroup", Bundle.getMessage("SignalHeads"))));
p2xsi.add(p21si);
_SignalGroupHeadModel = new SignalGroupSignalHeadModel();
JTable SignalGroupHeadTable = new JTable(_SignalGroupHeadModel);
TableRowSorter<SignalGroupSignalHeadModel> sgsSorter = new TableRowSorter<>(_SignalGroupHeadModel);
sgsSorter.setComparator(SignalGroupSignalHeadModel.SNAME_COLUMN, new SystemNameComparator());
RowSorterUtil.setSortOrder(sgsSorter, SignalGroupSignalHeadModel.SNAME_COLUMN, SortOrder.ASCENDING);
SignalGroupHeadTable.setRowSorter(sgsSorter);
SignalGroupHeadTable.setRowSelectionAllowed(false);
SignalGroupHeadTable.setPreferredScrollableViewportSize(new java.awt.Dimension(480, 160));
TableColumnModel SignalGroupSignalColumnModel = SignalGroupHeadTable.getColumnModel();
TableColumn includeColumnSi = SignalGroupSignalColumnModel.getColumn(SignalGroupSignalHeadModel.INCLUDE_COLUMN);
includeColumnSi.setResizable(false);
includeColumnSi.setMinWidth(30);
includeColumnSi.setMaxWidth(60);
TableColumn sNameColumnSi = SignalGroupSignalColumnModel.getColumn(SignalGroupSignalHeadModel.SNAME_COLUMN);
sNameColumnSi.setResizable(true);
sNameColumnSi.setMinWidth(75);
sNameColumnSi.setMaxWidth(95);
TableColumn uNameColumnSi = SignalGroupSignalColumnModel.getColumn(SignalGroupSignalHeadModel.UNAME_COLUMN);
uNameColumnSi.setResizable(true);
uNameColumnSi.setMinWidth(100);
uNameColumnSi.setMaxWidth(260);
TableColumn stateOnColumnSi = SignalGroupSignalColumnModel.getColumn(// a 6 column table
SignalGroupSignalHeadModel.STATE_ON_COLUMN);
stateOnColumnSi.setResizable(false);
// was 50
stateOnColumnSi.setMinWidth(Bundle.getMessage("SignalHeadStateFlashingYellow").length());
stateOnColumnSi.setMaxWidth(100);
TableColumn stateOffColumnSi = SignalGroupSignalColumnModel.getColumn(SignalGroupSignalHeadModel.STATE_OFF_COLUMN);
stateOffColumnSi.setResizable(false);
stateOffColumnSi.setMinWidth(50);
stateOffColumnSi.setMaxWidth(100);
TableColumn editColumnSi = SignalGroupSignalColumnModel.getColumn(SignalGroupSignalHeadModel.EDIT_COLUMN);
editColumnSi.setResizable(false);
// was 50
editColumnSi.setMinWidth(Bundle.getMessage("ButtonEdit").length());
editColumnSi.setMaxWidth(100);
JButton editButton = new JButton(Bundle.getMessage("ButtonEdit"));
setColumnToHoldButton(SignalGroupHeadTable, SignalGroupSignalHeadModel.EDIT_COLUMN, editButton);
_SignalGroupHeadScrollPane = new JScrollPane(SignalGroupHeadTable);
p2xsi.add(_SignalGroupHeadScrollPane, BorderLayout.CENTER);
// add tooltip to explain which head types are shown
p2xsi.setToolTipText(Bundle.getMessage("SignalGroupHeadTableTooltip"));
contentPane.add(p2xsi);
p2xsi.setVisible(true);
// add notes panel, may be empty (a dot on the screen)
JPanel pa = new JPanel();
pa.setLayout(new BoxLayout(pa, BoxLayout.Y_AXIS));
Border pBorder = BorderFactory.createEtchedBorder();
pa.setBorder(pBorder);
contentPane.add(pa);
// buttons at bottom of panel
JPanel pb = new JPanel();
pb.setLayout(new FlowLayout(FlowLayout.TRAILING));
pb.add(cancelButton);
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cancelPressed(e);
}
});
cancelButton.setVisible(true);
pb.add(deleteButton);
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deletePressed(e);
}
});
deleteButton.setToolTipText(Bundle.getMessage("DeleteSignalGroupInSystem"));
// [Update] Signal Group button in Add/Edit SignalGroup pane
pb.add(updateButton);
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updatePressed(e, false, true);
}
});
updateButton.setToolTipText(Bundle.getMessage("TooltipUpdate"));
updateButton.setVisible(true);
contentPane.add(pb);
// pack and release space
addFrame.pack();
p2xsiSpace.setVisible(false);
} else // set listener for window closing
{
mainSignalComboBox.setSelectedBean(null);
// reset title for new group
addFrame.setTitle(Bundle.getMessage("AddSignalGroup"));
}
addFrame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
// remind to save, if Signal Group was created or edited
if (SignalGroupDirty) {
InstanceManager.getDefault(jmri.UserPreferencesManager.class).showInfoMessage(Bundle.getMessage("ReminderTitle"), Bundle.getMessage("ReminderSaveString", Bundle.getMessage("SignalGroup")), "beantable.SignalGroupTableAction", // NOI18N
"remindSignalGroup");
SignalGroupDirty = false;
}
// hide first, could be gone by the time of the close event,
if (addFrame != null) {
addFrame.setVisible(false);
}
// so prevent NPE
// release editing soon, as long as NPEs occor in the following methods
inEditMode = false;
finishUpdate();
_SignalGroupHeadModel.dispose();
_AspectModel.dispose();
}
});
// display the pane
addFrame.setVisible(true);
}
use of jmri.SignalHead in project JMRI by JMRI.
the class SignalGroupSubTableAction method headStateFromBox.
/**
* Get the user choice for a Signal Group Signal Head's On and Off Appearance from a comboBox at the top of the Edit Head sub pane.
* @param box the comboBox object containing the user choice
* @return Value for the Appearance (color) set i.e. 0 for DARK
*/
int headStateFromBox(JComboBox<String> box) {
SignalHead sig = jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(curHeadName);
int result;
String mode;
if (sig != null) {
mode = (String) box.getSelectedItem();
result = jmri.util.StringUtil.getStateFromName(mode, sig.getValidStates(), sig.getValidStateNames());
} else {
mode = (String) box.getSelectedItem();
result = jmri.util.StringUtil.getStateFromName(mode, signalStatesValues, signalStates);
}
if (result < 0) {
log.warn("unexpected mode string in signalHeadMode: " + mode);
throw new IllegalArgumentException();
}
return result;
}
use of jmri.SignalHead in project JMRI by JMRI.
the class JsonSignalHeadHttpServiceTest method testDoGet.
@Test
public void testDoGet() throws JmriException {
//create a signalhead for testing
String sysName = "IH1";
String userName = "SH1";
SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
Assert.assertNotNull(s);
JsonNode result;
JsonSignalHeadHttpService service = new JsonSignalHeadHttpService(new ObjectMapper());
try {
//retrieve by systemname
result = service.doGet(JsonSignalHead.SIGNAL_HEAD, sysName, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JsonSignalHead.SIGNAL_HEAD, result.path(JSON.TYPE).asText());
Assert.assertEquals(sysName, result.path(JSON.DATA).path(JSON.NAME).asText());
//retrieve by username, should get systemname back
result = service.doGet(JsonSignalHead.SIGNAL_HEAD, userName, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(sysName, result.path(JSON.DATA).path(JSON.NAME).asText());
//verify initial aspect/state is Dark
Assert.assertEquals(SignalHead.DARK, result.path(JSON.DATA).path(JSON.STATE).asInt());
//change to Green, then verify change
s.setAppearance(SignalHead.GREEN);
result = service.doGet(JsonSignalHead.SIGNAL_HEAD, userName, Locale.ENGLISH);
Assert.assertEquals(SignalHead.GREEN, result.path(JSON.DATA).path(JSON.STATE).asInt());
//set Held, then verify change
s.setHeld(true);
result = service.doGet(JsonSignalHead.SIGNAL_HEAD, userName, Locale.ENGLISH);
Assert.assertEquals(true, result.path(JSON.DATA).path(JSON.TOKEN_HELD).asBoolean());
//set to Not Held, then verify change
s.setHeld(false);
result = service.doGet(JsonSignalHead.SIGNAL_HEAD, userName, Locale.ENGLISH);
Assert.assertEquals(false, result.path(JSON.DATA).path(JSON.TOKEN_HELD).asBoolean());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.SignalHead in project JMRI by JMRI.
the class JsonSignalHeadHttpServiceTest method testDoPost.
@Test
public void testDoPost() throws JmriException {
//create a signalhead for testing
String sysName = "IH1";
String userName = "SH1";
SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
Assert.assertNotNull(s);
JsonNode result = null;
JsonNode message = null;
ObjectMapper mapper = new ObjectMapper();
JsonSignalHeadHttpService service = new JsonSignalHeadHttpService(new ObjectMapper());
Assert.assertNotNull(service);
try {
//set signalhead to Green and verify change
message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.GREEN);
result = service.doPost(JsonSignalHead.SIGNAL_HEAD, userName, message, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(SignalHead.GREEN, s.getState());
Assert.assertEquals(SignalHead.GREEN, result.path(JSON.DATA).path(JSON.STATE).asInt());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
// try to set to FLASHLUNAR, which should not be allowed for this signalHead,
// so check for error, and verify state does not change
JsonException exception = null;
result = null;
message = null;
try {
message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.FLASHLUNAR);
result = service.doPost(JsonSignalHead.SIGNAL_HEAD, userName, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
Assert.assertEquals(SignalHead.GREEN, s.getState());
Assert.assertEquals(false, s.getHeld());
// set signalmast to Held, then verify
try {
message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.HELD);
result = service.doPost(JsonSignalHead.SIGNAL_HEAD, userName, message, Locale.ENGLISH);
Assert.assertEquals(true, s.getHeld());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
Assert.assertEquals(true, s.getHeld());
// set signalmast to something other than Held, then verify Held is released
try {
message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.RED);
result = service.doPost(JsonSignalHead.SIGNAL_HEAD, userName, message, Locale.ENGLISH);
Assert.assertEquals(false, s.getHeld());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.SignalHead in project JMRI by JMRI.
the class JsonSignalHeadSocketServiceTest method testOnMessageChange.
@Test
public void testOnMessageChange() {
JsonNode message;
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
//create a signalhead for testing
String sysName = "IH1";
String userName = "SH1";
SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
Assert.assertNotNull(s);
try {
// SignalHead Yellow
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.YELLOW);
service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
//state should be Yellow
Assert.assertEquals(SignalHead.YELLOW, s.getState());
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
// Try to set SignalHead to FLASHLUNAR, should throw error, remain at Yellow
Exception exception = null;
try {
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.FLASHLUNAR);
service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
} catch (IOException | JmriException | JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
//state should be Yellow
Assert.assertEquals(SignalHead.YELLOW, s.getAppearance());
}
Aggregations