use of jmri.jmrit.display.switchboardEditor.SwitchboardEditor in project JMRI by JMRI.
the class SwitchboardEditorXml method load.
/**
* Create a SwitchboardEditor object, then register and fill it, then pop it in a
* JFrame
*
* @param shared Top level Element to unpack.
* @return true if successful
*/
@Override
public boolean load(Element shared, Element perNode) {
boolean result = true;
// find coordinates
int x = 0;
int y = 0;
int height = 400;
int width = 300;
int rangemin = 1;
int rangemax = 32;
int columns = 4;
String type = "T";
String connection = "I";
String shape = "key";
String name;
try {
x = shared.getAttribute("x").getIntValue();
y = shared.getAttribute("y").getIntValue();
height = shared.getAttribute("height").getIntValue();
width = shared.getAttribute("width").getIntValue();
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert Switchboard's attribute");
result = false;
}
// find the name
// this will be replaced by the name as stored NOI18N
name = "Switchboard";
if (shared.getAttribute("name") != null) {
name = shared.getAttribute("name").getValue();
}
// confirm that panel hasn't already been loaded
if (jmri.jmrit.display.PanelMenu.instance().isPanelNameUsed(name)) {
log.warn("File contains a panel with the same name (" + name + ") as an existing panel");
result = false;
}
SwitchboardEditor panel = new SwitchboardEditor(name);
//panel.makeFrame(name);
jmri.jmrit.display.PanelMenu.instance().addEditorPanel(panel);
panel.getTargetFrame().setLocation(x, y);
panel.getTargetFrame().setSize(width, height);
panel.setTitle();
// Load editor option flags. This has to be done before the content
// items are loaded, to preserve the individual item settings.
Attribute a;
boolean value = true;
if ((a = shared.getAttribute("editable")) != null && a.getValue().equals("no")) {
value = false;
}
panel.setAllEditable(value);
value = true;
if ((a = shared.getAttribute("showtooltips")) != null && a.getValue().equals("no")) {
value = false;
}
panel.setAllShowTooltip(value);
value = true;
if ((a = shared.getAttribute("controlling")) != null && a.getValue().equals("no")) {
value = false;
}
panel.setAllControlling(value);
value = false;
if ((a = shared.getAttribute("hide")) != null && a.getValue().equals("yes")) {
value = true;
}
panel.setShowHidden(value);
value = true;
if ((a = shared.getAttribute("panelmenu")) != null && a.getValue().equals("no")) {
value = false;
}
panel.setPanelMenuVisible(value);
String state = "both";
if ((a = shared.getAttribute("scrollable")) != null) {
state = a.getValue();
}
panel.setScroll(state);
value = false;
if ((a = shared.getAttribute("hideunconnected")) != null && a.getValue().equals("yes")) {
value = true;
}
panel.setHideUnconnected(value);
try {
rangemin = shared.getAttribute("rangemin").getIntValue();
rangemax = shared.getAttribute("rangemax").getIntValue();
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert Switchboard's range");
result = false;
}
panel.setPanelMenuRangeMin(rangemin);
panel.setPanelMenuRangeMax(rangemax);
type = shared.getAttribute("type").getValue();
panel.setSwitchType(type);
connection = shared.getAttribute("connection").getValue();
panel.setSwitchManu(connection);
shape = shared.getAttribute("shape").getValue();
panel.setSwitchShape(shape);
try {
columns = shared.getAttribute("columns").getIntValue();
} catch (org.jdom2.DataConversionException e) {
log.error("failed to convert Switchboard's column count");
result = false;
}
panel.setColumns(columns);
String defaultTextColor = "black";
if (shared.getAttribute("defaulttextcolor") != null) {
defaultTextColor = shared.getAttribute("defaulttextcolor").getValue();
}
panel.setDefaultTextColor(defaultTextColor);
// set color if needed
try {
int red = shared.getAttribute("redBackground").getIntValue();
int blue = shared.getAttribute("blueBackground").getIntValue();
int green = shared.getAttribute("greenBackground").getIntValue();
//panel.setBackground(new Color(red, green, blue));
panel.setDefaultBackgroundColor(new Color(red, green, blue));
} catch (org.jdom2.DataConversionException e) {
log.warn("Could not parse color attributes!");
} catch (NullPointerException e) {
// considered normal if the attributes are not present
}
//set the (global) editor display widgets to their flag settings
panel.initView();
// load the contents with their individual option settings
List<Element> items = shared.getChildren();
for (int i = 0; i < items.size(); i++) {
// get the class, hence the adapter object to do loading
Element item = items.get(i);
String adapterName = item.getAttribute("class").getValue();
log.debug("load via " + adapterName);
try {
XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
// and do it
adapter.load(item, panel);
if (!panel.loadOK()) {
result = false;
}
} catch (Exception e) {
log.error("Exception while loading " + item.getName() + ":" + e);
result = false;
e.printStackTrace();
}
}
// dispose of url correction data
panel.disposeLoadData();
// display the results, with the editor in back
panel.pack();
panel.setAllEditable(panel.isEditable());
// we don't pack the target frame here, because size was specified
// TODO: Work out why, when calling this method, panel size is increased
// vertically (at least on MS Windows)
// always show the panel
panel.getTargetFrame().setVisible(true);
// register the resulting panel for later configuration
ConfigureManager cm = InstanceManager.getNullableDefault(jmri.ConfigureManager.class);
if (cm != null) {
cm.registerUser(panel);
}
// reset the size and position, in case the display caused it to change
panel.getTargetFrame().setLocation(x, y);
panel.getTargetFrame().setSize(width, height);
panel.updatePressed();
return result;
}
use of jmri.jmrit.display.switchboardEditor.SwitchboardEditor in project JMRI by JMRI.
the class SwitchboardEditorXml method store.
/**
* Default implementation for storing the contents of a SwitchboardEditor.
* Storing of beanswitch properties for use on web panel {@link SwitchboardEditor$BeanSwitchXml}
*
* @param o Object to store, of type SwitchboardEditor
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
SwitchboardEditor p = (SwitchboardEditor) o;
Element panel = new Element("switchboardeditor");
JFrame frame = p.getTargetFrame();
Dimension size = frame.getSize();
Point posn = frame.getLocation();
panel.setAttribute("class", "jmri.jmrit.display.switchboardEditor.configurexml.SwitchboardEditorXml");
panel.setAttribute("name", "" + frame.getTitle());
panel.setAttribute("x", "" + posn.x);
panel.setAttribute("y", "" + posn.y);
panel.setAttribute("height", "" + size.height);
panel.setAttribute("width", "" + size.width);
panel.setAttribute("editable", "" + (p.isEditable() ? "yes" : "no"));
panel.setAttribute("showtooltips", "" + (p.showTooltip() ? "yes" : "no"));
panel.setAttribute("controlling", "" + (p.allControlling() ? "yes" : "no"));
panel.setAttribute("hide", p.isVisible() ? "no" : "yes");
panel.setAttribute("panelmenu", p.isPanelMenuVisible() ? "yes" : "no");
panel.setAttribute("scrollable", p.getScrollable());
panel.setAttribute("hideunconnected", "" + (p.hideUnconnected() ? "yes" : "no"));
panel.setAttribute("rangemin", "" + p.getPanelMenuRangeMin());
panel.setAttribute("rangemax", "" + p.getPanelMenuRangeMax());
panel.setAttribute("type", p.getSwitchType());
panel.setAttribute("connection", p.getSwitchManu());
panel.setAttribute("shape", p.getSwitchShape());
panel.setAttribute("columns", "" + p.getColumns());
panel.setAttribute("defaulttextcolor", p.getDefaultTextColor());
if (p.getBackgroundColor() != null) {
panel.setAttribute("redBackground", "" + p.getBackgroundColor().getRed());
panel.setAttribute("greenBackground", "" + p.getBackgroundColor().getGreen());
panel.setAttribute("blueBackground", "" + p.getBackgroundColor().getBlue());
}
return panel;
}
use of jmri.jmrit.display.switchboardEditor.SwitchboardEditor in project JMRI by JMRI.
the class JsonUtilHttpService method getPanels.
public JsonNode getPanels(Locale locale, String format) {
ArrayNode root = mapper.createArrayNode();
// list loaded Panels (ControlPanelEditor, PanelEditor, LayoutEditor, SwitchboardEditor)
// list ControlPanelEditors
Editor.getEditors(ControlPanelEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
root.add(panel);
});
// list LayoutEditors and PanelEditors
Editor.getEditors(PanelEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
root.add(panel);
});
// list SwitchboardEditors
Editor.getEditors(SwitchboardEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
root.add(panel);
});
return root;
}
use of jmri.jmrit.display.switchboardEditor.SwitchboardEditor in project JMRI by JMRI.
the class SwitchboardServlet method getXmlPanel.
@Override
protected String getXmlPanel(String name) {
log.debug("Getting {} for {}", getPanelType(), name);
try {
SwitchboardEditor editor = (SwitchboardEditor) getEditor(name);
Element panel = new Element("panel");
JFrame frame = editor.getTargetFrame();
panel.setAttribute("name", name);
panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight()));
panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth()));
panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight()));
panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth()));
// add more properties
panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
panel.setAttribute("hideunconnected", (editor.hideUnconnected()) ? "yes" : "no");
panel.setAttribute("rangemin", Integer.toString(editor.getPanelMenuRangeMin()));
panel.setAttribute("rangemax", Integer.toString(editor.getPanelMenuRangeMax()));
panel.setAttribute("type", editor.getSwitchType());
panel.setAttribute("connection", editor.getSwitchManu());
panel.setAttribute("shape", editor.getSwitchShape());
panel.setAttribute("columns", Integer.toString(editor.getColumns()));
panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
log.debug("webserver Switchboard attribs ready");
Element bgColor = new Element("backgroundColor");
if (editor.getBackgroundColor() == null) {
// set to light grey
bgColor.setAttribute("red", Integer.toString(192));
bgColor.setAttribute("green", Integer.toString(192));
bgColor.setAttribute("blue", Integer.toString(192));
} else {
bgColor.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed()));
bgColor.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen()));
bgColor.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue()));
}
panel.addContent(bgColor);
Element text = new Element("text");
text.setAttribute("color", editor.getDefaultTextColor());
text.setAttribute("content", "For now, Switchboards only present buttons in JMRI WebServer.");
panel.addContent(text);
// include switches, Bug: how to delete the old ones?
// call method in SwitchboardEditor
List<BeanSwitch> _switches = editor.getSwitches();
log.debug("SwbServlet N switches: {}", _switches.size());
for (BeanSwitch sub : _switches) {
if (sub != null) {
try {
Element e = ConfigXmlManager.elementFromObject(sub);
if (e != null) {
log.debug("element name: {}", e.getName());
// }
try {
e.setAttribute("label", sub.getNameString());
e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
if (sub.getNamedBean() == null) {
e.setAttribute("connected", "false");
log.debug("switch {} NOT connected", sub.getNameString());
} else {
// activate click action via class
e.setAttribute("connected", "true");
}
} catch (NullPointerException ex) {
if (sub.getNamedBean() == null) {
log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
} else {
log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
}
}
// read shared attribs
e.setAttribute("textcolor", editor.getDefaultTextColor());
e.setAttribute("type", editor.getSwitchType());
e.setAttribute("connection", editor.getSwitchManu());
e.setAttribute("shape", editor.getSwitchShape());
e.setAttribute("columns", Integer.toString(editor.getColumns()));
// process and add
parsePortableURIs(e);
panel.addContent(e);
}
} catch (Exception ex) {
log.error("Error reading xml panel element: " + ex, ex);
}
}
}
Document doc = new Document(panel);
XMLOutputter out = new XMLOutputter();
out.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
return out.outputString(doc);
} catch (NullPointerException ex) {
log.warn("Requested Switchboard [" + name + "] does not exist.");
return "ERROR Requested Switchboard [" + name + "] does not exist.";
}
}
use of jmri.jmrit.display.switchboardEditor.SwitchboardEditor in project JMRI by JMRI.
the class JsonUtilHttpService method getPanel.
public ObjectNode getPanel(Locale locale, Editor editor, String format) {
if (editor.getAllowInFrameServlet()) {
String title = ((JmriJFrame) editor.getTargetPanel().getTopLevelAncestor()).getTitle();
if (!title.isEmpty() && !Arrays.asList(WebServerPreferences.getDefault().getDisallowedFrames()).contains(title)) {
String type = PANEL;
String name = "Panel";
if (editor instanceof ControlPanelEditor) {
type = CONTROL_PANEL;
name = "ControlPanel";
} else if (editor instanceof LayoutEditor) {
type = LAYOUT_PANEL;
name = "Layout";
} else if (editor instanceof SwitchboardEditor) {
type = SWITCHBOARD_PANEL;
name = "Switchboard";
}
ObjectNode root = this.mapper.createObjectNode();
root.put(TYPE, PANEL);
ObjectNode data = root.putObject(DATA);
// NOI18N
data.put(NAME, name + "/" + title.replaceAll(" ", "%20").replaceAll("#", "%23"));
// NOI18N
data.put(URL, "/panel/" + data.path(NAME).asText() + "?format=" + format);
data.put(USERNAME, title);
data.put(TYPE, type);
return root;
}
}
return null;
}
Aggregations