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