src/Tools/jEdit/src/find_dockable.scala
author wenzelm
Fri Aug 02 11:51:21 2013 +0200 (2013-08-02)
changeset 52846 82ac963c68cb
child 52848 9489ca1d55dd
permissions -rw-r--r--
dockable window for "find" dialog (GUI only);
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
wenzelm@52846
    29
  /* component state -- owned by Swing thread */
wenzelm@52846
    30
wenzelm@52846
    31
  private var zoom_factor = 100
wenzelm@52846
    32
  private var current_snapshot = Document.State.init.snapshot()
wenzelm@52846
    33
  private var current_state = Command.empty.init_state
wenzelm@52846
    34
  private var current_output: List[XML.Tree] = Nil
wenzelm@52846
    35
  private var current_location: Option[Command] = None
wenzelm@52846
    36
wenzelm@52846
    37
wenzelm@52846
    38
  /* pretty text area */
wenzelm@52846
    39
wenzelm@52846
    40
  val pretty_text_area = new Pretty_Text_Area(view)
wenzelm@52846
    41
  set_content(pretty_text_area)
wenzelm@52846
    42
wenzelm@52846
    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@52846
    52
  private def handle_output()
wenzelm@52846
    53
  {
wenzelm@52846
    54
    Swing_Thread.require()
wenzelm@52846
    55
  }
wenzelm@52846
    56
wenzelm@52846
    57
  private def apply_query(text: String)
wenzelm@52846
    58
  {
wenzelm@52846
    59
    Swing_Thread.require()
wenzelm@52846
    60
  }
wenzelm@52846
    61
wenzelm@52846
    62
  private def locate_query()
wenzelm@52846
    63
  {
wenzelm@52846
    64
    Swing_Thread.require()
wenzelm@52846
    65
  }
wenzelm@52846
    66
wenzelm@52846
    67
wenzelm@52846
    68
  /* main actor */
wenzelm@52846
    69
wenzelm@52846
    70
  private val main_actor = actor {
wenzelm@52846
    71
    loop {
wenzelm@52846
    72
      react {
wenzelm@52846
    73
        case _: Session.Global_Options =>
wenzelm@52846
    74
          Swing_Thread.later { handle_resize() }
wenzelm@52846
    75
        case changed: Session.Commands_Changed =>
wenzelm@52846
    76
          current_location match {
wenzelm@52846
    77
            case Some(command) if changed.commands.contains(command) =>
wenzelm@52846
    78
              Swing_Thread.later { handle_output() }
wenzelm@52846
    79
            case _ =>
wenzelm@52846
    80
          }
wenzelm@52846
    81
        case bad =>
wenzelm@52846
    82
          java.lang.System.err.println("Find_Dockable: ignoring bad message " + bad)
wenzelm@52846
    83
      }
wenzelm@52846
    84
    }
wenzelm@52846
    85
  }
wenzelm@52846
    86
wenzelm@52846
    87
  override def init()
wenzelm@52846
    88
  {
wenzelm@52846
    89
    Swing_Thread.require()
wenzelm@52846
    90
wenzelm@52846
    91
    PIDE.session.global_options += main_actor
wenzelm@52846
    92
    PIDE.session.commands_changed += main_actor
wenzelm@52846
    93
    handle_output()
wenzelm@52846
    94
  }
wenzelm@52846
    95
wenzelm@52846
    96
  override def exit()
wenzelm@52846
    97
  {
wenzelm@52846
    98
    Swing_Thread.require()
wenzelm@52846
    99
wenzelm@52846
   100
    PIDE.session.global_options -= main_actor
wenzelm@52846
   101
    PIDE.session.commands_changed -= main_actor
wenzelm@52846
   102
    delay_resize.revoke()
wenzelm@52846
   103
  }
wenzelm@52846
   104
wenzelm@52846
   105
wenzelm@52846
   106
  /* resize */
wenzelm@52846
   107
wenzelm@52846
   108
  private val delay_resize =
wenzelm@52846
   109
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@52846
   110
wenzelm@52846
   111
  addComponentListener(new ComponentAdapter {
wenzelm@52846
   112
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@52846
   113
  })
wenzelm@52846
   114
wenzelm@52846
   115
wenzelm@52846
   116
  /* controls */
wenzelm@52846
   117
wenzelm@52846
   118
  private val query = new HistoryTextArea("isabelle-find-theorems") {
wenzelm@52846
   119
    { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
wenzelm@52846
   120
    setColumns(25)
wenzelm@52846
   121
    setRows(1)
wenzelm@52846
   122
  }
wenzelm@52846
   123
wenzelm@52846
   124
  private val query_wrapped = Component.wrap(query)
wenzelm@52846
   125
wenzelm@52846
   126
  private val find = new Button("Find") {
wenzelm@52846
   127
    tooltip = "Find theorems meeting specified criteria"
wenzelm@52846
   128
    reactions += { case ButtonClicked(_) => apply_query(query.getText) }
wenzelm@52846
   129
  }
wenzelm@52846
   130
wenzelm@52846
   131
  private val locate = new Button("Locate") {
wenzelm@52846
   132
    tooltip = "Locate context of current query within source text"
wenzelm@52846
   133
    reactions += { case ButtonClicked(_) => locate_query() }
wenzelm@52846
   134
  }
wenzelm@52846
   135
wenzelm@52846
   136
  private val zoom = new GUI.Zoom_Box(factor => { zoom_factor = factor; handle_resize() }) {
wenzelm@52846
   137
    tooltip = "Zoom factor for output font size"
wenzelm@52846
   138
  }
wenzelm@52846
   139
wenzelm@52846
   140
  private val controls =
wenzelm@52846
   141
    new FlowPanel(FlowPanel.Alignment.Right)(query_wrapped, find, locate, zoom)
wenzelm@52846
   142
  add(controls.peer, BorderLayout.NORTH)
wenzelm@52846
   143
}