src/Tools/jEdit/src/find_dockable.scala
author wenzelm
Fri Aug 02 12:19:29 2013 +0200 (2013-08-02)
changeset 52848 9489ca1d55dd
parent 52846 82ac963c68cb
child 52851 e71b5160f242
permissions -rw-r--r--
some tracking of command location;
tuned;
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@52848
    60
wenzelm@52848
    61
    Document_View(view.getTextArea) match {
wenzelm@52848
    62
      case Some(doc_view) =>
wenzelm@52848
    63
        val snapshot = doc_view.model.snapshot()
wenzelm@52848
    64
        current_location = snapshot.node.command_at(doc_view.text_area.getCaretPosition).map(_._1)
wenzelm@52848
    65
      case None =>
wenzelm@52848
    66
    }
wenzelm@52846
    67
  }
wenzelm@52846
    68
wenzelm@52846
    69
  private def locate_query()
wenzelm@52846
    70
  {
wenzelm@52846
    71
    Swing_Thread.require()
wenzelm@52848
    72
wenzelm@52848
    73
    current_location match {
wenzelm@52848
    74
      case Some(command) =>
wenzelm@52848
    75
        val snapshot = PIDE.session.snapshot(command.node_name)
wenzelm@52848
    76
        val commands = snapshot.node.commands
wenzelm@52848
    77
        if (commands.contains(command)) {
wenzelm@52848
    78
          val sources = commands.iterator.takeWhile(_ != command).map(_.source)
wenzelm@52848
    79
          val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@52848
    80
          Hyperlink(command.node_name.node, line, column).follow(view)
wenzelm@52848
    81
        }
wenzelm@52848
    82
      case None =>
wenzelm@52848
    83
    }
wenzelm@52846
    84
  }
wenzelm@52846
    85
wenzelm@52846
    86
wenzelm@52846
    87
  /* main actor */
wenzelm@52846
    88
wenzelm@52846
    89
  private val main_actor = actor {
wenzelm@52846
    90
    loop {
wenzelm@52846
    91
      react {
wenzelm@52846
    92
        case _: Session.Global_Options =>
wenzelm@52846
    93
          Swing_Thread.later { handle_resize() }
wenzelm@52846
    94
        case changed: Session.Commands_Changed =>
wenzelm@52846
    95
          current_location match {
wenzelm@52846
    96
            case Some(command) if changed.commands.contains(command) =>
wenzelm@52846
    97
              Swing_Thread.later { handle_output() }
wenzelm@52846
    98
            case _ =>
wenzelm@52846
    99
          }
wenzelm@52846
   100
        case bad =>
wenzelm@52846
   101
          java.lang.System.err.println("Find_Dockable: ignoring bad message " + bad)
wenzelm@52846
   102
      }
wenzelm@52846
   103
    }
wenzelm@52846
   104
  }
wenzelm@52846
   105
wenzelm@52846
   106
  override def init()
wenzelm@52846
   107
  {
wenzelm@52846
   108
    Swing_Thread.require()
wenzelm@52846
   109
wenzelm@52846
   110
    PIDE.session.global_options += main_actor
wenzelm@52846
   111
    PIDE.session.commands_changed += main_actor
wenzelm@52848
   112
    handle_resize()
wenzelm@52846
   113
  }
wenzelm@52846
   114
wenzelm@52846
   115
  override def exit()
wenzelm@52846
   116
  {
wenzelm@52846
   117
    Swing_Thread.require()
wenzelm@52846
   118
wenzelm@52846
   119
    PIDE.session.global_options -= main_actor
wenzelm@52846
   120
    PIDE.session.commands_changed -= main_actor
wenzelm@52846
   121
    delay_resize.revoke()
wenzelm@52846
   122
  }
wenzelm@52846
   123
wenzelm@52846
   124
wenzelm@52846
   125
  /* resize */
wenzelm@52846
   126
wenzelm@52846
   127
  private val delay_resize =
wenzelm@52846
   128
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@52846
   129
wenzelm@52846
   130
  addComponentListener(new ComponentAdapter {
wenzelm@52846
   131
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@52846
   132
  })
wenzelm@52846
   133
wenzelm@52846
   134
wenzelm@52846
   135
  /* controls */
wenzelm@52846
   136
wenzelm@52846
   137
  private val query = new HistoryTextArea("isabelle-find-theorems") {
wenzelm@52846
   138
    { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
wenzelm@52846
   139
    setColumns(25)
wenzelm@52846
   140
    setRows(1)
wenzelm@52846
   141
  }
wenzelm@52846
   142
wenzelm@52846
   143
  private val query_wrapped = Component.wrap(query)
wenzelm@52846
   144
wenzelm@52846
   145
  private val find = new Button("Find") {
wenzelm@52846
   146
    tooltip = "Find theorems meeting specified criteria"
wenzelm@52846
   147
    reactions += { case ButtonClicked(_) => apply_query(query.getText) }
wenzelm@52846
   148
  }
wenzelm@52846
   149
wenzelm@52846
   150
  private val locate = new Button("Locate") {
wenzelm@52846
   151
    tooltip = "Locate context of current query within source text"
wenzelm@52846
   152
    reactions += { case ButtonClicked(_) => locate_query() }
wenzelm@52846
   153
  }
wenzelm@52846
   154
wenzelm@52846
   155
  private val zoom = new GUI.Zoom_Box(factor => { zoom_factor = factor; handle_resize() }) {
wenzelm@52846
   156
    tooltip = "Zoom factor for output font size"
wenzelm@52846
   157
  }
wenzelm@52846
   158
wenzelm@52846
   159
  private val controls =
wenzelm@52846
   160
    new FlowPanel(FlowPanel.Alignment.Right)(query_wrapped, find, locate, zoom)
wenzelm@52846
   161
  add(controls.peer, BorderLayout.NORTH)
wenzelm@52846
   162
}