Skip to content

Commit

Permalink
Redesigned import from files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias-Kohn committed Sep 30, 2020
1 parent 7076dbd commit ad597ea
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/main/scala/tigerjython/files/Document.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ class Document(protected val prefNode: JPreferences) {
val createDate = Date.from(attr.creationTime().toInstant)
if (createDate.before(getCreationDate))
prefNode.put("created", dateFormat.format(createDate))
var n = file.getName
if (n.toLowerCase.endsWith(".py"))
n = n.dropRight(3)
n = Documents.makeNameUnique(n)
name.setValue(n)
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/scala/tigerjython/files/Documents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ object Documents {
documents.remove(idx)
}

protected[files]
def makeNameUnique(name: String, index: Int = 0): String = {
val n =
if (index > 0)
"%s (%d)".format(name, index)
else
name
for (document <- documents)
if (document.name.get == n) {
return makeNameUnique(name, index+1)
}
n
}

/**
* Reads all the documents from the preferences.
*/
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/tigerjython/ui/tabs/DocumentItem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import javafx.scene.input.MouseEvent
import javafx.scene.layout.{BorderPane, VBox}
import javafx.scene.paint.Color
import javafx.scene.shape.Rectangle
import javafx.scene.text.Text

import scala.annotation.tailrec

Expand Down
73 changes: 73 additions & 0 deletions src/main/scala/tigerjython/ui/tabs/ImportDocumentItem.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of the 'TigerJython' project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package tigerjython.ui.tabs

import javafx.scene.{Group, Node}
import javafx.scene.paint.Color
import javafx.scene.shape.{Line, Rectangle}
import javafx.stage.FileChooser
import javafx.stage.FileChooser.ExtensionFilter
import tigerjython.core.Configuration
import tigerjython.files.Documents
import tigerjython.ui.{TigerJythonApplication, editor}

/**
* @author Tobias Kohn
*/
class ImportDocumentItem(val parentFrame: OpenDocumentTab) extends DocumentItem {

{
titleLabel.setText("Import/Open Document")
descriptionLabel.setText("Load a document from a file on disc")
}

protected lazy val fileChooser: FileChooser = {
val result = new FileChooser()
result.setInitialDirectory(Configuration.userHome.toFile)
result.getExtensionFilters.addAll(
new ExtensionFilter("Python Files", "*.py"),
new ExtensionFilter("All Files", "*.*")
)
result
}

def onClicked(): Unit = {
val selectedFile = fileChooser.showOpenDialog(TigerJythonApplication.mainStage)
if (selectedFile != null) {
val tab = editor.PythonEditorTab()
tab.loadDocument(Documents.importDocument(selectedFile))
TigerJythonApplication.tabManager.addTab(tab)
}
}

override def onMouseEnter(): Unit =
parentFrame.setPreviewText("")

override def onMouseLeave(): Unit = {}

override protected def createIcon(): Node = {
val result = new Rectangle(42, 59)
result.setStroke(Color.BLACK)
result.getStyleClass.add("paper")
val outline = new Rectangle(60, 59)
outline.setFill(Color.TRANSPARENT)
val g = new Group()
g.getChildren.addAll(
outline, result
)
g.getChildren.addAll(
new Line(12, 36, 12, 34),
new Line(30, 36, 30, 34),
new Line(12, 36, 30, 36),
new Line(21, 32, 21, 22),
new Line(21, 22, 17, 26),
new Line(21, 22, 25, 26)
)
g
}
}
6 changes: 4 additions & 2 deletions src/main/scala/tigerjython/ui/tabs/NewNotebookItem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package tigerjython.ui.tabs
import javafx.scene.{Group, Node}
import javafx.scene.paint.Color
import javafx.scene.shape.{Circle, Line, Rectangle}
import tigerjython.ui.{TigerJythonApplication, editor}
import tigerjython.ui.TigerJythonApplication
import tigerjython.ui.notebook.NotebookTab

/**
Expand Down Expand Up @@ -51,8 +51,10 @@ class NewNotebookItem(val parentFrame: OpenDocumentTab) extends DocumentItem {
g
}

def onClicked(): Unit =
def onClicked(): Unit = {
tigerjython.execute.TigerJythonProcess.preStart()
TigerJythonApplication.tabManager.addTab(NotebookTab())
}

override def onMouseEnter(): Unit =
parentFrame.setPreviewText("")
Expand Down
4 changes: 3 additions & 1 deletion src/main/scala/tigerjython/ui/tabs/OpenDocumentTab.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class OpenDocumentTab protected () extends TabFrame {
val prefButton = new Button()
prefButton.setGraphic(createPrefGraphic())
prefButton.setOnAction(_ => TigerJythonApplication.currentApplication.showPreferences())
result.getItems.addAll(findTextField, importBtn, filler, prefButton)
result.getItems.addAll(findTextField, filler, prefButton)
result
}

Expand Down Expand Up @@ -167,6 +167,8 @@ class OpenDocumentTab protected () extends TabFrame {
val notebook = new NewNotebookItem(this)
documentItems += notebook
items.getChildren.add(notebook)
val importItem = new ImportDocumentItem(this)
items.getChildren.add(importItem)
for (doc <- Documents.getListOfDocuments) {
val item = new OpenDocumentItem(this, doc)
documentItems += item
Expand Down

0 comments on commit ad597ea

Please sign in to comment.