src/Tools/jEdit/src/query_dockable.scala
changeset 75393 87ebf5a50283
parent 73987 fc363a3b690a
child 75404 a1363da718aa
equal deleted inserted replaced
75388:b3ca4a6ed74b 75393:87ebf5a50283
    17 import scala.swing.event.{SelectionChanged, ButtonClicked, Key, KeyPressed}
    17 import scala.swing.event.{SelectionChanged, ButtonClicked, Key, KeyPressed}
    18 
    18 
    19 import org.gjt.sp.jedit.View
    19 import org.gjt.sp.jedit.View
    20 
    20 
    21 
    21 
    22 object Query_Dockable
    22 object Query_Dockable {
    23 {
    23   private abstract class Operation(view: View) {
    24   private abstract class Operation(view: View)
       
    25   {
       
    26     val pretty_text_area = new Pretty_Text_Area(view)
    24     val pretty_text_area = new Pretty_Text_Area(view)
    27     def query_operation: Query_Operation[View]
    25     def query_operation: Query_Operation[View]
    28     def query: JComponent
    26     def query: JComponent
    29     def select: Unit
    27     def select: Unit
    30     def page: TabbedPane.Page
    28     def page: TabbedPane.Page
    31   }
    29   }
    32 }
    30 }
    33 
    31 
    34 class Query_Dockable(view: View, position: String) extends Dockable(view, position)
    32 class Query_Dockable(view: View, position: String) extends Dockable(view, position) {
    35 {
       
    36   /* common GUI components */
    33   /* common GUI components */
    37 
    34 
    38   private val zoom = new Font_Info.Zoom_Box { def changed = handle_resize() }
    35   private val zoom = new Font_Info.Zoom_Box { def changed = handle_resize() }
    39 
    36 
    40   private def make_query(property: String, tooltip: String, apply_query: () => Unit)
    37   private def make_query(
    41       : Completion_Popup.History_Text_Field =
    38     property: String,
    42     new Completion_Popup.History_Text_Field(property)
    39     tooltip: String,
    43     {
    40     apply_query: () => Unit
    44       override def processKeyEvent(evt: KeyEvent): Unit =
    41   ): Completion_Popup.History_Text_Field = {
    45       {
    42     new Completion_Popup.History_Text_Field(property) {
       
    43       override def processKeyEvent(evt: KeyEvent): Unit = {
    46         if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) apply_query()
    44         if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) apply_query()
    47         super.processKeyEvent(evt)
    45         super.processKeyEvent(evt)
    48       }
    46       }
    49       { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
    47       { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
    50       setColumns(40)
    48       setColumns(40)
    51       setToolTipText(tooltip)
    49       setToolTipText(tooltip)
    52       setFont(GUI.imitate_font(getFont, scale = 1.2))
    50       setFont(GUI.imitate_font(getFont, scale = 1.2))
    53     }
    51     }
       
    52   }
    54 
    53 
    55 
    54 
    56   /* consume status */
    55   /* consume status */
    57 
    56 
    58   def consume_status(
    57   def consume_status(
    59     process_indicator: Process_Indicator, status: Query_Operation.Status.Value): Unit =
    58     process_indicator: Process_Indicator,
    60   {
    59     status: Query_Operation.Status.Value
       
    60   ): Unit = {
    61     status match {
    61     status match {
    62       case Query_Operation.Status.WAITING =>
    62       case Query_Operation.Status.WAITING =>
    63         process_indicator.update("Waiting for evaluation of context ...", 5)
    63         process_indicator.update("Waiting for evaluation of context ...", 5)
    64       case Query_Operation.Status.RUNNING =>
    64       case Query_Operation.Status.RUNNING =>
    65         process_indicator.update("Running find operation ...", 15)
    65         process_indicator.update("Running find operation ...", 15)
    69   }
    69   }
    70 
    70 
    71 
    71 
    72   /* find theorems */
    72   /* find theorems */
    73 
    73 
    74   private val find_theorems = new Query_Dockable.Operation(view)
    74   private val find_theorems = new Query_Dockable.Operation(view) {
    75   {
       
    76     /* query */
    75     /* query */
    77 
    76 
    78     private val process_indicator = new Process_Indicator
    77     private val process_indicator = new Process_Indicator
    79 
    78 
    80     val query_operation =
    79     val query_operation =
    81       new Query_Operation(PIDE.editor, view, "find_theorems",
    80       new Query_Operation(PIDE.editor, view, "find_theorems",
    82         consume_status(process_indicator, _),
    81         consume_status(process_indicator, _),
    83         (snapshot, results, body) =>
    82         (snapshot, results, body) =>
    84           pretty_text_area.update(snapshot, results, Pretty.separate(body)))
    83           pretty_text_area.update(snapshot, results, Pretty.separate(body)))
    85 
    84 
    86     private def apply_query(): Unit =
    85     private def apply_query(): Unit = {
    87     {
       
    88       query.addCurrentToHistory()
    86       query.addCurrentToHistory()
    89       query_operation.apply_query(List(limit.text, allow_dups.selected.toString, query.getText))
    87       query_operation.apply_query(List(limit.text, allow_dups.selected.toString, query.getText))
    90     }
    88     }
    91 
    89 
    92     private val query_label = new Label("Find:") {
    90     private val query_label = new Label("Find:") {
   135   }
   133   }
   136 
   134 
   137 
   135 
   138   /* find consts */
   136   /* find consts */
   139 
   137 
   140   private val find_consts = new Query_Dockable.Operation(view)
   138   private val find_consts = new Query_Dockable.Operation(view) {
   141   {
       
   142     /* query */
   139     /* query */
   143 
   140 
   144     private val process_indicator = new Process_Indicator
   141     private val process_indicator = new Process_Indicator
   145 
   142 
   146     val query_operation =
   143     val query_operation =
   147       new Query_Operation(PIDE.editor, view, "find_consts",
   144       new Query_Operation(PIDE.editor, view, "find_consts",
   148         consume_status(process_indicator, _),
   145         consume_status(process_indicator, _),
   149         (snapshot, results, body) =>
   146         (snapshot, results, body) =>
   150           pretty_text_area.update(snapshot, results, Pretty.separate(body)))
   147           pretty_text_area.update(snapshot, results, Pretty.separate(body)))
   151 
   148 
   152     private def apply_query(): Unit =
   149     private def apply_query(): Unit = {
   153     {
       
   154       query.addCurrentToHistory()
   150       query.addCurrentToHistory()
   155       query_operation.apply_query(List(query.getText))
   151       query_operation.apply_query(List(query.getText))
   156     }
   152     }
   157 
   153 
   158     private val query_label = new Label("Find:") {
   154     private val query_label = new Label("Find:") {
   185   }
   181   }
   186 
   182 
   187 
   183 
   188   /* print operation */
   184   /* print operation */
   189 
   185 
   190   private val print_operation = new Query_Dockable.Operation(view)
   186   private val print_operation = new Query_Dockable.Operation(view) {
   191   {
       
   192     /* items */
   187     /* items */
   193 
   188 
   194     private class Item(val name: String, description: String, sel: Boolean)
   189     private class Item(val name: String, description: String, sel: Boolean) {
   195     {
       
   196       val checkbox = new CheckBox(name) {
   190       val checkbox = new CheckBox(name) {
   197         tooltip = "Print " + description
   191         tooltip = "Print " + description
   198         selected = sel
   192         selected = sel
   199         reactions += { case ButtonClicked(_) => apply_query() }
   193         reactions += { case ButtonClicked(_) => apply_query() }
   200       }
   194       }
   203     private var _items: List[Item] = Nil
   197     private var _items: List[Item] = Nil
   204 
   198 
   205     private def selected_items(): List[String] =
   199     private def selected_items(): List[String] =
   206       for (item <- _items if item.checkbox.selected) yield item.name
   200       for (item <- _items if item.checkbox.selected) yield item.name
   207 
   201 
   208     private def update_items(): List[Item] =
   202     private def update_items(): List[Item] = {
   209     {
       
   210       val old_items = _items
   203       val old_items = _items
   211       def was_selected(name: String): Boolean =
   204       def was_selected(name: String): Boolean =
   212         old_items.find(item => item.name == name) match {
   205         old_items.find(item => item.name == name) match {
   213           case None => false
   206           case None => false
   214           case Some(item) => item.checkbox.selected
   207           case Some(item) => item.checkbox.selected
   253       }
   246       }
   254     }
   247     }
   255 
   248 
   256     private val control_panel = Wrap_Panel()
   249     private val control_panel = Wrap_Panel()
   257 
   250 
   258     def select: Unit =
   251     def select: Unit = {
   259     {
       
   260       control_panel.contents.clear()
   252       control_panel.contents.clear()
   261       control_panel.contents += query_label
   253       control_panel.contents += query_label
   262       update_items().foreach(item => control_panel.contents += item.checkbox)
   254       update_items().foreach(item => control_panel.contents += item.checkbox)
   263       control_panel.contents ++=
   255       control_panel.contents ++=
   264         List(process_indicator.component, apply_button,
   256         List(process_indicator.component, apply_button,
   275 
   267 
   276   /* operations */
   268   /* operations */
   277 
   269 
   278   private val operations = List(find_theorems, find_consts, print_operation)
   270   private val operations = List(find_theorems, find_consts, print_operation)
   279 
   271 
   280   private val operations_pane = new TabbedPane
   272   private val operations_pane = new TabbedPane {
   281   {
       
   282     pages ++= operations.map(_.page)
   273     pages ++= operations.map(_.page)
   283     listenTo(selection)
   274     listenTo(selection)
   284     reactions += { case SelectionChanged(_) => select_operation() }
   275     reactions += { case SelectionChanged(_) => select_operation() }
   285   }
   276   }
   286 
   277 
   287   private def get_operation(): Option[Query_Dockable.Operation] =
   278   private def get_operation(): Option[Query_Dockable.Operation] =
   288     try { Some(operations(operations_pane.selection.index)) }
   279     try { Some(operations(operations_pane.selection.index)) }
   289     catch { case _: IndexOutOfBoundsException => None }
   280     catch { case _: IndexOutOfBoundsException => None }
   290 
   281 
   291   private def select_operation(): Unit =
   282   private def select_operation(): Unit = {
   292   {
       
   293     for (op <- get_operation()) { op.select; op.query.requestFocus() }
   283     for (op <- get_operation()) { op.select; op.query.requestFocus() }
   294     operations_pane.revalidate()
   284     operations_pane.revalidate()
   295   }
   285   }
   296 
   286 
   297   override def focusOnDefaultComponent(): Unit =
   287   override def focusOnDefaultComponent(): Unit = {
   298   {
       
   299     for (op <- get_operation()) op.query.requestFocus()
   288     for (op <- get_operation()) op.query.requestFocus()
   300   }
   289   }
   301 
   290 
   302   select_operation()
   291   select_operation()
   303   set_content(operations_pane)
   292   set_content(operations_pane)
   333   private val main =
   322   private val main =
   334     Session.Consumer[Session.Global_Options](getClass.getName) {
   323     Session.Consumer[Session.Global_Options](getClass.getName) {
   335       case _: Session.Global_Options => GUI_Thread.later { handle_resize() }
   324       case _: Session.Global_Options => GUI_Thread.later { handle_resize() }
   336     }
   325     }
   337 
   326 
   338   override def init(): Unit =
   327   override def init(): Unit = {
   339   {
       
   340     PIDE.session.global_options += main
   328     PIDE.session.global_options += main
   341     handle_resize()
   329     handle_resize()
   342     operations.foreach(op => op.query_operation.activate())
   330     operations.foreach(op => op.query_operation.activate())
   343   }
   331   }
   344 
   332 
   345   override def exit(): Unit =
   333   override def exit(): Unit = {
   346   {
       
   347     operations.foreach(op => op.query_operation.deactivate())
   334     operations.foreach(op => op.query_operation.deactivate())
   348     PIDE.session.global_options -= main
   335     PIDE.session.global_options -= main
   349     delay_resize.revoke()
   336     delay_resize.revoke()
   350   }
   337   }
   351 }
   338 }