refactor NewTodoItemStrategy

This commit is contained in:
hlq07 2018-05-28 03:06:55 +08:00
parent 8e463a3ec2
commit 323de79a96
3 changed files with 310 additions and 116 deletions

View File

@ -1,138 +1,192 @@
package org.cutem.cutecalendar.presenter; package org.cutem.cutecalendar.presenter;
import javafx.collections.FXCollections; import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox; import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.cutem.cutecalendar.model.TodoItem; import org.cutem.cutecalendar.model.TodoItem;
import org.cutem.cutecalendar.model.TodoItemFactory; import org.cutem.cutecalendar.model.TodoItemFactory;
import org.cutem.cutecalendar.model.TodoItemManager;
import org.cutem.cutecalendar.model.TodoItemUtil;
import org.cutem.cutecalendar.util.CalendarUtil; import org.cutem.cutecalendar.util.CalendarUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException; import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import static org.cutem.cutecalendar.model.TodoItem.*;
/** /**
* @author akasakaisami * @author akasakaisami
*/ */
public class NewTodoItemStrategy { public class NewTodoItemStrategy {
private static final String FXML_FILE = "fxml/addTodoItemStage.fxml";
private Parent root;
private ChoiceBox<String> typeChoicebox;
private TextField titleInput;
private TextField bgnInput;
private TextField endInput;
private TextField locationInput;
private TextField personnelInput;
private TextArea descriptionInput;
private Button finish;
private Button cancel;
private Stage addStage;
private Presenter mPresenter;
private ClassLoader mClassLoader = getClass().getClassLoader();
public NewTodoItemStrategy() {
initialize(); private Presenter mPresenter;
private TodoItem mNowParent;
public Stage newItemStage;
public ChoiceBox<Integer> typeBox;
public HashMap<String, StringProperty> props = new HashMap<>();
public void initialize() {
} }
private boolean checkFormation(@NotNull String name, @NotNull String value) {
switch (name) {
case "begin":
case "end":
case ALARM_START: {
return CalendarUtil.isValidCalendarString(value);
}
case REPEAT_WEEKS: {
return value.matches("\\d+");
}
case ALARM_INTERVAL: {
return TodoItemUtil.isValidAlarmIntString(value);
}
case ALARM_METHOD: {
return value.equals("area") || value.equals("dialog") || value.equals("none");
}
default: {
return true;
}
}
}
private boolean checkConflict(@Nullable TodoItem parent, @NotNull TodoItem newItem) {
if (parent != null && !TodoItemUtil.canBind(parent, newItem)) {
return false;
}
Collection<TodoItem> items = TodoItemManager.getInstance().getItems();
for (TodoItem item : items) {
if (parent != null && !item.equals(parent)) {
if (TodoItemUtil.isIntersectedConflict(item, newItem)) {
return false;
}
}
}
return true;
}
private void onClickClose() {
newItemStage.close();
}
private void onClickFinish() {
TodoItemFactory factory = new TodoItemFactory();
Calendar c1 = null;
Calendar c2 = null;
String title = "";
for (String key : props.keySet()) {
String value = props.get(key).getValue().trim();
if (key.equals(TITLE)) {
title = value;
}
if (value.length() <= 0) {
continue;
}
if (!checkFormation(key, value)) {
mPresenter.showAlert(0, key + " bad formatted");
return;
} else {
switch (key) {
case "begin":
c1 = CalendarUtil.constructCalendar(value);
break;
case "end":
c2 = CalendarUtil.constructCalendar(value);
break;
case "personnel":
String[] slices = value.split("[;,]");
factory.personnel(Arrays.asList(slices));
break;
default:
factory.stringProperties(key, value);
break;
}
}
}
if (title.length() <= 0) {
mPresenter.showAlert(0, "plz give me a title");
return;
}
if ((c1 == null && c2 == null)) {
int type = typeBox.getValue();
if (type != OTHERS) {
mPresenter.showAlert(0, "plz finish the time blank");
return;
} else if (mNowParent != null) {
mPresenter.showAlert(0, "cannot add this to-do item without time");
return;
} else {
mPresenter.addTimelessItem(title);
}
} else if (c1 == null || c2 == null) {
mPresenter.showAlert(0, "plz finish the time blank");
return;
} else if (c2.before(c1)) {
mPresenter.showAlert(0, "begin time after the end time");
return;
} else {
int type = typeBox.getValue();
factory.type(type);
TodoItem newItem = factory.build();
if (!checkConflict(mNowParent, newItem)) {
if (mNowParent != null) {
TodoItem[] tItems = TodoItemUtil.bind(mNowParent, newItem);
mPresenter.addTodoItem(tItems);
} else {
mPresenter.addTodoItem(newItem);
}
}
}
onClickClose();
}
private void clear() {
typeBox.setValue(OTHERS);
props.forEach((k, v) -> v.setValue(""));
}
// region Presenter new to-do item
public void attachPresenter(@NotNull Presenter presenter) { public void attachPresenter(@NotNull Presenter presenter) {
mPresenter = presenter; mPresenter = presenter;
} }
private void initialize() {
root = null;
try {
root = FXMLLoader.load(mClassLoader.getResource(FXML_FILE));
} catch (IOException e) {
// impossible
}
Scene scene = new Scene(root, 600, 400);
addStage = new Stage();
addStage.setScene(scene);
addStage.setTitle("Add a Todo Item");
ObservableList<String> ways = FXCollections.observableArrayList();
ways.add("Conference");
ways.add("Dates");
ways.add("Others");
typeChoicebox = (ChoiceBox<String>) root.lookup("#typeChoicebox");
typeChoicebox.setItems(ways);
typeChoicebox.setValue("Conference");
titleInput = (TextField) root.lookup("#titleInput");
bgnInput = (TextField) root.lookup("#bgnInput");
endInput = (TextField) root.lookup("#endInput");
locationInput = (TextField) root.lookup("#locationInput");
personnelInput = (TextField) root.lookup("#personnelInput");
descriptionInput = (TextArea) root.lookup("#descriptionInput");
finish = (Button) root.lookup("#finish");
cancel = (Button) root.lookup("#cancel");
finish.setOnAction(e -> {
String title = titleInput.getText();
String bgn = bgnInput.getText();
String end = endInput.getText();
String location = locationInput.getText();
String personnel = personnelInput.getText();
String description = descriptionInput.getText();
// if (title.equals("") || location.equals("") || personnel.equals("") || description.equals("")) {
// // mPresenter.showAlert(0, "Fail:You have some empty inputs.");
// // return;
// } else
if (!CalendarUtil.isValidCalendarString(bgn) || !CalendarUtil.isValidCalendarString(end)) {
mPresenter.showAlert(0, "Fail:Your CalendarString input is invalid.");
return;
}
TodoItemFactory factory = new TodoItemFactory();
int type = typeChoicebox.getValue().equals("Conference") ? 1 : typeChoicebox.getValue().equals("Dates") ? 2 : 3;
String[] persons = personnel.split(",");
for (String p : persons) {
factory.personnel(p);
}
factory.stringProperties(TodoItem.TITLE, title)
.beginTime(CalendarUtil.constructCalendar(bgn))
.endTime(CalendarUtil.constructCalendar(end))
.type(type)
.stringProperties(TodoItem.LOCATION, location)
.stringProperties(TodoItem.DESCRIPTION, description);
try {
TodoItem newItem = factory.build();
mPresenter.addTodoItem(newItem);
mPresenter.showAlert(0, "Succeed");
addStage.close();
} catch (IllegalStateException ex) {
mPresenter.showAlert(0, "fail");
}
});
cancel.setOnAction(e -> {
addStage.close();
});
}
public void showNewTodoItemStage() { public void showNewTodoItemStage() {
titleInput.setText(""); mNowParent = null;
bgnInput.setText(""); clear();
endInput.setText(""); newItemStage.show();
locationInput.setText("");
personnelInput.setText("");
descriptionInput.setText("");
addStage.show();
} }
public void showNewChildTodoItemStage(@NotNull TodoItem parent) {
mNowParent = parent;
clear();
newItemStage.show();
}
// endregion Presenter new to-do item
} }

