View Javadoc
1 /*** 2 * PersistentJInternalFrame.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.JInternalFrame; 36 37 /*** 38 * Class <code>PersistentJInternalFrame</code> furnishes its 39 * superclass: JInternalFrame 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 abstract class PersistentJInternalFrame extends JInternalFrame { 47 48 //****************************************************************************************/ 49 // instance variables 50 //****************************************************************************************/ 51 52 private JInternalFrameConfig m_frameConfig = new JInternalFrameConfig(); 53 54 //****************************************************************************************/ 55 // c'tors 56 //****************************************************************************************/ 57 58 /*** 59 * Creates a new <code>PersistentJInternalFrame</code> instance 60 * with a title. 61 * 62 * @param title a <code>String</code> value 63 */ 64 public PersistentJInternalFrame(String title) { 65 this(title, false, false, false, false); 66 } 67 68 /*** 69 * Creates a new <code>PersistentJInternalFrame</code> instance 70 * with title and switches for some boolean properties. 71 * 72 * @param title a <code>String</code> value 73 * @param resizable a <code>boolean</code> value 74 * @param closable a <code>boolean</code> value 75 * @param maximizable a <code>boolean</code> value 76 * @param iconifiable a <code>boolean</code> value 77 */ 78 public PersistentJInternalFrame(String title, boolean resizable, boolean closable, 79 boolean maximizable, boolean iconifiable) { 80 super(title, resizable, closable, maximizable, iconifiable); 81 recallConfig(); 82 } 83 84 //****************************************************************************************/ 85 // utility methods 86 //****************************************************************************************/ 87 88 /*** 89 * Set the default bounds of this PersistentJInternalFrame. 90 * 91 * @param bounds a <code>Rectangle</code> value 92 */ 93 protected void setDefaultBounds(Rectangle bounds) { 94 m_frameConfig.setDefaultBounds(bounds); 95 } 96 97 /*** 98 * Get the bounds of this PersistentJInternalFrame stored 99 * persistently. Call this method after the creation of a 100 * PersistentJInternalFrame or if the user requests to have the 101 * bounds restored to values stored during last execution. 102 */ 103 public void recallConfig() { 104 setBounds(m_frameConfig.getBounds()); 105 } 106 107 //****************************************************************************************/ 108 // internal config 109 //****************************************************************************************/ 110 111 /*** 112 * Class <code>JInternalFrameConfig</code> is used to overcome the 113 * shortage of single inheritance: we don't want explicitely to 114 * have to register each class which is interested in notification 115 * just before the termination. Each such class should do this 116 * automatically within its constructor and should have a method 117 * e.g. notifyOnExit() to be called before temination. In order to 118 * provide this functionality in a centralized manner a class 119 * <code>NotifyOnExit</code> is provided which does the 120 * registration. This <code>JInternalFrameConfig</code> class 121 * extends the <code>NotifyOnExit</code> class and inherits thus 122 * functionality. Its instances will be notified before 123 * termination. This way the enclosing class is not forced to 124 * implement the registration functionality. 125 */ 126 public class JInternalFrameConfig extends NotifyOnExit { 127 128 //************************************************************************************/ 129 // preference keys 130 //************************************************************************************/ 131 132 private final static String LOCATION_X = "locationX"; 133 private static final String LOCATION_Y = "locationY"; 134 private static final String WIDTH = "width"; 135 private static final String HEIGHT = "height"; 136 137 //************************************************************************************/ 138 // instance variables 139 //************************************************************************************/ 140 141 private String m_path = PersistentJInternalFrame.this.getClass().getName().replace('.', '/'); 142 private Preferences m_prefs = Preferences.userRoot().node(m_path); 143 private Rectangle m_defaultBounds = new Rectangle(10, 10, 200, 200); 144 145 //************************************************************************************/ 146 // utility methods 147 //************************************************************************************/ 148 149 /*** 150 * Set the default bounds for this config class. 151 * 152 * @param bounds a <code>Rectangle</code> value 153 */ 154 private void setDefaultBounds(Rectangle bounds) { 155 m_defaultBounds = bounds; 156 } 157 158 /*** 159 * Get the bounds stored for this config class. 160 * 161 * @return a <code>Rectangle</code> value 162 */ 163 private Rectangle getBounds() { 164 int locationX = m_prefs.getInt(LOCATION_X, m_defaultBounds.x); 165 int locationY = m_prefs.getInt(LOCATION_Y, m_defaultBounds.y); 166 int width = m_prefs.getInt(WIDTH, m_defaultBounds.width); 167 int height = m_prefs.getInt(HEIGHT, m_defaultBounds.height); 168 return new Rectangle(locationX, locationY, width, height); 169 } 170 171 /*** 172 * Store the current values persistently, so that they'll be 173 * available after a restart of the Dependency Tool. 174 */ 175 private void storePreferences() { 176 Rectangle bounds = PersistentJInternalFrame.this.getNormalBounds(); 177 m_prefs.putInt(LOCATION_X, bounds.x); 178 m_prefs.putInt(LOCATION_Y, bounds.y); 179 m_prefs.putInt(WIDTH, bounds.width); 180 m_prefs.putInt(HEIGHT, bounds.height); 181 } 182 183 //************************************************************************************/ 184 // implement NotifyOnExit.notifyOnExit() method 185 //************************************************************************************/ 186 187 /*** 188 * Notify the instance of this class that the Dependecy Tool 189 * is about to terminate. The 190 * <code>JInternalFrameConfig</code> will store its 191 * configuration persistently. 192 */ 193 public void notifyOnExit() { 194 storePreferences(); 195 } 196 } 197 }

This page was automatically generated by Maven