From f33b9f341819841e89ca9207353f977e1478f8ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Ladst=C3=A4tter?= Date: Tue, 30 Jul 2024 20:47:15 +0200 Subject: [PATCH] #242: implements multipleselection-copy to clipboard --- .../app/logorrr/util/ClipBoardUtils.scala | 14 +++++++++++++- .../app/logorrr/views/ops/CopyLogButton.scala | 18 +++++------------- .../app/logorrr/views/text/LogTextView.scala | 10 ++++++---- .../contextactions/CopyEntriesMenuItem.scala | 12 ++++++++++++ 4 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 app/src/main/scala/app/logorrr/views/text/contextactions/CopyEntriesMenuItem.scala diff --git a/app/src/main/scala/app/logorrr/util/ClipBoardUtils.scala b/app/src/main/scala/app/logorrr/util/ClipBoardUtils.scala index ca9b4e7b..51bac7c3 100644 --- a/app/src/main/scala/app/logorrr/util/ClipBoardUtils.scala +++ b/app/src/main/scala/app/logorrr/util/ClipBoardUtils.scala @@ -1,13 +1,25 @@ package app.logorrr.util +import app.logorrr.model.LogEntry +import javafx.collections.ObservableList import javafx.scene.input.{Clipboard, ClipboardContent} +import scala.collection.mutable.ListBuffer + object ClipBoardUtils { - def copyToClipboardText(s: String): Unit = { + def copyToClipboard(logEntries : ObservableList[LogEntry]) : Int = { + val lb = new ListBuffer[String] + logEntries.forEach(e => lb.addOne(e.value)) + copyToClipboard(lb.mkString("\n")) + lb.size + } + + def copyToClipboard(s: String): Unit = { val clipboard = Clipboard.getSystemClipboard() val content = new ClipboardContent() content.putString(s) clipboard.setContent(content) } + } diff --git a/app/src/main/scala/app/logorrr/views/ops/CopyLogButton.scala b/app/src/main/scala/app/logorrr/views/ops/CopyLogButton.scala index 7005f6f6..10216a4b 100644 --- a/app/src/main/scala/app/logorrr/views/ops/CopyLogButton.scala +++ b/app/src/main/scala/app/logorrr/views/ops/CopyLogButton.scala @@ -2,16 +2,14 @@ package app.logorrr.views.ops import app.logorrr.io.FileId import app.logorrr.model.LogEntry +import app.logorrr.util.ClipBoardUtils import app.logorrr.views.{UiNode, UiNodeFileIdAware} import javafx.animation.AnimationTimer import javafx.collections.ObservableList import javafx.scene.control.{Button, Tooltip} -import javafx.scene.input.{Clipboard, ClipboardContent} import org.kordamp.ikonli.fontawesome5.{FontAwesomeRegular, FontAwesomeSolid} import org.kordamp.ikonli.javafx.FontIcon -import scala.collection.mutable.ListBuffer - object CopyLogButton extends UiNodeFileIdAware { def uiNode(id: FileId): UiNode = UiNode(id, classOf[CopyLogButton]) @@ -40,10 +38,8 @@ class CopyLogButton(id: FileId, logEntries: ObservableList[LogEntry]) extends Bu setTooltip(defaultToolTip) setOnAction(_ => { - val lb = new ListBuffer[String] - logEntries.forEach(e => lb.addOne(e.value)) - copyToClipboard(lb.mkString("\n")) - defaultToolTip.setText(s"Copied ${lb.size} entries to clipboard") + val size = ClipBoardUtils.copyToClipboard(logEntries) + defaultToolTip.setText(s"Copied ${size} entries to clipboard") val bounds = localToScreen(getBoundsInLocal) val x = bounds.getMinX val y = bounds.getMaxY @@ -85,10 +81,6 @@ class CopyLogButton(id: FileId, logEntries: ObservableList[LogEntry]) extends Bu } } - def copyToClipboard(text: String): Unit = { - val clipboard = Clipboard.getSystemClipboard - val content = new ClipboardContent() - content.putString(text) - clipboard.setContent(content) - } } + + 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 de28a8d1..5becd4d6 100644 --- a/app/src/main/scala/app/logorrr/views/text/LogTextView.scala +++ b/app/src/main/scala/app/logorrr/views/text/LogTextView.scala @@ -4,7 +4,7 @@ import app.logorrr.conf.mut.MutLogFileSettings import app.logorrr.io.FileId import app.logorrr.model.LogEntry import app.logorrr.util.{CanLog, JfxUtils} -import app.logorrr.views.text.contextactions.{IgnoreAboveMenuItem, IgnoreBelowMenuItem} +import app.logorrr.views.text.contextactions.{CopyEntriesMenuItem, IgnoreAboveMenuItem, IgnoreBelowMenuItem} import app.logorrr.views.{UiNode, UiNodeFileIdAware} import javafx.collections.transformation.FilteredList import javafx.scene.control._ @@ -23,8 +23,8 @@ class LogTextView(mutLogFileSettings: MutLogFileSettings extends ListView[LogEntry] with CanLog { - setId(LogTextView.uiNode(mutLogFileSettings.getFileId).value) + getSelectionModel.setSelectionMode(SelectionMode.MULTIPLE) private lazy val selectedLineNumberListener = JfxUtils.onNew[LogEntry](e => { Option(e) match { @@ -40,7 +40,7 @@ class LogTextView(mutLogFileSettings: MutLogFileSettings refresh() // otherwise listview is not repainted correctly since calculation of the cellheight is broken atm }) - private lazy val scrollBarListener = JfxUtils.onNew[Number](_ => { + private lazy val scrollBarListener = JfxUtils.onNew[Number](_ => { val (first, last) = ListViewHelper.getVisibleRange(this) mutLogFileSettings.setFirstVisibleTextCellIndex(first) mutLogFileSettings.setLastVisibleTextCellIndex(last) @@ -133,13 +133,15 @@ class LogTextView(mutLogFileSettings: MutLogFileSettings setGraphic(entry) + val copySelectionMenuItem = new CopyEntriesMenuItem(getSelectionModel) + val ignoreAboveMenuItem = new IgnoreAboveMenuItem(mutLogFileSettings , e , filteredList , scrollToActiveLogEntry) val ignoreBelowMenuItem = new IgnoreBelowMenuItem(e, filteredList) - setContextMenu(new ContextMenu(ignoreAboveMenuItem, ignoreBelowMenuItem)) + setContextMenu(new ContextMenu(copySelectionMenuItem, ignoreAboveMenuItem, ignoreBelowMenuItem)) } } } diff --git a/app/src/main/scala/app/logorrr/views/text/contextactions/CopyEntriesMenuItem.scala b/app/src/main/scala/app/logorrr/views/text/contextactions/CopyEntriesMenuItem.scala new file mode 100644 index 00000000..9b56b0eb --- /dev/null +++ b/app/src/main/scala/app/logorrr/views/text/contextactions/CopyEntriesMenuItem.scala @@ -0,0 +1,12 @@ +package app.logorrr.views.text.contextactions + +import app.logorrr.model.LogEntry +import app.logorrr.util.{CanLog, ClipBoardUtils} +import javafx.scene.control.{MenuItem, MultipleSelectionModel} + +class CopyEntriesMenuItem(selectionModel: MultipleSelectionModel[LogEntry]) + extends MenuItem("copy selection to clipboard") with CanLog { + + setOnAction(_ => ClipBoardUtils.copyToClipboard(selectionModel.getSelectedItems)) + +}