src/Tools/jEdit/src/find_dockable.scala
author wenzelm
Mon May 05 22:14:56 2014 +0200 (2014-05-05)
changeset 56872 1435f0c771dc
parent 56866 c4512e94e15c
permissions -rw-r--r--
some complication with ListView.Renderer to get tooltips;
wenzelm@52846
     1
/*  Title:      Tools/jEdit/src/find_dockable.scala
wenzelm@52846
     2
    Author:     Makarius
wenzelm@52846
     3
wenzelm@52846
     4
Dockable window for "find" dialog.
wenzelm@52846
     5
*/
wenzelm@52846
     6
wenzelm@52846
     7
package isabelle.jedit
wenzelm@52846
     8
wenzelm@52846
     9
wenzelm@52846
    10
import isabelle._
wenzelm@52846
    11
wenzelm@56757
    12
import java.awt.event.{ComponentEvent, ComponentAdapter, KeyEvent}
wenzelm@56757
    13
import javax.swing.{JComponent, JTextField}
wenzelm@52846
    14
wenzelm@56872
    15
import scala.swing.{Button, Component, TextField, CheckBox, Label, ListView,
wenzelm@56757
    16
  ComboBox, TabbedPane, BorderPanel}
wenzelm@56759
    17
import scala.swing.event.{SelectionChanged, ButtonClicked, Key, KeyPressed}
wenzelm@52846
    18
wenzelm@52846
    19
import org.gjt.sp.jedit.View
wenzelm@52846
    20
wenzelm@52846
    21
wenzelm@56757
    22
object Find_Dockable
wenzelm@56757
    23
{
wenzelm@56758
    24
  private abstract class Operation(view: View)
wenzelm@56757
    25
  {
wenzelm@56758
    26
    val pretty_text_area = new Pretty_Text_Area(view)
wenzelm@56757
    27
    def query_operation: Query_Operation[View]
wenzelm@56757
    28
    def query: JComponent
wenzelm@56759
    29
    def select: Unit
wenzelm@56757
    30
    def page: TabbedPane.Page
wenzelm@56757
    31
  }
wenzelm@56757
    32
}
wenzelm@56757
    33
wenzelm@52846
    34
