Skip to content

Commit

Permalink
Merge pull request #3 from DMeechan/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
DMeechan authored Mar 24, 2017
2 parents 6d29c0b + fbe22e1 commit 91e30e5
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 123 deletions.
Binary file modified download/Simply Done.exe
Binary file not shown.
Binary file modified download/Simply Done.jar
Binary file not shown.
4 changes: 3 additions & 1 deletion src/ClockView.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ private void setActiveTask() {
//System.out.println("New active task: " + activeTask.getName() + " of length: " + activeTask.getMinutes());

// update global
setActiveColour(activeTask.getColour());
java.awt.Color awtColor = activeTask.getColour();
javafx.scene.paint.Color fxColor = javafx.scene.paint.Color.rgb(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue(), awtColor.getAlpha() / 255.0);
setActiveColour(fxColor);
setTaskLength(activeTask.getMinutes() * 60);
taskLabel.setText(activeTask.getName().toUpperCase());

Expand Down
208 changes: 145 additions & 63 deletions src/SchedulerController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.reflect.TypeToken;
import com.jfoenix.controls.*;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
Expand All @@ -15,23 +15,27 @@
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import org.controlsfx.glyphfont.Glyph;
import org.hildan.fxgson.FxGson;

import java.awt.*;
import java.io.*;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Optional;
import java.util.ResourceBundle;

Expand All @@ -50,10 +54,11 @@ public class SchedulerController implements Initializable {
// store them in separate lists so can easily move tasks between them
private ObservableList<Task> notDoneTasks, doneTasks;
@FXML private JFXListView<Task> tasksListView;
@FXML private VBox tasksContainer;
private final BooleanProperty sceneActive = new SimpleBooleanProperty();
private BooleanProperty reset = new SimpleBooleanProperty();

private final BooleanProperty reset = new SimpleBooleanProperty();
private File savedTasksFile;
private final Gson gson = FxGson.coreBuilder().setPrettyPrinting().disableHtmlEscaping().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

public SchedulerController() {
setSceneActive(true);
Expand All @@ -76,6 +81,9 @@ public SchedulerController() {
// set up taskViewList listeners and CustomCells
initializeTaskViewList();

// set up custom tasksContainer
//displayTasks(false);

// listen for changes so edit task pane is disabled
// if a task is moved or deleted by CustomCell
// to prevent bugs from occurring
Expand Down Expand Up @@ -142,10 +150,20 @@ private void loadSaveData() {

if (savedTasksFile.exists()) {

ObservableList<Task> list = FXCollections.observableArrayList();
//ObservableList<Task> list = FXCollections.observableArrayList();

try {
list.addAll(readGsonStream(savedTasksFile));
ArrayList<Task> list = readGsonStream(savedTasksFile);
for (Task task : list) {
if(task.isNotDone()) {
notDoneTasks.add(task);
} else {
doneTasks.add(task);
}
}

/*
//list.addAll(readGsonStream(savedTasksFile));
for (Iterator<Task> item = list.iterator(); item.hasNext();) {
Task task = item.next();
Expand All @@ -155,6 +173,7 @@ private void loadSaveData() {
doneTasks.add(task);
}
}
*/

} catch (IOException e) {
System.out.println("Error reading file. Please turn it off and on again.");
Expand All @@ -167,25 +186,9 @@ private void loadSaveData() {

}

private ObservableList<Task> readGsonStream(File tasksFile) throws IOException {
Gson gson = FxGson.coreBuilder().setPrettyPrinting().disableHtmlEscaping().create();

// change input to abc
JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(tasksFile), "UTF-8"));
ObservableList<Task> list = FXCollections.observableArrayList();
reader.beginArray();
while (reader.hasNext()) {
Task task = gson.fromJson(reader, Task.class);
list.add(task);
}
reader.endArray();
reader.close();
return list;

}

public void writeSaveData() {
ObservableList<Task> list = FXCollections.observableArrayList();
//ObservableList<Task> list = FXCollections.observableArrayList();
ArrayList<Task> list = new ArrayList<Task>();
list.addAll(getNotDoneTasks());
list.addAll(getDoneTasks());

Expand All @@ -199,36 +202,47 @@ public void writeSaveData() {

}

private void writeGsonStream(ObservableList<Task> list) throws IOException {
//OutputStream savedTasksStream = getClass().getClassLoader().getResourceAsStream("tasks.json");
private ArrayList<Task> readGsonStream(File tasksFile) throws IOException {
//ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(tasksFile));
InputStream inputStream = new FileInputStream(tasksFile);
InputStreamReader isr = new InputStreamReader(inputStream, "UTF-8");
//JsonReader reader = new JsonReader(isr);

Gson gson = FxGson.coreBuilder().setPrettyPrinting().disableHtmlEscaping().create();
Type listType = new TypeToken<ArrayList<Task>>() {}.getType();

return gson.fromJson(isr, listType);
}

private void writeGsonStream(ArrayList<Task> target) throws IOException {
Type listType = new TypeToken<ArrayList<Task>>() {}.getType();

OutputStream outputStream = new FileOutputStream(savedTasksFile);
//ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(savedTasksFile));

JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.setIndent(" ");
writer.beginArray();
for (Task task : list) {
gson.toJson(task, Task.class, writer);
//try (JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8"))) {
try (OutputStreamWriter osr = new OutputStreamWriter(outputStream, "UTF-8")) {
gson.toJson(target, listType, osr);
//writer.endObject(); // ?
}
writer.endArray();
writer.close();

}

// OLD GSON READER

private void addSampleData() {
newTask("> This is a task. Here's how you can make your own:", 1, Color.web("#2ecc71"));
newTask("---> 1. Type the name of a task into the textbox at the top", 2, Color.web("#3498db"));
newTask("---> 2. Drag the slider to the amount of time you expect the task will take", 3, Color.web("#e74c3c"));
newTask("---> 3. Click the coloured square on the right and choose a colour for the task", 4, Color.web("#e67e22"));
newTask("---> 4. Click the 'Add Task' button to add the task to your to-do list", 5, Color.web("#4db6ac"));
newTask("> Click the checkbox on the right to mark this task as complete!", 6, Color.web("#ffa726"));
newTask("---> To view all your completed tasks, click 'Show Completed Tasks' above", 7, Color.web("#ba68c8"));
newTask("---> Once you've added all your tasks, click the Start Tasks button below", 8, Color.web("#4E6A9C"));
newTask("---> Then a timer will start counting the time left for the first task", 9, Color.web("#66bb6a"));
newTask("---> And the total time you've got left until every task should be done", 10, Color.web("#64b5f6"));
newTask("> Delete each of these tasks and then have fun getting everything Simply Done!", 11, Color.web("#12854a"));

newTask("> This is a task. Here's how you can make your own:", 1, Color.decode("#2ecc71"));
newTask("---> 1. Type the name of a task into the textbox at the top", 2, Color.decode("#3498db"));
newTask("---> 2. Drag the slider to the amount of time you expect the task will take", 3, Color.decode("#e74c3c"));
newTask("---> 3. Click the coloured square on the right and choose a colour for the task", 4, Color.decode("#e67e22"));
newTask("---> 4. Click the 'Add Task' button to add the task to your to-do list", 5, Color.decode("#4db6ac"));
newTask("> Click the checkbox on the right to mark this task as complete!", 6, Color.decode("#ffa726"));
newTask("---> To view all your completed tasks, click 'Show Completed Tasks' above", 7, Color.decode("#ba68c8"));
newTask("---> Once you've added all your tasks, click the Start Tasks button below", 8, Color.decode("#4E6A9C"));
newTask("---> Then a timer will start counting the time left for the first task", 9, Color.decode("#66bb6a"));
newTask("---> And the total time you've got left until every task should be done", 10, Color.decode("#64b5f6"));
newTask("> Delete each of these tasks and then have fun getting everything Simply Done!", 11, Color.decode("#12854a"));


}

Expand All @@ -241,7 +255,7 @@ private void addSampleData() {
Task task = tasksListView.getSelectionModel().getSelectedItem();
task.setMinutes(Integer.parseInt(newTaskMinsLabel.getText()));
task.setName(newTaskNameTextField.getText());
task.setColour(newTaskColour.getValue());
task.setColour(colorFxToAwt(newTaskColour.getValue()));

deactivateEditMode();

Expand All @@ -257,9 +271,8 @@ private void addSampleData() {
}

@FXML private void clickColourPicker() {
Color colour = newTaskColour.getValue();
// update the colours of the edit pane when the user picks a colour
updateEditModeColours(colour);
updateEditModeColours(colorFxToAwt(newTaskColour.getValue()));
}

@FXML private void clickStartTasks() {
Expand Down Expand Up @@ -317,12 +330,12 @@ private void addSampleData() {

private void resetEditModeUI() {
// after adding / editing a task, reset the edit task box to its default values
Color colour = Color.web("#12854A");
javafx.scene.paint.Color colour = javafx.scene.paint.Color.web("#12854A");
//String c = ClockView.colorToHex(colour);
//newTaskColour.setStyle("fx-base: " + c);
newTaskColour.setValue(colour);
// set colours back to default
updateEditModeColours(colour);
updateEditModeColours(colorFxToAwt(colour));

editTaskBox.setStyle("-fx-background-color: transparent");
newTaskButton.setText("ADD TASK");
Expand All @@ -331,9 +344,10 @@ private void resetEditModeUI() {

}

private void updateEditModeColours(Color colour) {
private void updateEditModeColours(Color awtColor) {
// need to format the string because otherwise it's returned in a weird format
// the weird format starts in 0x and ends in 2 additional characters for the alpha layer
javafx.scene.paint.Color colour = colorAwtToFx(awtColor);
String c = String.format( "#%02X%02X%02X",
(int)( colour.getRed() * 255 ),
(int)( colour.getGreen() * 255 ),
Expand Down Expand Up @@ -369,7 +383,7 @@ private void activateEditMode() {

//String c = ClockView.colorToHex(task.getColour());
//newTaskColour.setStyle("fx-base: " + c);
newTaskColour.setValue(task.getColour());
newTaskColour.setValue(colorAwtToFx(task.getColour()));
updateEditModeColours(task.getColour());
editTaskBox.setStyle("-fx-background-color: #e3e9ed");

Expand All @@ -385,7 +399,7 @@ private void newTask() {
if(notDoneTasks.size() >= 12) {
Main.outputError("Too many tasks. Please complete some of them first!");
} else {
notDoneTasks.add(new Task(newTaskNameTextField.getText(), Integer.parseInt(newTaskMinsLabel.getText()),newTaskColour.getValue()));
notDoneTasks.add(new Task(newTaskNameTextField.getText(), Integer.parseInt(newTaskMinsLabel.getText()),colorFxToAwt(newTaskColour.getValue())));
}
}

Expand Down Expand Up @@ -440,14 +454,76 @@ public File getSavedTasksFile() {
return savedTasksFile;
}

public void setSavedTasksFile(File savedTasksFile) {
this.savedTasksFile = savedTasksFile;

public void displayTasks(boolean isDisplayingCompletedTasks) {
createTaskCells(tasksContainer, getNotDoneTasks());

}


private void createTaskCells(VBox container, ObservableList<Task> taskList) {
for(Task task : taskList) {
TaskCell cell = new TaskCell(task);
container.getChildren().add(cell);

cell.statusProperty().addListener(v -> {
switch(cell.getStatus()) {
// move to not done
case 0:
moveTask(task, cell, notDoneTasks, doneTasks);
break;
// move to done
case 1:
moveTask(task, cell, doneTasks, notDoneTasks);
break;
// delete
case 2:
if (task.isNotDone()) {
notDoneTasks.remove(task);
} else {
doneTasks.remove(task);
}
container.getChildren().remove(cell);
break;
default:
break;
}
});

}
}

private void moveTask(Task task, TaskCell cell, ObservableList<Task> moveTo, ObservableList<Task> moveFrom) {

if (moveTo.size() < 12) {
task.setNotDone(true);
cell.setStatus(0);
moveFrom.remove(task);
moveTo.add(task);
}
}

private java.awt.Color colorFxToAwt(javafx.scene.paint.Color fx) {
return new Color((float) fx.getRed(),
(float) fx.getGreen(),
(float) fx.getBlue(),
(float) fx.getOpacity());
}

private javafx.scene.paint.Color colorAwtToFx(java.awt.Color awtColor) {
int r = awtColor.getRed();
int g = awtColor.getGreen();
int b = awtColor.getBlue();
int a = awtColor.getAlpha();
double opacity = a / 255.0 ;

return javafx.scene.paint.Color.rgb(r, g, b, opacity);
}

//////////////////////////////
// CUSTOM CELL FOR LISTVIEW //
//////////////////////////////

static class CustomCell extends ListCell<Task> {

private final Glyph trash = new Glyph("FontAwesome", "TRASH_ALT");
Expand All @@ -459,7 +535,7 @@ static class CustomCell extends ListCell<Task> {
final Text secondsText = new Text(":00");
final Separator separator = new Separator();
final Text taskNameText = new Text("TASK NAME TEXT");
final Pane pane = new Pane();
final Pane spacer = new Pane();
final JFXButton deleteButton = new JFXButton("");
final JFXButton doneButton = new JFXButton("");

Expand Down Expand Up @@ -545,28 +621,34 @@ public void updateItem(Task task, boolean empty) {
// SETTING UP THE CELL

private void setProperties() {
container.setAlignment(Pos.CENTER);

container.setAlignment(Pos.CENTER_LEFT);
container.setPrefSize(390.0, 25.0);

separator.setOrientation(Orientation.VERTICAL);
HBox.setMargin(separator, new Insets(0, 13.0, 0, 13.0));

//HBox.setHgrow(spacer, Priority.SOMETIMES);

setupText(minutesText, 14.0, 3.0, 0.0, 3.0, 0.0, TextAlignment.RIGHT);
setupText(secondsText, 14.0, 3.0, 0.0, 3.0, 0.0, TextAlignment.LEFT);
setupText(taskNameText, 12.0, 5.0, 5.0, 5.0, 5.0, TextAlignment.CENTER);

minutesText.setWrappingWidth(19);
secondsText.setWrappingWidth(19);

//minutesText.setWrappingWidth(50.0);
HBox.setHgrow(taskNameText, Priority.ALWAYS);
HBox.setHgrow(pane, Priority.ALWAYS);
HBox.setHgrow(spacer, Priority.ALWAYS);

setupButton(deleteButton);
setupButton(doneButton);

deleteButton.setGraphic(trash);
doneButton.setGraphic(check);

container.getChildren().addAll(minutesText, secondsText, separator, taskNameText, pane, deleteButton, doneButton);
container.getChildren().addAll(minutesText, secondsText,
//spacer,
separator, taskNameText, spacer, deleteButton, doneButton);
}

private void setupText(Text text, double fontSize, double top, double right, double bottom, double left, TextAlignment alignment) {
Expand Down
Loading

0 comments on commit 91e30e5

Please sign in to comment.