Skip to content

Commit

Permalink
improv: Prevent editing of inclusions when selection is empty and cle…
Browse files Browse the repository at this point in the history
…ar inclusions when selection is empty
  • Loading branch information
Griefed committed Sep 22, 2023
1 parent 0e40be4 commit 234919a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ open class ScrollTextField(
private val undoManager = UndoManager()
val suggestionProvider: SuggestionProvider?

var isEditable: Boolean
get() {
return textField.isEditable
}
set(value) {
textField.isEditable = value
}
var text: String
get() {
return textField.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import java.io.File
import java.util.regex.PatternSyntaxException
import javax.swing.*
import javax.swing.event.DocumentEvent
import javax.swing.event.ListDataEvent
import javax.swing.event.ListDataListener
import javax.swing.event.ListSelectionEvent


Expand Down Expand Up @@ -191,27 +193,64 @@ class InclusionsEditor(
list.addMouseListener(object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
super.mouseClicked(e)
clearselection(e)
clearSelection(e)
}

override fun mousePressed(e: MouseEvent) {
super.mousePressed(e)
clearselection(e)
clearSelection(e)
}

override fun mouseReleased(e: MouseEvent) {
super.mouseReleased(e)
clearselection(e)
clearSelection(e)
}

fun clearselection(e: MouseEvent) {
fun clearSelection(e: MouseEvent) {
val index = list.locationToIndex(e.point)
val bounds = list.getCellBounds(index, index)
if (bounds == null || !bounds.contains(e.point)) {
list.clearSelection()
if (bounds == null || !bounds.contains(e.point) || list.model.size <= 0) {
emtpySelection()
} else {
enableInputs()
}
}
})
list.model.addListDataListener(object : ListDataListener {
override fun intervalAdded(e: ListDataEvent?) {
checkSize()
}

override fun intervalRemoved(e: ListDataEvent?) {
checkSize()
}

override fun contentsChanged(e: ListDataEvent?) {
checkSize()
}

fun checkSize() {
if (list.model.size <= 0 || list.isSelectionEmpty) {
emtpySelection()
}
}
})
if (list.model.size <= 0) {
emtpySelection()
}
}

private fun emtpySelection() {
list.clearSelection()
tip.text = Gui.createserverpack_gui_inclusions_editor_tip_default.toString()
source.isEditable = false
destination.isEditable = false
inclusionFilter.isEditable = false
exclusionFilter.isEditable = false
source.text = ""
destination.text = ""
inclusionFilter.text = ""
exclusionFilter.text = ""
}

/**
Expand Down Expand Up @@ -266,78 +305,94 @@ class InclusionsEditor(
fun updateIndex() {
if (!list.valueIsAdjusting) {
list.selectedIndex = 0
enableInputs()
}
}

private fun enableInputs() {
source.isEditable = true
destination.isEditable = true
inclusionFilter.isEditable = true
exclusionFilter.isEditable = true
}

/**
* @author Griefed
*/
fun sourceWasEdited() {
if (File(configEditor.getModpackDirectory(), source.text).exists() || File(source.text).exists()) {
list.selectedValue.source = source.text
timer.restart()
list.updateUI()
sourceInfo.info()
} else {
timer.stop()
sourceInfo.error(Gui.createserverpack_gui_inclusions_editor_source_error(source.text))
if (list.model.size > 0 && !list.isSelectionEmpty) {
if (File(configEditor.getModpackDirectory(), source.text).exists() || File(source.text).exists()) {
list.selectedValue.source = source.text
timer.restart()
list.updateUI()
sourceInfo.info()
} else {
timer.stop()
sourceInfo.error(Gui.createserverpack_gui_inclusions_editor_source_error(source.text))
}
configEditor.validateInputFields()
}
configEditor.validateInputFields()
}

/**
* @author Griefed
*/
fun destinationWasEdited() {
if (apiWrapper.stringUtilities.checkForInvalidPathCharacters(destination.text)) {
list.selectedValue.destination = destination.text
destinationInfo.info()
list.updateUI()
} else {
timer.stop()
destinationInfo.error(Gui.createserverpack_gui_inclusions_editor_destination_error(destination.text))
if (list.model.size > 0 && !list.isSelectionEmpty) {
if (apiWrapper.stringUtilities.checkForInvalidPathCharacters(destination.text)) {
list.selectedValue.destination = destination.text
destinationInfo.info()
list.updateUI()
} else {
timer.stop()
destinationInfo.error(Gui.createserverpack_gui_inclusions_editor_destination_error(destination.text))
}
}
}

/**
* @author Griefed
*/
fun inclusionFilterWasEdited() {
try {
inclusionFilter.text.toRegex()
list.selectedValue.inclusionFilter = inclusionFilter.text
timer.restart()
inclusionInfo.info()
list.updateUI()
} catch (ex: PatternSyntaxException) {
timer.stop()
var exception = ex.message ?: ex.description
exception = exception
.replace("\t", "%20")
.replace("\n", "<br>")
.replace(" ", "&nbsp;")
inclusionInfo.error("<html>${Gui.createserverpack_gui_inclusions_editor_filter_error(exception)}</html>")
if (list.model.size > 0 && !list.isSelectionEmpty) {
try {
inclusionFilter.text.toRegex()
list.selectedValue.inclusionFilter = inclusionFilter.text
timer.restart()
inclusionInfo.info()
list.updateUI()
} catch (ex: PatternSyntaxException) {
timer.stop()
var exception = ex.message ?: ex.description
exception = exception
.replace("\t", "%20")
.replace("\n", "<br>")
.replace(" ", "&nbsp;")
inclusionInfo.error("<html>${Gui.createserverpack_gui_inclusions_editor_filter_error(exception)}</html>")
}
}
}

/**
* @author Griefed
*/
fun exclusionFilterWasEdited() {
try {
exclusionFilter.text.toRegex()
list.selectedValue.exclusionFilter = exclusionFilter.text
timer.restart()
exclusionInfo.info()
list.updateUI()
} catch (ex: PatternSyntaxException) {
timer.stop()
var exception = ex.message ?: ex.description
exception = exception
.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;")
.replace("\n", "<br>")
.replace(" ", "&nbsp;")
exclusionInfo.error("<html>${Gui.createserverpack_gui_inclusions_editor_filter_error(exception)}</html>")
if (list.model.size > 0 && !list.isSelectionEmpty) {
try {
exclusionFilter.text.toRegex()
list.selectedValue.exclusionFilter = exclusionFilter.text
timer.restart()
exclusionInfo.info()
list.updateUI()
} catch (ex: PatternSyntaxException) {
timer.stop()
var exception = ex.message ?: ex.description
exception = exception
.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;")
.replace("\n", "<br>")
.replace(" ", "&nbsp;")
exclusionInfo.error("<html>${Gui.createserverpack_gui_inclusions_editor_filter_error(exception)}</html>")
}
}
}

Expand All @@ -347,7 +402,10 @@ class InclusionsEditor(
private fun selectionOccurred(event: ListSelectionEvent) {
when {
event.valueIsAdjusting -> return
list.selectedIndex == -1 -> return
list.selectedIndex == -1 || list.model.size <= 0 -> {
emtpySelection()
return
}
}
selectedInclusion = list.selectedValue
source.text = selectedInclusion!!.source
Expand Down Expand Up @@ -412,13 +470,23 @@ class InclusionsEditor(
private fun addEntry(entry: InclusionSpecification) {
inclusionModel.addElement(entry)
list.selectedIndex = list.lastVisibleIndex
if (list.model.size == 1) {
list.selectedIndex = 0
}
}

/**
* @author Griefed
*/
private fun removeEntry(index: Int): InclusionSpecification {
return inclusionModel.remove(index)
val removed = inclusionModel.remove(index)
if (list.model.size == 1) {
list.selectedIndex = 0
}
if (list.model.size <= 0) {
emtpySelection()
}
return removed
}

/**
Expand Down

0 comments on commit 234919a

Please sign in to comment.