class Find_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@52846
    35
{
wenzelm@56757
    36
  /* common GUI components */
wenzelm@56757
    37
wenzelm@56757
    38
  private var zoom_factor = 100
wenzelm@56757
    39
wenzelm@56757
    40
  private val zoom =
wenzelm@56757
    41
    new GUI.Zoom_Box(factor => if (zoom_factor != factor) { zoom_factor = factor; handle_resize() })
wenzelm@56757
    42
    {
wenzelm@56757
    43
      tooltip = "Zoom factor for output font size"
wenzelm@56757
    44
    }
wenzelm@52846
    45
wenzelm@52846
    46
wenzelm@56757
    47
  private def make_query(property: String, tooltip: String, apply_query: () => Unit): JTextField =
wenzelm@56757
    48
    new Completion_Popup.History_Text_Field(property)
wenzelm@56757
    49
    {
wenzelm@56757
    50
      override def processKeyEvent(evt: KeyEvent)
wenzelm@56757
    51
      {
wenzelm@56757
    52
        if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) apply_query()
wenzelm@56757
    53
        super.processKeyEvent(evt)
wenzelm@56757
    54
      }
wenzelm@56757
    55
      { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
wenzelm@56757
    56
      setColumns(40)
wenzelm@56757
    57
      setToolTipText(tooltip)
wenzelm@56757
    58
      setFont(GUI.imitate_font(Font_Info.main_family(), getFont, 1.2))
wenzelm@56757
    59
    }
wenzelm@52865
    60
wenzelm@52935
    61
wenzelm@56757
    62
  /* consume status */
wenzelm@56757
    63
wenzelm@56757
    64
  def consume_status(process_indicator: Process_Indicator, status: Query_Operation.Status.Value)
wenzelm@52935
    65
  {
wenzelm@52935
    66
    status match {
wenzelm@52935
    67
      case Query_Operation.Status.WAITING =>
wenzelm@52935
    68
        process_indicator.update("Waiting for evaluation of context ...", 5)
wenzelm@52935
    69
      case Query_Operation.Status.RUNNING =>
wenzelm@52935
    70
        process_indicator.update("Running find operation ...", 15)
wenzelm@54640
    71
      case Query_Operation.Status.FINISHED =>
wenzelm@52935
    72
        process_indicator.update(null, 0)
wenzelm@52935
    73
    }
wenzelm@52935
    74
  }
wenzelm@52935
    75
wenzelm@56757
    76
wenzelm@56757
    77
  /* find theorems */
wenzelm@56757
    78
wenzelm@56758
    79
  private val find_theorems = new Find_Dockable.Operation(view)
wenzelm@56757
    80
  {
wenzelm@56757
    81
    /* query */
wenzelm@56757
    82
wenzelm@56757
    83
    private val process_indicator = new Process_Indicator
wenzelm@56757
    84
wenzelm@56757
    85
    val query_operation =
wenzelm@56757
    86
      new Query_Operation(PIDE.editor, view, "find_theorems",
wenzelm@56757
    87
        consume_status(process_indicator, _),
wenzelm@56757
    88
        (snapshot, results, body) =>
wenzelm@56757
    89
          pretty_text_area.update(snapshot, results, Pretty.separate(body)))
wenzelm@56757
    90
wenzelm@56757
    91
    private def apply_query()
wenzelm@56757
    92
    {
wenzelm@56757
    93
      query_operation.apply_query(List(limit.text, allow_dups.selected.toString, query.getText))
wenzelm@56757
    94
    }
wenzelm@56757
    95
wenzelm@56757
    96
    private val query_label = new Label("Search criteria:") {
wenzelm@56757
    97
      tooltip =
wenzelm@56757
    98
        GUI.tooltip_lines(
wenzelm@56757
    99
          "Search criteria for find operation, e.g.\n\"_ = _\" \"op +\" name: Group -name: monoid")
wenzelm@56757
   100
    }
wenzelm@56757
   101
wenzelm@56757
   102
    val query = make_query("isabelle-find-theorems", query_label.tooltip, apply_query _)
wenzelm@56757
   103
wenzelm@56757
   104
wenzelm@56758
   105
    /* GUI page */
wenzelm@56757
   106
wenzelm@56757
   107
    private val limit = new TextField(PIDE.options.int("find_theorems_limit").toString, 5) {
wenzelm@56757
   108
      tooltip = "Limit of displayed results"
wenzelm@56757
   109
      verifier = (s: String) =>
wenzelm@56757
   110
        s match { case Properties.Value.Int(x) => x >= 0 case _ => false }
wenzelm@56757
   111
      listenTo(keys)
wenzelm@56757
   112
      reactions += { case KeyPressed(_, Key.Enter, 0, _) => apply_query() }
wenzelm@56757
   113
    }
wenzelm@56757
   114
wenzelm@56757
   115
    private val allow_dups = new CheckBox("Duplicates") {
wenzelm@56757
   116
      tooltip = "Show all versions of matching theorems"
wenzelm@56757
   117
      selected = false
wenzelm@56757
   118
    }
wenzelm@56757
   119
wenzelm@56757
   120
    private val apply_button = new Button("Apply") {
wenzelm@56757
   121
      tooltip = "Find theorems meeting specified criteria"
wenzelm@56757
   122
      reactions += { case ButtonClicked(_) => apply_query() }
wenzelm@56757
   123
    }
wenzelm@56757
   124
wenzelm@56759
   125
    private val control_panel =
wenzelm@56759
   126
      new Wrap_Panel(Wrap_Panel.Alignment.Right)(
wenzelm@56759
   127
        query_label, Component.wrap(query), limit, allow_dups,
wenzelm@56759
   128
        process_indicator.component, apply_button)
wenzelm@56759
   129
wenzelm@56759
   130
    def select { control_panel.contents += zoom }
wenzelm@56757
   131
wenzelm@56757
   132
    val page =
wenzelm@56757
   133
      new TabbedPane.Page("Theorems", new BorderPanel {
wenzelm@56759
   134
        add(control_panel, BorderPanel.Position.North)
wenzelm@56757
   135
        add(Component.wrap(pretty_text_area), BorderPanel.Position.Center)
wenzelm@56757
   136
      }, apply_button.tooltip)
wenzelm@56757
   137
  }
wenzelm@56757
   138
wenzelm@56757
   139
wenzelm@56758
   140
  /* find consts */
wenzelm@56758
   141
wenzelm@56758
   142
  private val find_consts = new Find_Dockable.Operation(view)
wenzelm@56758
   143
  {
wenzelm@56758
   144
    /* query */
wenzelm@56758
   145
wenzelm@56758
   146
    private val process_indicator = new Process_Indicator
wenzelm@56758
   147
wenzelm@56758
   148
    val query_operation =
wenzelm@56758
   149
      new Query_Operation(PIDE.editor, view, "find_consts",
wenzelm@56758
   150
        consume_status(process_indicator, _),
wenzelm@56758
   151
        (snapshot, results, body) =>
wenzelm@56758
   152
          pretty_text_area.update(snapshot, results, Pretty.separate(body)))
wenzelm@56758
   153
wenzelm@56758
   154
    private def apply_query()
wenzelm@56758
   155
    {
wenzelm@56758
   156
      query_operation.apply_query(List(query.getText))
wenzelm@56758
   157
    }
wenzelm@56758
   158
wenzelm@56758
   159
    private val query_label = new Label("Search criteria:") {
wenzelm@56758
   160
      tooltip = GUI.tooltip_lines("Name / type patterns for constants")
wenzelm@56758
   161
    }
wenzelm@56758
   162
wenzelm@56758
   163
    val query = make_query("isabelle-find-consts", query_label.tooltip, apply_query _)
wenzelm@56758
   164
wenzelm@56758
   165
wenzelm@56758
   166
    /* GUI page */
wenzelm@56758
   167
wenzelm@56758
   168
    private val apply_button = new Button("Apply") {
wenzelm@56758
   169
      tooltip = "Find constants by name / type patterns"
wenzelm@56758
   170
      reactions += { case ButtonClicked(_) => apply_query() }
wenzelm@56758
   171
    }
wenzelm@56758
   172
wenzelm@56759
   173
    private val control_panel =
wenzelm@56759
   174
      new Wrap_Panel(Wrap_Panel.Alignment.Right)(
wenzelm@56759
   175
        query_label, Component.wrap(query), process_indicator.component, apply_button)
wenzelm@56759
   176
wenzelm@56759
   177
    def select { control_panel.contents += zoom }
wenzelm@56758
   178
wenzelm@56758
   179
    val page =
wenzelm@56758
   180
      new TabbedPane.Page("Constants", new BorderPanel {
wenzelm@56759
   181
        add(control_panel, BorderPanel.Position.North)
wenzelm@56758
   182
        add(Component.wrap(pretty_text_area), BorderPanel.Position.Center)
wenzelm@56758
   183
      }, apply_button.tooltip)
wenzelm@56758
   184
  }
wenzelm@56758
   185
wenzelm@56758
   186
wenzelm@56864
   187
  /* print operation */
wenzelm@56864
   188
wenzelm@56864
   189
  private val print_operation = new Find_Dockable.Operation(view)
wenzelm@56864
   190
  {
wenzelm@56864
   191
    /* query */
wenzelm@56864
   192
wenzelm@56864
   193
    val query_operation =
wenzelm@56864
   194
      new Query_Operation(PIDE.editor, view, "print_operation", _ => (),
wenzelm@56864
   195
        (snapshot, results, body) =>
wenzelm@56864
   196
          pretty_text_area.update(snapshot, results, Pretty.separate(body)))
wenzelm@56864
   197
wenzelm@56864
   198
    private def apply_query()
wenzelm@56864
   199
    {
wenzelm@56872
   200
      query_operation.apply_query(List(_selector.selection.item))
wenzelm@56864
   201
    }
wenzelm@56864
   202
wenzelm@56866
   203
    private val query_label = new Label("Print:")
wenzelm@56866
   204
wenzelm@56864
   205
    def query: JComponent = _selector.peer.asInstanceOf[JComponent]
wenzelm@56864
   206
wenzelm@56864
   207
wenzelm@56872
   208
    /* items */
wenzelm@56864
   209
wenzelm@56872
   210
    case class Item(name: String, description: String)
wenzelm@56872
   211
wenzelm@56872
   212
    class Renderer(old_renderer: ListView.Renderer[String], items: List[Item])
wenzelm@56872
   213
      extends ListView.Renderer[String]
wenzelm@56864
   214
    {
wenzelm@56872
   215
      def componentFor(list: ListView[_],
wenzelm@56872
   216
        selected: Boolean, focused: Boolean, item: String, index: Int) =
wenzelm@56872
   217
      {
wenzelm@56872
   218
        val component = old_renderer.componentFor(list, selected, focused, item, index)
wenzelm@56872
   219
        try { component.tooltip = items(index).description }
wenzelm@56872
   220
        catch { case _: IndexOutOfBoundsException => }
wenzelm@56872
   221
        component
wenzelm@56872
   222
      }
wenzelm@56872
   223
    }
wenzelm@56872
   224
wenzelm@56872
   225
    private var _selector_item: Option[String] = None
wenzelm@56872
   226
    private var _selector = new ComboBox[String](Nil)
wenzelm@56872
   227
wenzelm@56872
   228
    private def set_selector()
wenzelm@56872
   229
    {
wenzelm@56872
   230
      val items = Print_Operation.print_operations(PIDE.session).map(p => Item(p._1, p._2))
wenzelm@56872
   231
wenzelm@56864
   232
      _selector =
wenzelm@56872
   233
        new ComboBox(items.map(_.name)) {
wenzelm@56865
   234
          _selector_item.foreach(item => selection.item = item)
wenzelm@56864
   235
          listenTo(selection)
wenzelm@56864
   236
          reactions += {
wenzelm@56864
   237
            case SelectionChanged(_) =>
wenzelm@56865
   238
              _selector_item = Some(selection.item)
wenzelm@56864
   239
              apply_query()
wenzelm@56864
   240
          }
wenzelm@56864
   241
        }
wenzelm@56872
   242
      _selector.renderer = new Renderer(_selector.renderer, items)
wenzelm@56864
   243
    }
wenzelm@56872
   244
    set_selector()
wenzelm@56872
   245
wenzelm@56872
   246
wenzelm@56872
   247
    /* GUI page */
wenzelm@56864
   248
wenzelm@56865
   249
    private val apply_button = new Button("Apply") {
wenzelm@56865
   250
      tooltip = "Apply to current context"
wenzelm@56865
   251
      reactions += { case ButtonClicked(_) => apply_query() }
wenzelm@56865
   252
    }
wenzelm@56865
   253
wenzelm@56864
   254
    private val control_panel = new Wrap_Panel(Wrap_Panel.Alignment.Right)()
wenzelm@56864
   255
wenzelm@56864
   256
    def select
wenzelm@56864
   257
    {
wenzelm@56872
   258
      set_selector()
wenzelm@56864
   259
      control_panel.contents.clear
wenzelm@56866
   260
      control_panel.contents ++= List(query_label, _selector, apply_button, zoom)
wenzelm@56864
   261
    }
wenzelm@56864
   262
wenzelm@56864
   263
    val page =
wenzelm@56864
   264
      new TabbedPane.Page("Print ...", new BorderPanel {
wenzelm@56864
   265
        add(control_panel, BorderPanel.Position.North)
wenzelm@56864
   266
        add(Component.wrap(pretty_text_area), BorderPanel.Position.Center)
wenzelm@56864
   267
      }, "Print information from context")
wenzelm@56864
   268
  }
wenzelm@56864
   269
wenzelm@56864
   270
wenzelm@56757
   271
  /* operations */
wenzelm@56757
   272
wenzelm@56864
   273
  private val operations = List(find_theorems, find_consts, print_operation)
wenzelm@56757
   274
wenzelm@56759
   275
  private val operations_pane = new TabbedPane
wenzelm@56759
   276
  {
wenzelm@56759
   277
    pages ++= operations.map(_.page)
wenzelm@56759
   278
    listenTo(selection)
wenzelm@56759
   279
    reactions += { case SelectionChanged(_) => select_operation() }
wenzelm@56759
   280
  }
wenzelm@56757
   281
wenzelm@56759
   282
  private def get_operation(): Option[Find_Dockable.Operation] =
wenzelm@56759
   283
    try { Some(operations(operations_pane.selection.index)) }
wenzelm@56759
   284
    catch { case _: IndexOutOfBoundsException => None }
wenzelm@56759
   285
wenzelm@56759
   286
  private def select_operation() {
wenzelm@56865
   287
    for (op <- get_operation()) { op.select; op.query.requestFocus }
wenzelm@56759
   288
    operations_pane.revalidate
wenzelm@56757
   289
  }
wenzelm@52865
   290
wenzelm@56759
   291
  override def focusOnDefaultComponent { for (op <- get_operation()) op.query.requestFocus }
wenzelm@56759
   292
wenzelm@56759
   293
  select_operation()
wenzelm@56759
   294
  set_content(operations_pane)
wenzelm@56759
   295
wenzelm@52865
   296
wenzelm@52865
   297
  /* resize */
wenzelm@52865
   298
wenzelm@56757
   299
  private def handle_resize(): Unit =
wenzelm@56757
   300
    Swing_Thread.require {
wenzelm@56757
   301
      for (op <- operations) {
wenzelm@56757
   302
        op.pretty_text_area.resize(
wenzelm@56757
   303
          Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom_factor / 100))
wenzelm@56757
   304
      }
wenzelm@56757
   305
    }