View File

@ -0,0 +1,138 @@
package org.cutem.cutecalendar.presenter;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.cutem.cutecalendar.model.TodoItem;
import org.cutem.cutecalendar.model.TodoItemFactory;
import org.cutem.cutecalendar.util.CalendarUtil;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
/**
* @author akasakaisami
*/
public class OldNewTodoItemStrategy {
private static final String FXML_FILE = "fxml/addTodoItemStage.fxml";
private Parent root;
private ChoiceBox<String> typeChoicebox;
private TextField titleInput;
private TextField bgnInput;
private TextField endInput;
private TextField locationInput;
private TextField personnelInput;
private TextArea descriptionInput;
private Button finish;
private Button cancel;
private Stage addStage;
private Presenter mPresenter;
private ClassLoader mClassLoader = getClass().getClassLoader();
public OldNewTodoItemStrategy() {
initialize();
}
public void attachPresenter(@NotNull Presenter presenter) {
mPresenter = presenter;
}
private void initialize() {
root = null;
try {
root = FXMLLoader.load(mClassLoader.getResource(FXML_FILE));
} catch (IOException e) {
// impossible
}
Scene scene = new Scene(root, 600, 400);
addStage = new Stage();
addStage.setScene(scene);
addStage.setTitle("Add a Todo Item");
ObservableList<String> ways = FXCollections.observableArrayList();
ways.add("Conference");
ways.add("Dates");
ways.add("Others");
typeChoicebox = (ChoiceBox<String>) root.lookup("#typeChoicebox");
typeChoicebox.setItems(ways);
typeChoicebox.setValue("Conference");
titleInput = (TextField) root.lookup("#titleInput");
bgnInput = (TextField) root.lookup("#bgnInput");
endInput = (TextField) root.lookup("#endInput");
locationInput = (TextField) root.lookup("#locationInput");
personnelInput = (TextField) root.lookup("#personnelInput");
descriptionInput = (TextArea) root.lookup("#descriptionInput");
finish = (Button) root.lookup("#finish");
cancel = (Button) root.lookup("#cancel");
finish.setOnAction(e -> {
String title = titleInput.getText();
String bgn = bgnInput.getText();
String end = endInput.getText();
String location = locationInput.getText();
String personnel = personnelInput.getText();
String description = descriptionInput.getText();
// if (title.equals("") || location.equals("") || personnel.equals("") || description.equals("")) {
// // mPresenter.showAlert(0, "Fail:You have some empty inputs.");
// // return;
// } else
if (!CalendarUtil.isValidCalendarString(bgn) || !CalendarUtil.isValidCalendarString(end)) {
mPresenter.showAlert(0, "Fail:Your CalendarString input is invalid.");
return;
}
TodoItemFactory factory = new TodoItemFactory();
int type = typeChoicebox.getValue().equals("Conference") ? 1 : typeChoicebox.getValue().equals("Dates") ? 2 : 3;
String[] persons = personnel.split(",");
for (String p : persons) {
factory.personnel(p);
}
factory.stringProperties(TodoItem.TITLE, title)
.beginTime(CalendarUtil.constructCalendar(bgn))
.endTime(CalendarUtil.constructCalendar(end))
.type(type)
.stringProperties(TodoItem.LOCATION, location)
.stringProperties(TodoItem.DESCRIPTION, description);
try {
TodoItem newItem = factory.build();
mPresenter.addTodoItem(newItem);
mPresenter.showAlert(0, "Succeed");
addStage.close();
} catch (IllegalStateException ex) {
mPresenter.showAlert(0, "fail");
}
});
cancel.setOnAction(e -> {
addStage.close();
});
}
public void showNewTodoItemStage() {
titleInput.setText("");
bgnInput.setText("");
endInput.setText("");
locationInput.setText("");
personnelInput.setText("");
descriptionInput.setText("");
addStage.show();
}
}

View File

@ -200,9 +200,11 @@ public abstract class Presenter {
} }
protected final void addTodoItem(@NotNull TodoItem item) { protected final void addTodoItem(@NotNull TodoItem... items) {
manager.add(item); for (TodoItem item : items) {
addAlarm(item); manager.add(item);
addAlarm(item);
}
save(); save();
repaint(); repaint();
} }