Created
April 29, 2016 21:29
-
-
Save dante-byte/21eefd4fec1042b733c9af51b731ad04 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 sample; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.ListView; | |
import javafx.scene.control.TextField; | |
import jodd.json.JsonParser; | |
import jodd.json.JsonSerializer; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.ResourceBundle; | |
import java.util.Scanner; | |
public class Controller implements Initializable { | |
@FXML | |
ListView todoList; | |
@FXML | |
TextField todoText; | |
ObservableList<ToDoItem> todoItems = FXCollections.observableArrayList(); | |
ArrayList<ToDoItem> savableList; | |
String controllerFileName = "controller.json"; | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
retrieveItem(); | |
if(savableList != null){ | |
todoItems.addAll(savableList); | |
} else { | |
savableList = new ArrayList<ToDoItem>(); | |
} | |
todoList.setItems(todoItems); | |
} | |
public void addItem() { | |
System.out.println("Adding item ..."); | |
todoItems.add(new ToDoItem(todoText.getText())); | |
todoText.setText(""); | |
return; | |
} | |
public void removeItem() { | |
System.out.println("Removing item ..."); | |
ToDoItem todoItem = (ToDoItem) todoList.getSelectionModel().getSelectedItem(); | |
System.out.println("Removing " + todoItem.text + " ..."); | |
todoItems.remove(todoItem); | |
} | |
public void toggleItem() { | |
System.out.println("Toggling item ..."); | |
ToDoItem todoItem = (ToDoItem) todoList.getSelectionModel().getSelectedItem(); | |
if (todoItem != null) { | |
todoItem.isDone = !todoItem.isDone; | |
todoList.setItems(null); | |
todoList.setItems(todoItems); | |
} | |
} | |
public void saveItem() { | |
try { | |
File sampleFile = new File(controllerFileName); | |
JsonSerializer jsonSerizlizer = new JsonSerializer().deep(true); | |
MyToDoList theListObject = new MyToDoList(todoItems); | |
String jsonString = jsonSerizlizer.serialize(theListObject); | |
System.out.println(jsonString); | |
FileWriter jsonWriter = new FileWriter(sampleFile); | |
jsonWriter.write(jsonString); | |
jsonWriter.close(); | |
} catch (Exception exception){ | |
exception.printStackTrace(); | |
} | |
} | |
public void retrieveItem() { | |
try { | |
Scanner fileScanner = new Scanner(new File(controllerFileName)); | |
fileScanner.useDelimiter("\\Z"); // read the input until the "end of the input" delimiter | |
String fileContents = fileScanner.next(); | |
JsonParser ToDoItemParser = new JsonParser(); | |
MyToDoList returnList = ToDoItemParser.parse(fileContents, MyToDoList.class); | |
savableList = returnList.todoItems; | |
} catch (IOException ioException) { | |
} | |
} | |
} | |
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 sample; | |
/** | |
* Created by Donta White on 4/22/2016. | |
*/ | |
public class JSONTester { | |
public static void main(String[] args){ | |
} | |
} |
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 sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root, 300, 275)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
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 sample; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by Donta White on 4/29/2016. | |
*/ | |
public class MyToDoList { | |
public ArrayList<ToDoItem> todoItems; | |
public MyToDoList(List<ToDoItem> incomingList){ | |
todoItems = new ArrayList<ToDoItem>(incomingList); | |
} | |
public MyToDoList(){ | |
} | |
} |
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 sample; | |
import javafx.beans.Observable; | |
import javafx.collections.ObservableList; | |
/** | |
* Created by Donta White on 4/21/2016. | |
*/ | |
public class ToDoItem { | |
public String text; | |
public boolean isDone; | |
public ToDoItem(String text) { | |
this.text = text; | |
this.isDone = false; | |
} | |
public ToDoItem() { | |
} | |
@Override | |
public String toString() { | |
if (isDone) { | |
return text + " (done)"; | |
} else { | |
return text; | |
} | |
// A one-line version of the logic above: | |
// return text + (isDone ? " (done)" : ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment