From 8a3dc790c7fcb2997e3cadda3744ad7564cb5b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Ladst=C3=A4tter?= Date: Wed, 25 Oct 2023 00:00:31 +0200 Subject: [PATCH] #150: simplifies code --- .../app/logorrr/views/block/BlockImage.scala | 15 ++++--- .../app/logorrr/views/block/BlockView.scala | 43 +++++++++---------- .../logorrr/views/block/BlockViewPane.scala | 13 ++++-- .../logorrr/views/block/LPixelBuffer.scala | 4 +- .../app/logorrr/views/text/LogTextView.scala | 33 ++++++-------- 5 files changed, 53 insertions(+), 55 deletions(-) diff --git a/app/src/main/scala/app/logorrr/views/block/BlockImage.scala b/app/src/main/scala/app/logorrr/views/block/BlockImage.scala index 1b2b60eb..57c38608 100644 --- a/app/src/main/scala/app/logorrr/views/block/BlockImage.scala +++ b/app/src/main/scala/app/logorrr/views/block/BlockImage.scala @@ -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). @@ -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) => { @@ -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 @@ -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) + } diff --git a/app/src/main/scala/app/logorrr/views/block/BlockView.scala b/app/src/main/scala/app/logorrr/views/block/BlockView.scala index 80a4c093..948abb20 100644 --- a/app/src/main/scala/app/logorrr/views/block/BlockView.scala +++ b/app/src/main/scala/app/logorrr/views/block/BlockView.scala @@ -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) } @@ -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) } } } @@ -90,7 +90,7 @@ class BlockView(name: String private val blockImage = new BlockImage(name , widthProperty , blockSizeProperty - , entriesProperty + , entries , filtersProperty , selectedElemProperty) @@ -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 = { @@ -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) diff --git a/app/src/main/scala/app/logorrr/views/block/BlockViewPane.scala b/app/src/main/scala/app/logorrr/views/block/BlockViewPane.scala index 7478cae1..c9e2310f 100644 --- a/app/src/main/scala/app/logorrr/views/block/BlockViewPane.scala +++ b/app/src/main/scala/app/logorrr/views/block/BlockViewPane.scala @@ -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 @@ -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 diff --git a/app/src/main/scala/app/logorrr/views/block/LPixelBuffer.scala b/app/src/main/scala/app/logorrr/views/block/LPixelBuffer.scala index 334cb26d..9423422e 100644 --- a/app/src/main/scala/app/logorrr/views/block/LPixelBuffer.scala +++ b/app/src/main/scala/app/logorrr/views/block/LPixelBuffer.scala @@ -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 { @@ -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 { diff --git a/app/src/main/scala/app/logorrr/views/text/LogTextView.scala b/app/src/main/scala/app/logorrr/views/text/LogTextView.scala index 0965f265..caf87539 100644 --- a/app/src/main/scala/app/logorrr/views/text/LogTextView.scala +++ b/app/src/main/scala/app/logorrr/views/text/LogTextView.scala @@ -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._ @@ -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 => } }) @@ -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")