001package jmri.jmrit.throttle.implementation; 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 or of a Container 011 * Will store location, size, visibility and iconification 012 * 013 * <hr> 014 * This file is part of JMRI. 015 * <p> 016 * JMRI is free software; you can redistribute it and/or modify it under the 017 * terms of version 2 of the GNU General Public License as published by the Free 018 * Software Foundation. See the "COPYING" file for a copy of this license. 019 * <p> 020 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 021 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 022 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 023 * 024 * @author Lionel Jeanson 2007-2026 025 * 026 */ 027 028public class WindowPreferences { 029 030 /** 031 * Collect JInternalFrame preferences. 032 * 033 * @param c The JInternalFrame being XMLed. 034 * @return An Element containing the following prefs: 035 * <ul> 036 * <li> x location 037 * <li> y location 038 * <li> width 039 * <li> height 040 * <li> isIcon 041 * </ul> 042 */ 043 public static Element getPreferences(JInternalFrame c) { 044 Element window = getPreferences((Container) c); 045 window.setAttribute("isIconified", String.valueOf(c.isIcon())); 046 return window; 047 } 048 049 /** 050 * Set JInternalFrame preferences from an XML Element. 051 * 052 * @param c The JInternalFrame being set. 053 * @param e An Element containing the following prefs: 054 * <ul> 055 * <li> x location 056 * <li> y location 057 * <li> width 058 * <li> height 059 * <li> isIcon 060 * </ul> 061 */ 062 public static void setPreferences(JInternalFrame c, Element e) { 063 setPreferences((Container) c, e); 064 try { 065 if (e.getAttribute("isIconified") != null) { 066 c.setIcon(e.getAttribute("isIconified").getBooleanValue()); 067 } 068 } catch (org.jdom2.DataConversionException | PropertyVetoException ex) { 069 log.warn("Exception setting preferences", ex); 070 } 071 } 072 073 /** 074 * Collect container preferences. 075 * 076 * @param c The container being XMLed. 077 * @return An Element containing the following prefs: 078 * <ul> 079 * <li> x location 080 * <li> y location 081 * <li> width 082 * <li> height 083 * </ul> 084 */ 085 public static Element getPreferences(Container c) { 086 Element window = new Element("window"); 087 window.setAttribute("x", String.valueOf(c.getLocation().x)); 088 window.setAttribute("y", String.valueOf(c.getLocation().y)); 089 Dimension size = c.getSize(); 090 window.setAttribute("width", String.valueOf(size.width)); 091 window.setAttribute("height", String.valueOf(size.height)); 092 window.setAttribute("isVisible", String.valueOf(c.isVisible())); 093 return window; 094 } 095 096 /** 097 * Set Container preferences from an XML Element. 098 * 099 * @param c The Container being set. 100 * @param e An Element containing the following prefs: 101 * <ul> 102 * <li> x location 103 * <li> y location 104 * <li> width 105 * <li> height 106 * </ul> 107 * @param ignorePosition true to not set location, false to set. 108 */ 109 public static void setPreferences(Container c, Element e, boolean ignorePosition) { 110 if (c == null) { 111 return; 112 } 113 try { 114 int x = e.getAttribute("x").getIntValue(); 115 int y = e.getAttribute("y").getIntValue(); 116 int width = e.getAttribute("width").getIntValue(); 117 int height = e.getAttribute("height").getIntValue(); 118 if (!ignorePosition) { 119 c.setLocation(x, y); 120 } 121 c.setSize(width, height); 122 if (e.getAttribute("isVisible") != null) { 123 c.setVisible(e.getAttribute("isVisible").getBooleanValue()); 124 } 125 } catch (org.jdom2.DataConversionException ex) { 126 log.warn("Exception setting preferences", ex); 127 } 128 } 129 130 public static void setPreferences(Container c, Element e) { 131 setPreferences(c, e, false); 132 } 133 134 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WindowPreferences.class); 135}