Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(admin): add launcher instances dropdown selection feature #9

Merged
merged 10 commits into from
Jun 26, 2024
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Use the manual or automated method to get started 🚀:
Enter it and then wait for the sync process to finish, the game will launch with the new synced
content.
8. To make this process easier for all the players, export the instance from **Step 5** that's configured
to use the script, make sure to include the used JAR file and the `kraft-sync-data` folder
to use the script, make sure to include the `kraft-sync.jar` file and the `kraft-sync-data` folder
and exclude the content that will be synced like the `mods` as the script will download them once the player import
the instance and launch the instance, you can include them though, the script will sync them if they're outdated.

Expand Down
7 changes: 5 additions & 2 deletions admin/src/main/kotlin/gui/AdminMainWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ class AdminMainWindow : BaseJFrame() {
init {
title = "${ProjectInfoConstants.DISPLAY_NAME} Admin"
defaultCloseOperation = EXIT_ON_CLOSE
minimumSize = Dimension(300, 300)
preferredSize = Dimension(450, 350)

val size = Dimension(450, 350)

minimumSize = size
preferredSize = size

val tabbedPane = JTabbedPane()

Expand Down
29 changes: 0 additions & 29 deletions admin/src/main/kotlin/gui/components/FilePicker.kt

This file was deleted.

134 changes: 134 additions & 0 deletions admin/src/main/kotlin/gui/components/InstanceDirectoryInputField.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package gui.components

import gui.utils.ComboItem
import gui.utils.GuiUtils
import gui.utils.getSelectedItemOrThrow
import gui.utils.handleResult
import gui.utils.onClick
import gui.utils.onItemChanged
import gui.utils.onItemSelected
import gui.utils.row
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import launchers.Instance
import launchers.LauncherDataSource
import launchers.LauncherDataSourceFactory
import launchers.MinecraftLauncher
import utils.buildHtml
import java.awt.Component
import java.awt.Dimension
import javax.swing.JButton
import javax.swing.JComboBox
import javax.swing.JComponent
import javax.swing.JFileChooser
import javax.swing.JTextField
import javax.swing.plaf.basic.BasicComboBoxEditor

fun instanceDirectoryInputField(
inputComboBox: JComboBox<ComboItem<Instance>>,
launcherComboBox: JComboBox<MinecraftLauncher>,
parentComponent: JComponent,
preferredLabelWidth: Int,
): JComponent =
labeledInputField(
labelText = "Instance directory",
tooltipText =
buildHtml {
text("The launcher instance directory.")
newLine()
text("Usually it's same path when you click ")
boldText("Open Instance Folder")
text(" for the selected instance in the launcher.")
newLines(2)
text("If the root instance folder does not have Minecraft specific files and folders")
newLine()
text("like mods, options.txt, resourcepacks and others. Navigate to ")
boldText(".minecraft")
newLine()
text("folder which is usually inside the instance/profile folder.")
newLines(2)
text(
"You can also choose the instance directly using the dropdown menu, this will only work",
)
newLine()
text("if the selected launcher store the data in a known place and the launcher is installed on the system.")
}.buildBodyAsText(),
inputComponent =
row(
inputComboBox.apply {
isEditable = true

// Set custom editor to limit the width of the combo box text field
editor =
object : BasicComboBoxEditor() {
override fun getEditorComponent(): Component {
val editorComponent = super.getEditorComponent() as JTextField
editorComponent.preferredSize =
Dimension(150, editorComponent.preferredSize.height)
editorComponent.toolTipText = editorComponent.text
return editorComponent
}
}

fun setDropdownItems() {
val launcherDataSource: LauncherDataSource? =
LauncherDataSourceFactory.getHandlerOrNull(launcherComboBox.getSelectedItemOrThrow())

removeAllItems()

CoroutineScope(Dispatchers.IO).launch {
launcherDataSource?.getInstances()?.getOrNull()?.forEach {
addItem(
ComboItem(
value = it,
label = it.instanceName,
),
)
}
}
}
setDropdownItems()

launcherComboBox.onItemChanged { _, _ ->
setDropdownItems()
}

onItemSelected { item, _ ->
// Change the selected item value to the path as this will be handled as Text Field

if (item == null) {
// Calling removeAllItems() will update the selected item to be null
return@onItemSelected
}

// This will be triggered twice upon an item change, the first will be the item type,
// The second which is changed in this lambda, which is why we need to check to avoid
// unexpected error on the second change. The (item) will be null if the selected item
// is null or is different from the JComboBox<E> type of the elements
selectedItem = item.value.launcherInstanceDirectory.absolutePath
}
},
JButton("Browse").onClick {
val fileChooser =
JFileChooser().apply {
dialogTitle = "Choose the instance directory"
fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
}
val result = fileChooser.showOpenDialog(parentComponent)
val selectedFile =
fileChooser.handleResult(
result = result,
onErrorWhileChoosingFile = {
GuiUtils.showErrorMessage(
title = "Unexpected Error",
message = "An error occurred while trying to pick the launcher instance directory.",
parentComponent = parentComponent,
)
},
) ?: return@onClick
inputComboBox.selectedItem = selectedFile.absolutePath
},
),
preferredLabelWidth = preferredLabelWidth,
)
47 changes: 0 additions & 47 deletions admin/src/main/kotlin/gui/components/InstanceDirectoryTextField.kt

This file was deleted.

Loading