src/Tools/jEdit/src/find_dockable.scala
author wenzelm
Sat Apr 26 21:16:50 2014 +0200 (2014-04-26)
changeset 56757 d6fdf08282f3
parent 56751 2080e752ed40
child 56758 d203b9c400a2
permissions -rw-r--r--
some rearrangements to support multiple find operations;
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@56757
    17
import scala.swing.event.{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@56757
    24
  private abstract class Operation
wenzelm@56757
    25
  {
wenzelm@56757
    26
    def query_operation: Query_Operation[View]
wenzelm@56757
    27
    def query: JComponent
wenzelm@56757
    28
    def pretty_text_area: Pretty_Text_Area
wenzelm@56757
    29
    def page: TabbedPane.Page
wenzelm@56757
    30
  }
wenzelm@56757
    31
}
wenzelm@56757
    32
wenzelm@52846
    33
class Find_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@52846
    34
{
wenzelm@56757
    35
  /* common GUI components */
wenzelm@56757
    36
wenzelm@56757
    37
  private var zoom_factor = 100
wenzelm@56757
    38
wenzelm@56757
    39
  private val zoom =
wenzelm@56757
    40
    new GUI.Zoom_Box(factor => if (zoom_factor != factor) { zoom_factor = factor; handle_resize() })
wenzelm@56757
    41
    {
wenzelm@56757
    42
      tooltip = "Zoom factor for output font size"
wenzelm@56757
    43
    }
wenzelm@52846
    44
wenzelm@52846
    45
wenzelm@56757
    46
  private def make_query(property: String, tooltip: String, apply_query: () => Unit): JTextField =
wenzelm@56757
    47
    new Completion_Popup.History_Text_Field(property)
wenzelm@56757
    48
    {
wenzelm@56757
    49
      override def processKeyEvent(evt: KeyEvent)
wenzelm@56757
    50
      {
wenzelm@56757
    51
        if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) apply_query()
wenzelm@56757
    52
        super.processKeyEvent(evt)
wenzelm@56757
    53
      }
wenzelm@56757
    54
      { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
wenzelm@56757
    55
      setColumns(40)
wenzelm@56757
    56
      setToolTipText(tooltip)
wenzelm@56757
    57
      setFont(GUI.imitate_font(Font_Info.main_family(), getFont, 1.2))
wenzelm@56757
    58
    }
wenzelm@52865
    59
wenzelm@52935
    60
wenzelm@56757
    61
  /* consume status */
wenzelm@56757
    62
wenzelm@56757
    63
  def consume_status(process_indicator: Process_Indicator, status: Query_Operation.Status.Value)
wenzelm@52935
    64
  {
wenzelm@52935
    65
    status match {
wenzelm@52935
    66
      case Query_Operation.Status.WAITING =>
wenzelm@52935
    67
        process_indicator.update("Waiting for evaluation of context ...", 5)
wenzelm@52935
    68
      case Query_Operation.Status.RUNNING =>
wenzelm@52935
    69
        process_indicator.update("Running find operation ...", 15)
wenzelm@54640
    70
      case Query_Operation.Status.FINISHED =>
wenzelm@52935
    71
        process_indicator.update(null, 0)
wenzelm@52935
    72
    }
wenzelm@52935
    73
  }
wenzelm@52935
    74
wenzelm@56757
    75
wenzelm@56757
    76
  /* find theorems */
wenzelm@56757
    77
wenzelm@56757
    78
  private val find_theorems = new Find_Dockable.Operation
wenzelm@56757
    79
  {
wenzelm@56757
    80
    val pretty_text_area = new Pretty_Text_Area(view)
wenzelm@56757
    81
wenzelm@56757
    82
wenzelm@56757
    83
    /* query */
wenzelm@56757
    84
wenzelm@56757
    85
    private val process_indicator = new Process_Indicator
wenzelm@56757
    86
wenzelm@56757
    87
    val query_operation =
wenzelm@56757
    88
      new Query_Operation(PIDE.editor, view, "find_theorems",
wenzelm@56757
    89
        consume_status(process_indicator, _),
wenzelm@56757
    90
        (snapshot, results, body) =>
wenzelm@56757
    91
          pretty_text_area.update(snapshot, results, Pretty.separate(body)))
wenzelm@56757
    92
wenzelm@56757
    93
    private def apply_query()
wenzelm@56757
    94
    {
wenzelm@56757
    95
      query_operation.apply_query(List(limit.text, allow_dups.selected.toString, query.getText))
wenzelm@56757
    96
    }
wenzelm@56757
    97
wenzelm@56757
    98
    private val query_label = new Label("Search criteria:") {
wenzelm@56757
    99
      tooltip =
wenzelm@56757
   100
        GUI.tooltip_lines(
wenzelm@56757
   101
          "Search criteria for find operation, e.g.\n\"_ = _\" \"op +\" name: Group -name: monoid")
wenzelm@56757
   102
    }
wenzelm@56757
   103
wenzelm@56757
   104
    val query = make_query("isabelle-find-theorems", query_label.tooltip, apply_query _)
wenzelm@56757
   105
wenzelm@56757
   106
wenzelm@56757
   107
    /* controls */
wenzelm@56757
   108
wenzelm@56757
   109
    private val limit = new TextField(PIDE.options.int("find_theorems_limit").toString, 5) {
wenzelm@56757
   110
      tooltip = "Limit of displayed results"
wenzelm@56757
   111
      verifier = (s: String) =>
wenzelm@56757
   112
        s match { case Properties.Value.Int(x) => x >= 0 case _ => false }
wenzelm@56757
   113
      listenTo(keys)
wenzelm@56757
   114
      reactions += { case KeyPressed(_, Key.Enter, 0, _) => apply_query() }
wenzelm@56757
   115
    }
wenzelm@56757
   116
wenzelm@56757
   117
    private val allow_dups = new CheckBox("Duplicates") {
wenzelm@56757
   118
      tooltip = "Show all versions of matching theorems"
wenzelm@56757
   119
      selected = false
wenzelm@56757
   120
    }
wenzelm@56757
   121
wenzelm@56757
   122
    private val apply_button = new Button("Apply") {
wenzelm@56757
   123
      tooltip = "Find theorems meeting specified criteria"
wenzelm@56757
   124
      reactions += { case ButtonClicked(_) => apply_query() }
wenzelm@56757
   125
    }
wenzelm@56757
   126
wenzelm@56757
   127
wenzelm@56757
   128
    /* GUI page */
wenzelm@56757
   129
wenzelm@56757
   130
    private val controls: List[Component] =
wenzelm@56757
   131
      List(query_label, Component.wrap(query), limit, allow_dups,
wenzelm@56757
   132
        process_indicator.component, apply_button, zoom)
wenzelm@56757
   133
wenzelm@56757
   134
    val page =
wenzelm@56757
   135
      new TabbedPane.Page("Theorems", new BorderPanel {
wenzelm@56757
   136
        add(new Wrap_Panel(Wrap_Panel.Alignment.Right)(controls:_*), BorderPanel.Position.North)
wenzelm@56757
   137
        add(Component.wrap(pretty_text_area), BorderPanel.Position.Center)
wenzelm@56757
   138
      }, apply_button.tooltip)
wenzelm@56757
   139
  }
wenzelm@56757
   140
wenzelm@56757
   141
wenzelm@56757
   142
  /* operations */
wenzelm@56757
   143
wenzelm@56757
   144
  private val operations = List(find_theorems)
wenzelm@56757
   145
wenzelm@56757
   146
  private val tabs = new TabbedPane { for (op <- operations) pages += op.page }
wenzelm@56757
   147
  set_content(tabs)
wenzelm@56757
   148
wenzelm@56757
   149
  override def focusOnDefaultComponent {
wenzelm@56757
   150
    try { operations(tabs.selection.index).query.requestFocus }
wenzelm@56757
   151
    catch { case _: IndexOutOfBoundsException => }
wenzelm@56757
   152
  }
wenzelm@52865
   153
wenzelm@52865
   154
wenzelm@52865
   155
  /* resize */
wenzelm@52865
   156
wenzelm@56757
   157
  private def handle_resize(): Unit =
wenzelm@56757
   158
    Swing_Thread.require {
wenzelm@56757
   159
      for (op <- operations) {
wenzelm@56757
   160
        op.pretty_text_area.resize(
wenzelm@56757
   161
          Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom_factor / 100))
wenzelm@56757
   162
      }
wenzelm@56757
   163
    }
