Skip to content

Commit

Permalink
#242: implements multipleselection-copy to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Jul 30, 2024
1 parent 2850607 commit f33b9f3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
14 changes: 13 additions & 1 deletion app/src/main/scala/app/logorrr/util/ClipBoardUtils.scala
Original file line number Diff line number Diff line change
@@ -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)
}

}
18 changes: 5 additions & 13 deletions app/src/main/scala/app/logorrr/views/ops/CopyLogButton.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}


10 changes: 6 additions & 4 deletions app/src/main/scala/app/logorrr/views/text/LogTextView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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))

}

0 comments on commit f33b9f3

Please sign in to comment.