Last active
July 11, 2023 18:16
-
-
Save eugener/468e9dec3fe847271d240897812983e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.oxbow.codebridge.ui.main | |
import java.io.File | |
import scala.reflect.runtime.universe | |
import org.controlsfx.control.StatusBar | |
import org.oxbow.codebridge.scm.Repository | |
import org.oxbow.codebridge.util.InjectableController | |
import javafx.application.Platform | |
import javafx.fxml.FXML | |
import javafx.geometry.Orientation | |
import javafx.scene.control.Label | |
import javafx.scene.control.MenuBar | |
import javafx.scene.control.Separator | |
import javafx.scene.control.TabPane | |
import javafx.scene.control.ToolBar | |
import javafx.stage.DirectoryChooser | |
import javafx.stage.Stage | |
import org.oxbow.codebridge.State | |
import org.oxbow.codebridge.RepoViewState | |
import org.oxbow.codebridge.StageState | |
import org.oxbow.codebridge.ui.common.Dialogs | |
import javafx.scene.control.SplitPane | |
import javafx.scene.control.Tab | |
import javafx.scene.control.Tooltip | |
import org.oxbow.codebridge.ui.repo.RepositoryBrowserController | |
import javafx.event.Event | |
import org.oxbow.codebridge.util.JFXImplicits._ | |
import javafx.stage.WindowEvent | |
import org.oxbow.codebridge.scm.RepositoryOpenException | |
import org.oxbow.codebridge.util.FxmlTools | |
class MainWindowController extends InjectableController { | |
@FXML var statusBar: StatusBar = _ | |
@FXML var menuBar: MenuBar = _ | |
@FXML var toolBar: ToolBar = _ | |
@FXML var tabs: TabPane = _ | |
private var stage: Stage = _ | |
@FXML def initialize: Unit = { | |
val versionLabel = new Label(inject[String]("app.version")) | |
statusBar.getRightItems.addAll(new Separator(Orientation.VERTICAL), versionLabel) | |
} | |
def setStage( stage: Stage ): Unit = { | |
this.stage = stage | |
stage.setOnCloseRequest{ e: WindowEvent => storeState } | |
restoreState | |
} | |
def restoreState: Unit = { | |
val state = State.read | |
state.repoStates.foreach(showRepo) | |
state.stage.update(stage) | |
} | |
def storeState: Unit = { | |
import collection.JavaConversions._ | |
val selectedTab = tabs.getSelectionModel.getSelectedItem | |
State( | |
controllers.map{ case (tab,c) => | |
c.repository.close | |
c.state.copy( active = tab == selectedTab ) | |
}.toArray, | |
new StageState(stage) | |
).write | |
} | |
//TODO: Call exit on stage closing event | |
@FXML def exit = Platform.exit | |
@FXML def openRepository: Unit = { | |
val directoryChooser = new DirectoryChooser | |
directoryChooser.setTitle("Open repository") | |
Option(directoryChooser.showDialog(stage)).foreach( f => showRepo( RepoViewState(f.toString, true))) | |
} | |
protected def showRepo( state: RepoViewState ): Unit = { | |
try { | |
val (content, controller) = FxmlTools.load[RepositoryBrowserController] | |
controller.state = state | |
val repo = controller.repository | |
val tab = new Tab(repo.name) | |
tab.setTooltip( new Tooltip(repo.location.toString)) | |
tab.setContent(content) | |
tab.setUserData(controller) | |
tab.setOnCloseRequest{ e: Event => | |
if ( !Dialogs.confirm( stage, s"Close repository '${repo.name}'?")) e.consume | |
} | |
tab.setOnClosed{repo.close} | |
tabs.getTabs.add(tab) | |
if (state.active) tabs.getSelectionModel.select(tab) | |
} catch { | |
case e: RepositoryOpenException => { Dialogs.info(stage, e.getMessage); selectRepo(state.location) } | |
case e: Throwable => Dialogs.error(stage, e.getMessage) | |
} | |
} | |
private def controllers = { | |
import collection.JavaConversions._ | |
tabs.getTabs.toList.map( tab => (tab,tab.getUserData.asInstanceOf[RepositoryBrowserController])) | |
} | |
protected def selectRepo(path: String): Unit = { | |
controllers | |
.find{case (tab,c) => path == c.repository.location.toString } | |
.foreach{ case (tab,_) => tabs.getSelectionModel.select(tab) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment