]> gitweb @ CieloNegro.org - task-reporter.git/blobdiff - src/main/scala/jp/ymir/taskReporter/ui/TaskEditor.scala
wip
[task-reporter.git] / src / main / scala / jp / ymir / taskReporter / ui / TaskEditor.scala
index 17b77dd54fe4bf07cdeed07f29fd8e03444e9bd1..84a929eac8f4694826322e5669697d9551c09ec7 100644 (file)
 package jp.ymir.taskReporter.ui
-import com.toedter.calendar._
 import java.awt.Insets
+import java.util.Calendar
 import jp.ymir.taskReporter.core._
+import scala.collection.immutable._
 import scala.swing._
+import scala.swing.event._
 
 class TaskEditor extends GridBagPanel {
-  import GridBagPanel.Anchor._
-  import GridBagPanel.Fill._
+  import GridBagPanel._
 
-  val commonInsets = new Insets(2, 3, 2, 3)
+  case class TaskUpdated(t: Task) extends Event
 
-  add(
-    new Label("報告日"),
-    new Constraints { gridx = 0; gridy = 0 })
-  add(
-    Component.wrap(new JDateChooser() {
-      setDateFormatString("yyyy-MM-dd")
-      getJCalendar().setTodayButtonVisible(true)
-      getJCalendar().setWeekOfYearVisible(false)
-    }),
-    new Constraints() {
-      gridx = 1; gridy = 0; anchor = West
-      ipadx = 3; ipady = 3; insets = commonInsets
-    })
+  def task: (Calendar => Task) = d => Task(
+    date                   = d,
+    ticketID               = noneIfEmpty(ticketID.text).map(_.toInt),
+    title                  = title.text,
+    expectedCompletionDate = expectedCompletionDate.calendar.getOrElse(Task.endOfCurrentHalf),
+    deadline               = deadline.calendar,
+    status                 = status.selection.item,
+    description            = noneIfEmpty(description.text)
+  )
 
+  def task_=(t: Task) = {
+    ticketID.text                   = t.ticketID.fold("")(_.toString)
+    title.text                      = t.title
+    expectedCompletionDate.calendar = Some(t.expectedCompletionDate)
+    deadline.calendar               = t.deadline
+    status.selection.item           = t.status
+    description.text                = t.description.getOrElse("")
+  }
+
+  private val commonInsets = new Insets(2, 3, 2, 3)
+
+  private def noneIfEmpty[T <% StringLike[_]](str: T): Option[T] = str match {
+    case "" => None
+    case _  => Some(str)
+  }
+
+  private val ticketID = new TextField(6) {
+    def isDigit(c : Char) = c >= '0' && c <= '9'
+    inputVerifier       = _ => wrapString(text).forall(isDigit)
+    horizontalAlignment = Alignment.Right
+    peer.setMargin(commonInsets)
+  }
   add(
     new Label("チケットID"),
-    new Constraints { gridx = 0; gridy = 1 })
+    new Constraints { gridx = 0; gridy = 0 })
   add(
-    new TextField(6) {
-      val isDigit = (c : Char) => c >= '0' && c <= '9'
-      inputVerifier = _ => wrapString(text).forall(isDigit)
-      peer.setMargin(commonInsets)
-    },
+    ticketID,
     new Constraints() {
-      gridx = 1; gridy = 1; anchor = West; insets = commonInsets
+      gridx = 1; gridy = 0; anchor = Anchor.West; insets = commonInsets
     })
+  ticketID.minimumSize = ticketID.preferredSize
 
+  private val title = new TextField() {
+    peer.setMargin(commonInsets)
+  }
   add(
     new Label("作業名"),
-    new Constraints { gridx = 0; gridy = 2 })
+    new Constraints { gridx = 0; gridy = 1 })
   add(
-    new TextField() {
-      peer.setMargin(commonInsets)
-    },
+    title,
     new Constraints() {
-      gridx = 1; gridy = 2; fill = Horizontal; weightx = 1.0
+      gridx = 1; gridy = 1; fill = Fill.Horizontal; weightx = 1.0
       insets = commonInsets
     })
 
+  private val expectedCompletionDate = new DateChooser {
+    dateFormatString = "yyyy-MM-dd"
+    calendarChooser.todayButtonVisible = true
+    calendarChooser.weekOfYearVisible  = false
+  }
   add(
     new Label("作業完了予定年月日"),
-    new Constraints { gridx = 0; gridy = 3 })
+    new Constraints { gridx = 0; gridy = 2 })
   add(
-    Component.wrap(new JDateChooser() {
-      setDateFormatString("yyyy-MM-dd")
-      getJCalendar().setTodayButtonVisible(true)
-      getJCalendar().setWeekOfYearVisible(false)
-    }),
+    expectedCompletionDate,
     new Constraints() {
-      gridx = 1; gridy = 3; anchor = West
-      ipadx = 3; ipady = 3; insets = commonInsets
+      gridx = 1; gridy = 2; anchor = Anchor.West
+      ipadx = 3; ipady = 2; insets = commonInsets
     })
+  expectedCompletionDate.minimumSize = expectedCompletionDate.preferredSize
 
+  private val deadline = new DateChooser {
+    dateFormatString = "yyyy-MM-dd"
+    calendarChooser.nullDateButtonVisible = true
+    calendarChooser.todayButtonVisible    = true
+    calendarChooser.weekOfYearVisible     = false
+  }
   add(
     new Label("タスク期限"),
-    new Constraints { gridx = 0; gridy = 4 })
+    new Constraints { gridx = 0; gridy = 3 })
   add(
-    Component.wrap(new JDateChooser() {
-      setDateFormatString("yyyy-MM-dd")
-      getJCalendar().setTodayButtonVisible(true)
-      getJCalendar().setNullDateButtonVisible(true)
-      getJCalendar().setWeekOfYearVisible(false)
-    }),
+    deadline,
     new Constraints() {
-      gridx = 1; gridy = 4; anchor = West
+      gridx = 1; gridy = 3; anchor = Anchor.West
       ipadx = 3; ipady = 3; insets = commonInsets
     })
+  deadline.minimumSize = deadline.preferredSize
 
+  private val status = new ComboBox[Task.Status](Task.Status.all)
   add(
     new Label("状態"),
-    new Constraints { gridx = 0; gridy = 5 })
+    new Constraints { gridx = 0; gridy = 4 })
   add(
-    new ComboBox[Task.Status](Task.Status.all),
+    status,
     new Constraints {
-      gridx = 1; gridy = 5; anchor = West
+      gridx = 1; gridy = 4; anchor = Anchor.West
       ipadx = 3; ipady = 3; insets = commonInsets
     })
 
+  private val description = new TextArea() {
+    rows = 5
+    peer.setMargin(commonInsets)
+  }
+  private val scrollingDescription = new ScrollPane(description)
   add(
     new Label("説明"),
-    new Constraints { gridx = 0; gridy = 6 })
+    new Constraints { gridx = 0; gridy = 5 })
   add(
-    new ScrollPane(
-      new TextArea() {
-        rows = 5
-        peer.setMargin(commonInsets)
-      }),
+    scrollingDescription,
     new Constraints() {
-      gridx = 1; gridy = 6; fill = Horizontal; weightx = 1.0
+      gridx = 1; gridy = 5; fill = Fill.Horizontal; weightx = 1.0
       insets = commonInsets
     })
+  scrollingDescription.minimumSize = scrollingDescription.preferredSize
 }