Skip to content

Commit

Permalink
implement commong operations in ide
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Oct 23, 2020
1 parent 2f622f0 commit 66a561b
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
core/src/commonMain/kotlin/it/unibo/tuprolog/Info.kt

*.hprof
*~
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
Expand Down
20 changes: 19 additions & 1 deletion ide/src/main/kotlin/it/unibo/tuprolog/ui/gui/FileTabView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.io.IOException

@Suppress("UNUSED_PARAMETER")
class FileTabView(
public val file: File,
file: File,
private val model: PrologIDEModel,
private val ideController: PrologIDEController,
initialText: String = ""
Expand Down Expand Up @@ -51,11 +51,22 @@ class FileTabView(
@FXML
lateinit var codeArea: CodeArea

var wholeText: String
get() = codeArea.text
set(value) {
codeArea.replaceText(value)
model.setFile(file, codeArea.text)
}

fun notifyOperators(operators: OperatorSet) {
syntaxColoring.operators = operators
syntaxColoring.applyHighlightingNow()
}

fun updateSyntaxColoring() {
syntaxColoring.applyHighlightingNow()
}

// @FXML
// lateinit var btnClose: Button

Expand All @@ -66,6 +77,13 @@ class FileTabView(
}
}

var file: File = file
get
set(value) {
field = value
text = value.name
}

@FXML
fun onMousePressedOnCodeArea(e: MouseEvent) {
ideController.onMouseClickedOnCurrentFile(e)
Expand Down
182 changes: 182 additions & 0 deletions ide/src/main/kotlin/it/unibo/tuprolog/ui/gui/PrologIDEController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import javafx.scene.image.ImageView
import javafx.scene.input.KeyEvent
import javafx.scene.input.MouseEvent
import javafx.scene.layout.Region
import javafx.stage.FileChooser
import javafx.stage.Stage
import org.fxmisc.richtext.CodeArea
import java.io.File
Expand Down Expand Up @@ -81,6 +82,9 @@ class PrologIDEController : Initializable {
@FXML
private lateinit var btnSaveFile: MenuItem

@FXML
private lateinit var btnSaveFileAs: MenuItem

@FXML
private lateinit var btnReloadFile: MenuItem

Expand All @@ -90,6 +94,30 @@ class PrologIDEController : Initializable {
@FXML
private lateinit var btnQuit: MenuItem

@FXML
private lateinit var btnUndo: MenuItem

@FXML
private lateinit var btnRedo: MenuItem

@FXML
private lateinit var btnCut: MenuItem

@FXML
private lateinit var btnCopy: MenuItem

@FXML
private lateinit var btnPaste: MenuItem

@FXML
private lateinit var btnDelete: MenuItem

@FXML
private lateinit var btnSelectAll: MenuItem

@FXML
private lateinit var btnUnselectAll: MenuItem

@FXML
private lateinit var btnAbout: MenuItem

Expand Down Expand Up @@ -205,6 +233,8 @@ class PrologIDEController : Initializable {
model.onWarning.subscribe(this::onWarning)
model.onError.subscribe(this::onError)
model.onFileLoaded.subscribe(this::onFileLoaded)
model.onFileClosed.subscribe(this::onFileClosed)
model.onFileSelected.subscribe(this::onFileSelected)
model.onNewSolver.subscribe(this::onNewSolver)
model.onNewStaticKb.subscribe(this::onNewStaticKb)

Expand Down Expand Up @@ -238,6 +268,40 @@ class PrologIDEController : Initializable {

private fun onFileLoaded(e: Pair<File, String>) = onUiThread {
tabsFiles.tabs.add(FileTabView(e.first, model, this, e.second))
handleSomeOpenFiles()
}

private fun onFileClosed(e: File) = onUiThread {
fileTabs.firstOrNull { it.file == e }?.let {
tabsFiles.tabs -= it
handleNoMoreOpenFiles()
}
}

private fun onFileSelected(e: File) = onUiThread {
fileTabs.firstOrNull { it.file == e }?.let {
it.updateSyntaxColoring()
tabsFiles.selectionModel.select(it)
}
}

private fun handleNoMoreOpenFiles() {
if (fileTabs.none()) {
btnCloseFile.isDisable = true
btnSaveFile.isDisable = true
btnSaveFileAs.isDisable = true
btnReloadFile.isDisable = true
lblStatus.text = "Line: - | Column: -"
}
}

private fun handleSomeOpenFiles() {
if (fileTabs.any()) {
btnCloseFile.isDisable = false
btnSaveFile.isDisable = false
btnSaveFileAs.isDisable = false
btnReloadFile.isDisable = false
}
}

private fun onStdoutPrinted(output: String) = onUiThread {
Expand Down Expand Up @@ -481,6 +545,124 @@ class PrologIDEController : Initializable {
model.newFile()
}

@FXML
fun onOpenFilePressed(e: ActionEvent) {
val fileChooser = FileChooser()
fileChooser.extensionFilters.addAll(
FileChooser.ExtensionFilter("Prolog file", "*.pl", "*.2p"),
FileChooser.ExtensionFilter("Text file", "*.txt"),
FileChooser.ExtensionFilter("Any file", "*"),
)
fileChooser.initialDirectory = File(System.getProperty("user.home"))
fileChooser.title = "Open file..."
val file = fileChooser.showOpenDialog(stage)
model.loadFile(file)
}

@FXML
fun onCloseFilePressed(e: ActionEvent) {
model.closeFile(model.currentFile!!)
}

@FXML
fun onSaveFilePressed(e: ActionEvent) {
try {
model.saveFile(model.currentFile!!)
val alert = Alert(Alert.AlertType.INFORMATION)
alert.title = "Save file"
alert.headerText = "File correctly saved"
alert.contentText = model.currentFile?.canonicalPath
alert.dialogPane.minHeight = Region.USE_PREF_SIZE
alert.showAndWait()
} catch (e: Exception) {
TODO()
}
}

@FXML
fun onSaveFileAsPressed(e: ActionEvent) {
val fileChooser = FileChooser()
fileChooser.extensionFilters.addAll(
FileChooser.ExtensionFilter("Prolog file", "*.pl"),
FileChooser.ExtensionFilter("Text file", "*.txt"),
FileChooser.ExtensionFilter("2P file", "*.2p"),
)
fileChooser.initialDirectory = File(System.getProperty("user.home"))
fileChooser.title = "Save file as..."
val file = fileChooser.showSaveDialog(stage)
model.renameFile(model.currentFile!!, file)
model.saveFile(file)
currentFileTab?.file = file
}

@FXML
fun onReloadFilePressed(e: ActionEvent) {
currentFileTab?.let {
it.wholeText = model.currentFile!!.readText()
}
}

@FXML
fun onUndoPressed(e: ActionEvent) {
currentFileTab?.let {
it.codeArea.undo()
}
}

@FXML
fun onRedoPressed(e: ActionEvent) {
currentFileTab?.let {
it.codeArea.redo()
}
}

@FXML
fun onCutPressed(e: ActionEvent) {
currentFileTab?.let {
it.codeArea.cut()
}
}

@FXML
fun onCopyPressed(e: ActionEvent) {
currentFileTab?.let {
it.codeArea.copy()
}
}

@FXML
fun onPastePressed(e: ActionEvent) {
currentFileTab?.let {
it.codeArea.paste()
}
}

@FXML
fun onDeletePressed(e: ActionEvent) {
currentFileTab?.let {
it.codeArea.deleteText(it.codeArea.selection)
}
}

@FXML
fun onSelectAllPressed(e: ActionEvent) {
currentFileTab?.let {
it.codeArea.selectAll()
}
}

@FXML
fun onUnselectAllPressed(e: ActionEvent) {
currentFileTab?.let {
it.codeArea.deselect()
}
}

@FXML
fun onSettingsPressed(e: ActionEvent) {

}

@FXML
fun onAbout(e: ActionEvent) {
val dialog = Alert(Alert.AlertType.INFORMATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ interface PrologIDEModel {

fun selectFile(file: File)

fun closeFile(file: File)

fun getFile(file: File): String

fun setFile(file: File, theory: String)

fun renameFile(file: File, newFile: File)

fun setCurrentFile(theory: String)

fun setStdin(content: String)
Expand Down
10 changes: 10 additions & 0 deletions ide/src/main/kotlin/it/unibo/tuprolog/ui/gui/PrologIDEModelImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol
files[file] = theory
}

override fun renameFile(file: File, newFile: File) {
files[newFile] = files[file]!!
files -= file
}

override fun setCurrentFile(theory: String) {
setFile(currentFile!!, theory)
}
Expand Down Expand Up @@ -199,6 +204,11 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol
}
}

override fun closeFile(file: File) {
files -= file
onFileClosed.push(file)
}

override var query: String = ""

// override var goal: Struct
Expand Down
28 changes: 14 additions & 14 deletions ide/src/main/resources/it/unibo/tuprolog/ui/gui/PrologIDEView.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="btnNewFile" mnemonicParsing="false" onAction="#onNewFilePressed" text="New" />
<MenuItem fx:id="btnOpenFile" disable="true" mnemonicParsing="false" text="Open…" />
<MenuItem fx:id="btnOpenFile" mnemonicParsing="false" onAction="#onOpenFilePressed" text="Open…" />
<Menu disable="true" mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem fx:id="btnCloseFile" disable="true" mnemonicParsing="false" text="Close" />
<MenuItem fx:id="btnSaveFile" disable="true" mnemonicParsing="false" text="Save" />
<MenuItem disable="true" mnemonicParsing="false" text="Save As…" />
<MenuItem fx:id="btnReloadFile" disable="true" mnemonicParsing="false" text="Revert" />
<MenuItem fx:id="btnCloseFile" mnemonicParsing="false" onAction="#onCloseFilePressed" text="Close" />
<MenuItem fx:id="btnSaveFile" mnemonicParsing="false" onAction="#onSaveFilePressed" text="Save" />
<MenuItem fx:id="btnSaveFileAs" mnemonicParsing="false" onAction="#onSaveFileAsPressed" text="Save As…" />
<MenuItem fx:id="btnReloadFile" mnemonicParsing="false" onAction="#onReloadFilePressed" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem fx:id="btnSettings" disable="true" mnemonicParsing="false" text="Settings…" />
<MenuItem fx:id="btnSettings" disable="true" mnemonicParsing="false" onAction="#onSettingsPressed" text="Settings…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem fx:id="btnQuit" mnemonicParsing="false" onAction="#onQuitRequested" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem disable="true" mnemonicParsing="false" text="Undo" />
<MenuItem disable="true" mnemonicParsing="false" text="Redo" />
<MenuItem fx:id="btnUndo" mnemonicParsing="false" onAction="#onUndoPressed" text="Undo" />
<MenuItem fx:id="btnRedo" mnemonicParsing="false" onAction="#onRedoPressed" text="Redo" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem disable="true" mnemonicParsing="false" text="Cut" />
<MenuItem disable="true" mnemonicParsing="false" text="Copy" />
<MenuItem disable="true" mnemonicParsing="false" text="Paste" />
<MenuItem disable="true" mnemonicParsing="false" text="Delete" />
<MenuItem fx:id="btnCut" mnemonicParsing="false" onAction="#onCutPressed" text="Cut" />
<MenuItem fx:id="btnCopy" mnemonicParsing="false" onAction="#onCopyPressed" text="Copy" />
<MenuItem fx:id="btnPaste" mnemonicParsing="false" onAction="#onPastePressed" text="Paste" />
<MenuItem fx:id="btnDelete" mnemonicParsing="false" onAction="#onDeletePressed" text="Delete" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem disable="true" mnemonicParsing="false" text="Select All" />
<MenuItem disable="true" mnemonicParsing="false" text="Unselect All" />
<MenuItem fx:id="btnSelectAll" mnemonicParsing="false" onAction="#onSelectAllPressed" text="Select All" />
<MenuItem fx:id="btnUnselectAll" mnemonicParsing="false" onAction="#onUnselectAllPressed" text="Unselect All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
Expand Down

0 comments on commit 66a561b

Please sign in to comment.