use of org.jdom2.Attribute 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 org.jdom2.Attribute in project JMRI by JMRI.
the class PaneProgPane method newVariable.
/**
* Add the representation of a single variable. The variable is defined by a
* JDOM variable Element from the XML file.
*
* @param var element containing variable
* @param col column to insert label into
* @param g panel layout manager
* @param cs constraints on layout manager
* @param showStdName show the name following the rules for the
* <em>nameFmt</em> element
*/
public void newVariable(Element var, JComponent col, GridBagLayout g, GridBagConstraints cs, boolean showStdName) {
// get the name
String name = var.getAttribute("item").getValue();
// if it doesn't exist, do nothing
int i = _varModel.findVarIndex(name);
if (i < 0) {
log.trace("Variable \"{}\" not found, omitted", name);
return;
}
// Leave here for now. Need to track pre-existing corner-case issue
// log.info("Entry item="+name+";cs.gridx="+cs.gridx+";cs.gridy="+cs.gridy+";cs.anchor="+cs.anchor+";cs.ipadx="+cs.ipadx);
// check label orientation
Attribute attr;
// this default is also set in the DTD
String layout = "left";
if ((attr = var.getAttribute("layout")) != null && attr.getValue() != null) {
layout = attr.getValue();
}
// load label if specified, else use name
String label = name;
if (!showStdName) {
// get name attribute from variable, as that's the mfg name
label = _varModel.getLabel(i);
}
String temp = LocaleSelector.getAttribute(var, "label");
if (temp != null) {
label = temp;
}
// get representation; store into the list to be programmed
JComponent rep = getRepresentation(name, var);
varList.add(i);
// create the paired label
JLabel l = new WatchingLabel(label, rep);
int spaceWidth = getFontMetrics(l.getFont()).stringWidth(" ");
// assemble v from label, rep
switch(layout) {
case "left":
cs.anchor = GridBagConstraints.EAST;
cs.ipadx = spaceWidth;
g.setConstraints(l, cs);
col.add(l);
cs.ipadx = 0;
cs.gridx++;
cs.anchor = GridBagConstraints.WEST;
g.setConstraints(rep, cs);
col.add(rep);
break;
// log.info("Exit item="+name+";cs.gridx="+cs.gridx+";cs.gridy="+cs.gridy+";cs.anchor="+cs.anchor+";cs.ipadx="+cs.ipadx);
case "right":
cs.anchor = GridBagConstraints.EAST;
g.setConstraints(rep, cs);
col.add(rep);
cs.gridx++;
cs.anchor = GridBagConstraints.WEST;
cs.ipadx = spaceWidth;
g.setConstraints(l, cs);
col.add(l);
cs.ipadx = 0;
break;
case "below":
// variable in center of upper line
cs.anchor = GridBagConstraints.CENTER;
g.setConstraints(rep, cs);
col.add(rep);
// label aligned like others
cs.gridy++;
cs.anchor = GridBagConstraints.WEST;
cs.ipadx = spaceWidth;
g.setConstraints(l, cs);
col.add(l);
cs.ipadx = 0;
break;
case "above":
// label aligned like others
cs.anchor = GridBagConstraints.WEST;
cs.ipadx = spaceWidth;
g.setConstraints(l, cs);
col.add(l);
cs.ipadx = 0;
// variable in center of lower line
cs.gridy++;
cs.anchor = GridBagConstraints.CENTER;
g.setConstraints(rep, cs);
col.add(rep);
break;
default:
log.error("layout internally inconsistent: " + layout);
}
}
use of org.jdom2.Attribute in project JMRI by JMRI.
the class VSDecoderPreferences method save.
public void save() {
if (prefFile == null) {
return;
}
XmlFile xf = new XmlFile() {
};
// odd syntax is due to XmlFile being abstract
xf.makeBackupFile(prefFile);
File file = new File(prefFile);
try {
//The file does not exist, create it before writing
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (// make directory, check result
!parentDir.mkdir()) {
log.error("failed to make parent directory");
}
}
if (// create file, check result
!file.createNewFile()) {
log.error("createNewFile failed");
}
} catch (Exception exp) {
log.error("Exception while writing the new VSDecoder preferences file, may not be complete: " + exp);
}
try {
Element root = new Element("vsdecoder-preferences");
//Document doc = XmlFile.newDocument(root, XmlFile.dtdLocation+"vsdecoder-preferences.dtd");
Document doc = XmlFile.newDocument(root);
// add XSLT processing instruction
// <?xml-stylesheet type="text/xsl" href="XSLT/throttle.xsl"?>
/*TODO java.util.Map<String,String> m = new java.util.HashMap<String,String>();
m.put("type", "text/xsl");
m.put("href", jmri.jmrit.XmlFile.xsltLocation+"throttles-preferences.xsl");
ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
doc.addContent(0,p);*/
root.setContent(store());
xf.writeXML(file, doc);
} catch (Exception ex) {
// TODO fix null value for Attribute
log.warn("Exception in storing vsdecoder preferences xml: " + ex);
}
}
use of org.jdom2.Attribute in project JMRI by JMRI.
the class WiThrottlePreferences method load.
@Override
public void load(Element child) {
Attribute a;
if ((a = child.getAttribute("isUseEStop")) != null) {
setUseEStop(a.getValue().equalsIgnoreCase("true"));
this.asLoadedUseEStop = this.isUseEStop();
}
if ((a = child.getAttribute("getEStopDelay")) != null) {
try {
setEStopDelay(Integer.valueOf(a.getValue()));
this.asLoadedEStopDelay = this.getEStopDelay();
} catch (NumberFormatException e) {
log.debug(e.getLocalizedMessage(), e);
}
}
if ((a = child.getAttribute("isUseMomF2")) != null) {
setUseMomF2(a.getValue().equalsIgnoreCase("true"));
this.asLoadedUseMomF2 = this.isUseMomF2();
}
if ((a = child.getAttribute("getPort")) != null) {
try {
setPort(a.getIntValue());
} catch (DataConversionException ex) {
log.error("Port {} is invalid.", a.getValue());
}
this.asLoadedPort = this.getPort();
}
if ((a = child.getAttribute("isAllowTrackPower")) != null) {
setAllowTrackPower(a.getValue().equalsIgnoreCase("true"));
this.asLoadedAllowTrackPower = this.isAllowTrackPower();
}
if ((a = child.getAttribute("isAllowTurnout")) != null) {
setAllowTurnout(a.getValue().equalsIgnoreCase("true"));
this.asLoadedAllowTurnout = this.isAllowTurnout();
}
if ((a = child.getAttribute("isAllowRoute")) != null) {
setAllowRoute(a.getValue().equalsIgnoreCase("true"));
this.asLoadedAllowRoute = this.isAllowRoute();
}
if ((a = child.getAttribute("isAllowConsist")) != null) {
setAllowConsist(a.getValue().equalsIgnoreCase("true"));
this.asLoadedAllowConsist = this.isAllowConsist();
}
if ((a = child.getAttribute("isUseWiFiConsist")) != null) {
setUseWiFiConsist(a.getValue().equalsIgnoreCase("true"));
this.asLoadedUseWiFiConsist = this.isUseWiFiConsist();
}
}
use of org.jdom2.Attribute in project JMRI by JMRI.
the class GuiLafConfigPaneXml method load.
@Override
public boolean load(Element shared, Element perNode) {
boolean result = true;
UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
java.util.Hashtable<String, String> installedLAFs = new java.util.Hashtable<String, String>(plafs.length);
for (int i = 0; i < plafs.length; i++) {
installedLAFs.put(plafs[i].getName(), plafs[i].getClassName());
}
String name = shared.getAttribute("LAFclass").getValue();
String className = installedLAFs.get(name);
log.debug("GUI selection: " + name + " class name: " + className);
// set the GUI
if (className != null) {
InstanceManager.getDefault(GuiLafPreferencesManager.class).setLookAndFeel(name);
try {
if (!className.equals(UIManager.getLookAndFeel().getClass().getName())) {
log.debug("set GUI to " + name + "," + className);
updateLookAndFeel(name, className);
} else {
log.debug("skip updateLAF as already has className==" + className);
}
} catch (Exception ex) {
log.error("Exception while setting GUI look & feel: " + ex);
result = false;
}
}
Attribute langAttr = shared.getAttribute("LocaleLanguage");
Attribute countryAttr = shared.getAttribute("LocaleCountry");
Attribute varAttr = shared.getAttribute("LocaleVariant");
if (countryAttr != null && langAttr != null && varAttr != null) {
Locale locale = new Locale(langAttr.getValue(), countryAttr.getValue(), varAttr.getValue());
Locale.setDefault(locale);
InstanceManager.getDefault(GuiLafPreferencesManager.class).setLocale(locale);
}
Attribute clickAttr = shared.getAttribute("nonStandardMouseEvent");
if (clickAttr != null) {
boolean nonStandardMouseEvent = clickAttr.getValue().equals("yes");
jmri.util.swing.SwingSettings.setNonStandardMouseEvent(nonStandardMouseEvent);
InstanceManager.getDefault(GuiLafPreferencesManager.class).setNonStandardMouseEvent(nonStandardMouseEvent);
}
Attribute graphicAttr = shared.getAttribute("graphicTableState");
if (graphicAttr != null) {
boolean graphicTableState = graphicAttr.getValue().equals("yes");
InstanceManager.getDefault(GuiLafPreferencesManager.class).setGraphicTableState(graphicTableState);
}
GuiLafConfigPane g = new GuiLafConfigPane();
ConfigureManager cm = jmri.InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
if (cm != null) {
cm.registerPref(g);
}
Attribute fontsize = shared.getAttribute("fontsize");
if (fontsize != null) {
int size = Integer.parseInt(fontsize.getValue());
InstanceManager.getDefault(GuiLafPreferencesManager.class).setFontSize(size);
this.setUIFontSize(size);
}
return result;
}
Aggregations