src/Tools/jEdit/src/find_dockable.scala
author wenzelm
Mon Aug 05 17:14:02 2013 +0200 (2013-08-05)
changeset 52865 02a7e7180ee5
parent 52864 c2da0d3b974d
child 52874 91032244e4ca
permissions -rw-r--r--
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
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@52846
    12
import scala.actors.Actor._
wenzelm@52846
    13
wenzelm@52846
    14
import scala.swing.{FlowPanel, Button, Component}
wenzelm@52846
    15
import scala.swing.event.ButtonClicked
wenzelm@52846
    16
wenzelm@52846
    17
import java.awt.BorderLayout
wenzelm@52846
    18
import java.awt.event.{ComponentEvent, ComponentAdapter}
wenzelm@52846
    19
wenzelm@52846
    20
import org.gjt.sp.jedit.View
wenzelm@52846
    21
import org.gjt.sp.jedit.gui.HistoryTextArea
wenzelm@52846
    22
wenzelm@52846
    23
wenzelm@52846
    24
class Find_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@52846
    25
{
wenzelm@52846
    26
  Swing_Thread.require()
wenzelm@52846
    27
wenzelm@52846
    28
  val pretty_text_area = new Pretty_Text_Area(view)
wenzelm@52846
    29
  set_content(pretty_text_area)
wenzelm@52846
    30
wenzelm@52846
    31
wenzelm@52865
    32
  /* query operation */
wenzelm@52865
    33
wenzelm@52865
    34
  private val find_theorems =
wenzelm@52865
    35
    Query_Operation(view, "find_theorems",
wenzelm@52865
    36
      (snapshot, results, body) =>
wenzelm@52865
    37
        pretty_text_area.update(snapshot, results, Pretty.separate(body)))
wenzelm@52865
    38
wenzelm@52865
    39
wenzelm@52865
    40
  /* resize */
wenzelm@52865
    41
wenzelm@52865
    42
  private var zoom_factor = 100
wenzelm@52865
    43
wenzelm@52846
    44
  private def handle_resize()
wenzelm@52846
    45
  {
wenzelm@52846
    46
    Swing_Thread.require()
wenzelm@52846
    47
wenzelm@52846
    48
    pretty_text_area.resize(Rendering.font_family(),
wenzelm@52846
    49
      (Rendering.font_size("jedit_font_scale") * zoom_factor / 100).round)
wenzelm@52846
    50
  }
wenzelm@52846
    51
wenzelm@52865
    52
  private val delay_resize =
wenzelm@52865
    53
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@52854
    54
wenzelm@52865
    55
  addComponentListener(new ComponentAdapter {
wenzelm@52865
    56
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@52865
    57
  })
wenzelm@52846
    58
wenzelm@52846
    59
wenzelm@52846
    60
  /* main actor */
wenzelm@52846
    61
wenzelm@52846
    62
  private val main_actor = actor {
wenzelm@52846
    63
    loop {
wenzelm@52846
    64
      react {
wenzelm@52846
    65
        case _: Session.Global_Options =>
wenzelm@52846
    66
          Swing_Thread.later { handle_resize() }
wenzelm@52846
    67
        case bad =>
wenzelm@52846
    68
          java.lang.System.err.println("Find_Dockable: ignoring bad message " + bad)
wenzelm@52846
    69
      }
wenzelm@52846
    70
    }
wenzelm@52846
    71
  }
wenzelm@52846
    72
wenzelm@52846
    73
  override def init()
wenzelm@52846
    74
  {
wenzelm@52846
    75
    Swing_Thread.require()
wenzelm@52846
    76
wenzelm@52846
    77
    PIDE.session.global_options += main_actor
wenzelm@52848
    78
    handle_resize()
wenzelm@52865
    79
    find_theorems.activate()
wenzelm@52846
    80
  }
wenzelm@52846
    81
wenzelm@52846
    82
  override def exit()
wenzelm@52846
    83
  {
wenzelm@52846
    84
    Swing_Thread.require()
wenzelm@52846
    85
wenzelm@52865
    86
    find_theorems.deactivate()
wenzelm@52846
    87
    PIDE.session.global_options -= main_actor
wenzelm@52846
    88
    delay_resize.revoke()
wenzelm@52846
    89
  }
wenzelm@52846
    90
wenzelm@52846
    91
wenzelm@52846
    92
  /* controls */
wenzelm@52846
    93
wenzelm@52846
    94
  private val query = new HistoryTextArea("isabelle-find-theorems") {
wenzelm@52846
    95
    { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
wenzelm@52846
    96
    setColumns(25)
wenzelm@52846
    97
    setRows(1)
wenzelm@52846
    98
  }
wenzelm@52846
    99
wenzelm@52846
   100
  private val query_wrapped = Component.wrap(query)
wenzelm@52846
   101
wenzelm@52846
   102
  private val find = new Button("Find") {
wenzelm@52846
   103
    tooltip = "Find theorems meeting specified criteria"
wenzelm@52865
   104
    reactions += { case ButtonClicked(_) => find_theorems.apply_query(List(query.getText)) }
wenzelm@52846
   105
  }
wenzelm@52846
   106
wenzelm@52846
   107
  private val locate = new Button("Locate") {
wenzelm@52846
   108
    tooltip = "Locate context of current query within source text"
wenzelm@52865
   109
    reactions += { case ButtonClicked(_) => find_theorems.locate_query() }
wenzelm@52846
   110
  }
wenzelm@52846
   111
wenzelm@52846
   112
  private val zoom = new GUI.Zoom_Box(factor => { zoom_factor = factor; handle_resize() }) {
wenzelm@52846
   113
    tooltip = "Zoom factor for output font size"
wenzelm@52846
   114
  }
wenzelm@52846
   115
wenzelm@52846
   116
  private val controls =
wenzelm@52846
   117
    new FlowPanel(FlowPanel.Alignment.Right)(query_wrapped, find, locate, zoom)
wenzelm@52846
   118
  add(controls.peer, BorderLayout.NORTH)
wenzelm@52846
   119
}