wenzelm@52846
   306
wenzelm@52865
   307
  private val delay_resize =
wenzelm@52865
   308
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@52854
   309
wenzelm@52865
   310
  addComponentListener(new ComponentAdapter {
wenzelm@52865
   311
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@52865
   312
  })
wenzelm@52846
   313
wenzelm@52846
   314
wenzelm@56715
   315
  /* main */
wenzelm@52846
   316
wenzelm@56715
   317
  private val main =
wenzelm@56715
   318
    Session.Consumer[Session.Global_Options](getClass.getName) {
wenzelm@56715
   319
      case _: Session.Global_Options => Swing_Thread.later { handle_resize() }
wenzelm@52846
   320
    }
wenzelm@52846
   321
wenzelm@52846
   322
  override def init()
wenzelm@52846
   323
  {
wenzelm@56715
   324
    PIDE.session.global_options += main
wenzelm@52848
   325
    handle_resize()
wenzelm@56757
   326
    operations.foreach(op => op.query_operation.activate())
wenzelm@52846
   327
  }
wenzelm@52846
   328
wenzelm@52846
   329
  override def exit()
wenzelm@52846
   330
  {
wenzelm@56757
   331
    operations.foreach(op => op.query_operation.deactivate())
wenzelm@56715
   332
    PIDE.session.global_options -= main
wenzelm@52846
   333
    delay_resize.revoke()
wenzelm@52846
   334
  }
wenzelm@56757
   335
}
wenzelm@52846
   336