src/Tools/jEdit/src/find_dockable.scala
author wenzelm
Fri Aug 02 16:02:06 2013 +0200 (2013-08-02)
changeset 52851 e71b5160f242
parent 52848 9489ca1d55dd
child 52854 92932931bd82
permissions -rw-r--r--
minimal print function "find_theorems", which merely echos its arguments;
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@52851
    31
  private val identification = Document_ID.make().toString
wenzelm@52851
    32
wenzelm@52846
    33
  private var zoom_factor = 100
wenzelm@52846
    34
  private var current_snapshot = Document.State.init.snapshot()
wenzelm@52846
    35
  private var current_state = Command.empty.init_state
wenzelm@52846
    36
  private var current_output: List[XML.Tree] = Nil
wenzelm@52846
    37
  private var current_location: Option[Command] = None
wenzelm@52851
    38
  private var current_query: String = ""
wenzelm@52851
    39
wenzelm@52851
    40
  private val FIND_THEOREMS = "find_theorems"
wenzelm@52846
    41
wenzelm@52846
    42
wenzelm@52846
    43
  /* pretty text area */
wenzelm@52846
    44
wenzelm@52846
    45
  val pretty_text_area = new Pretty_Text_Area(view)
wenzelm@52846
    46
  set_content(pretty_text_area)
wenzelm@52846
    47
wenzelm@52846
    48
wenzelm@52846
    49
  private def handle_resize()
wenzelm@52846
    50
  {
wenzelm@52846
    51
    Swing_Thread.require()
wenzelm@52846
    52
wenzelm@52846
    53
    pretty_text_area.resize(Rendering.font_family(),
wenzelm@52846
    54
      (Rendering.font_size("jedit_font_scale") * zoom_factor / 100).round)
wenzelm@52846
    55
  }
wenzelm@52846
    56
wenzelm@52846
    57
  private def handle_output()
wenzelm@52846
    58
  {
wenzelm@52846
    59
    Swing_Thread.require()
wenzelm@52846
    60
  }
wenzelm@52846
    61
wenzelm@52851
    62
  private def clear_overlay()
wenzelm@52846
    63
  {
wenzelm@52846
    64
    Swing_Thread.require()
wenzelm@52848
    65
wenzelm@52851
    66
    for {
wenzelm@52851
    67
      command <- current_location
wenzelm@52851
    68
      buffer <- JEdit_Lib.jedit_buffer(command.node_name.node)
wenzelm@52851
    69
      model <- PIDE.document_model(buffer)
wenzelm@52851
    70
    } model.remove_overlay(command, FIND_THEOREMS, List(identification, current_query))
wenzelm@52851
    71
wenzelm@52851
    72
    current_location = None
wenzelm@52851
    73
    current_query = ""
wenzelm@52851
    74
  }
wenzelm@52851
    75
wenzelm@52851
    76
  private def apply_query(query: String)
wenzelm@52851
    77
  {
wenzelm@52851
    78
    Swing_Thread.require()
wenzelm@52851
    79
wenzelm@52851
    80
    clear_overlay()
wenzelm@52848
    81
    Document_View(view.getTextArea) match {
wenzelm@52848
    82
      case Some(doc_view) =>
wenzelm@52848
    83
        val snapshot = doc_view.model.snapshot()
wenzelm@52851
    84
        snapshot.node.command_at(doc_view.text_area.getCaretPosition).map(_._1) match {
wenzelm@52851
    85
          case Some(command) =>
wenzelm@52851
    86
            current_location = Some(command)
wenzelm@52851
    87
            current_query = query
wenzelm@52851
    88
            doc_view.model.add_overlay(command, FIND_THEOREMS, List(identification, query))
wenzelm@52851
    89
          case None =>
wenzelm@52851
    90
        }
wenzelm@52848
    91
      case None =>
wenzelm@52848
    92
    }
wenzelm@52846
    93
  }
wenzelm@52846
    94
wenzelm@52846
    95
  private def locate_query()
wenzelm@52846
    96
  {
wenzelm@52846
    97
    Swing_Thread.require()
wenzelm@52848
    98
wenzelm@52848
    99
    current_location match {
wenzelm@52848
   100
      case Some(command) =>
wenzelm@52848
   101
        val snapshot = PIDE.session.snapshot(command.node_name)
wenzelm@52848
   102
        val commands = snapshot.node.commands
wenzelm@52848
   103
        if (commands.contains(command)) {
wenzelm@52848
   104
          val sources = commands.iterator.takeWhile(_ != command).map(_.source)
wenzelm@52848
   105
          val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@52848
   106
          Hyperlink(command.node_name.node, line, column).follow(view)
wenzelm@52848
   107
        }
wenzelm@52848
   108
      case None =>
wenzelm@52848
   109
    }
wenzelm@52846
   110
  }
