use of jmri.jmrit.symbolicprog.VariableValue in project JMRI by JMRI.
the class PaneProgFrame method processModifierElements.
/**
* If there are any modifier elements, process them.
*/
protected void processModifierElements(Element e, final PaneProgPane pane, VariableTableModel model, final JTabbedPane tabPane, final int index) {
QualifierAdder qa = new QualifierAdder() {
@Override
protected Qualifier createQualifier(VariableValue var, String relation, String value) {
return new PaneQualifier(pane, var, Integer.parseInt(value), relation, tabPane, index);
}
@Override
protected void addListener(java.beans.PropertyChangeListener qc) {
pane.addPropertyChangeListener(qc);
}
};
qa.processModifierElements(e, model);
}
use of jmri.jmrit.symbolicprog.VariableValue in project JMRI by JMRI.
the class PaneProgPane method setToRead.
/**
* Set the "ToRead" parameter in all variables and CVs on this pane.
*
* @param justChanges true if this is read changes, false if read all
* @param startProcess true if this is the start of processing, false if
* cleaning up at end
*/
void setToRead(boolean justChanges, boolean startProcess) {
if (!container.isBusy() || // the frame has already setToRead
(!startProcess)) {
// we want to setToRead false if the pane's process is being stopped
for (int i = 0; i < varList.size(); i++) {
int varNum = varList.get(i);
VariableValue var = _varModel.getVariable(varNum);
if (justChanges) {
if (var.isChanged()) {
var.setToRead(startProcess);
} else {
var.setToRead(false);
}
} else {
var.setToRead(startProcess);
}
}
if (isCvTablePane) {
// make sure list of CVs up to date if table
setCvListFromTable();
}
for (int cvNum : cvList) {
CvValue cv = _cvModel.getCvByRow(cvNum);
if (justChanges) {
if (VariableValue.considerChanged(cv)) {
cv.setToRead(startProcess);
} else {
cv.setToRead(false);
}
} else {
cv.setToRead(startProcess);
}
}
for (int i = 0; i < indexedCvList.size(); i++) {
CvValue icv = _indexedCvModel.getCvByRow(i);
if (justChanges) {
if (VariableValue.considerChanged(icv)) {
icv.setToRead(startProcess);
} else {
icv.setToRead(false);
}
} else {
icv.setToRead(startProcess);
}
}
}
}
use of jmri.jmrit.symbolicprog.VariableValue in project JMRI by JMRI.
the class PaneProgPane method makeLabel.
/**
* Create label from Element.
*
* @param e element containing label contents
* @param c panel to insert label into
* @param g panel layout manager
* @param cs constraints on layout manager
*/
protected void makeLabel(Element e, JPanel c, GridBagLayout g, GridBagConstraints cs) {
String text = LocaleSelector.getAttribute(e, "text");
if (text == null || text.equals("")) {
// label subelement deprecated 3.7.5
text = LocaleSelector.getAttribute(e, "label");
}
final JLabel l = new JLabel(text);
l.setAlignmentX(1.0f);
cs.fill = GridBagConstraints.BOTH;
log.trace("Add label: {} cs: {} fill: {} x: {} y: {}", l.getText(), cs.gridwidth, cs.fill, cs.gridx, cs.gridy);
g.setConstraints(l, cs);
c.add(l);
cs.fill = GridBagConstraints.NONE;
cs.gridwidth = 1;
cs.gridheight = 1;
// handle qualification if any
QualifierAdder qa = new QualifierAdder() {
@Override
protected Qualifier createQualifier(VariableValue var, String relation, String value) {
return new JComponentQualifier(l, var, Integer.parseInt(value), relation);
}
@Override
protected void addListener(java.beans.PropertyChangeListener qc) {
l.addPropertyChangeListener(qc);
}
};
qa.processModifierElements(e, _varModel);
}
use of jmri.jmrit.symbolicprog.VariableValue in project JMRI by JMRI.
the class PaneProgPane method newGridItem.
/**
* Create a grid item from the JDOM Element
*
* @param element element containing grid item contents
* @param showStdName show the name following the rules for the
* <em>nameFmt</em> element
* @param modelElem element containing the decoder model
* @param globs properties to configure the layout
* @return a panel containing the group
*/
public JPanel newGridItem(Element element, boolean showStdName, Element modelElem, GridGlobals globs) {
// get item-level attributes
List<Attribute> itemAttList = element.getAttributes();
List<Attribute> attList = new ArrayList<>(globs.gridAttList);
// merge grid and item-level attributes
attList.addAll(itemAttList);
// log.info("New gridtiem -----------------------------------------------");
// log.info("Attribute list:"+attList);
attList.add(new Attribute(LAST_GRIDX, ""));
attList.add(new Attribute(LAST_GRIDY, ""));
// log.info("Previous gridxCurrent="+globs.gridxCurrent+";gridyCurrent="+globs.gridyCurrent);
for (int j = 0; j < attList.size(); j++) {
Attribute attrib = attList.get(j);
String attribName = attrib.getName();
String attribRawValue = attrib.getValue();
Field constraint = null;
String constraintType = null;
// make sure we only process the last gridx or gridy attribute in the list
if (attribName.equals("gridx")) {
Attribute a = new Attribute(LAST_GRIDX, attribRawValue);
attList.set(attList.size() - 2, a);
//. don't process now
continue;
}
if (attribName.equals("gridy")) {
Attribute a = new Attribute(LAST_GRIDY, attribRawValue);
attList.set(attList.size() - 1, a);
//. don't process now
continue;
}
if (attribName.equals(LAST_GRIDX)) {
// we must be at end of original list, restore last gridx
attribName = "gridx";
if (attribRawValue.equals("")) {
// don't process blank (unused)
continue;
}
}
if (attribName.equals(LAST_GRIDY)) {
// we must be at end of original list, restore last gridy
attribName = "gridy";
if (attribRawValue.equals("")) {
// don't process blank (unused)
continue;
}
}
if ((attribName.equals("gridx") || attribName.equals("gridy")) && attribRawValue.equals("RELATIVE")) {
// NEXT is a synonym for RELATIVE
attribRawValue = "NEXT";
}
if (attribName.equals("gridx") && attribRawValue.equals("CURRENT")) {
attribRawValue = String.valueOf(Math.max(0, globs.gridxCurrent));
}
if (attribName.equals("gridy") && attribRawValue.equals("CURRENT")) {
attribRawValue = String.valueOf(Math.max(0, globs.gridyCurrent));
}
if (attribName.equals("gridx") && attribRawValue.equals("NEXT")) {
attribRawValue = String.valueOf(++globs.gridxCurrent);
}
if (attribName.equals("gridy") && attribRawValue.equals("NEXT")) {
attribRawValue = String.valueOf(++globs.gridyCurrent);
}
// log.info("attribName="+attribName+";attribRawValue="+attribRawValue);
try {
constraint = globs.gridConstraints.getClass().getDeclaredField(attribName);
constraintType = constraint.getType().toString();
constraint.setAccessible(true);
} catch (NoSuchFieldException ex) {
log.error("Unrecognised attribute \"" + attribName + "\", skipping");
continue;
}
switch(constraintType) {
case "int":
{
int attribValue;
try {
attribValue = Integer.valueOf(attribRawValue);
constraint.set(globs.gridConstraints, attribValue);
} catch (IllegalAccessException ey) {
log.error("Unable to set constraint \"" + attribName + ". IllegalAccessException error thrown.");
} catch (NumberFormatException ex) {
try {
Field constant = globs.gridConstraints.getClass().getDeclaredField(attribRawValue);
constant.setAccessible(true);
attribValue = (Integer) GridBagConstraints.class.getField(attribRawValue).get(constant);
constraint.set(globs.gridConstraints, attribValue);
} catch (NoSuchFieldException ey) {
log.error("Invalid value \"" + attribRawValue + "\" for attribute \"" + attribName + "\"");
} catch (IllegalAccessException ey) {
log.error("Unable to set constraint \"" + attribName + ". IllegalAccessException error thrown.");
}
}
break;
}
case "double":
{
double attribValue;
try {
attribValue = Double.valueOf(attribRawValue);
constraint.set(globs.gridConstraints, attribValue);
} catch (IllegalAccessException ey) {
log.error("Unable to set constraint \"" + attribName + ". IllegalAccessException error thrown.");
} catch (NumberFormatException ex) {
log.error("Invalid value \"" + attribRawValue + "\" for attribute \"" + attribName + "\"");
}
break;
}
case "class java.awt.Insets":
try {
String[] insetStrings = attribRawValue.split(",");
if (insetStrings.length == 4) {
Insets attribValue = new Insets(Integer.valueOf(insetStrings[0]), Integer.valueOf(insetStrings[1]), Integer.valueOf(insetStrings[2]), Integer.valueOf(insetStrings[3]));
constraint.set(globs.gridConstraints, attribValue);
} else {
log.error("Invalid value \"" + attribRawValue + "\" for attribute \"" + attribName + "\"");
log.error("Value should be four integers of the form \"top,left,bottom,right\"");
}
} catch (IllegalAccessException ey) {
log.error("Unable to set constraint \"" + attribName + ". IllegalAccessException error thrown.");
} catch (NumberFormatException ex) {
log.error("Invalid value \"" + attribRawValue + "\" for attribute \"" + attribName + "\"");
log.error("Value should be four integers of the form \"top,left,bottom,right\"");
}
break;
default:
log.error("Required \"" + constraintType + "\" handler for attribute \"" + attribName + "\" not defined in JMRI code");
log.error("Please file a JMRI bug report at https://sourceforge.net/p/jmri/bugs/new/");
break;
}
}
// log.info("Updated globs.GridBagConstraints.gridx="+globs.gridConstraints.gridx+";globs.GridBagConstraints.gridy="+globs.gridConstraints.gridy);
// create a panel to add as a new grid item
final JPanel c = new JPanel();
panelList.add(c);
GridBagLayout g = new GridBagLayout();
GridBagConstraints cs = new GridBagConstraints();
c.setLayout(g);
// handle the xml definition
// for all elements in the grid item
List<Element> elemList = element.getChildren();
log.trace("newGridItem starting with {} elements", elemList.size());
for (int i = 0; i < elemList.size(); i++) {
// update the grid position
cs.gridy = 0;
cs.gridx++;
Element e = elemList.get(i);
String name = e.getName();
log.trace("newGridItem processing {} element", name);
// decode the type
if (name.equals("display")) {
// its a variable
// load the variable
newVariable(e, c, g, cs, showStdName);
} else if (name.equals("separator")) {
// its a separator
JSeparator j = new JSeparator(javax.swing.SwingConstants.VERTICAL);
cs.fill = GridBagConstraints.BOTH;
cs.gridheight = GridBagConstraints.REMAINDER;
g.setConstraints(j, cs);
c.add(j);
cs.fill = GridBagConstraints.NONE;
cs.gridheight = 1;
} else if (name.equals("label")) {
cs.gridheight = GridBagConstraints.REMAINDER;
makeLabel(e, c, g, cs);
} else if (name.equals("soundlabel")) {
cs.gridheight = GridBagConstraints.REMAINDER;
makeSoundLabel(e, c, g, cs);
} else if (name.equals("cvtable")) {
makeCvTable(cs, g, c);
} else if (name.equals("indxcvtable")) {
log.debug("starting to build IndexedCvTable pane");
JTable indxcvTable = new JTable(_indexedCvModel);
JScrollPane cvScroll = new JScrollPane(indxcvTable);
indxcvTable.setDefaultRenderer(JTextField.class, new ValueRenderer());
indxcvTable.setDefaultRenderer(JButton.class, new ValueRenderer());
indxcvTable.setDefaultEditor(JTextField.class, new ValueEditor());
indxcvTable.setDefaultEditor(JButton.class, new ValueEditor());
indxcvTable.setRowHeight(new JButton("X").getPreferredSize().height);
indxcvTable.setPreferredScrollableViewportSize(new Dimension(700, indxcvTable.getRowHeight() * 14));
cvScroll.setColumnHeaderView(indxcvTable.getTableHeader());
// don't want a horizontal scroll bar
// Need to see the whole row at one time
// indxcvTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
cs.gridwidth = GridBagConstraints.REMAINDER;
g.setConstraints(cvScroll, cs);
c.add(cvScroll);
cs.gridwidth = 1;
// remember which indexed CVs to read/write
for (int j = 0; j < _indexedCvModel.getRowCount(); j++) {
String sz = "CV" + _indexedCvModel.getName(j);
int in = _varModel.findVarIndex(sz);
indexedCvList.add(in);
}
_cvTable = true;
log.debug("end of building IndexedCvTable pane");
} else if (name.equals("fnmapping")) {
pickFnMapPanel(c, g, cs, modelElem);
} else if (name.equals("dccaddress")) {
JPanel l = addDccAddressPanel(e);
if (l.getComponentCount() > 0) {
cs.gridheight = GridBagConstraints.REMAINDER;
g.setConstraints(l, cs);
c.add(l);
cs.gridheight = 1;
}
} else if (name.equals("column")) {
// nested "column" elements ...
cs.gridheight = GridBagConstraints.REMAINDER;
JPanel l = newColumn(e, showStdName, modelElem);
if (l.getComponentCount() > 0) {
panelList.add(l);
g.setConstraints(l, cs);
c.add(l);
cs.gridheight = 1;
}
} else if (name.equals("row")) {
// nested "row" elements ...
cs.gridwidth = GridBagConstraints.REMAINDER;
JPanel l = newRow(e, showStdName, modelElem);
if (l.getComponentCount() > 0) {
panelList.add(l);
g.setConstraints(l, cs);
c.add(l);
cs.gridwidth = 1;
}
} else if (name.equals("grid")) {
// nested "grid" elements ...
cs.gridwidth = GridBagConstraints.REMAINDER;
JPanel l = newGrid(e, showStdName, modelElem);
if (l.getComponentCount() > 0) {
panelList.add(l);
g.setConstraints(l, cs);
c.add(l);
cs.gridwidth = 1;
}
} else if (name.equals("group")) {
// nested "group" elements ...
JPanel l = newGroup(e, showStdName, modelElem);
if (l.getComponentCount() > 0) {
panelList.add(l);
g.setConstraints(l, cs);
c.add(l);
}
} else if (!name.equals("qualifier")) {
// its a mistake
log.error("No code to handle element of type " + e.getName() + " in newGridItem");
}
}
globs.gridxCurrent = globs.gridConstraints.gridx;
globs.gridyCurrent = globs.gridConstraints.gridy;
// add glue to the bottom to allow resize
if (c.getComponentCount() > 0) {
c.add(Box.createVerticalGlue());
}
// handle qualification if any
QualifierAdder qa = new QualifierAdder() {
@Override
protected Qualifier createQualifier(VariableValue var, String relation, String value) {
return new JComponentQualifier(c, var, Integer.parseInt(value), relation);
}
@Override
protected void addListener(java.beans.PropertyChangeListener qc) {
c.addPropertyChangeListener(qc);
}
};
qa.processModifierElements(element, _varModel);
return c;
}
use of jmri.jmrit.symbolicprog.VariableValue in project JMRI by JMRI.
the class PaneProgPane method nextConfirm.
/**
* If there are any more compare operations to be done on this pane, do the
* next one.
* <P>
* Each invocation of this method compare one CV; completion of that request
* will cause it to happen again, reading the next one, until there's
* nothing left to read.
* <P>
* @return true is a compare has been started, false if the pane is
* complete.
*/
boolean nextConfirm() {
// look for possible CVs
while (cvListIterator != null && cvListIterator.hasNext()) {
int cvNum = cvListIterator.next();
CvValue cv = _cvModel.getCvByRow(cvNum);
if (log.isDebugEnabled()) {
log.debug("nextConfirm cv index " + cvNum + " state " + cv.getState());
}
if (cv.isToRead()) {
if (log.isDebugEnabled()) {
log.debug("start confirm of cv " + cvNum);
}
setBusy(true);
if (_programmingCV != null) {
log.error("listener already set at confirm start");
}
_programmingCV = _cvModel.getCvByRow(cvNum);
_read = true;
// get notified when that state changes so can repeat
_programmingCV.addPropertyChangeListener(this);
// and make the compare request
_programmingCV.confirm(_cvModel.getStatusLabel());
if (log.isDebugEnabled()) {
log.debug("return from starting CV confirm");
}
// only make one request at a time!
return true;
}
}
// found no CVs needing read, try indexed CVs
while ((indexedCvList.size() > 0) && (indexedCvListIndex < indexedCvList.size())) {
int indxVarNum = indexedCvList.get(indexedCvListIndex);
int indxState = _varModel.getState(indxVarNum);
if (log.isDebugEnabled()) {
log.debug("nextRead indexed cv @ row index " + indexedCvListIndex + " state " + indxState);
}
VariableValue iCv = _varModel.getVariable(indxVarNum);
indexedCvListIndex++;
if (iCv.isToRead()) {
String sz = "start confirm of indexed cv " + (_indexedCvModel.getCvByRow(indexedCvListIndex - 1)).cvName();
if (log.isDebugEnabled()) {
log.debug(sz);
}
setBusy(true);
if (_programmingIndexedCV != null) {
log.error("listener already set at confirm start");
}
_programmingIndexedCV = _varModel.getVariable(indxVarNum);
_read = true;
// get notified when that state changes so can repeat
_programmingIndexedCV.addPropertyChangeListener(this);
// and make the compare request
_programmingIndexedCV.confirmAll();
if (log.isDebugEnabled()) {
log.debug("return from starting indexed CV confirm");
}
// only make one request at a time!
return true;
}
}
// nothing to program, end politely
if (log.isDebugEnabled()) {
log.debug("nextConfirm found nothing to do");
}
confirmChangesButton.setSelected(false);
// reset both, as that's final state we want
confirmAllButton.setSelected(false);
setBusy(false);
container.paneFinished();
return false;
}
Aggregations