src/Tools/jEdit/src/find_dockable.scala
author wenzelm
Sat Apr 26 22:35:19 2014 +0200 (2014-04-26)
changeset 56759 790f7562cd0e
parent 56758 d203b9c400a2
child 56864 0446c7ac2e32
permissions -rw-r--r--
proper handling of shared zoom component: update layout dynamically;
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@56757
    15
import scala.swing.{Button, Component, TextField, CheckBox, Label,
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@56757
   187
  /* operations */
wenzelm@56757
   188
wenzelm@56758
   189
  private val operations = List(find_theorems, find_consts)
wenzelm@56757
   190
wenzelm@56759
   191
  private val operations_pane = new TabbedPane
wenzelm@56759
   192
  {
wenzelm@56759
   193
    pages ++= operations.map(_.page)
wenzelm@56759
   194
    listenTo(selection)
wenzelm@56759
   195
    reactions += { case SelectionChanged(_) => select_operation() }
wenzelm@56759
   196
  }
wenzelm@56757
   197
wenzelm@56759
   198
  private def get_operation(): Option[Find_Dockable.Operation] =
wenzelm@56759
   199
    try { Some(operations(operations_pane.selection.index)) }
wenzelm@56759
   200
    catch { case _: IndexOutOfBoundsException => None }
wenzelm@56759
   201
wenzelm@56759
   202
  private def select_operation() {
wenzelm@56759
   203
    for (op <- get_operation()) op.select
wenzelm@56759
   204
    operations_pane.revalidate
wenzelm@56757
   205
  }
wenzelm@52865
   206
wenzelm@56759
   207
  override def focusOnDefaultComponent { for (op <- get_operation()) op.query.requestFocus }
wenzelm@56759
   208
wenzelm@56759
   209
  select_operation()
wenzelm@56759
   210
  set_content(operations_pane)
wenzelm@56759
   211
wenzelm@52865
   212
wenzelm@52865
   213
  /* resize */
wenzelm@52865
   214
wenzelm@56757
   215
  private def handle_resize(): Unit =
wenzelm@56757
   216
    Swing_Thread.require {
wenzelm@56757
   217
      for (op <- operations) {
wenzelm@56757
   218
        op.pretty_text_area.resize(
wenzelm@56757
   219
          Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom_factor / 100))
wenzelm@56757
   220
      }
wenzelm@56757
   221
    }
wenzelm@52846
   222
wenzelm@52865
   223
  private val delay_resize =
wenzelm@52865
   224
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@52854
   225
wenzelm@52865
   226
  addComponentListener(new ComponentAdapter {
wenzelm@52865
   227
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@52865
   228
  })
wenzelm@52846
   229
wenzelm@52846
   230
wenzelm@56715
   231
  /* main */
wenzelm@52846
   232
wenzelm@56715
   233
  private val main =
wenzelm@56715
   234
    Session.Consumer[Session.Global_Options](getClass.getName) {
wenzelm@56715
   235
      case _: Session.Global_Options => Swing_Thread.later { handle_resize() }
wenzelm@52846
   236
    }
wenzelm@52846
   237
wenzelm@52846
   238
  override def init()
wenzelm@52846
   239
  {
wenzelm@56715
   240
    PIDE.session.global_options += main
wenzelm@52848
   241
    handle_resize()
wenzelm@56757
   242
    operations.foreach(op => op.query_operation.activate())
wenzelm@52846
   243
  }
wenzelm@52846
   244
wenzelm@52846
   245
  override def exit()
wenzelm@52846
   246
  {
wenzelm@56757
   247
    operations.foreach(op => op.query_operation.deactivate())
wenzelm@56715
   248
    PIDE.session.global_options -= main
wenzelm@52846
   249
    delay_resize.revoke()
wenzelm@52846
   250
  }
wenzelm@56757
   251
}
wenzelm@52846
   252