|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JavaFX frontend for SciJava JSR-223-compliant scripting plugins. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2019 HHMI Janelia Research Campus. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | +package org.scijava.scripting.fx |
| 30 | + |
| 31 | +import javafx.geometry.Insets |
| 32 | +import javafx.scene.control.ButtonType |
| 33 | +import javafx.scene.control.Dialog |
| 34 | +import javafx.scene.control.DialogPane |
| 35 | +import javafx.scene.input.KeyCode |
| 36 | +import javafx.scene.input.KeyCodeCombination |
| 37 | +import javafx.scene.input.KeyCombination |
| 38 | +import javafx.scene.layout.Region |
| 39 | +import javafx.stage.Modality |
| 40 | +import org.scijava.Context |
| 41 | + |
| 42 | +class SciJavaReplFXDialog @JvmOverloads constructor( |
| 43 | + context: Context, |
| 44 | + bindings: Map<String, *> = mapOf<String, Any>(), |
| 45 | + width: Double? = null, |
| 46 | + height: Double? = null, |
| 47 | + increaseFontKeys: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.EQUALS, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_ANY)), |
| 48 | + decreaseFontKeys: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.MINUS, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_ANY)), |
| 49 | + evalKeys: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.ENTER, KeyCombination.CONTROL_DOWN)), |
| 50 | + exitKeyCombination: Collection<KeyCombination> = setOf (KeyCodeCombination(KeyCode.D, KeyCombination.CONTROL_DOWN)), |
| 51 | + createNewReplCombination: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN)), |
| 52 | + cycleTabsForwardCombination: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.TAB, KeyCombination.CONTROL_DOWN)), |
| 53 | + cycleTabsBackwardCombination: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.TAB, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN)) |
| 54 | +) : Dialog<Any>() { |
| 55 | + |
| 56 | + |
| 57 | + @JvmOverloads constructor( |
| 58 | + context: Context, |
| 59 | + vararg bindings: Pair<String, *>, |
| 60 | + width: Double? = null, |
| 61 | + height: Double? = null, |
| 62 | + increaseFontKeys: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.EQUALS, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_ANY)), |
| 63 | + decreaseFontKeys: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.MINUS, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_ANY)), |
| 64 | + evalKeys: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.ENTER, KeyCombination.CONTROL_DOWN)), |
| 65 | + exitKeyCombination: Collection<KeyCombination> = setOf (KeyCodeCombination(KeyCode.D, KeyCombination.CONTROL_DOWN)), |
| 66 | + createNewReplCombination: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN)), |
| 67 | + cycleTabsForwardKombination: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.TAB, KeyCombination.CONTROL_DOWN)), |
| 68 | + cycleTabsBackwardKombination: Collection<KeyCombination> = setOf(KeyCodeCombination(KeyCode.TAB, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN)) |
| 69 | + ) : this( |
| 70 | + context, |
| 71 | + mapOf(*bindings), |
| 72 | + width, |
| 73 | + height, |
| 74 | + increaseFontKeys, |
| 75 | + decreaseFontKeys, |
| 76 | + evalKeys, |
| 77 | + exitKeyCombination, |
| 78 | + createNewReplCombination, |
| 79 | + cycleTabsForwardKombination, |
| 80 | + cycleTabsBackwardKombination) |
| 81 | + |
| 82 | + val tabs = ScijavaReplFXTabs( |
| 83 | + context = context, |
| 84 | + bindings = bindings, |
| 85 | + increaseFontKeys = increaseFontKeys, |
| 86 | + decreaseFontKeys = decreaseFontKeys, |
| 87 | + evalKeys = evalKeys, |
| 88 | + exitKeyCombination = exitKeyCombination, |
| 89 | + createNewReplCombination = createNewReplCombination, |
| 90 | + cycleTabsForwardCombination = cycleTabsForwardCombination, |
| 91 | + cycleTabsBackwardCombination = cycleTabsBackwardCombination) |
| 92 | + |
| 93 | + private var widthOnHiding: Double? = width |
| 94 | + private var heightOnHiding: Double? = height |
| 95 | + private var wasHiding = !isShowing |
| 96 | + |
| 97 | + init { |
| 98 | + // need DialogPane with custom createButtonBar to remove empty space at bottom |
| 99 | + dialogPane = object: DialogPane() { |
| 100 | + override fun createButtonBar() = Region() |
| 101 | + } |
| 102 | + dialogPane.content = tabs.node |
| 103 | + isResizable = true |
| 104 | + dialogPane.padding = Insets.EMPTY |
| 105 | + initModality(Modality.NONE) |
| 106 | + setOnShowing { |
| 107 | + if (!tabs.hasPrompts) |
| 108 | + tabs.createAndAddTab() |
| 109 | + wasHiding = !isShowing |
| 110 | + } |
| 111 | + setOnShown { |
| 112 | + if (wasHiding) { |
| 113 | + widthOnHiding?.let { this.width = it } |
| 114 | + heightOnHiding?.let { this.height = it } |
| 115 | + } |
| 116 | + } |
| 117 | + setOnHiding { |
| 118 | + widthOnHiding = this.width |
| 119 | + heightOnHiding = this.height |
| 120 | + } |
| 121 | + // weirdly, an invisible close button is required for the close icon to work |
| 122 | + // https://stackoverflow.com/questions/32048348/javafx-scene-control-dialogr-wont-close-on-pressing-x |
| 123 | + // https://stackoverflow.com/questions/37619885/javafx-fxml-dialog-cant-close-it-with-the-x-button?noredirect=1&lq=1 |
| 124 | + dialogPane.buttonTypes.setAll(ButtonType.CLOSE) |
| 125 | + dialogPane |
| 126 | + .lookupButton(ButtonType.CLOSE) |
| 127 | + .also { it.isVisible = false } |
| 128 | + .also { it.managedProperty().bind(it.visibleProperty()) } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments