Skip to content

Commit

Permalink
#188: moves tab actions in own package, simplifies tab shutdown code
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Dec 27, 2023
1 parent cd3a52a commit accddbb
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import app.logorrr.model.LogEntry
import app.logorrr.util._
import app.logorrr.views.LogoRRRAccelerators
import app.logorrr.views.autoscroll.LogTailer
import app.logorrr.views.logfiletab.actions.{CloseAllFilesMenuItem, CloseLeftFilesMenuItem, CloseMenuItem, CloseOtherFilesMenuItem, CloseRightFilesMenuItem, OpenInFinderMenuItem}
import app.logorrr.views.search.Fltr
import javafx.beans.binding.Bindings
import javafx.collections.{ListChangeListener, ObservableList}
Expand Down Expand Up @@ -114,7 +115,7 @@ class LogFileTab(val mutLogFileSettings: MutLogFileSettings
logFileTabContent.init()

/** don't monitor file anymore if tab is closed, free listeners */
setOnCloseRequest((_: Event) => cleanupBeforeClose())
setOnCloseRequest((_: Event) => shutdown())

if (mutLogFileSettings.isAutoScrollActive) {
startTailer()
Expand Down Expand Up @@ -166,11 +167,6 @@ class LogFileTab(val mutLogFileSettings: MutLogFileSettings
menu
}

def cleanupBeforeClose(): Unit = {
shutdown()
LogoRRRGlobals.removeLogFile(pathAsString)
}

private def addListeners(): Unit = {
selectedProperty().addListener(selectedListener)

Expand All @@ -195,6 +191,7 @@ class LogFileTab(val mutLogFileSettings: MutLogFileSettings
stopTailer()
}
removeListeners()
LogoRRRGlobals.removeLogFile(pathAsString)
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package app.logorrr.views.logfiletab.actions

import app.logorrr.views.logfiletab.LogFileTab
import javafx.scene.control.{MenuItem, Tab}

import scala.jdk.CollectionConverters.CollectionHasAsScala

class CloseAllFilesMenuItem(fileTab: => LogFileTab) extends MenuItem("Close All Files") {
private val tabPane = fileTab.getTabPane
setOnAction(_ => {
val toBeDeleted: Seq[Tab] = {
tabPane.getTabs.asScala.flatMap { t =>
t.asInstanceOf[LogFileTab].shutdown()
Option(t)
}.toSeq
}
tabPane.getTabs.removeAll(toBeDeleted: _*)
})

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package app.logorrr.views.logfiletab.actions

import app.logorrr.views.logfiletab.LogFileTab
import javafx.scene.control.{MenuItem, Tab}

import scala.jdk.CollectionConverters.CollectionHasAsScala

class CloseLeftFilesMenuItem(fileTab: => LogFileTab) extends MenuItem("Close Files to the Left") {
private val tabPane = fileTab.getTabPane
setOnAction(_ => {
var deletethem = true
val toBeDeleted: Seq[Tab] = {
tabPane.getTabs.asScala.flatMap { t =>
if (t.asInstanceOf[LogFileTab].pathAsString == fileTab.pathAsString) {
deletethem = false
None
} else {
if (deletethem) {
t.asInstanceOf[LogFileTab].shutdown()
Option(t)
} else {
None
}
}
}.toSeq
}
tabPane.getTabs.removeAll(toBeDeleted: _*)
})

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.logorrr.views.logfiletab.actions

import app.logorrr.views.logfiletab.LogFileTab
import javafx.scene.control.MenuItem

class CloseMenuItem(fileTab: => LogFileTab) extends MenuItem("Close") {

setOnAction(_ => Option(fileTab.getTabPane.getSelectionModel.getSelectedItem).foreach { t =>
fileTab.shutdown()
fileTab.getTabPane.getTabs.remove(t)
})

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app.logorrr.views.logfiletab.actions

import app.logorrr.views.logfiletab.LogFileTab
import javafx.scene.control.{MenuItem, Tab}

import scala.jdk.CollectionConverters.CollectionHasAsScala

class CloseOtherFilesMenuItem(fileTab: => LogFileTab) extends MenuItem("Close Other Files") {
private val tabPane = fileTab.getTabPane
setOnAction(_ => {
val toBeDeleted: Seq[Tab] = {
tabPane.getTabs.asScala.flatMap { t =>
if (t.asInstanceOf[LogFileTab].pathAsString == fileTab.pathAsString) {
None
} else {
t.asInstanceOf[LogFileTab].shutdown()
Option(t)
}
}.toSeq
}
tabPane.getTabs.removeAll(toBeDeleted: _*)
})

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package app.logorrr.views.logfiletab.actions

import app.logorrr.views.logfiletab.LogFileTab
import javafx.scene.control.{MenuItem, Tab}

import scala.jdk.CollectionConverters.CollectionHasAsScala


class CloseRightFilesMenuItem(fileTab: => LogFileTab) extends MenuItem("Close Files to the Right") {
private val tabPane = fileTab.getTabPane
setOnAction(_ => {
var deletethem = false
val toBeDeleted: Seq[Tab] = {
tabPane.getTabs.asScala.flatMap { t =>
if (t.asInstanceOf[LogFileTab].pathAsString == fileTab.pathAsString) {
deletethem = true
None
} else {
if (deletethem) {
t.asInstanceOf[LogFileTab].shutdown()
Option(t)
} else {
None
}
}
}.toSeq
}
tabPane.getTabs.removeAll(toBeDeleted: _*)
})

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package app.logorrr.views.logfiletab.actions

import app.logorrr.util.OsUtil
import javafx.scene.control.MenuItem

import java.awt.Desktop
import java.nio.file.Paths

object OpenInFinderMenuItem {
val menuItemText: String = if (OsUtil.isWin) {
"Show Log in Explorer"
} else if (OsUtil.isMac) {
"Show Log in Finder"
} else {
"Show Log ..."
}
}

class OpenInFinderMenuItem(pathAsString: String) extends MenuItem(OpenInFinderMenuItem.menuItemText) {

setOnAction(_ => Desktop.getDesktop.open(Paths.get(pathAsString).getParent.toFile))

}
6 changes: 1 addition & 5 deletions app/src/main/scala/app/logorrr/views/main/MainTabPane.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class MainTabPane extends TabPane with CanLog {
initDnD()
}


/**
* Defines what should happen when a tab is selected
* */
Expand All @@ -62,10 +61,7 @@ class MainTabPane extends TabPane with CanLog {
/** shutdown all tabs */
def shutdown(): Unit = {
getSelectionModel.selectedItemProperty().removeListener(selectedTabListener)
getLogFileTabs.foreach(t => {
t.shutdown()
LogoRRRGlobals.removeLogFile(t.pathAsString)
})
getLogFileTabs.foreach(_.shutdown())
getTabs.clear()
}

Expand Down

0 comments on commit accddbb

Please sign in to comment.