src/Tools/jEdit/src/state_dockable.scala
author wenzelm
Mon Jan 09 20:26:59 2017 +0100 (2017-01-09)
changeset 64854 f5aa712e6250
parent 64813 7283f41d05ab
child 65246 848965b5befc
permissions -rw-r--r--
tuned signature;
wenzelm@61208
     1
/*  Title:      Tools/jEdit/src/state_dockable.scala
wenzelm@61208
     2
    Author:     Makarius
wenzelm@61208
     3
wenzelm@61208
     4
Dockable window for proof state output.
wenzelm@61208
     5
*/
wenzelm@61208
     6
wenzelm@61208
     7
package isabelle.jedit
wenzelm@61208
     8
wenzelm@61208
     9
wenzelm@61208
    10
import isabelle._
wenzelm@61208
    11
wenzelm@61208
    12
import scala.swing.{Button, CheckBox}
wenzelm@61208
    13
import scala.swing.event.ButtonClicked
wenzelm@61208
    14
wenzelm@61208
    15
import java.awt.BorderLayout
wenzelm@61208
    16
import java.awt.event.{ComponentEvent, ComponentAdapter}
wenzelm@61208
    17
wenzelm@61208
    18
import org.gjt.sp.jedit.View
wenzelm@61208
    19
wenzelm@61208
    20
wenzelm@61208
    21
