Advertisement
mishaxz

WindowPosition [Kotlin]

Feb 18th, 2017
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.75 KB | None | 0 0
  1. package helper.javafx
  2.  
  3. import javafx.stage.Stage
  4. import java.awt.Point
  5. import java.util.concurrent.ConcurrentHashMap
  6. import java.util.prefs.Preferences
  7. import kotlin.reflect.KClass
  8.  
  9. /**
  10.  * ## WindowPosition
  11.  *  - based on: [Broadly Applicable: JavaFX Restore Window Size & Position](http://broadlyapplicable.blogspot.hk/2015/02/javafx-restore-window-size-position.html)
  12.  *
  13.  * ### usage
  14.  * - call [Stage.restore] on the [Stage] object after it has been created
  15.  *
  16.  * ### example
  17.  *      primaryStage.restore(this) // save/restore WindowPosition
  18.  *      primaryStage.show()
  19.  */
  20.  
  21. private object WindowPosition {
  22.     private const val WINDOW_POSITION_X = "Window_Position_X"
  23.     private const val WINDOW_POSITION_Y = "Window_Position_Y"
  24.     private const val WINDOW_WIDTH      = "Window_Width"
  25.     private const val WINDOW_HEIGHT     = "Window_Height"
  26.  
  27.     private val offsets: MutableMap<KClass<out Any>, Point> by lazy { ConcurrentHashMap<KClass<out Any>, Point>() }
  28.  
  29.     /**
  30.      * ## loads the position and size settings previously saved by [save]
  31.      * - loads from [Preferences] under the node specified by [node]
  32.      * - if there aren't any, uses the defaults specified by [stage]
  33.      * @param caller the _calling object_
  34.      * @param stage the [Stage]
  35.      * @param key the key to save under `e.g. "MainWindow"`
  36.      * @param offset the [Point] offset used for each subsequent call for the [caller] class
  37.      * @see save
  38.      */
  39.     internal fun restore(caller: Any, stage: Stage, key: String, offset: Point?=null) {
  40.         var newOffset: Point?=null
  41.         offset?.let {
  42.             val off = offsets[caller::class]
  43.             newOffset = if (off == null) Point(0,0) else Point(off.x + it.x, off.y + it.y)
  44.             offsets[caller::class] = newOffset!!
  45.         }
  46.  
  47.         val pref = Preferences.userRoot().node(node(caller, stage, key))
  48.         val x = pref.getDouble(WINDOW_POSITION_X, stage.x) + (newOffset?.x ?: 0)
  49.         val y = pref.getDouble(WINDOW_POSITION_Y, stage.y) + (newOffset?.y ?: 0)
  50.         if (x >= 0 && y >= 0) { // might be minimized or something
  51.             val width = pref.getDouble(WINDOW_WIDTH, stage.width)
  52.             val height = pref.getDouble(WINDOW_HEIGHT, stage.height)
  53.             stage.x = x
  54.             stage.y = y
  55.             stage.width = width
  56.             stage.height = height
  57.         }
  58.  
  59.         // add listeners
  60.         stage.widthProperty() .addListener { obs, old, selected -> save(caller, stage, key) }
  61.         stage.heightProperty().addListener { obs, old, selected -> save(caller, stage, key) }
  62.         stage.xProperty()     .addListener { obs, old, selected -> save(caller, stage, key) }
  63.         stage.yProperty()     .addListener { obs, old, selected -> save(caller, stage, key) }
  64.     }
  65.  
  66.     /**
  67.      * ## saves the position and size settings of the [Stage] to [Preferences]
  68.      * - saves to [Preferences] to the node specified by [node]
  69.      * @param caller the _calling object_
  70.      * @param stage the [Stage]
  71.      * @param key the key to save under `e.g. "MainWindow"`
  72.      * @see restore
  73.      */
  74.     private fun save(caller: Any, stage: Stage, key: String) {
  75.         if (stage.x < 0 || stage.y < 0) return // might be minimized or something
  76.         val preferences = Preferences.userRoot().node(node(caller, stage, key))
  77.         preferences.putDouble(WINDOW_POSITION_X, stage.x)
  78.         preferences.putDouble(WINDOW_POSITION_Y, stage.y)
  79.         preferences.putDouble(WINDOW_WIDTH, stage.width)
  80.         preferences.putDouble(WINDOW_HEIGHT, stage.height)
  81.     }
  82.  
  83.     private fun node(caller: Any, stage: Stage, key: String) = "${caller.javaClass.name}/WindowPosition/$key}"
  84. }
  85.  
  86. fun Stage.restore(caller: Any, key: String, offset: Point?=null) = WindowPosition.restore(caller, this, key, offset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement