src/Tools/jEdit/src/find_dockable.scala
author wenzelm
Thu, 21 Nov 2013 21:55:29 +0100
changeset 54640 bbd2fa353809
parent 54368 36dc6aa4fe87
child 55618 995162143ef4
permissions -rw-r--r--
back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command; resolve sendback wrt. static command id, to allow active area even after exec_id is removed (cf. prune_delay);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/find_dockable.scala
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     3
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     4
Dockable window for "find" dialog.
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     5
*/
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     6
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     8
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
     9
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    10
import isabelle._
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    11
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    12
import scala.actors.Actor._
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    13
53711
8ce7795256e1 improved FlowLayout for wrapping of components over multiple lines;
wenzelm
parents: 53177
diff changeset
    14
import scala.swing.{Button, Component, TextField, CheckBox, Label, ComboBox}
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    15
import scala.swing.event.ButtonClicked
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    16
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    17
import java.awt.BorderLayout
52886
1e22e8101f47 enabled key event to apply query;
wenzelm
parents: 52885
diff changeset
    18
import java.awt.event.{ComponentEvent, ComponentAdapter, KeyEvent}
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    19
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    20
import org.gjt.sp.jedit.View
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    21
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    22
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    23
class Find_Dockable(view: View, position: String) extends Dockable(view, position)
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    24
{
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    25
  val pretty_text_area = new Pretty_Text_Area(view)
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    26
  set_content(pretty_text_area)
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    27
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    28
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    29
  /* query operation */
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    30
52935
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    31
  private val process_indicator = new Process_Indicator
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    32
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    33
  private def consume_status(status: Query_Operation.Status.Value)
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    34
  {
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    35
    status match {
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    36
      case Query_Operation.Status.WAITING =>
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    37
        process_indicator.update("Waiting for evaluation of context ...", 5)
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    38
      case Query_Operation.Status.RUNNING =>
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    39
        process_indicator.update("Running find operation ...", 15)
54640
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54368
diff changeset
    40
      case Query_Operation.Status.FINISHED =>
52935
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    41
        process_indicator.update(null, 0)
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    42
    }
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    43
  }
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52886
diff changeset
    44
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    45
  private val find_theorems =
52971
31926d2c04ee tuned signature -- more abstract PIDE editor operations;
wenzelm
parents: 52951
diff changeset
    46
    new Query_Operation(PIDE.editor, view, "find_theorems", consume_status _,
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    47
      (snapshot, results, body) =>
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    48
        pretty_text_area.update(snapshot, results, Pretty.separate(body)))
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    49
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    50
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    51
  /* resize */
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    52
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    53
  private var zoom_factor = 100
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    54
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    55
  private def handle_resize()
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    56
  {
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    57
    Swing_Thread.require()
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    58
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    59
    pretty_text_area.resize(Rendering.font_family(),
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    60
      (Rendering.font_size("jedit_font_scale") * zoom_factor / 100).round)
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    61
  }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    62
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    63
  private val delay_resize =
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    64
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
52854
92932931bd82 more general Output.result: allow to update arbitrary properties;
wenzelm
parents: 52851
diff changeset
    65
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    66
  addComponentListener(new ComponentAdapter {
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    67
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    68
  })
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    69
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    70
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    71
  /* main actor */
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    72
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    73
  private val main_actor = actor {
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    74
    loop {
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    75
      react {
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    76
        case _: Session.Global_Options =>
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    77
          Swing_Thread.later { handle_resize() }
53177
dcac8d837b9c more uniform treatment of Swing_Thread context switch: prefer asynchronous Swing_Thread.later from actor;
wenzelm
parents: 52971
diff changeset
    78
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    79
        case bad =>
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    80
          java.lang.System.err.println("Find_Dockable: ignoring bad message " + bad)
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    81
      }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    82
    }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    83
  }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    84
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    85
  override def init()
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    86
  {
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    87
    PIDE.session.global_options += main_actor
52848
9489ca1d55dd some tracking of command location;
wenzelm
parents: 52846
diff changeset
    88
    handle_resize()
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    89
    find_theorems.activate()
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    90
  }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    91
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    92
  override def exit()
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    93
  {
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents: 52864
diff changeset
    94
    find_theorems.deactivate()
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    95
    PIDE.session.global_options -= main_actor
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    96
    delay_resize.revoke()
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    97
  }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    98
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
    99
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   100
  /* controls */
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   101
52942
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   102
  private def clicked {
52943
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   103
    find_theorems.apply_query(
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   104
      List(limit.text, allow_dups.selected.toString, context.selection.item.name, query.getText))
52942
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   105
  }
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   106
52946
976bd071360c tuned GUI;
wenzelm
parents: 52944
diff changeset
   107
  private val query_label = new Label("Search criteria:") {
54367
e358b79b533a tuned tooltips;
wenzelm
parents: 53872
diff changeset
   108
    tooltip =
e358b79b533a tuned tooltips;
wenzelm
parents: 53872
diff changeset
   109
      GUI.tooltip_lines(List(
e358b79b533a tuned tooltips;
wenzelm
parents: 53872
diff changeset
   110
        "Search criteria for find operation, e.g.",
e358b79b533a tuned tooltips;
wenzelm
parents: 53872
diff changeset
   111
        "",
e358b79b533a tuned tooltips;
wenzelm
parents: 53872
diff changeset
   112
        "  \"_ = _\" \"op +\" name: Group -name: monoid"))
52946
976bd071360c tuned GUI;
wenzelm
parents: 52944
diff changeset
   113
  }
52886
1e22e8101f47 enabled key event to apply query;
wenzelm
parents: 52885
diff changeset
   114
53784
322a3ff42b33 completion popup for history text field;
wenzelm
parents: 53711
diff changeset
   115
  private val query = new Completion_Popup.History_Text_Field("isabelle-find-theorems") {
52886
1e22e8101f47 enabled key event to apply query;
wenzelm
parents: 52885
diff changeset
   116
    override def processKeyEvent(evt: KeyEvent)
1e22e8101f47 enabled key event to apply query;
wenzelm
parents: 52885
diff changeset
   117
    {
1e22e8101f47 enabled key event to apply query;
wenzelm
parents: 52885
diff changeset
   118
      if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) clicked
1e22e8101f47 enabled key event to apply query;
wenzelm
parents: 52885
diff changeset
   119
      super.processKeyEvent(evt)
1e22e8101f47 enabled key event to apply query;
wenzelm
parents: 52885
diff changeset
   120
    }
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   121
    { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
52885
9e4bae21494d prefer single-line HistoryTextField;
wenzelm
parents: 52880
diff changeset
   122
    setColumns(40)
52946
976bd071360c tuned GUI;
wenzelm
parents: 52944
diff changeset
   123
    setToolTipText(query_label.tooltip)
54368
36dc6aa4fe87 less tiny font for search criteria;
wenzelm
parents: 54367
diff changeset
   124
    setFont(GUI.imitate_font(Rendering.font_family(), getFont, 1.2))
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   125
  }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   126
52943
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   127
  private case class Context_Entry(val name: String, val description: String)
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   128
  {
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   129
    override def toString = description
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   130
  }
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   131
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   132
  private val context_entries =
52946
976bd071360c tuned GUI;
wenzelm
parents: 52944
diff changeset
   133
    new Context_Entry("", "current context") ::
52943
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   134
      PIDE.thy_load.loaded_theories.toList.sorted.map(name => Context_Entry(name, name))
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   135
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   136
  private val context = new ComboBox[Context_Entry](context_entries) {
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   137
    tooltip = "Search in pre-loaded theory (default: context of current command)"
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   138
  }
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   139
52942
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   140
  private val limit = new TextField(PIDE.options.int("find_theorems_limit").toString, 5) {
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   141
    tooltip = "Limit of displayed results"
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   142
    verifier = (s: String) =>
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   143
      s match { case Properties.Value.Int(x) => x >= 0 case _ => false }
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   144
  }
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   145
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   146
  private val allow_dups = new CheckBox("Duplicates") {
52951
de4bccddcdbd adjust tooltip for duplicates option
kleing
parents: 52946
diff changeset
   147
    tooltip = "Show all versions of matching theorems"
52942
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   148
    selected = false
07093b66fc9d more GUI options;
wenzelm
parents: 52935
diff changeset
   149
  }
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   150
52880
91f7fcaa2147 more generic button;
wenzelm
parents: 52874
diff changeset
   151
  private val apply_query = new Button("Apply") {
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   152
    tooltip = "Find theorems meeting specified criteria"
52886
1e22e8101f47 enabled key event to apply query;
wenzelm
parents: 52885
diff changeset
   153
    reactions += { case ButtonClicked(_) => clicked }
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   154
  }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   155
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   156
  private val zoom = new GUI.Zoom_Box(factor => { zoom_factor = factor; handle_resize() }) {
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   157
    tooltip = "Zoom factor for output font size"
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   158
  }
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   159
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   160
  private val controls =
53711
8ce7795256e1 improved FlowLayout for wrapping of components over multiple lines;
wenzelm
parents: 53177
diff changeset
   161
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(
52943
14ddcc0ad7df enable search in pre-loaded theory;
wenzelm
parents: 52942
diff changeset
   162
      query_label, Component.wrap(query), context, limit, allow_dups,
52944
4b053d8d0e7e removed "Locate" button, to avoid confusion about the slightly odd meaning of current_command with explicit theory context;
wenzelm
parents: 52943
diff changeset
   163
      process_indicator.component, apply_query, zoom)
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   164
  add(controls.peer, BorderLayout.NORTH)
53787
e64389fe2d2c focus on default component according to jEdit window management;
wenzelm
parents: 53785
diff changeset
   165
e64389fe2d2c focus on default component according to jEdit window management;
wenzelm
parents: 53785
diff changeset
   166
  override def focusOnDefaultComponent { query.requestFocus }
52846
82ac963c68cb dockable window for "find" dialog (GUI only);
wenzelm
parents:
diff changeset
   167
}