diff --git a/app/src/main/java/org/cutem/cutecalendar/presenter/newitems/ConferenceTypeNewItemBuilder.java b/app/src/main/java/org/cutem/cutecalendar/presenter/newitems/ConferenceTypeNewItemBuilder.java new file mode 100644 index 0000000..16d0757 --- /dev/null +++ b/app/src/main/java/org/cutem/cutecalendar/presenter/newitems/ConferenceTypeNewItemBuilder.java @@ -0,0 +1,125 @@ +package org.cutem.cutecalendar.presenter.newitems; + +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.control.TextField; +import org.cutem.cutecalendar.model.TodoItem; +import org.cutem.cutecalendar.model.TodoItemFactory; +import org.cutem.cutecalendar.model.TodoItemUtil; +import org.cutem.cutecalendar.util.CalendarUtil; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.util.Calendar; + +public class ConferenceTypeNewItemBuilder extends NewItemBuilder { + public static class Factory implements IFactory { + @Override + public String getTypeName() { + return TYPE_NAME; + } + + @Override + public NewItemBuilder construct(@Nullable TodoItem parent) { + return new OtherTypeNewItemBuilder(parent); + } + } + + private static final String CONFERENCE_TYPE_LAYOUT = "fxml/newitem_layouts/conference_type_layout.fxml"; + + private static final int TYPE = TodoItem.CONFERENCE; + + private static final String TYPE_NAME = TodoItem.typeName(TYPE); + + private ClassLoader mClassLoader = getClass().getClassLoader(); + + @FXML + private TextField titleText; + @FXML + private TextField bgnTimeText; + @FXML + private TextField endTimeText; + @FXML + private TextField locationText; + @FXML + private TextField descriptionText; + + protected ConferenceTypeNewItemBuilder(TodoItem parent) { + super(parent); + + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(mClassLoader.getResource(CONFERENCE_TYPE_LAYOUT)); + loader.setRoot(this); + loader.setController(this); + + try { + loader.load(); + } catch (IOException e) { + throw new IllegalStateException("cannot load " + CONFERENCE_TYPE_LAYOUT); + } + } + + @Override + public @Nullable Object[] buildItem() { + + String b = bgnTimeText.getText(); + String e = endTimeText.getText(); + String title = titleText.getText(); + String location = locationText.getText(); + String description = descriptionText.getText(); + + if (title.isEmpty()) { // fail + showAlert("议题还没有填"); + return null; + } else if (location.isEmpty()) { // fail + showAlert("地点还没有填"); + return null; + } else if (description.isEmpty()) { // fail + showAlert("内容还没有填"); + return null; + } else if (b.trim().isEmpty() || e.trim().isEmpty()) { + showAlert("时间段还没有填"); + return null; + } + + + // try build a TodoItem + // failed? + if (!CalendarUtil.isValidCalendarString(b) || !CalendarUtil.isValidCalendarString(e)) { + showAlert("give a valid time"); + return null; + } + Calendar c1 = CalendarUtil.constructCalendar(b); + Calendar c2 = CalendarUtil.constructCalendar(e); + + // failed? + if (c2.before(c1)) { + showAlert("开始时间在结束时间后"); + return null; + } + + // build + TodoItemFactory fac = new TodoItemFactory(); + fac.type(TYPE).beginTime(c1).endTime(c2).stringProperties(TodoItem.TITLE, title).stringProperties(TodoItem.LOCATION, location).stringProperties(TodoItem.DESCRIPTION, description); + TodoItem item = fac.build(); + + if (checkConflict(item)) { + if (mParent == null) { + return new Object[]{item}; + } else { + TodoItem[] res = TodoItemUtil.bind(mParent, item); + return new Object[]{res[0], res[1]}; + } + } else { // fail + return null; + } + + } + + @Override + public String getTypeName() { + return TYPE_NAME; + } + + +}