View Javadoc
1 /*** 2 * PersistentJFrame.java 3 * 4 * Project: Dependency Tool 5 * 6 * WHEN WHO WHAT 7 * 06.06.2003 pko initial public release 8 * 20.02.2003 pko creation 9 * 10 * Copyright 2003 ELCA Informatique SA 11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland 12 * www.elca.ch 13 * 14 * This library is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU Lesser General Public License 16 * as published by the Free Software Foundation; either version 2.1 of 17 * the License, or (at your option) any later version. 18 * 19 * This library is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * Lesser General Public License for more details. 23 * 24 * You should have received a copy of the GNU Lesser General Public 25 * License along with this library; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 27 * USA 28 */ 29 30 package ch.elca.dependency.util.gui; 31 32 import ch.elca.dependency.util.NotifyOnExit; 33 import java.awt.Rectangle; 34 import java.util.prefs.Preferences; 35 import javax.swing.JFrame; 36 37 /*** 38 * Class <code>PersistentJFrame</code> furnishes its 39 * superclass: JFrame with persistent size and location. The 40 * persistence is achieved with the help of 41 * <code>java.util.prefs.Preferences</code> class. 42 * 43 * @author Pawel Kowalski 44 * @version 1.0-beta 45 */ 46 public class PersistentJFrame extends JFrame { 47 48 //****************************************************************************************/ 49 // instance variables 50 //****************************************************************************************/ 51 52 private JFrameConfig m_frameConfig = new JFrameConfig(); 53 54 //****************************************************************************************/ 55 // c'tors 56 //****************************************************************************************/ 57 58 /*** 59 * Creates a new <code>PersistentJFrame</code> instance. 60 */ 61 public PersistentJFrame() { 62 this(""); 63 } 64 65 /*** 66 * Creates a new <code>PersistentJFrame</code> instance with a 67 * title. 68 * 69 * @param title a <code>String</code> value 70 */ 71 public PersistentJFrame(String title) { 72 super(title); 73 recallConfig(); 74 } 75 76 //****************************************************************************************/ 77 // utility methods 78 //****************************************************************************************/ 79 80 /*** 81 * Set the default bounds of this PersistentJFrame. 82 * 83 * @param bounds a <code>Rectangle</code> value 84 */ 85 protected void setDefaultBounds(Rectangle bounds) { 86 m_frameConfig.setDefaultBounds(bounds); 87 } 88 89 /*** 90 * Get the configuration of this PersistentJFrame stored 91 * persistently. Call this method after the creation of a 92 * PersistentJFrame or if the user requests to have the 93 * configuration restored to values stored during last execution. 94 */ 95 public void recallConfig() { 96 setBounds(m_frameConfig.getBounds()); 97 setExtendedState(m_frameConfig.isMaximized()); 98 } 99 100 //****************************************************************************************/ 101 // internal config 102 //****************************************************************************************/ 103 104 /*** 105 * Class <code>JFrameConfig</code> is used to overcome the 106 * shortage of single inheritance: we don't want explicitely to 107 * have to register each class which is interested in notification 108 * just before the termination. Each such class should do this 109 * automatically within its constructor and should have a method 110 * e.g. notifyOnExit() to be called before temination. In order to 111 * provide this functionality in a centralized manner a class 112 * <code>NotifyOnExit</code> is provided which does the 113 * registration. This <code>JFrameConfig</code> class 114 * extends the <code>NotifyOnExit</code> class and inherits thus 115 * functionality. Its instances will be notified before 116 * termination. This way the enclosing class is not forced to 117 * implement the registration functionality. 118 */ 119 public class JFrameConfig extends NotifyOnExit { 120 121 //************************************************************************************/ 122 // preference keys 123 //************************************************************************************/ 124 125 private static final String LOCATION_X = "locationX"; 126 private static final String LOCATION_Y = "LocationY"; 127 private static final String WIDTH = "Width"; 128 private static final String HEIGHT = "Height"; 129 private static final String MAXIMIZED = "Maximized"; 130 131 //************************************************************************************/ 132 // instance variables 133 //************************************************************************************/ 134 135 private String m_path = PersistentJFrame.this.getClass().getName().replace('.', '/'); 136 private Preferences m_prefs = Preferences.userRoot().node(m_path); 137 private Rectangle m_defaultBounds = new Rectangle(10, 10, 300, 300); 138 139 //************************************************************************************/ 140 // utility methods 141 //************************************************************************************/ 142 143 /*** 144 * Set the default bounds for this config class. 145 * 146 * @param bounds a <code>Rectangle</code> value 147 */ 148 private void setDefaultBounds(Rectangle bounds) { 149 m_defaultBounds = bounds; 150 } 151 152 /*** 153 * Get the bounds stored for this config class. 154 * 155 * @return a <code>Rectangle</code> value 156 */ 157 private Rectangle getBounds() { 158 int locationX = m_prefs.getInt(LOCATION_X, m_defaultBounds.x); 159 int locationY = m_prefs.getInt(LOCATION_Y, m_defaultBounds.y); 160 int width = m_prefs.getInt(WIDTH, m_defaultBounds.width); 161 int height = m_prefs.getInt(HEIGHT, m_defaultBounds.height); 162 return new Rectangle(locationX, locationY, width, height); 163 } 164 165 /*** 166 * Recall wheter this <code>PersistentJFrame</code> has been 167 * maximized before termination. 168 * 169 * @return an <code>int</code> value 170 */ 171 private int isMaximized() { 172 return m_prefs.getInt(MAXIMIZED, 0); 173 } 174 175 /*** 176 * Store the current values persistently, so that they'll be 177 * available after a restart of the Dependency Tool. 178 */ 179 private void storePreferences() { 180 if (PersistentJFrame.this.getExtendedState() == JFrame.MAXIMIZED_BOTH) { 181 m_prefs.putInt(MAXIMIZED, PersistentJFrame.this.getExtendedState()); 182 } else { 183 Rectangle bounds = PersistentJFrame.this.getBounds(); 184 m_prefs.putInt(LOCATION_X, bounds.x); 185 m_prefs.putInt(LOCATION_Y, bounds.y); 186 m_prefs.putInt(WIDTH, bounds.width); 187 m_prefs.putInt(HEIGHT, bounds.height); 188 } 189 } 190 191 //************************************************************************************/ 192 // implement NotifyOnExit.notifyOnExit() method 193 //************************************************************************************/ 194 195 /*** 196 * Notify the instance of this class that the Dependecy Tool 197 * is about to terminate. The <code>JFrameConfig</code> will 198 * store its configuration persistently. 199 */ 200 public void notifyOnExit() { 201 storePreferences(); 202 } 203 } 204 }

This page was automatically generated by Maven