132 |
132 |
133 |
133 |
134 /* list selector */ |
134 /* list selector */ |
135 |
135 |
136 object Selector { |
136 object Selector { |
137 sealed abstract class Entry[A] { |
137 object Value { |
138 def get_value: Option[A] = |
138 def unapply[A](entry: Entry[A]): Option[A] = |
139 this match { |
139 entry match { |
140 case item: Item[_] => Some(item.value) |
140 case item: Item[_] => Some(item.value) |
141 case _ => None |
141 case _ => None |
142 } |
142 } |
143 } |
143 } |
144 case class Item[A](value: A, description: String = "", batch: Int = 0) extends Entry[A] { |
144 sealed abstract class Entry[A] { def get_value: Option[A] = Value.unapply(this) } |
|
145 private case class Item[A](value: A, description: String, batch: Int) extends Entry[A] { |
145 override def toString: String = proper_string(description) getOrElse value.toString |
146 override def toString: String = proper_string(description) getOrElse value.toString |
146 } |
147 } |
147 case class Separator[A](batch: Int = 0) extends Entry[A] { |
148 private case class Separator[A](batch: Int) extends Entry[A] { |
148 override def toString: String = "---" |
149 override def toString: String = "---" |
149 } |
150 } |
150 |
151 |
151 def item(name: String, batch: Int = 0): Item[String] = Item(name, batch = batch) |
152 def item[A](value: A): Entry[A] = Item(value, "", 0) |
152 def separator(batch: Int = 0): Separator[String] = Separator(batch = batch) |
153 def item_description[A](value: A, description: String): Entry[A] = Item(value, description, 0) |
153 } |
154 |
154 |
155 def make_entries[A](batches: List[List[Entry[A]]]): List[Entry[A]] = { |
155 class Selector[A](val entries: List[Selector.Entry[A]]) |
156 val item_batches: List[List[Item[A]]] = |
156 extends ComboBox[Selector.Entry[A]](entries) { |
157 batches.map(_.flatMap( |
|
158 { case item: Item[_] => Some(item.asInstanceOf[Item[A]]) case _ => None })) |
|
159 val sep_entries: List[Entry[A]] = |
|
160 item_batches.filter(_.nonEmpty).zipWithIndex.flatMap({ case (batch, i) => |
|
161 Separator[A](i) :: batch.map(_.copy(batch = i)) |
|
162 }) |
|
163 sep_entries.tail |
|
164 } |
|
165 } |
|
166 |
|
167 class Selector[A](batches: List[Selector.Entry[A]]*) |
|
168 extends ComboBox[Selector.Entry[A]](Selector.make_entries(batches.toList)) { |
157 def changed(): Unit = {} |
169 def changed(): Unit = {} |
|
170 |
|
171 lazy val entries: List[Selector.Entry[A]] = Selector.make_entries(batches.toList) |
|
172 |
|
173 def find_value(pred: A => Boolean): Option[Selector.Entry[A]] = |
|
174 entries.find({ case item: Selector.Item[A] => pred(item.value) case _ => false }) |
|
175 |
|
176 def selection_value: Option[A] = selection.item.get_value |
158 |
177 |
159 override lazy val peer: JComboBox[Selector.Entry[A]] = |
178 override lazy val peer: JComboBox[Selector.Entry[A]] = |
160 new JComboBox[Selector.Entry[A]](ComboBox.newConstantModel(entries)) with SuperMixin { |
179 new JComboBox[Selector.Entry[A]](ComboBox.newConstantModel(entries)) with SuperMixin { |
161 private var key_released = false |
180 private var key_released = false |
162 private var sep_selected = false |
181 private var sep_selected = false |
204 |
223 |
205 private val Zoom_Factor = "([0-9]+)%?".r |
224 private val Zoom_Factor = "([0-9]+)%?".r |
206 |
225 |
207 class Zoom extends Selector[String]( |
226 class Zoom extends Selector[String]( |
208 List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%") |
227 List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%") |
209 .map(GUI.Selector.item(_)) |
228 .map(GUI.Selector.item) |
210 ) { |
229 ) { |
211 def factor: Int = parse(selection.item.toString) |
230 def factor: Int = parse(selection.item.toString) |
212 |
231 |
213 private def parse(text: String): Int = |
232 private def parse(text: String): Int = |
214 text match { |
233 text match { |