wenzelm@52846
   111
wenzelm@52846
   112
wenzelm@52846
   113
  /* main actor */
wenzelm@52846
   114
wenzelm@52846
   115
  private val main_actor = actor {
wenzelm@52846
   116
    loop {
wenzelm@52846
   117
      react {
wenzelm@52846
   118
        case _: Session.Global_Options =>
wenzelm@52846
   119
          Swing_Thread.later { handle_resize() }
wenzelm@52846
   120
        case changed: Session.Commands_Changed =>
wenzelm@52846
   121
          current_location match {
wenzelm@52846
   122
            case Some(command) if changed.commands.contains(command) =>
wenzelm@52846
   123
              Swing_Thread.later { handle_output() }
wenzelm@52846
   124
            case _ =>
wenzelm@52846
   125
          }
wenzelm@52846
   126
        case bad =>
wenzelm@52846
   127
          java.lang.System.err.println("Find_Dockable: ignoring bad message " + bad)
wenzelm@52846
   128
      }
wenzelm@52846
   129
    }
wenzelm@52846
   130
  }
wenzelm@52846
   131
wenzelm@52846
   132
  override def init()
wenzelm@52846
   133
  {
wenzelm@52846
   134
    Swing_Thread.require()
wenzelm@52846
   135
wenzelm@52846
   136
    PIDE.session.global_options += main_actor
wenzelm@52846
   137
    PIDE.session.commands_changed += main_actor
wenzelm@52848
   138
    handle_resize()
wenzelm@52846
   139
  }
wenzelm@52846
   140
wenzelm@52846
   141
  override def exit()
wenzelm@52846
   142
  {
wenzelm@52846
   143
    Swing_Thread.require()
wenzelm@52846
   144
wenzelm@52846
   145
    PIDE.session.global_options -= main_actor
wenzelm@52846
   146
    PIDE.session.commands_changed -= main_actor
wenzelm@52846
   147
    delay_resize.revoke()
wenzelm@52846
   148
  }
wenzelm@52846
   149
wenzelm@52846
   150
wenzelm@52846
   151
  /* resize */
wenzelm@52846
   152
wenzelm@52846
   153
  private val delay_resize =
wenzelm@52846
   154
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@52846
   155
wenzelm@52846
   156
  addComponentListener(new ComponentAdapter {
wenzelm@52846
   157
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@52846
   158
  })
wenzelm@52846
   159
wenzelm@52846
   160
wenzelm@52846
   161
  /* controls */
wenzelm@52846
   162
wenzelm@52846
   163
  private val query = new HistoryTextArea("isabelle-find-theorems") {
wenzelm@52846
   164
    { val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) }
wenzelm@52846
   165
    setColumns(25)
wenzelm@52846
   166
    setRows(1)
wenzelm@52846
   167
  }
wenzelm@52846
   168
wenzelm@52846
   169
  private val query_wrapped = Component.wrap(query)
wenzelm@52846
   170
wenzelm@52846
   171
  private val find = new Button("Find") {
wenzelm@52846
   172
    tooltip = "Find theorems meeting specified criteria"
wenzelm@52846
   173
    reactions += { case ButtonClicked(_) => apply_query(query.getText) }
wenzelm@52846
   174
  }
wenzelm@52846
   175
wenzelm@52846
   176
  private val locate = new Button("Locate") {
wenzelm@52846
   177
    tooltip = "Locate context of current query within source text"
wenzelm@52846
   178
    reactions += { case ButtonClicked(_) => locate_query() }
wenzelm@52846
   179
  }
wenzelm@52846
   180
wenzelm@52846
   181
  private val zoom = new GUI.Zoom_Box(factor => { zoom_factor = factor; handle_resize() }) {
wenzelm@52846
   182
    tooltip = "Zoom factor for output font size"
wenzelm@52846
   183
  }
wenzelm@52846
   184
wenzelm@52846
   185
  private val controls =
wenzelm@52846
   186
    new FlowPanel(FlowPanel.Alignment.Right)(query_wrapped, find, locate, zoom)
wenzelm@52846
   187
  add(controls.peer, BorderLayout.NORTH)
wenzelm@52846
   188
}