Skip to content

Commit 07ef656

Browse files
committed
Focus the first editor when a dialog shows up
1 parent d37f00c commit 07ef656

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

ganttproject/src/main/java/biz/ganttproject/app/PropertySheet.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class PropertyPaneBuilder(private val localizer: Localizer, private val gridPane
7676
internal val validationErrors = FXCollections.observableMap(mutableMapOf<ObservableProperty<*>, String>())
7777
internal val isEscCloseEnabled = SimpleBooleanProperty(true)
7878
internal val rowBuilders = mutableListOf<RowBuilder>()
79+
internal var onRequestFocus: (()->Unit)? = null
7980

8081
fun stylesheet(stylesheet: String) {
8182
gridPane.stylesheets.add(stylesheet)
@@ -139,6 +140,9 @@ class PropertyPaneBuilder(private val localizer: Localizer, private val gridPane
139140

140141
private fun createOptionItem(property: ObservableProperty<*>, editor: Node, options: PropertyDisplayOptions<*>? = null): OptionRowBuilder {
141142
property.isWritable.addWatcher { evt -> editor.isDisable = !evt.newValue }
143+
if (onRequestFocus == null) {
144+
onRequestFocus = { editor.requestFocus() }
145+
}
142146
return OptionRowBuilder(property, editor, getOptionLabel(property), options)
143147
}
144148

@@ -472,6 +476,11 @@ class PropertySheetBuilder(private val localizer: Localizer) {
472476
paneBuilder.rowBuilders.forEachIndexed { _, builder ->
473477
rowNum = builder.build(gridPane, rowNum) + 1
474478
}
479+
gridPane.focusedProperty().addListener { _, _, newValue ->
480+
if (newValue) {
481+
paneBuilder.onRequestFocus?.invoke()
482+
}
483+
}
475484
return PropertySheet(gridPane, paneBuilder.validationErrors, paneBuilder.isEscCloseEnabled)
476485
}
477486

ganttproject/src/main/java/net/sourceforge/ganttproject/gui/GanttDialogProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public Unit invoke(JComponent node, String title) {
129129
var mainPropertiesPanel = taskPropertiesController.getMainPropertiesPanel();
130130

131131
try {
132-
tabbedPane.getTabs().add(new Tab(mainPropertiesPanel.getTitle(), mainPropertiesPanel.getFxNode()));
132+
tabbedPane.getTabs().add(new Tab(mainPropertiesPanel.getTitle(), mainPropertiesPanel.getFxComponent()));
133133
} catch (Exception e) {
134134
e.printStackTrace();
135135
}
@@ -155,6 +155,7 @@ public Unit invoke(JComponent node, String title) {
155155
}
156156
return null;
157157
});
158+
mainPropertiesPanel.requestFocus();
158159
dialogController.resize();
159160
return null;
160161
}); return null; });

ganttproject/src/main/java/net/sourceforge/ganttproject/gui/taskproperties/MainPropertiesPanel.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import javax.swing.SwingUtilities
4747

4848
class MainPropertiesPanel(private val task: Task, private val taskView: TaskView) {
4949
val title: String = RootLocalizer.formatText("general")
50+
val fxComponent by lazy { getFxNode() }
5051

5152
private val nameOption = ObservableString("name", task.name)
5253
private val milestoneOption = ObservableBoolean("milestone", task.isMilestone)
@@ -67,6 +68,8 @@ class MainPropertiesPanel(private val task: Task, private val taskView: TaskView
6768
it.putValue(GPAction.TEXT_DISPLAY, ContentDisplay.TEXT_ONLY)
6869
it.isEnabled = earliestStartOption.isWritable.value
6970
}
71+
private var onRequestFocus = {}
72+
7073
private fun onHasEarliestStartChange(hasEarliestStart: Boolean) {
7174
earliestStartOption.setWritable(hasEarliestStart)
7275
copyStartDateAction.isEnabled = hasEarliestStart
@@ -78,8 +81,8 @@ class MainPropertiesPanel(private val task: Task, private val taskView: TaskView
7881
onHasEarliestStartChange(hasEarliestStart.value)
7982
}
8083

81-
fun getFxNode() = StackPane().apply {
82-
background = Background(BackgroundFill("Panel.background".colorFromUiManager(), CornerRadii.EMPTY, Insets.EMPTY))
84+
private fun getFxNode() = StackPane().apply {
85+
background = Background(BackgroundFill("Panel.background".colorFromUiManager(), CornerRadii.EMPTY, Insets.EMPTY))
8386
val leftPane = PropertySheetBuilder(i18n).pane {
8487
stylesheet("/biz/ganttproject/task/TaskPropertiesDialog.css")
8588
title("section.main")
@@ -134,6 +137,8 @@ class MainPropertiesPanel(private val task: Task, private val taskView: TaskView
134137
}
135138
}
136139
}
140+
onRequestFocus = leftPane::requestFocus
141+
137142
val grid = GridPane()
138143

139144
val rightPane = PropertySheetBuilder(i18n).pane {
@@ -200,6 +205,8 @@ class MainPropertiesPanel(private val task: Task, private val taskView: TaskView
200205
taskMutator.setShape(value.paint)
201206
}
202207
}
208+
209+
fun requestFocus() = onRequestFocus()
203210
}
204211

205212
private fun Task.canBeMilestone() = this.nestedTasks.isEmpty()

0 commit comments

Comments
 (0)