001package jmri.web.servlet.panel; 002 003import java.util.List; 004import javax.servlet.annotation.WebServlet; 005import javax.servlet.http.HttpServlet; 006import javax.swing.JFrame; 007 008import jmri.jmrit.display.IndicatorTrackIcon; 009import jmri.jmrit.display.IndicatorTurnoutIcon; 010import jmri.jmrit.display.Positionable; 011import jmri.jmrit.display.controlPanelEditor.ControlPanelEditor; 012import org.jdom2.Document; 013import org.jdom2.Element; 014import org.jdom2.output.Format; 015import org.jdom2.output.XMLOutputter; 016import org.openide.util.lookup.ServiceProvider; 017import org.slf4j.Logger; 018import org.slf4j.LoggerFactory; 019 020/** 021 * Return xml (for specified ControlPanel) suitable for use by external clients. 022 * <p> 023 * See JMRI Web Server - Panel Servlet Help in help/en/html/web/PanelServlet.shtml for an example description of 024 * the interaction between the Web Servlets, the Web Browser and the JMRI application. 025 * 026 * @author Randall Wood (C) 2016 027 */ 028@WebServlet(name = "ControlPanelServlet", 029 urlPatterns = {"/panel/ControlPanel"}) 030@ServiceProvider(service = HttpServlet.class) 031public class ControlPanelServlet extends AbstractPanelServlet { 032 033 @Override 034 protected String getPanelType() { 035 return "ControlPanel"; 036 } 037 038 @Override 039 protected String getXmlPanel(String name) { 040 log.debug("Getting {} for {}", getPanelType(), name); 041 ControlPanelEditor editor = (ControlPanelEditor) getEditor(name); 042 if (editor == null) { 043 log.warn("Requested ControlPanel [{}] does not exist.", name); 044 return "ERROR Requested panel [" + name + "] does not exist."; 045 } 046 047 Element panel = new Element("panel"); 048 049 JFrame frame = editor.getTargetFrame(); 050 051 panel.setAttribute("name", name); 052 panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight())); 053 panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth())); 054 panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight())); 055 panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth())); 056 057 panel.setAttribute("showtooltips", (editor.showToolTip()) ? "yes" : "no"); 058 panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no"); 059 if (editor.getBackgroundColor() != null) { 060 Element color = new Element("backgroundColor"); 061 color.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed())); 062 color.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen())); 063 color.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue())); 064 panel.addContent(color); 065 } 066 067 // include contents 068 List<Positionable> contents = editor.getContents(); 069 log.debug("N elements: {}", contents.size()); 070 for (Positionable sub : contents) { 071 if (sub != null) { 072 Element e = new Element("temp"); 073 try { 074 e = positionableElement(sub); 075 } catch (Exception ex) { 076 log.error("Error storing panel element: {}", ex, ex); 077 } 078 // where required, add special stuff to positionable here to use in Web Server 079 switch (e.getName()) { 080 case "indicatorturnouticon" : 081 // if separate occ.sensor was set on icon, names for sensor plus the turnout were 082 // already copied to e as part of 'contents' 083 Element elem = new Element("oblocksysname"); 084 if (((IndicatorTurnoutIcon) sub).getOccBlock() != null) { // optional for CPE, not read on load 085 String itoioblock = ((IndicatorTurnoutIcon) sub).getOccBlock().getSystemName(); 086 elem.addContent(itoioblock); 087 log.debug("CP-SERVLET ITOI = {}", itoioblock); 088 } else { 089 elem.addContent("none"); // NOI18N 090 log.debug("no oblocksensor configured on ITOI {}", sub.getNameString()); 091 } 092 e.addContent(elem); 093 break; 094 case "indicatortrackicon" : 095 // if separate occ.sensor was set on icon, its name was already copied to e as part of 'contents' 096 elem = new Element("oblocksysname"); 097 if (((IndicatorTrackIcon) sub).getOccBlock() != null) { // optional for CPE, not read on load 098 String itioblock = ((IndicatorTrackIcon) sub).getOccBlock().getSystemName(); 099 elem.addContent(itioblock); 100 log.debug("CP-SERVLET ITI = {}", itioblock); 101 } else { 102 elem.addContent("none"); // NOI18N 103 log.debug("no oblocksensor configured on ITI {}", sub.getNameString()); 104 } 105 e.addContent(elem); 106 break; 107 case "" : 108 default : 109 // nothing extra 110 } 111 panel.addContent(e); 112 } 113 } 114 115 Document doc = new Document(panel); 116 XMLOutputter out = new XMLOutputter(); 117 out.setFormat(Format.getPrettyFormat() 118 .setLineSeparator(System.getProperty("line.separator")) 119 .setTextMode(Format.TextMode.TRIM)); 120 121 return out.outputString(doc); 122 } 123 124 @Override 125 protected String getJsonPanel(String name) { 126 // TODO Auto-generated method stub 127 return "ERROR JSON support not implemented"; 128 } 129 130 private final static Logger log = LoggerFactory.getLogger(ControlPanelServlet.class); 131 132}