package textbender.o.awt; // Copyright 2005, 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.awt.*; import java.awt.event.*; import java.util.prefs.Preferences; import textbender.g.lang.ThreadRestricted; import textbender.g.util.prefs.*; /** Preference storage for component size. * Stores under keys "width" and "height". */ @ThreadRestricted( "AWT event dispatch" ) public final class ComponentSizePreference { /** Constructs a ComponentSizePreference. * Begins to automatically store changes only after you call restore(). *

* There is currently no way to remove it from the component. * Create one only per component, or they will accumulate. *

* * @param c component whose size to store and restore * @param preferences per {@linkplain #preferences() preferences}() */ public ComponentSizePreference( Component c, Preferences preferences ) { component = c; this.preferences = preferences; } // ------------------------------------------------------------------------------------ /** Returns the preference node used for storage. */ public Preferences preferences() { return preferences; } private final Preferences preferences; /** Restores the preferred size, and begins automatically storing changes. * Normally, called once only; subsequent calls should have no effect. * * @param defaultSize in case no stored preference available */ public void restore( Dimension defaultSize ) { restore( defaultSize.width, defaultSize.height ); } /** Restores the preferred size, and begins automatically storing changes. * Normally, called once only; subsequent calls should have no effect. * * @param defaultWidth in case no stored preference available * @param defaultHeight in case no stored preference available */ public void restore( int defaultWidth, int defaultHeight ) { final boolean toInitialize; // lazilly synchronized( ComponentSizePreference.this ) // for atomic test/set { toInitialize = storedWidth == null; // though normally called only once if( toInitialize ) { storedWidth = new IntegerPreferenceDC( preferences(), "width", defaultWidth ); storedHeight = new IntegerPreferenceDC( preferences(), "height", defaultHeight ); } } component.setSize( storedWidth.get(), storedHeight.get() ); // now restored, safe to overwrite storage: if( toInitialize ) component.addHierarchyListener( new HierarchyListener() // removes itself later { public void hierarchyChanged( HierarchyEvent e ) { if( component.isShowing() ) // thus waiting till after it is realized, per below { ComponentBoundsPreference.runBuffer.schedule( new Runnable() { public final void run() // a little later { assert ComponentBoundsPreference.runBuffer.delayMillisecondsMinimum() >= 500; component.addComponentListener( new ComponentAdapter() // no need to unregister, registry does not outlive this listener { public void componentResized( ComponentEvent e ) // fired one or more times during realization // the actual size may not be as restored as above // so, to avoid creep, we have waited as long as possible before listening for these events { storedWidth.put( component.getWidth() ); storedHeight.put( component.getHeight() ); } }); } }); component.removeHierarchyListener( this ); // once is enough } } }); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private IntegerPreference storedWidth; // final when initialized in restoreSize private IntegerPreference storedHeight; // " protected final Component component; }