Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ancavar committed May 1, 2023
1 parent bd88cf0 commit da4cfdf
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 77 deletions.
5 changes: 2 additions & 3 deletions app/src/main/kotlin/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package app

import app.view.MainView
import tornadofx.*
class MyApp : App(MainView::class){
}
class MyApp : App(MainView::class)

fun main() {
launch<MyApp>()
}
}
12 changes: 5 additions & 7 deletions app/src/main/kotlin/app/controller/AVLController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import javafx.scene.text.TextBoundsType
import tornadofx.Controller
import kotlin.math.min

class AVLController: Controller() {
class AVLController : Controller() {
fun isNumeric(s: String): Boolean {
return try {
s.toInt()
Expand All @@ -36,7 +36,7 @@ class AVLController: Controller() {
treePane.children.clear()
}

//make here not null check
// make here not null check
fun drawTree(tree: AVLTree<Int, String>, treePane: Pane) {
treePane.children.clear()
val root = tree.getRoot()
Expand Down Expand Up @@ -66,7 +66,6 @@ class AVLController: Controller() {

treePane.children.add(nodeStackPane)


if (node.left != null) {
val leftX = x - offsetX
val leftY = y + 50
Expand Down Expand Up @@ -95,8 +94,7 @@ class AVLController: Controller() {
}
fun getTreeFromJson(name: String): AVLTree<Int, String>? {
val controller = JsonController()
val tree = controller.getTree(name)
return tree
return controller.getTree(name)
}

fun deleteTreeFromDB(name: String) {
Expand All @@ -108,8 +106,8 @@ class AVLController: Controller() {
val controller = JsonController()
controller.saveTree(tree, treeName)
}
fun deleteNode(value: Int, tree: AVLTree<Int, String>, treePane: Pane){
fun deleteNode(value: Int, tree: AVLTree<Int, String>, treePane: Pane) {
tree.remove(value)
drawTree(tree, treePane)
}
}
}
17 changes: 8 additions & 9 deletions app/src/main/kotlin/app/controller/BSTController.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package app.controller

import bst.BSTree
import bst.nodes.BSTNode
import javafx.scene.layout.Pane
import javafx.scene.paint.Color
import javafx.scene.shape.Circle
import javafx.scene.shape.Line

import tornadofx.Controller
import bst.db.controllers.SQLController
import bst.nodes.BSTNode
import javafx.collections.FXCollections.observableArrayList
import javafx.collections.ObservableList
import javafx.scene.control.Tooltip
import javafx.scene.layout.Pane
import javafx.scene.layout.StackPane
import javafx.scene.paint.Color
import javafx.scene.shape.Circle
import javafx.scene.shape.Line
import javafx.scene.text.Text
import javafx.scene.text.TextBoundsType
import tornadofx.Controller
import kotlin.math.min

class BSTController : Controller() {
Expand All @@ -37,7 +36,7 @@ class BSTController : Controller() {
treePane.children.clear()
}

//make here not null check
// make here not null check
fun drawTree(tree: BSTree<Int, String>, treePane: Pane) {
treePane.children.clear()
val root = tree.getRoot()
Expand Down Expand Up @@ -109,7 +108,7 @@ class BSTController : Controller() {
val controller = SQLController()
controller.saveTree(tree, treeName)
}
fun deleteNode(value: Int, tree: BSTree<Int, String>, treePane: Pane){
fun deleteNode(value: Int, tree: BSTree<Int, String>, treePane: Pane) {
tree.remove(value)
drawTree(tree, treePane)
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/kotlin/app/controller/RBTController.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app.controller

import bst.RedBlackTree
import bst.nodes.RBTNode
import bst.db.controllers.Neo4jController
import bst.nodes.RBTNode
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.scene.control.Tooltip
Expand All @@ -16,7 +16,7 @@ import javafx.scene.text.TextBoundsType
import tornadofx.Controller
import kotlin.math.min

class RBTController: Controller() {
class RBTController : Controller() {
fun isNumeric(s: String): Boolean {
return try {
s.toInt()
Expand All @@ -36,7 +36,7 @@ class RBTController: Controller() {
treePane.children.clear()
}

//make here not null check
// make here not null check
fun drawTree(tree: RedBlackTree<Int, String>, treePane: Pane) {
treePane.children.clear()
val root = tree.getRoot()
Expand Down Expand Up @@ -106,7 +106,7 @@ class RBTController: Controller() {
val controller = Neo4jController()
controller.saveTree(tree, treeName)
}
fun deleteNode(value: Int, tree: RedBlackTree<Int, String>, treePane: Pane){
fun deleteNode(value: Int, tree: RedBlackTree<Int, String>, treePane: Pane) {
tree.remove(value)
drawTree(tree, treePane)
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/app/view/MainView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package app.view
import app.view.treeView.BinarySearchTreeView
import tornadofx.*

class MainView: View(){
val tree: BinarySearchTreeView by inject()
class MainView : View() {
private val tree: BinarySearchTreeView by inject()
override val root = vbox {
add(tree)
}
Expand Down
31 changes: 14 additions & 17 deletions app/src/main/kotlin/app/view/treeView/AVLTreeView.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package app.view.treeView
import app.controller.AVLController
import bst.AVLTree
import bst.BSTree
import javafx.beans.property.SimpleStringProperty
import javafx.scene.control.Alert
import javafx.scene.layout.Pane
import tornadofx.*
class AVLTreeView: View() {

class AVLTreeView : View() {
private val controller: AVLController by inject()
private var tree = AVLTree<Int, String>()
private val treePane = Pane()
Expand All @@ -15,7 +15,7 @@ class AVLTreeView: View() {
private var trees = controller.getTreesList()
private var selectedItem: String? = ""
private val treeName = SimpleStringProperty()
private val valueFotDeletion = SimpleStringProperty()
private val valueForDeletion = SimpleStringProperty()

init {
// Add listeners to the width and height properties of the scene
Expand Down Expand Up @@ -71,14 +71,14 @@ class AVLTreeView: View() {
}
form {
fieldset {
field("Key input") {
field("Key") {
textfield(key)
}
field("Value input") {
field("Value") {
textfield(value)
}

button("Add Node") {
button("Add") {
action {
if (key.value != null && value.value != null && controller.isNumeric(key.value)) {
controller.insertNode(tree, treePane, key.value.toInt(), value.value)
Expand All @@ -89,15 +89,14 @@ class AVLTreeView: View() {
value.value = ""
}
}
field("Key input"){
textfield(valueFotDeletion)
field("Key") {
textfield(valueForDeletion)
}
button("Delete node"){
button("Delete") {
action {
if (controller.isNumeric(valueFotDeletion.value)){
controller.deleteNode(valueFotDeletion.value.toInt(), tree, treePane)
}
else{
if (controller.isNumeric(valueForDeletion.value)) {
controller.deleteNode(valueForDeletion.value.toInt(), tree, treePane)
} else {
alert(type = Alert.AlertType.ERROR, header = "Deletion Error")
}
}
Expand All @@ -114,10 +113,8 @@ class AVLTreeView: View() {
if (!availableTrees.items.contains(treeName.value)) {
availableTrees.items.add(treeName.value)
}
}
else{
} else {
alert(type = Alert.AlertType.ERROR, header = "Can not save tree with empty root")

}
}
}
Expand All @@ -141,4 +138,4 @@ class AVLTreeView: View() {
style = "-fx-border-color: black;"
}
}
}
}
32 changes: 14 additions & 18 deletions app/src/main/kotlin/app/view/treeView/BinarySearchTreeView.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package app.view.treeView

import tornadofx.*
import app.controller.BSTController
import bst.BSTree
import javafx.beans.property.SimpleStringProperty
import javafx.scene.layout.Pane
import javafx.scene.control.Alert

import javafx.scene.layout.Pane
import tornadofx.*

class BinarySearchTreeView : View() {
private val controller: BSTController by inject()
Expand All @@ -17,7 +16,7 @@ class BinarySearchTreeView : View() {
private var trees = controller.getTreesList()
private var selectedItem: String? = ""
private val treeName = SimpleStringProperty()
private val valueFotDeletion = SimpleStringProperty()
private val valueForDeletion = SimpleStringProperty()

init {
// Add listeners to the width and height properties of the scene
Expand Down Expand Up @@ -73,14 +72,14 @@ class BinarySearchTreeView : View() {
}
form {
fieldset {
field("Key input") {
field("Key") {
textfield(key)
}
field("Value input") {
field("Value") {
textfield(value)
}

button("Add Node") {
button("Add") {
action {
if (key.value != null && value.value != null && controller.isNumeric(key.value)) {
controller.insertNode(tree, treePane, key.value.toInt(), value.value)
Expand All @@ -91,15 +90,14 @@ class BinarySearchTreeView : View() {
value.value = ""
}
}
field("Key input"){
textfield(valueFotDeletion)
field("Key") {
textfield(valueForDeletion)
}
button("Delete node"){
button("Delete") {
action {
if (controller.isNumeric(valueFotDeletion.value)){
controller.deleteNode(valueFotDeletion.value.toInt(), tree, treePane)
}
else{
if (controller.isNumeric(valueForDeletion.value)) {
controller.deleteNode(valueForDeletion.value.toInt(), tree, treePane)
} else {
alert(type = Alert.AlertType.ERROR, header = "Deletion Error")
}
}
Expand All @@ -111,15 +109,13 @@ class BinarySearchTreeView : View() {
button("Save tree") {
action {
if (tree.getRoot() != null) {
// tree.treeName = treeName.value
// tree.treeName = treeName.value
controller.saveTree(tree, treeName.value)
if (!availableTrees.items.contains(treeName.value)) {
availableTrees.items.add(treeName.value)
}
}
else{
} else {
alert(type = Alert.AlertType.ERROR, header = "Can not save tree with empty root")

}
}
}
Expand Down
30 changes: 13 additions & 17 deletions app/src/main/kotlin/app/view/treeView/RedBlackTreeView.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package app.view.treeView

import app.controller.RBTController
import tornadofx.*
import bst.RedBlackTree
import bst.db.controllers.Neo4jController
import javafx.beans.property.SimpleStringProperty
import javafx.scene.layout.Pane
import javafx.scene.control.Alert

import javafx.scene.layout.Pane
import tornadofx.*

class RedBlackTreeView : View() {
private val controller: RBTController by inject()
Expand All @@ -18,7 +16,7 @@ class RedBlackTreeView : View() {
private var trees = controller.getTreesList()
private var selectedItem: String? = ""
private val treeName = SimpleStringProperty()
private val valueFotDeletion = SimpleStringProperty()
private val valueForDeletion = SimpleStringProperty()

init {
// Add listeners to the width and height properties of the scene
Expand Down Expand Up @@ -74,14 +72,14 @@ class RedBlackTreeView : View() {
}
form {
fieldset {
field("Key input") {
field("Key") {
textfield(key)
}
field("Value input") {
field("Value") {
textfield(value)
}

button("Add Node") {
button("Add") {
action {
if (key.value != null && value.value != null && controller.isNumeric(key.value)) {
controller.insertNode(tree, treePane, key.value.toInt(), value.value)
Expand All @@ -92,15 +90,14 @@ class RedBlackTreeView : View() {
value.value = ""
}
}
field("Value input"){
textfield(valueFotDeletion)
field("Value") {
textfield(valueForDeletion)
}
button("Delete node"){
button("Delete") {
action {
if (controller.isNumeric(valueFotDeletion.value)){
controller.deleteNode(valueFotDeletion.value.toInt(), tree, treePane)
}
else{
if (controller.isNumeric(valueForDeletion.value)) {
controller.deleteNode(valueForDeletion.value.toInt(), tree, treePane)
} else {
alert(type = Alert.AlertType.ERROR, header = "Deletion Error")
}
}
Expand All @@ -117,8 +114,7 @@ class RedBlackTreeView : View() {
if (!availableTrees.items.contains(treeName.value)) {
availableTrees.items.add(treeName.value)
}
}
else{
} else {
alert(type = Alert.AlertType.ERROR, header = "Can not save tree with empty root")
}
}
Expand Down

0 comments on commit da4cfdf

Please sign in to comment.