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