Skip to content

Commit

Permalink
#182: open zip files via menu bar as well
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Jan 1, 2024
1 parent af617cf commit ab1ee1d
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 62 deletions.
2 changes: 1 addition & 1 deletion app/src/main/scala/app/logorrr/conf/LogoRRRGlobals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object LogoRRRGlobals extends CanLog {
})

if (OsUtil.enableSecurityBookmarks) {
if (fileId.isZip) {
if (fileId.isZipEntry) {
// only release path if no other file is opened anymore for this particular zip file
val zipInQuestion = fileId.extractZipFileId
if (!LogoRRRGlobals.getOrderedLogFileSettings.map(_.fileId.extractZipFileId).contains(zipInQuestion)) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/scala/app/logorrr/conf/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ case class Settings(stageSettings: StageSettings

def filterWithValidPaths(): Settings = copy(fileSettings = fileSettings.filter { case (_, d) =>
// if entry is part of a zip file, test the path of the zip file
if (d.fileId.isZip) {
if (d.fileId.isZipEntry) {
IoManager.isPathValid(d.fileId.extractZipFileId.asPath)
} else {
IoManager.isPathValid(d.path)
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/scala/app/logorrr/io/IoManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ object IoManager extends CanLog {
* map represents the file name in the zip file, and the value is the contents of the file as string.
*
* @param zipFile the zip file
* @param filters if empty, return all files, else only those which match given file ids
* @return
*/
def unzip(zipFile: Path, filters: Set[FileId]): Map[FileId, ObservableList[LogEntry]] = {
def unzip(zipFile: Path, filters: Set[FileId] = Set()): Map[FileId, ObservableList[LogEntry]] = {
registerPath(zipFile)
var resultMap: Map[FileId, ObservableList[LogEntry]] = Map()
try {
Expand Down Expand Up @@ -148,4 +149,7 @@ object IoManager extends CanLog {
OsxBridge.registerPath(zipFile.toAbsolutePath.toString)
}
}

def isZip(path : Path) : Boolean = path.getFileName.toString.endsWith(".zip")

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class LogFileTab(val fileId: FileId
with TimerCode
with CanLog {

if (fileId.isZip) {
if (fileId.isZipEntry) {
setStyle(LogFileTab.ZipBackgroundStyle)
} else {
setStyle(LogFileTab.BackgroundStyle)
Expand Down Expand Up @@ -113,7 +113,7 @@ class LogFileTab(val fileId: FileId

private val selectedListener = JfxUtils.onNew[lang.Boolean](b => {
if (b) {
if (fileId.isZip) {
if (fileId.isZipEntry) {
setStyle(LogFileTab.ZipBackgroundSelectedStyle)
} else {
setStyle(LogFileTab.BackgroundSelectedStyle)
Expand All @@ -124,7 +124,7 @@ class LogFileTab(val fileId: FileId
LogoRRRAccelerators.setActiveRegexToggleButton(logFileTabContent.opsToolBar.regexToggleButton)
recalculateChunkListViewAndScrollToActiveElement()
} else {
if (fileId.isZip) {
if (fileId.isZipEntry) {
setStyle(LogFileTab.ZipBackgroundStyle)
} else {
setStyle(LogFileTab.BackgroundStyle)
Expand Down Expand Up @@ -217,7 +217,7 @@ class LogFileTab(val fileId: FileId
}

private def initBindings(): Unit = {
if (fileId.isZip) {
if (fileId.isZipEntry) {
textProperty.bind(Bindings.concat(fileId.zipEntryPath))
} else {
textProperty.bind(Bindings.concat(fileId.fileName))
Expand Down
23 changes: 11 additions & 12 deletions app/src/main/scala/app/logorrr/views/main/LogoRRRMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import scala.concurrent.{Await, Future}
class LogoRRRMain(closeStage: => Unit) extends BorderPane with CanLog {

val mainTabPane = new MainTabPane
val bar = new MainMenuBar(() => getScene.getWindow, openLogFile, closeAllLogFiles(), closeStage)

val bar = new MainMenuBar(() => getScene.getWindow, openFile, closeAllLogFiles(), closeStage)

def getLogFileTabs: mutable.Seq[LogFileTab] = mainTabPane.getLogFileTabs

Expand All @@ -36,7 +37,7 @@ class LogoRRRMain(closeStage: => Unit) extends BorderPane with CanLog {
}

private def loadLogFiles(settings: Seq[LogFileSettings]): Unit = {
val (zipSettings, fileSettings) = settings.partition(p => p.fileId.isZip)
val (zipSettings, fileSettings) = settings.partition(p => p.fileId.isZipEntry)
val zipSettingsMap: Map[FileId, LogFileSettings] = zipSettings.map(s => s.fileId -> s).toMap
// zips is a map which contains fileIds as keys which have to be loaded, and as values their corresponding
// settings. this is necessary as not to lose settings from previous runs
Expand Down Expand Up @@ -80,19 +81,17 @@ class LogoRRRMain(closeStage: => Unit) extends BorderPane with CanLog {

}


def contains(fileId: FileId): Boolean = mainTabPane.contains(fileId)

/** called when 'Open File' is selected. */
def openLogFile(path: Path): Unit = {
val fileId = FileId(path)

if (!contains(fileId)) {
mainTabPane.addLogFile(path)
/** called when 'Open File' from the main menu bar is selected. */
def openFile(fileId: FileId): Unit = {
if (IoManager.isZip(fileId.asPath)) {
mainTabPane.openZipFile(fileId.asPath)
} else if (contains(fileId)) {
mainTabPane.selectFile(fileId).recalculateChunkListViewAndScrollToActiveElement()
} else {
mainTabPane.selectLog(fileId).recalculateChunkListViewAndScrollToActiveElement()
mainTabPane.addFile(fileId)
}

}

/** removes all log files */
Expand All @@ -101,7 +100,7 @@ class LogoRRRMain(closeStage: => Unit) extends BorderPane with CanLog {
LogoRRRGlobals.clearLogFileSettings()
}

def selectLog(fileId: FileId): LogFileTab = mainTabPane.selectLog(fileId)
def selectLog(fileId: FileId): LogFileTab = mainTabPane.selectFile(fileId)

def selectLastLogFile(): Unit = mainTabPane.selectLastLogFile()

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/scala/app/logorrr/views/main/MainMenuBar.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.logorrr.views.main

import app.logorrr.io.FileId
import app.logorrr.util.{CanLog, OsUtil}
import app.logorrr.views.menubar.{FileMenu, HelpMenu}
import javafx.scene.control.MenuBar
Expand All @@ -8,7 +9,7 @@ import javafx.stage.Window
import java.nio.file.Path

class MainMenuBar(getWindow: () => Window
, openLogFile: Path => Unit
, openFile: FileId => Unit
, closeAllLogFiles: => Unit
, closeApplication: => Unit)
extends MenuBar
Expand All @@ -19,7 +20,7 @@ class MainMenuBar(getWindow: () => Window

private def init(): Unit = {
getMenus.clear()
getMenus.addAll(new FileMenu(getWindow, openLogFile, closeAllLogFiles, closeApplication), new HelpMenu(openLogFile))
getMenus.addAll(new FileMenu(getWindow, openFile, closeAllLogFiles, closeApplication), new HelpMenu(openFile))
}

init()
Expand Down
61 changes: 35 additions & 26 deletions app/src/main/scala/app/logorrr/views/main/MainTabPane.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,41 @@ class MainTabPane extends TabPane with CanLog {
val path = f.toPath
if (Files.isDirectory(path)) {
dropDirectory(path)
} else if (path.getFileName.toString.endsWith(".zip")) {
// by default read all files which are contained in the zip file
IoManager.unzip(path, Set()).foreach {
case (fileId, entries) =>
if (!contains(fileId)) {
{
addEntriesFromZip(LogFileSettings(fileId), entries)
selectLog(fileId)
}
} else {
logTrace(s"${fileId.absolutePathAsString} is already opened, selecting tab ...")
selectLog(fileId)
}
}
} else if (IoManager.isZip(path)) {
openZipFile(path)
} else {
dropLogFile(path)
openFile(FileId(path))
}
}
})
}

// by default read all files which are contained in the zip file

/**
* Unzips given path, interprets contents as log files and adds them to the GUI
*
* @param path zip file to open
*/
def openZipFile(path: Path): Unit = {
if (IoManager.isZip(path)) {
IoManager.unzip(path).foreach {
case (fileId, entries) =>
if (!contains(fileId)) {
addEntriesFromZip(LogFileSettings(fileId), entries)
selectFile(fileId)
} else {
logTrace(s"${fileId.absolutePathAsString} is already opened, selecting tab ...")
selectFile(fileId)
}
}
} else {
logWarn(s"Tried to open file as zip, but filename was: '${path.toAbsolutePath.toString}'.")
}
}

private def dropDirectory(path: Path): Unit = {
Files.list(path).filter((p: Path) => Files.isRegularFile(p)).forEach((t: Path) => dropLogFile(t))
Files.list(path).filter((p: Path) => Files.isRegularFile(p)).forEach((t: Path) => openFile(FileId(t)))
}

/**
Expand All @@ -102,7 +114,7 @@ class MainTabPane extends TabPane with CanLog {
getTabs.clear()
}

def selectLog(fileId: FileId): LogFileTab = {
def selectFile(fileId: FileId): LogFileTab = {
getLogFileTabs.find(_.fileId == fileId) match {
case Some(logFileTab) =>
logTrace(s"Activated tab for '$fileId'.")
Expand All @@ -117,28 +129,25 @@ class MainTabPane extends TabPane with CanLog {

def selectLastLogFile(): Unit = getSelectionModel.selectLast()

private def dropLogFile(path: Path): Unit = {
val fileId = FileId(path)

if (Files.exists(path)) {
private def openFile(fileId: FileId): Unit = {
if (Files.exists(fileId.asPath)) {
if (!contains(fileId)) {
addLogFile(path)
addFile(fileId)
} else {
logTrace(s"${fileId.absolutePathAsString} is already opened, selecting tab ...")
selectLog(fileId)
selectFile(fileId)
}
} else {
logWarn(s"${fileId.absolutePathAsString} does not exist.")
}
}

def addLogFile(path: Path): Unit = {
val fileId = FileId(path)
def addFile(fileId: FileId): Unit = {
val logFileSettings = LogFileSettings(fileId)
LogoRRRGlobals.registerSettings(logFileSettings)
val entries = IoManager.readEntries(logFileSettings.path, logFileSettings.someLogEntryInstantFormat)
addLogFileTab(LogFileTab(LogoRRRGlobals.getLogFileSettings(fileId), entries))
selectLog(fileId)
selectFile(fileId)
}

def addEntriesFromZip(logFileSettings: LogFileSettings, entries: ObservableList[LogEntry]): LogFileTab = timeR({
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/scala/app/logorrr/views/menubar/FileMenu.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.logorrr.views.menubar

import app.logorrr.conf.LogoRRRGlobals
import app.logorrr.conf.LogoRRRGlobals
import app.logorrr.io.FileId
import app.logorrr.util.{CanLog, OsUtil}
import javafx.scene.control.{Menu, MenuItem}
import javafx.stage.{FileChooser, Window}
Expand All @@ -20,15 +21,15 @@ class LogoRRRFileChooser(title: String) {
}

}

object FileMenu {

class OpenMenuItem(getWindow: () => Window
, openLogFile: Path => Unit)
class OpenMenuItem(getWindow: () => Window, openFile: FileId => Unit)
extends MenuItem("Open") with CanLog {

setOnAction(_ => {
new LogoRRRFileChooser("Open log file").showAndWait(getWindow()) match {
case Some(logFile) => openLogFile(logFile)
case Some(logFile) => openFile(FileId(logFile))
case None => logTrace("Cancelled open file ...")
}

Expand All @@ -46,13 +47,13 @@ object FileMenu {
}

class FileMenu(getWindow: () => Window
, openLogFile: Path => Unit
, openFile: FileId => Unit
, closeAllLogFiles: => Unit
, closeApplication: => Unit) extends Menu("File") with CanLog {

def init(): Unit = {
getItems.clear()
getItems.add(new FileMenu.OpenMenuItem(getWindow, openLogFile))
getItems.add(new FileMenu.OpenMenuItem(getWindow, openFile))
getItems.add(new FileMenu.CloseAllMenuItem(closeAllLogFiles))
if (OsUtil.isWin) {
getItems.add(new FileMenu.QuitMenuItem(closeApplication))
Expand Down
12 changes: 5 additions & 7 deletions app/src/main/scala/app/logorrr/views/menubar/HelpMenu.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package app.logorrr.views.menubar

import app.logorrr.io.FilePaths
import app.logorrr.io.{FileId, FilePaths}
import app.logorrr.meta.AppMeta
import app.logorrr.views.about.AboutScreen
import app.logorrr.views.menubar.HelpMenu.{AboutMenuItem, LogMenuItem}
import javafx.scene.Scene
import javafx.scene.control.{Menu, MenuItem}
import javafx.stage.{Modality, Stage}

import java.nio.file.Path

object HelpMenu {

class LogMenuItem(openLogFile: Path => Unit) extends MenuItem("Open LogoRRRs log") {
class LogMenuItem(openLogFile: FileId => Unit) extends MenuItem("Open LogoRRRs log") {
setOnAction(_ => {
openLogFile(FilePaths.logFilePath)
openLogFile(FileId(FilePaths.logFilePath))
})
}

Expand All @@ -31,6 +29,6 @@ object HelpMenu {
}
}

class HelpMenu(openLogFile: Path => Unit) extends Menu("Help") {
getItems.addAll(new LogMenuItem(openLogFile), new AboutMenuItem())
class HelpMenu(openFile: FileId => Unit) extends Menu("Help") {
getItems.addAll(new LogMenuItem(openFile), new AboutMenuItem())
}
2 changes: 1 addition & 1 deletion core/src/main/scala/app/logorrr/io/FileId.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ case class FileId(value: String) {
extractZipFileId.fileName + value.substring(value.indexOf(".zip@") + 4, value.length)
}

def isZip: Boolean = value.contains(".zip@")
def isZipEntry: Boolean = value.contains(".zip@")

def asPath: Path = Paths.get(value)

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/app/logorrr/io/FileIdSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FileIdSpec extends AnyWordSpec {
}
"zipentrypath" in {
val id = FileId("real/path/a.zip@an/entry/file.log")
assert(id.isZip)
assert(id.isZipEntry)
assert(id.zipEntryPath == "a.zip@an/entry/file.log")
}
"zippath" in {
Expand Down

0 comments on commit ab1ee1d

Please sign in to comment.