class State_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@61208
    22
{
wenzelm@61208
    23
  GUI_Thread.require {}
wenzelm@61208
    24
wenzelm@61208
    25
wenzelm@61208
    26
  /* text area */
wenzelm@61208
    27
wenzelm@61208
    28
  val pretty_text_area = new Pretty_Text_Area(view)
wenzelm@61208
    29
  set_content(pretty_text_area)
wenzelm@61208
    30
wenzelm@61208
    31
  override def detach_operation = pretty_text_area.detach_operation
wenzelm@61208
    32
wenzelm@61208
    33
  private val print_state =
wenzelm@61208
    34
    new Query_Operation(PIDE.editor, view, "print_state", _ => (),
wenzelm@61208
    35
      (snapshot, results, body) =>
wenzelm@61208
    36
        pretty_text_area.update(snapshot, results, Pretty.separate(body)))
wenzelm@61208
    37
wenzelm@61208
    38
wenzelm@61208
    39
  /* resize */
wenzelm@61208
    40
wenzelm@61208
    41
  private val delay_resize =
wenzelm@61208
    42
    GUI_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@61208
    43
wenzelm@61208
    44
  addComponentListener(new ComponentAdapter {
wenzelm@61208
    45
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@61208
    46
    override def componentShown(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@61208
    47
  })
wenzelm@61208
    48
wenzelm@61208
    49
  private def handle_resize()
wenzelm@61208
    50
  {
wenzelm@61208
    51
    GUI_Thread.require {}
wenzelm@61208
    52
wenzelm@61208
    53
    pretty_text_area.resize(
wenzelm@61208
    54
      Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom.factor / 100))
wenzelm@61208
    55
  }
wenzelm@61208
    56
wenzelm@61208
    57
wenzelm@61211
    58
  /* update */
wenzelm@61210
    59
wenzelm@61801
    60
  def update_request(): Unit =
wenzelm@61801
    61
    GUI_Thread.require { print_state.apply_query(Nil) }
wenzelm@61720
    62
wenzelm@61211
    63
  def update()
wenzelm@61210
    64
  {
wenzelm@61210
    65
    GUI_Thread.require {}
wenzelm@61210
    66
wenzelm@64813
    67
    Document_Model.get(view.getBuffer).map(_.snapshot()) match {
wenzelm@61211
    68
      case Some(snapshot) =>
wenzelm@61211
    69
        (PIDE.editor.current_command(view, snapshot), print_state.get_location) match {
wenzelm@61211
    70
          case (Some(command1), Some(command2)) if command1.id == command2.id =>
wenzelm@61801
    71
          case _ => update_request()
wenzelm@61211
    72
        }
wenzelm@61211
    73
      case None =>
wenzelm@61210
    74
    }
wenzelm@61210
    75
  }
wenzelm@61210
    76
wenzelm@61210
    77
wenzelm@61801
    78
  /* auto update */
wenzelm@61801
    79
wenzelm@61801
    80
  private var auto_update_enabled = true
wenzelm@61801
    81
wenzelm@61801
    82
  private def auto_update(): Unit =
wenzelm@61801
    83
    GUI_Thread.require { if (auto_update_enabled) update() }
wenzelm@61801
    84
wenzelm@61801
    85
wenzelm@61208
    86
  /* controls */
wenzelm@61208
    87
wenzelm@61720
    88
  private val auto_update_button = new CheckBox("Auto update") {
wenzelm@61720
    89
    tooltip = "Indicate automatic update following cursor movement"
wenzelm@61801
    90
    reactions += { case ButtonClicked(_) => auto_update_enabled = this.selected; auto_update() }
wenzelm@61801
    91
    selected = auto_update_enabled
wenzelm@61720
    92
  }
wenzelm@61720
    93
wenzelm@61220
    94
  private val update_button = new Button("<html><b>Update</b></html>") {
wenzelm@61208
    95
    tooltip = "Update display according to the command at cursor position"
wenzelm@61801
    96
    reactions += { case ButtonClicked(_) => update_request() }
wenzelm@61208
    97
  }
wenzelm@61208
    98
wenzelm@61211
    99
  private val locate_button = new Button("Locate") {
wenzelm@61208
   100
    tooltip = "Locate printed command within source text"
wenzelm@61208
   101
    reactions += { case ButtonClicked(_) => print_state.locate_query() }
wenzelm@61208
   102
  }
wenzelm@61208
   103
wenzelm@61208
   104
  private val zoom = new Font_Info.Zoom_Box { def changed = handle_resize() }
wenzelm@61208
   105
wenzelm@61208
   106
  private val controls =
wenzelm@61720
   107
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(auto_update_button, update_button,
wenzelm@61720
   108
      locate_button, pretty_text_area.search_label, pretty_text_area.search_field, zoom)
wenzelm@61208
   109
  add(controls.peer, BorderLayout.NORTH)
wenzelm@61208
   110
wenzelm@61208
   111
wenzelm@61208
   112
  /* main */
wenzelm@61208
   113
wenzelm@61208
   114
  private val main =
wenzelm@61208
   115
    Session.Consumer[Any](getClass.getName) {
wenzelm@61208
   116
      case _: Session.Global_Options =>
wenzelm@61208
   117
        GUI_Thread.later { handle_resize() }
wenzelm@61720
   118
wenzelm@61720
   119
      case changed: Session.Commands_Changed =>
wenzelm@61801
   120
        if (changed.assignment) GUI_Thread.later { auto_update() }
wenzelm@61720
   121
wenzelm@61720
   122
      case Session.Caret_Focus =>
wenzelm@61801
   123
        GUI_Thread.later { auto_update() }
wenzelm@61208
   124
    }
wenzelm@61208
   125
wenzelm@61208
   126
  override def init()
wenzelm@61208
   127
  {
wenzelm@61208
   128
    PIDE.session.global_options += main
wenzelm@61720
   129
    PIDE.session.commands_changed += main
wenzelm@61720
   130
    PIDE.session.caret_focus += main
wenzelm@61208
   131
    handle_resize()
wenzelm@61208
   132
    print_state.activate()
wenzelm@61801
   133
    auto_update()
wenzelm@61208
   134
  }
wenzelm@61208
   135
wenzelm@61208
   136
  override def exit()
wenzelm@61208
   137
  {
wenzelm@61208
   138
    print_state.deactivate()
wenzelm@61720
   139
    PIDE.session.caret_focus -= main
wenzelm@61208
   140
    PIDE.session.global_options -= main
wenzelm@61720
   141
    PIDE.session.commands_changed -= main
wenzelm@61208
   142
    delay_resize.revoke()
wenzelm@61208
   143
  }
wenzelm@61208
   144
}