use of jmri.LightManager in project JMRI by JMRI.
the class LightTableAction method createModel.
/**
* Create the JTable DataModel, along with the changes for the specific case
* of Lights.
*/
@Override
protected void createModel() {
// load graphic state column display preference
_graphicState = InstanceManager.getDefault(GuiLafPreferencesManager.class).isGraphicTableState();
m = new BeanTableDataModel() {
public static final int ENABLECOL = NUMCOLUMN;
public static final int INTENSITYCOL = ENABLECOL + 1;
public static final int EDITCOL = INTENSITYCOL + 1;
protected String enabledString = Bundle.getMessage("ColumnHeadEnabled");
protected String intensityString = Bundle.getMessage("ColumnHeadIntensity");
@Override
public int getColumnCount() {
return NUMCOLUMN + 3;
}
@Override
public String getColumnName(int col) {
if (col == EDITCOL) {
// no heading on "Edit"
return "";
}
if (col == INTENSITYCOL) {
return intensityString;
}
if (col == ENABLECOL) {
return enabledString;
} else {
return super.getColumnName(col);
}
}
@Override
public Class<?> getColumnClass(int col) {
if (col == EDITCOL) {
return JButton.class;
}
if (col == INTENSITYCOL) {
return Double.class;
}
if (col == ENABLECOL) {
return Boolean.class;
} else if (col == VALUECOL && _graphicState) {
// use an image to show light state
return JLabel.class;
} else {
return super.getColumnClass(col);
}
}
@Override
public int getPreferredWidth(int col) {
// override default value for UserName column
if (col == USERNAMECOL) {
return new JTextField(16).getPreferredSize().width;
}
if (col == EDITCOL) {
return new JTextField(6).getPreferredSize().width;
}
if (col == INTENSITYCOL) {
return new JTextField(6).getPreferredSize().width;
}
if (col == ENABLECOL) {
return new JTextField(6).getPreferredSize().width;
} else {
return super.getPreferredWidth(col);
}
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == EDITCOL) {
return true;
}
if (col == INTENSITYCOL) {
return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).isIntensityVariable();
}
if (col == ENABLECOL) {
return true;
} else {
return super.isCellEditable(row, col);
}
}
@Override
public String getValue(String name) {
Light l = lightManager.getBySystemName(name);
if (l == null) {
return ("Failed to find " + name);
}
int val = l.getState();
switch(val) {
case Light.ON:
return Bundle.getMessage("LightStateOn");
case Light.INTERMEDIATE:
return Bundle.getMessage("LightStateIntermediate");
case Light.OFF:
return Bundle.getMessage("LightStateOff");
case Light.TRANSITIONINGTOFULLON:
return Bundle.getMessage("LightStateTransitioningToFullOn");
case Light.TRANSITIONINGHIGHER:
return Bundle.getMessage("LightStateTransitioningHigher");
case Light.TRANSITIONINGLOWER:
return Bundle.getMessage("LightStateTransitioningLower");
case Light.TRANSITIONINGTOFULLOFF:
return Bundle.getMessage("LightStateTransitioningToFullOff");
default:
return "Unexpected value: " + val;
}
}
@Override
public Object getValueAt(int row, int col) {
switch(col) {
case EDITCOL:
return Bundle.getMessage("ButtonEdit");
case INTENSITYCOL:
return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).getTargetIntensity();
case ENABLECOL:
return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).getEnabled();
default:
return super.getValueAt(row, col);
}
}
@Override
public void setValueAt(Object value, int row, int col) {
switch(col) {
case EDITCOL:
// Use separate Runnable so window is created on top
class WindowMaker implements Runnable {
int row;
WindowMaker(int r) {
row = r;
}
@Override
public void run() {
// set up to edit
addPressed(null);
fixedSystemName.setText((String) getValueAt(row, SYSNAMECOL));
// don't really want to stop Light w/o user action
editPressed();
}
}
WindowMaker t = new WindowMaker(row);
javax.swing.SwingUtilities.invokeLater(t);
break;
case INTENSITYCOL:
// alternate
try {
Light l = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
double intensity = ((Double) value);
if (intensity < 0) {
intensity = 0;
}
if (intensity > 1.0) {
intensity = 1.0;
}
l.setTargetIntensity(intensity);
} catch (IllegalArgumentException e1) {
status1.setText(Bundle.getMessage("LightError16"));
}
break;
case ENABLECOL:
// alternate
Light l = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
boolean v = l.getEnabled();
l.setEnabled(!v);
break;
case VALUECOL:
if (_graphicState) {
// respond to clicking on ImageIconRenderer CellEditor
Light ll = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
clickOn(ll);
fireTableRowsUpdated(row, row);
break;
}
//$FALL-THROUGH$
default:
super.setValueAt(value, row, col);
break;
}
}
/**
* Delete the bean after all the checking has been done.
* <P>
* Deactivate the light, then use the superclass to delete it.
*/
@Override
void doDelete(NamedBean bean) {
((Light) bean).deactivateLight();
super.doDelete(bean);
}
// all properties update for now
@Override
protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
return true;
}
@Override
public Manager getManager() {
return lightManager;
}
@Override
public NamedBean getBySystemName(String name) {
return lightManager.getBySystemName(name);
}
@Override
public NamedBean getByUserName(String name) {
return lightManager.getByUserName(name);
}
@Override
protected String getMasterClassName() {
return getClassName();
}
@Override
public void clickOn(NamedBean t) {
int oldState = ((Light) t).getState();
int newState;
switch(oldState) {
case Light.ON:
newState = Light.OFF;
break;
case Light.OFF:
newState = Light.ON;
break;
default:
newState = Light.OFF;
log.warn("Unexpected Light state " + oldState + " becomes OFF");
break;
}
((Light) t).setState(newState);
}
@Override
public JButton configureButton() {
return new JButton(" " + Bundle.getMessage("LightStateOff") + " ");
}
@Override
protected String getBeanType() {
return Bundle.getMessage("BeanNameLight");
}
/**
* Customize the light table Value (State) column to show an appropriate graphic for the light state
* if _graphicState = true, or (default) just show the localized state text
* when the TableDataModel is being called from ListedTableAction.
*
* @param table a JTable of Lights
*/
@Override
protected void configValueColumn(JTable table) {
// have the value column hold a JPanel (icon)
//setColumnToHoldButton(table, VALUECOL, new JLabel("123456")); // for small round icon, but cannot be converted to JButton
// add extras, override BeanTableDataModel
log.debug("Light configValueColumn (I am {})", super.toString());
if (_graphicState) {
// load icons, only once
// editor
table.setDefaultEditor(JLabel.class, new ImageIconRenderer());
// item class copied from SwitchboardEditor panel
table.setDefaultRenderer(JLabel.class, new ImageIconRenderer());
} else {
// classic text style state indication
super.configValueColumn(table);
}
}
/**
* Visualize state in table as a graphic, customized for Lights (2 states + ... for transitioning).
* Renderer and Editor are identical, as the cell contents are not actually edited,
* only used to toggle state using {@link #clickOn(NamedBean)}.
* @see jmri.jmrit.beantable.sensor.SensorTableDataModel.ImageIconRenderer
* @see jmri.jmrit.beantable.BlockTableAction#createModel()
* @see jmri.jmrit.beantable.TurnoutTableAction#createModel()
*/
class ImageIconRenderer extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
protected JLabel label;
// also used in display.switchboardEditor
protected String rootPath = "resources/icons/misc/switchboard/";
// for Light
protected char beanTypeChar = 'L';
protected String onIconPath = rootPath + beanTypeChar + "-on-s.png";
protected String offIconPath = rootPath + beanTypeChar + "-off-s.png";
protected BufferedImage onImage;
protected BufferedImage offImage;
protected ImageIcon onIcon;
protected ImageIcon offIcon;
protected int iconHeight = -1;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
log.debug("Renderer Item = {}, State = {}", row, value);
if (iconHeight < 0) {
// load resources only first time, either for renderer or editor
loadIcons();
log.debug("icons loaded");
}
return updateLabel((String) value, row);
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
log.debug("Renderer Item = {}, State = {}", row, value);
if (iconHeight < 0) {
// load resources only first time, either for renderer or editor
loadIcons();
log.debug("icons loaded");
}
return updateLabel((String) value, row);
}
public JLabel updateLabel(String value, int row) {
if (iconHeight > 0) {
// if necessary, increase row height;
//table.setRowHeight(row, Math.max(table.getRowHeight(), iconHeight - 5)); // TODO adjust table row height for Lights
}
if (value.equals(Bundle.getMessage("LightStateOff")) && offIcon != null) {
label = new JLabel(offIcon);
label.setVerticalAlignment(JLabel.BOTTOM);
log.debug("offIcon set");
} else if (value.equals(Bundle.getMessage("LightStateOn")) && onIcon != null) {
label = new JLabel(onIcon);
label.setVerticalAlignment(JLabel.BOTTOM);
log.debug("onIcon set");
} else if (value.equals(Bundle.getMessage("BeanStateInconsistent"))) {
// centered text alignment
label = new JLabel("X", JLabel.CENTER);
label.setForeground(Color.red);
log.debug("Light state inconsistent");
iconHeight = 0;
} else if (value.equals(Bundle.getMessage("LightStateIntermediate"))) {
// centered text alignment
label = new JLabel("...", JLabel.CENTER);
log.debug("Light state in transition");
iconHeight = 0;
} else {
// failed to load icon
// centered text alignment
label = new JLabel(value, JLabel.CENTER);
log.warn("Error reading icons for LightTable");
iconHeight = 0;
}
label.setToolTipText(value);
label.addMouseListener(new MouseAdapter() {
@Override
public final void mousePressed(MouseEvent evt) {
log.debug("Clicked on icon in row {}", row);
stopCellEditing();
}
});
return label;
}
@Override
public Object getCellEditorValue() {
log.debug("getCellEditorValue, me = {})", this.toString());
return this.toString();
}
/**
* Read and buffer graphics. Only called once for this table.
* @see #getTableCellEditorComponent(JTable, Object, boolean, int, int)
*/
protected void loadIcons() {
try {
onImage = ImageIO.read(new File(onIconPath));
offImage = ImageIO.read(new File(offIconPath));
} catch (IOException ex) {
log.error("error reading image from {} or {}", onIconPath, offIconPath, ex);
}
log.debug("Success reading images");
int imageWidth = onImage.getWidth();
int imageHeight = onImage.getHeight();
// scale icons 50% to fit in table rows
Image smallOnImage = onImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_DEFAULT);
Image smallOffImage = offImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_DEFAULT);
onIcon = new ImageIcon(smallOnImage);
offIcon = new ImageIcon(smallOffImage);
iconHeight = onIcon.getIconHeight();
}
}
};
// end of custom data model
}
use of jmri.LightManager in project JMRI by JMRI.
the class InternalLightManagerTest method testIsVariableLight.
@Test
public void testIsVariableLight() {
// create and register the manager object
InternalLightManager alm = new InternalLightManager();
jmri.InstanceManager.setLightManager(alm);
// ask for a Light, and check type
LightManager lm = jmri.InstanceManager.lightManagerInstance();
Assert.assertTrue(lm.newLight("IL21", "my name").isIntensityVariable());
}
use of jmri.LightManager in project JMRI by JMRI.
the class InternalLightManagerTest method testIsVariableLight.
@Test
public void testIsVariableLight() {
// create and register the manager object
InternalLightManager alm = new InternalLightManager();
jmri.InstanceManager.setLightManager(alm);
// ask for a Light, and check type
LightManager lm = jmri.InstanceManager.lightManagerInstance();
Assert.assertTrue(lm.newLight("IL21", "my name").isIntensityVariable());
}
use of jmri.LightManager in project JMRI by JMRI.
the class AbstractLightManagerConfigXML method store.
/**
* Default implementation for storing the contents of a LightManager
*
* @param o Object to store, of type LightManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element lights = new Element("lights");
setStoreElementClass(lights);
LightManager tm = (LightManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not lights to include
if (!iter.hasNext()) {
return null;
}
// store the lights
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
break;
}
log.debug("system name is " + sname);
Light lgt = tm.getBySystemName(sname);
Element elem = new Element("light");
elem.addContent(new Element("systemName").addContent(sname));
// store common parts
storeCommon(lgt, elem);
// write variable intensity attributes
elem.setAttribute("minIntensity", "" + lgt.getMinIntensity());
elem.setAttribute("maxIntensity", "" + lgt.getMaxIntensity());
// write transition attribute
elem.setAttribute("transitionTime", "" + lgt.getTransitionTime());
// save child lightcontrol entries
ArrayList<LightControl> lcList = lgt.getLightControlList();
Element lcElem = null;
for (int i = 0; i < lcList.size(); i++) {
LightControl lc = lcList.get(i);
if (lc != null) {
lcElem = new Element("lightcontrol");
int type = lc.getControlType();
lcElem.setAttribute("controlType", "" + type);
if (type == Light.SENSOR_CONTROL) {
lcElem.setAttribute("controlSensor", lc.getControlSensorName());
lcElem.setAttribute("sensorSense", "" + lc.getControlSensorSense());
} else if (type == Light.FAST_CLOCK_CONTROL) {
lcElem.setAttribute("fastClockOnHour", "" + lc.getFastClockOnHour());
lcElem.setAttribute("fastClockOnMin", "" + lc.getFastClockOnMin());
lcElem.setAttribute("fastClockOffHour", "" + lc.getFastClockOffHour());
lcElem.setAttribute("fastClockOffMin", "" + lc.getFastClockOffMin());
} else if (type == Light.TURNOUT_STATUS_CONTROL) {
lcElem.setAttribute("controlTurnout", lc.getControlTurnoutName());
lcElem.setAttribute("turnoutState", "" + lc.getControlTurnoutState());
} else if (type == Light.TIMED_ON_CONTROL) {
lcElem.setAttribute("timedControlSensor", lc.getControlTimedOnSensorName());
lcElem.setAttribute("duration", "" + lc.getTimedOnDuration());
}
if (type == Light.TWO_SENSOR_CONTROL) {
lcElem.setAttribute("controlSensor", lc.getControlSensorName());
lcElem.setAttribute("controlSensor2", lc.getControlSensor2Name());
lcElem.setAttribute("sensorSense", "" + lc.getControlSensorSense());
}
elem.addContent(lcElem);
}
}
lights.addContent(elem);
}
}
return lights;
}
use of jmri.LightManager in project JMRI by JMRI.
the class DCCppLightManagerTest method testAsAbstractFactory.
@Test
public void testAsAbstractFactory() {
// create and register the manager object
DCCppLightManager xlm = new DCCppLightManager(xnis, "DCCPP");
jmri.InstanceManager.setLightManager(xlm);
// ask for a Light, and check type
LightManager lm = jmri.InstanceManager.lightManagerInstance();
Light tl = lm.newLight("DCCPPL21", "my name");
if (log.isDebugEnabled()) {
log.debug("received light value " + tl);
}
Assert.assertTrue(null != (DCCppLight) tl);
// make sure loaded into tables
if (log.isDebugEnabled()) {
log.debug("by system name: " + lm.getBySystemName("DCCPPL21"));
}
if (log.isDebugEnabled()) {
log.debug("by user name: " + lm.getByUserName("my name"));
}
Assert.assertTrue(null != lm.getBySystemName("DCCPPL21"));
Assert.assertTrue(null != lm.getByUserName("my name"));
}
Aggregations