wenzelm@52846
   164
wenzelm@52865
   165
  private val delay_resize =
wenzelm@52865
   166
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@52854
   167
wenzelm@52865
   168
  addComponentListener(new ComponentAdapter {
wenzelm@52865
   169
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@52865
   170
  })
wenzelm@52846
   171
wenzelm@52846
   172
wenzelm@56715
   173
  /* main */
wenzelm@52846
   174
wenzelm@56715
   175
  private val main =
wenzelm@56715
   176
    Session.Consumer[Session.Global_Options](getClass.getName) {
wenzelm@56715
   177
      case _: Session.Global_Options => Swing_Thread.later { handle_resize() }
wenzelm@52846
   178
    }
wenzelm@52846
   179
wenzelm@52846
   180
  override def init()
wenzelm@52846
   181
  {
wenzelm@56715
   182
    PIDE.session.global_options += main
wenzelm@52848
   183
    handle_resize()
wenzelm@56757
   184
    operations.foreach(op => op.query_operation.activate())
wenzelm@52846
   185
  }
wenzelm@52846
   186
wenzelm@52846
   187
  override def exit()
wenzelm@52846
   188
  {
wenzelm@56757
   189
    operations.foreach(op => op.query_operation.deactivate())
wenzelm@56715
   190
    PIDE.session.global_options -= main
wenzelm@52846
   191
    delay_resize.revoke()
wenzelm@52846
   192
  }
wenzelm@56757
   193
}
wenzelm@52846
   194