001package jmri.jmrit.throttle; 002 003import java.awt.Container; 004import java.awt.Dimension; 005import java.beans.PropertyVetoException; 006import javax.swing.JInternalFrame; 007import org.jdom2.Element; 008 009/** 010 * A helper class for getting and setting XML attributes of a JInternalFrame. 011 */ 012public class WindowPreferences { 013 014 /** 015 * Collect JInternalFrame preferences. 016 * 017 * @param c The JInternalFrame being XMLed. 018 * @return An Element containing the following prefs: 019 * <ul> 020 * <li> x location 021 * <li> y location 022 * <li> width 023 * <li> height 024 * <li> isIcon 025 * </ul> 026 */ 027 public static Element getPreferences(JInternalFrame c) { 028 Element window = getPreferences((Container) c); 029 window.setAttribute("isIconified", String.valueOf(c.isIcon())); 030 return window; 031 } 032 033 /** 034 * Set JInternalFrame preferences from an XML Element. 035 * 036 * @param c The JInternalFrame being set. 037 * @param e An Element containing the following prefs: 038 * <ul> 039 * <li> x location 040 * <li> y location 041 * <li> width 042 * <li> height 043 * <li> isIcon 044 * </ul> 045 */ 046 public static void setPreferences(JInternalFrame c, Element e) { 047 setPreferences((Container) c, e); 048 try { 049 if (e.getAttribute("isIconified") != null) { 050 c.setIcon(e.getAttribute("isIconified").getBooleanValue()); 051 } 052 } catch (org.jdom2.DataConversionException | PropertyVetoException ex) { 053 log.warn("Exception setting preferences", ex); 054 } 055 } 056 057 /** 058 * Collect container preferences. 059 * 060 * @param c The container being XMLed. 061 * @return An Element containing the following prefs: 062 * <ul> 063 * <li> x location 064 * <li> y location 065 * <li> width 066 * <li> height 067 * </ul> 068 */ 069 public static Element getPreferences(Container c) { 070 Element window = new Element("window"); 071 window.setAttribute("x", String.valueOf(c.getLocation().x)); 072 window.setAttribute("y", String.valueOf(c.getLocation().y)); 073 Dimension size = c.getSize(); 074 window.setAttribute("width", String.valueOf(size.width)); 075 window.setAttribute("height", String.valueOf(size.height)); 076 window.setAttribute("isVisible", String.valueOf(c.isVisible())); 077 return window; 078 } 079 080 /** 081 * Set Container preferences from an XML Element. 082 * 083 * @param c The Container being set. 084 * @param e An Element containing the following prefs: 085 * <ul> 086 * <li> x location 087 * <li> y location 088 * <li> width 089 * <li> height 090 * </ul> 091 * @param ignorePosition true to not set location, false to set. 092 */ 093 public static void setPreferences(Container c, Element e, boolean ignorePosition) { 094 try { 095 int x = e.getAttribute("x").getIntValue(); 096 int y = e.getAttribute("y").getIntValue(); 097 int width = e.getAttribute("width").getIntValue(); 098 int height = e.getAttribute("height").getIntValue(); 099 if (!ignorePosition) { 100 c.setLocation(x, y); 101 } 102 c.setSize(width, height); 103 if (e.getAttribute("isVisible") != null) { 104 c.setVisible(e.getAttribute("isVisible").getBooleanValue()); 105 } 106 } catch (org.jdom2.DataConversionException ex) { 107 log.warn("Exception setting preferences", ex); 108 } 109 } 110 111 public static void setPreferences(Container c, Element e) { 112 setPreferences(c, e, false); 113 } 114 115 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WindowPreferences.class); 116}