Skip to content

Commit

Permalink
#132: improves naming, logging for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Nov 27, 2023
1 parent b074676 commit 151f81e
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 80 deletions.
2 changes: 1 addition & 1 deletion app/src/main/scala/app/logorrr/LogoRRRApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object LogoRRRApp extends CanLog {
class LogoRRRApp extends javafx.application.Application with CanLog {

def start(stage: Stage): Unit = {
// make sure to set css before anything is initalized otherwise the rules won't apply
// make sure to set css before anything is initialized otherwise the rules won't apply
Application.setUserAgentStylesheet("/app/logorrr/LogoRRR.css")
val settings: Settings = SettingsIO.fromFile()
LogoRRRGlobals.set(settings, getHostServices)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/scala/app/logorrr/util/JfxUtils.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.logorrr.util

import javafx.application.Platform
import javafx.beans.{InvalidationListener, Observable}
import javafx.beans.value.{ChangeListener, ObservableValue}
import javafx.collections.ListChangeListener
import javafx.scene.control.ListView
Expand Down Expand Up @@ -34,6 +35,7 @@ object JfxUtils extends CanLog {
)
}

def mkInvalidationListener(invalidated: Observable => Unit): InvalidationListener = (observable: Observable) => invalidated(observable)

def onNew[T](f: T => Unit): ChangeListener[T] = (_: ObservableValue[_ <: T], _: T, t1: T) => f(t1)

Expand Down
63 changes: 32 additions & 31 deletions app/src/main/scala/app/logorrr/views/block/ChunkListView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import app.logorrr.model.LogEntry
import app.logorrr.util.{CanLog, JfxUtils}
import app.logorrr.views.search.Filter
import javafx.beans.property.{SimpleDoubleProperty, SimpleIntegerProperty}
import javafx.beans.{InvalidationListener, Observable}
import javafx.collections.{FXCollections, ObservableList}
import javafx.scene.control.ListView

Expand Down Expand Up @@ -49,18 +48,28 @@ class ChunkListView(val logEntries: ObservableList[LogEntry]
extends ListView[Chunk]
with CanLog {

private val repaintInvalidationListener = new InvalidationListener {
override def invalidated(observable: Observable): Unit = {
repaint()
}
}

val repaintChangeListener = JfxUtils.onNew[Number](_ => repaint())
val refreshListener = JfxUtils.onNew[Number](_ => {
repaint()
})
private val repaintInvalidationListener = JfxUtils.mkInvalidationListener(_ => calculateItems("invalidation"))

val selectedRp = repaintChangeListener("selected")
val dividersRp = repaintChangeListener("dividers")
val blockSizeRp = repaintChangeListener("blockSize")
val widthRp = repaintChangeListener("width")
val heightRp = repaintChangeListener("height")

def repaintChangeListener(ctx: String) = JfxUtils.onNew[Number](n => calculateItems(ctx + s" (${n.intValue()})"))

def init(): Unit = {
getStylesheets.add(getClass.getResource("/app/logorrr/ChunkListView.css").toExternalForm)

setCellFactory((lv: ListView[Chunk]) =>
new ChunkListCell(selectedLineNumberProperty
, lv.widthProperty()
, blockSizeProperty
, filtersProperty
, selectInTextView)
)

addListeners()
}

Expand Down Expand Up @@ -90,31 +99,23 @@ class ChunkListView(val logEntries: ObservableList[LogEntry]

private def addListeners(): Unit = {
logEntries.addListener(repaintInvalidationListener)

selectedLineNumberProperty.addListener(refreshListener)
dividersProperty.addListener(repaintChangeListener)
blockSizeProperty.addListener(repaintChangeListener)
widthProperty().addListener(repaintChangeListener)
heightProperty().addListener(repaintChangeListener)
selectedLineNumberProperty.addListener(selectedRp)
dividersProperty.addListener(dividersRp)
blockSizeProperty.addListener(blockSizeRp)
widthProperty().addListener(widthRp)
heightProperty().addListener(heightRp)
}

def removeListeners(): Unit = {
logEntries.removeListener(repaintInvalidationListener)

selectedLineNumberProperty.addListener(refreshListener)
dividersProperty.removeListener(repaintChangeListener)
blockSizeProperty.removeListener(repaintChangeListener)
widthProperty().removeListener(repaintChangeListener)
heightProperty().removeListener(repaintChangeListener)
selectedLineNumberProperty.removeListener(selectedRp)
dividersProperty.removeListener(dividersRp)
blockSizeProperty.removeListener(blockSizeRp)
widthProperty().removeListener(widthRp)
heightProperty().removeListener(heightRp)
}

getStylesheets.add(getClass.getResource("/app/logorrr/ChunkListView.css").toExternalForm)

setCellFactory((lv: ListView[Chunk]) => new ChunkListCell(selectedLineNumberProperty
, lv.widthProperty()
, blockSizeProperty
, filtersProperty
, selectInTextView))

/**
* Repaint method takes care of painting the blocks on the raw byte array.
Expand All @@ -123,9 +124,9 @@ class ChunkListView(val logEntries: ObservableList[LogEntry]
* if not all prerequisites are met. Also, this has to run on the javafx thread such that those updates are displayed
* correctly.
*/
// do not remove JfxUtils.execOnUiThread
def repaint(): Unit = {
def calculateItems(ctx: String): Unit = {
if (widthProperty().get() > 0 && heightProperty.get() > 0 && blockSizeProperty.get() > 0) {
logTrace(s"repaint $ctx")
Try {
val chunks = Chunk.mkChunks(logEntries, widthProperty, blockSizeProperty, heightProperty)
setItems(FXCollections.observableArrayList(chunks: _*))
Expand All @@ -135,7 +136,7 @@ class ChunkListView(val logEntries: ObservableList[LogEntry]
}
refresh()
} else {
val msg = s"Not repainted. (width: ${widthProperty().get()}, blockSize: ${blockSizeProperty.get()}, height: ${heightProperty().get()})"
val msg = s"repaint NOT: (ctx: $ctx, width: ${widthProperty().get()}, blockSize: ${blockSizeProperty.get()}, height: ${heightProperty().get()})"
logTrace(msg)
}
}
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/scala/app/logorrr/views/logfiletab/LogFileTab.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,17 @@ class LogFileTab(val mutLogFileSettings: MutLogFileSettings
/* change active text field depending on visible tab */
LogoRRRAccelerators.setActiveSearchTextField(mainTabContent.opsToolBar.searchTextField)
LogoRRRAccelerators.setActiveRegexToggleButton(mainTabContent.opsToolBar.regexToggleButton)
repaint()
mainTabContent.repaintChunk()
} else {
setStyle(LogFileTab.BackgroundStyle)
}
})

def repaint(): Unit = mainTabContent.repaintChunk()

def scrollToActiveChunk() :Unit = mainTabContent.scrollToActiveChunk()
def scrollToActiveElementAndRepaint() :Unit = {
mainTabContent.repaintChunk()
mainTabContent.scrollToActiveElement()
}

def init(): Unit = timeR({
setTooltip(new LogFileTabToolTip(pathAsString, entries))
Expand All @@ -117,8 +119,7 @@ class LogFileTab(val mutLogFileSettings: MutLogFileSettings

setOnSelectionChanged(_ => {
if (isSelected) {
mainTabContent.scrollToActiveChunk()
mainTabContent.repaintChunk()
scrollToActiveElementAndRepaint()
}
})

Expand All @@ -143,7 +144,7 @@ class LogFileTab(val mutLogFileSettings: MutLogFileSettings
Option(getTabPane).foreach(_ => setContextMenu(mkContextMenu()))
case java.lang.Boolean.FALSE => setContextMenu(null)
})
}, s"Loaded `$pathAsString` with ${entries.size()} entries.")
}, s"Init `$pathAsString`")


private def mkContextMenu(): ContextMenu = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class LogFileTabContent(mutLogFileSettings: MutLogFileSettings

private lazy val pane = new SplitPane(chunkListView, logTextView)

private lazy val repaintChunkListViewListener = JfxUtils.onNew[Number](n => {
private lazy val dividerPositionRepaintListener = JfxUtils.onNew[Number](n => {
if (n.doubleValue() > 0.1) {
chunkListView.repaint()
chunkListView.calculateItems("dividerPosition")
}
})

def init(): Unit = {
divider.setPosition(mutLogFileSettings.getDividerPosition())
divider.positionProperty().addListener(repaintChunkListViewListener)
divider.positionProperty().addListener(dividerPositionRepaintListener)

setTop(opsRegion)
setCenter(pane)
Expand All @@ -67,20 +67,22 @@ class LogFileTabContent(mutLogFileSettings: MutLogFileSettings
}


def divider: SplitPane.Divider = pane.getDividers.get(0)
private def divider: SplitPane.Divider = pane.getDividers.get(0)

def getDividerPosition = divider.getPosition

def removeListeners(): Unit = {
divider.positionProperty().removeListener(repaintChunkListViewListener)
divider.positionProperty().removeListener(dividerPositionRepaintListener)
chunkListView.removeListeners()
}

def addFilter(filter: Filter): Unit = mutLogFileSettings.filtersProperty.add(filter)

def removeFilter(filter: Filter): Unit = mutLogFileSettings.filtersProperty.remove(filter)

def repaintChunk(): Unit = chunkListView.repaint()
def repaintChunk(): Unit = chunkListView.calculateItems("rrrepaint")

def scrollToActiveChunk() = {
def scrollToActiveElement(): Unit = {
chunkListView.scrollToActiveChunk()
logTextView.scrollToActiveLogEntry()
}
Expand Down
31 changes: 14 additions & 17 deletions app/src/main/scala/app/logorrr/views/main/LogoRRRMain.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package app.logorrr.views.main

import app.logorrr.conf.LogoRRRGlobals
import app.logorrr.model.{LogEntry, LogFileSettings}
import app.logorrr.model.LogFileSettings
import app.logorrr.util.{CanLog, JfxUtils}
import app.logorrr.views.logfiletab.LogFileTab
import javafx.collections.ObservableList
import javafx.scene.layout.BorderPane
import javafx.stage.Window

Expand Down Expand Up @@ -39,33 +38,33 @@ class LogoRRRMain(closeStage: => Unit) extends BorderPane with CanLog {

private def loadLogFiles(settings: Seq[LogFileSettings]): Unit = {
val futures: Future[Seq[LogFileTab]] = Future.sequence {
logInfo(s"Loading ${settings.length} log files: ${settings.map(_.pathAsString).mkString("['", "',`'", "']")}")
val filtered =
settings.map(lfs => Future {
val tab = new LogFileTab(LogoRRRGlobals.getLogFileSettings(lfs.pathAsString), lfs.readEntries())
val entries = lfs.readEntries()
val tab = new LogFileTab(LogoRRRGlobals.getLogFileSettings(lfs.pathAsString), entries)
tab.init()
logTrace(s"Loaded ${lfs.pathAsString} with ${entries.size()} lines ... ")
tab
})
filtered
}
val lfs = Await.result(futures, Duration.Inf)
logTrace("Loaded " + lfs.size + " files ... ")


println("no jfx thread before this")
lfs.foreach(tab => mainTabPane.addLogFileTab(tab))

// only after loading all files we initialize the 'add' listener
// otherwise we would overwrite the active log everytime
mainTabPane.initSelectionListener()

// after loading everything, set active log like specified in the config file
// select log has to be performed on the fx thread
LogoRRRGlobals.getSomeActive.foreach(pathAsString => {
JfxUtils.execOnUiThread({
val lft = selectLog(pathAsString)
lft.scrollToActiveChunk()
lft.repaint()
JfxUtils.execOnUiThread {
lfs.foreach(tab => mainTabPane.addLogFileTab(tab))
// after loading everything, set active log like specified in the config file
// select log has to be performed on the fx thread
LogoRRRGlobals.getSomeActive.foreach(pathAsString => {
selectLog(pathAsString).scrollToActiveElementAndRepaint()
})
})
}
}

/** called when 'Open File' is selected. */
Expand All @@ -75,9 +74,7 @@ class LogoRRRMain(closeStage: => Unit) extends BorderPane with CanLog {
if (!mainTabPane.contains(pathAsString)) {
mainTabPane.addLogFile(path)
} else {
val lft = mainTabPane.selectLog(pathAsString)
lft.scrollToActiveChunk()
lft.repaint()
mainTabPane.selectLog(pathAsString).scrollToActiveElementAndRepaint()
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ case class LogoRRRStage(stage: Stage) extends CanLog {
// to save global filter state
val activeFilters =
(for (logFileTab <- logorrrMain.getLogFileTabs) yield {
logFileTab.pathAsString -> (logFileTab.mainTabContent.activeFilters, logFileTab.mainTabContent.divider.positionProperty().get())
logFileTab.pathAsString -> (logFileTab.mainTabContent.activeFilters, logFileTab.mainTabContent.getDividerPosition)
}).toMap

val updatedSettings =
Expand Down
24 changes: 10 additions & 14 deletions app/src/main/scala/app/logorrr/views/main/MainTabPane.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ object MainTabPane {
}

class MainTabPane extends TabPane with CanLog {

// leave her otherwise css rendering breaks
setStyle(MainTabPane.BackgroundStyle)

val selectedLogFileTab: ChangeListener[Tab] = JfxUtils.onNew {
val selectedTabListener: ChangeListener[Tab] = JfxUtils.onNew {
case logFileTab: LogFileTab =>
logTrace(s"Selected: ${logFileTab.pathAsString}")
LogoRRRGlobals.setSomeActive(Option(logFileTab.pathAsString))
Expand All @@ -38,8 +38,8 @@ class MainTabPane extends TabPane with CanLog {
case x => getSelectionModel.select(null)
}


def init(): Unit = {

initDnD()
}

Expand All @@ -48,13 +48,7 @@ class MainTabPane extends TabPane with CanLog {
* Defines what should happen when a tab is selected
* */
def initSelectionListener(): Unit = {
getSelectionModel.selectedItemProperty().addListener(selectedLogFileTab)
}


def add(tab: LogFileTab): Unit = {
getTabs.add(tab)
setStyle(null)
getSelectionModel.selectedItemProperty().addListener(selectedTabListener)
}

def contains(p: String): Boolean = getLogFileTabs.exists(lr => lr.pathAsString == p)
Expand All @@ -68,7 +62,7 @@ class MainTabPane extends TabPane with CanLog {

/** shutdown all tabs */
def shutdown(): Unit = {
getSelectionModel.selectedItemProperty().removeListener(selectedLogFileTab)
getSelectionModel.selectedItemProperty().removeListener(selectedTabListener)
getLogFileTabs.foreach(t => {
t.shutdown()
LogoRRRGlobals.removeLogFile(t.pathAsString)
Expand All @@ -77,7 +71,7 @@ class MainTabPane extends TabPane with CanLog {
}

def selectLog(pathAsString: String): LogFileTab = {
logTrace(s"selecting tab `$pathAsString` ...")
Thread.sleep(2000)
getLogFileTabs.find(_.pathAsString == pathAsString) match {
case Some(logFileTab) =>
logTrace(s"Activated tab for `$pathAsString`.")
Expand Down Expand Up @@ -139,7 +133,9 @@ class MainTabPane extends TabPane with CanLog {


/** Adds a new logfile to display */
def addLogFileTab(tab: LogFileTab): Unit = JfxUtils.execOnUiThread(add(tab))

def addLogFileTab(tab: LogFileTab): Unit = {
getTabs.add(tab)
setStyle(null)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ChunkListTestApp extends Application with CanLog {

val refreshListener = JfxUtils.onNew[Number](n => {
if (n.doubleValue() > 0.1) {
clv.repaint()
clv.calculateItems("testapp")
}
})

Expand All @@ -95,8 +95,8 @@ class ChunkListTestApp extends Application with CanLog {

stage.showingProperty().addListener((_, _, isNowShowing) => {
if (isNowShowing) {
clv.repaint()
System.out.println("Scene is loaded and displayed!")
clv.calculateItems("testapp")
logTrace("Scene is loaded and displayed!")
}
})

Expand Down

0 comments on commit 151f81e

Please sign in to comment.