package textbender.o.awt; // Copyright 2001-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 location. * Stores under keys "x" and "y". */ @ThreadRestricted( "AWT event dispatch" ) public class ComponentLocationPreference { /** Constructs a ComponentLocationPreference. * 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 location to store and restore * @param preferences per {@linkplain #preferences() preferences}() */ public ComponentLocationPreference( 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 location, and begins automatically storing changes. * Normally, called once only; subsequent calls should have no effect. * * @param defaultLocation in case no stored preference available */ // * @param runBuffer configured per // * {@linkplain #restore(int,int,RunDelayer) restore}(int,int,RunDelayer) // public void restore( Point defaultLocation, RunDelayer runBuffer ) public void restore( Point defaultLocation ) { restore( defaultLocation.x, defaultLocation.y ); } /** Restores the preferred location, and begins automatically storing changes. * Normally, called once only; subsequent calls should have no effect. * * @param defaultX in case no stored preference available * @param defaultY in case no stored preference available */ // * @param runBuffer of at least 500 ms delay // public void restore( int defaultX, int defaultY, final RunDelayer runBuffer ) public void restore( int defaultX, int defaultY ) { final boolean toInitialize; // lazilly assert java.awt.EventQueue.isDispatchThread(); // synchronized( ComponentLocationPreference.this ) // for atomic test/set // { toInitialize = storedX == null; // though normally called only once if( toInitialize ) { storedX = new IntegerPreferenceDC( preferences, "x", defaultX ); storedY = new IntegerPreferenceDC( preferences, "y", defaultY ); } // } component.setLocation( storedX.get(), storedY.get() ); // now restored, safe to overwrite storage: //System.out.println( "CLP" ); if( toInitialize ) component.addHierarchyListener( new HierarchyListener() // removes itself later { public void hierarchyChanged( HierarchyEvent e ) { //System.out.println( "CLP chang" ); if( component.isShowing() ) // thus waiting till after it is realized, per below { //System.out.println( "CLP sched" ); ComponentBoundsPreference.runBuffer.schedule( new Runnable() { public final void run() // a little later { //System.out.println( "CLP run" ); assert ComponentBoundsPreference.runBuffer.delayMillisecondsMinimum() >= 500; component.addComponentListener( new ComponentAdapter() // no need to unregister, registry does not outlive this listener { public void componentMoved( ComponentEvent e ) // fired one or more times during realization // the actual location may not be as restored as above (e.g. I have seen it translated +1,+1 under X Windows) // so, to avoid creep, we have waited as long as possible before listening for these events { //System.out.println( "CLP store" ); storedX.put( component.getX() ); storedY.put( component.getY() ); } }); } }); component.removeHierarchyListener( this ); // once is enough } } }); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// protected final Component component; private IntegerPreference storedX; // final when initialized in restoreLocation private IntegerPreference storedY; // " }