Skip to content

Commit

Permalink
#150: simplifies code
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Oct 24, 2023
1 parent 9a63ea0 commit 8a3dc79
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 55 deletions.
15 changes: 8 additions & 7 deletions app/src/main/scala/app/logorrr/views/block/BlockImage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ object BlockImage {
class BlockImage(name: String
, widthProperty: SimpleIntegerProperty
, blockSizeProperty: SimpleIntegerProperty
, entriesProperty: SimpleListProperty[LogEntry]
, entriesProperty: java.util.List[LogEntry]
, filtersProperty: SimpleListProperty[Filter]
, selectedElemProperty: SimpleObjectProperty[LogEntry]) extends CanLog {

var lpb: LPixelBuffer = _

private var lpb: LPixelBuffer = _
/**
* height property is calculated on the fly depending on the blockwidth/blockheight,
* width of BlockImage, number of elements and max size of possible of texture (4096).
Expand All @@ -40,8 +39,8 @@ class BlockImage(name: String
val imageProperty = new SimpleObjectProperty[WritableImage]()

private val heightListener: ChangeListener[Number] = JfxUtils.onNew[Number](height => {
// logTrace("heightListener " + height)
resetBackingImage(getWidth, height.intValue)
// logTrace("heightListener " + height)
setImage(getWidth, height.intValue)
})

private val blockSizeListener: InvalidationListener = (_: Observable) => {
Expand Down Expand Up @@ -80,7 +79,7 @@ class BlockImage(name: String
}


private def resetBackingImage(width: Int, height: Int): Unit = {
private def setImage(width: Int, height: Int): Unit = {
lpb = LPixelBuffer(width
, height
, blockSizeProperty
Expand All @@ -92,10 +91,12 @@ class BlockImage(name: String
}

// todo check visibility
def repaint(ctx: String): Unit = lpb.repaint(ctx, filtersProperty.asScala.toSeq, selectedElemProperty.get())
def repaint(ctx: String): Unit = Option(lpb).foreach(_.repaint(ctx, filtersProperty.asScala.toSeq, selectedElemProperty.get()))

def setHeight(height: Int): Unit = heightProperty.set(height)

def getWidth: Int = widthProperty.get()

def draw(i: Int, color: Color): Unit = lpb.draw(i, color)

}
43 changes: 20 additions & 23 deletions app/src/main/scala/app/logorrr/views/block/BlockView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ class BlockView(name: String
, filtersProperty: SimpleListProperty[Filter]
, blockSizeProperty: SimpleIntegerProperty
, outerWidthProperty: ReadOnlyDoubleProperty
, selectedElemProperty: SimpleObjectProperty[LogEntry]) extends ImageView with CanLog {
, selectedElemProperty: SimpleObjectProperty[LogEntry]
, entries: java.util.List[LogEntry]) extends ImageView with CanLog {

private val widthProperty = new SimpleIntegerProperty(outerWidthProperty.get().intValue())

val entriesProperty = new SimpleListProperty[LogEntry](FXCollections.observableArrayList())

private val onClickListener: ChangeListener[LogEntry] = JfxUtils.onNew {
logEntry =>
selectedElemProperty.set(logEntry)
val i = entriesProperty.indexOf(logEntry)
val i = entries.indexOf(logEntry)
if (i >= 0) {
blockImage.lpb.draw(i, Color.YELLOW)
blockImage.draw(i, Color.YELLOW)
} else {
logWarn("onClickListener " + i)
}
Expand All @@ -80,7 +80,7 @@ class BlockView(name: String
private val selectedLineNumberListener = new ChangeListener[Number] {
override def changed(observableValue: ObservableValue[_ <: Number], t: Number, t1: Number): Unit = {
if (t.intValue() != t1.intValue()) {
blockImage.lpb.draw(t1.intValue(), Color.YELLOW)
blockImage.draw(t1.intValue(), Color.YELLOW)
}
}
}
Expand All @@ -90,7 +90,7 @@ class BlockView(name: String
private val blockImage = new BlockImage(name
, widthProperty
, blockSizeProperty
, entriesProperty
, entries
, filtersProperty
, selectedElemProperty)

Expand All @@ -101,31 +101,30 @@ class BlockView(name: String
if (proposedWidth > BlockView.MinWidth) {
setWidth(proposedWidth)
} else {
// logTrace(s"Proposed width ($proposedWidth) < SQView.MinWidth (${BlockView.MinWidth}), not adjusting width of canvas ...")
// logTrace(s"Proposed width ($proposedWidth) < SQView.MinWidth (${BlockView.MinWidth}), not adjusting width of canvas ...")
}
} else {
// logTrace(s"ScrollPaneWidth ($scrollPaneWidth) >= SQImage.MaxWidth (${BlockImage.MaxWidth}), not adjusting width of canvas ...")
// logTrace(s"ScrollPaneWidth ($scrollPaneWidth) >= SQImage.MaxWidth (${BlockImage.MaxWidth}), not adjusting width of canvas ...")
}
})

val mouseEventHandler = new EventHandler[MouseEvent]() {
override def handle(me: MouseEvent): Unit = {
val index = BlockView.indexOf(me.getX.toInt, me.getY.toInt, blockSizeProperty.get, widthProperty.get)
getEntryAt(index) match {
case Some(value) => selectedEntryProperty.set(value)
case None => System.err.println("no element found")
}
}
}


init()

def init(): Unit = {

setOnMouseClicked(new EventHandler[MouseEvent]() {
override def handle(me: MouseEvent): Unit = {
val index = BlockView.indexOf(me.getX.toInt, me.getY.toInt, blockSizeProperty.get, widthProperty.get)
getEntryAt(index) match {
case Some(value) => selectedEntryProperty.set(value)
case None => System.err.println("no element found")
}
}
})

setOnMouseClicked(mouseEventHandler)
addListener()
addBindings()

}

private def addBindings(): Unit = {
Expand Down Expand Up @@ -158,9 +157,7 @@ class BlockView(name: String

def setWidth(width: Int): Unit = widthProperty.set(width)

def setEntries(entries: java.util.List[LogEntry]): Unit = entriesProperty.setAll(entries)

private def getEntryAt(index: Int): Option[LogEntry] = Try(entriesProperty.get(index)).toOption
private def getEntryAt(index: Int): Option[LogEntry] = Try(entries.get(index)).toOption

def repaint(ctx: String): Unit = blockImage.repaint(ctx)

Expand Down
13 changes: 9 additions & 4 deletions app/src/main/scala/app/logorrr/views/block/BlockViewPane.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ class BlockViewPane(selectedLineNumberProperty: SimpleIntegerProperty)
, filtersProperty
, blockSizeProperty
, widthProperty
, selectedElemProperty)
, selectedElemProperty
, entriesProperty)
blockView.setWidth(getWidth.toInt)
blockView.setHeight(blockHeight)
blockView.setEntries(entriesProperty)
Seq(blockView)
} else {
// if the virtual canvas height exceeds SQImage.MaxHeight, iterate and create new SQViews
Expand All @@ -132,9 +132,14 @@ class BlockViewPane(selectedLineNumberProperty: SimpleIntegerProperty)
val blockViewEntries = entriesProperty.subList(curIndex, end)
val name = s"${curIndex}_$end"
val blockView =
new BlockView(name, selectedLineNumberProperty, filtersProperty, blockSizeProperty, widthProperty, selectedElemProperty)
new BlockView(name
, selectedLineNumberProperty
, filtersProperty
, blockSizeProperty
, widthProperty
, selectedElemProperty
, blockViewEntries)
blockView.setWidth(getWidth.toInt)
blockView.setEntries(blockViewEntries)
blockView.setHeight(BlockView.calcVirtualHeight(blockSize, blockSize, getWidth.toInt, blockViewEntries.size))
lb.addOne(blockView)
curIndex = curIndex + nrElemsInSqView
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/scala/app/logorrr/views/block/LPixelBuffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import scala.jdk.CollectionConverters.CollectionHasAsScala
case class LPixelBuffer(width: Int
, height: Int
, blockSizeProperty: SimpleIntegerProperty
, entriesProperty: SimpleListProperty[LogEntry]
, entries: java.util.List[LogEntry]
, filtersProperty: SimpleListProperty[Filter]
, selectedEntryProperty: SimpleObjectProperty[LogEntry]
, rawInts: Array[Int]) extends {
Expand Down Expand Up @@ -46,7 +46,7 @@ case class LPixelBuffer(width: Int
updateBuffer((_: PixelBuffer[IntBuffer]) => {
cleanBackground()
var i = 0
entriesProperty.forEach(e => {
entries.forEach(e => {
if (e.equals(selectedEntry)) {
drawRect(i, Color.YELLOW)
} else {
Expand Down
33 changes: 14 additions & 19 deletions app/src/main/scala/app/logorrr/views/text/LogTextView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import app.logorrr.util.{ClipBoardUtils, JfxUtils}
import javafx.beans.value.ChangeListener
import javafx.collections.transformation.FilteredList
import javafx.scene.control._
import javafx.scene.layout.BorderPane
import javafx.scene.paint.Color

import scala.jdk.CollectionConverters._
Expand All @@ -23,25 +22,17 @@ object LogTextView {


class LogTextView(mutLogFileSettings: MutLogFileSettings
, filteredList: FilteredList[LogEntry]) extends BorderPane {
, filteredList: FilteredList[LogEntry]) extends ListView[LogEntry] {
/** 'pragmatic way' to determine width of max elems in this view */
val maxLength: Int = filteredList.size().toString.length

val listView: ListView[LogEntry] = {
val lv = new ListView[LogEntry]()
lv.getStyleClass.add("dense")
lv.setItems(filteredList)
val i = mutLogFileSettings.selectedLineNumberProperty.get()
lv.getSelectionModel.select(i)
lv
}

private val selectedLineNumberListener: ChangeListener[Number] = JfxUtils.onNew((n: Number) => {
Option(listView.getItems.filtered((t: LogEntry) => t.lineNumber == n.intValue()).get(0)) match {
Option(getItems.filtered((t: LogEntry) => t.lineNumber == n.intValue()).get(0)) match {
case Some(value) =>
val relativeIndex = listView.getItems.indexOf(value)
listView.getSelectionModel.select(relativeIndex)
listView.scrollTo(relativeIndex - ((listView.getHeight / LogTextView.fixedCellSize) / 2).toInt)
val relativeIndex = getItems.indexOf(value)
getSelectionModel.select(relativeIndex)
scrollTo(relativeIndex - ((getHeight / LogTextView.fixedCellSize) / 2).toInt)
case None =>
}
})
Expand All @@ -52,27 +43,31 @@ class LogTextView(mutLogFileSettings: MutLogFileSettings
*
*/
def init(): Unit = {
listView.getSelectionModel.selectedIndexProperty().addListener(JfxUtils.onNew({ i: Number => {
getStyleClass.add("dense")
setItems(filteredList)
val i = mutLogFileSettings.selectedLineNumberProperty.get()
getSelectionModel.select(i)

getSelectionModel.selectedIndexProperty().addListener(JfxUtils.onNew({ i: Number => {
mutLogFileSettings.selectedLineNumberProperty.removeListener(selectedLineNumberListener)
mutLogFileSettings.setSelectedLineNumber(i.intValue())
mutLogFileSettings.selectedLineNumberProperty.addListener(selectedLineNumberListener)
}
}))
listView.setCellFactory((_: ListView[LogEntry]) => new LogEntryListCell())
setCellFactory((_: ListView[LogEntry]) => new LogEntryListCell())
mutLogFileSettings.selectedLineNumberProperty.addListener(selectedLineNumberListener)

setCenter(listView)
}

/** scroll to end of listview */
def scrollToEnd(): Unit = {
listView.scrollTo(listView.getItems.size)
scrollTo(getItems.size)
}

class LogEntryListCell extends ListCell[LogEntry] {
styleProperty().bind(mutLogFileSettings.fontStyleBinding)
setGraphic(null)

val copyCurrentToClipboard = new MenuItem("copy text to clipboard")
val separator = new SeparatorMenuItem()
val ignoreAbove = new MenuItem("Ignore entries above")
Expand Down

0 comments on commit 8a3dc79

Please sign in to comment.