src/Tools/jEdit/src/state_dockable.scala
author wenzelm
Sat, 13 Aug 2022 23:04:53 +0200
changeset 75853 f981111768ec
parent 75852 fcc25bb49def
child 75854 2163772eeaf2
permissions -rw-r--r--
clarified signature;
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 java.awt.BorderLayout
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    13
import java.awt.event.{ComponentEvent, ComponentAdapter}
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 org.gjt.sp.jedit.View
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    16
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    17
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    18
class State_Dockable(view: View, position: String) extends Dockable(view, position) {
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    19
  GUI_Thread.require {}
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    20
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    21
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    22
  /* text area */
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    23
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    24
  val pretty_text_area = new Pretty_Text_Area(view)
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    25
  set_content(pretty_text_area)
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    26
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 66591
diff changeset
    27
  override def detach_operation: Option[() => Unit] = pretty_text_area.detach_operation
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    28
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    29
  private val print_state =
66082
2d12a730a380 clarified modules;
wenzelm
parents: 65246
diff changeset
    30
    new Query_Operation(PIDE.editor, view, "print_state", _ => (),
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    31
      (snapshot, results, body) =>
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    32
        pretty_text_area.update(snapshot, results, Pretty.separate(body)))
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    33
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    34
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    35
  /* resize */
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    36
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    37
  private val delay_resize =
71704
b9a5eb0f3b43 clarified modules;
wenzelm
parents: 71601
diff changeset
    38
    Delay.first(PIDE.options.seconds("editor_update_delay"), gui = true) { handle_resize() }
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    39
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    40
  addComponentListener(new ComponentAdapter {
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    41
    override def componentResized(e: ComponentEvent): Unit = delay_resize.invoke()
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    42
    override def componentShown(e: ComponentEvent): Unit = delay_resize.invoke()
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    43
  })
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    44
75812
d6e8d12494be clarified signature;
wenzelm
parents: 75810
diff changeset
    45
  private def handle_resize(): Unit =
75839
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75812
diff changeset
    46
    GUI_Thread.require { pretty_text_area.zoom(zoom) }
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    47
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    48
61211
8a5d5ca5d8bc added action "isabelle-update-state";
wenzelm
parents: 61210
diff changeset
    49
  /* update */
61210
a3a05d734858 support for auto update via caret focus;
wenzelm
parents: 61208
diff changeset
    50
61801
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    51
  def update_request(): Unit =
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    52
    GUI_Thread.require { print_state.apply_query(Nil) }
61720
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
    53
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    54
  def update(): Unit = {
61210
a3a05d734858 support for auto update via caret focus;
wenzelm
parents: 61208
diff changeset
    55
    GUI_Thread.require {}
a3a05d734858 support for auto update via caret focus;
wenzelm
parents: 61208
diff changeset
    56
66091
wenzelm
parents: 66082
diff changeset
    57
    PIDE.editor.current_node_snapshot(view) match {
61211
8a5d5ca5d8bc added action "isabelle-update-state";
wenzelm
parents: 61210
diff changeset
    58
      case Some(snapshot) =>
66082
2d12a730a380 clarified modules;
wenzelm
parents: 65246
diff changeset
    59
        (PIDE.editor.current_command(view, snapshot), print_state.get_location) match {
61211
8a5d5ca5d8bc added action "isabelle-update-state";
wenzelm
parents: 61210
diff changeset
    60
          case (Some(command1), Some(command2)) if command1.id == command2.id =>
61801
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    61
          case _ => update_request()
61211
8a5d5ca5d8bc added action "isabelle-update-state";
wenzelm
parents: 61210
diff changeset
    62
        }
8a5d5ca5d8bc added action "isabelle-update-state";
wenzelm
parents: 61210
diff changeset
    63
      case None =>
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
  }
a3a05d734858 support for auto update via caret focus;
wenzelm
parents: 61208
diff changeset
    66
a3a05d734858 support for auto update via caret focus;
wenzelm
parents: 61208
diff changeset
    67
61801
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    68
  /* auto update */
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    69
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    70
  private var auto_update_enabled = true
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    71
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    72
  private def auto_update(): Unit =
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    73
    GUI_Thread.require { if (auto_update_enabled) update() }
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    74
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
    75
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    76
  /* controls */
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    77
75852
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
    78
  private val auto_update_button = new GUI.Bool("Auto update", init = auto_update_enabled) {
61720
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
    79
    tooltip = "Indicate automatic update following cursor movement"
75852
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
    80
    override def clicked(state: Boolean): Unit = {
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
    81
      auto_update_enabled = state
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
    82
      auto_update()
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
    83
    }
61720
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
    84
  }
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
    85
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
    86
  private val update_button = new GUI.Button("<html><b>Update</b></html>") {
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    87
    tooltip = "Update display according to the command at cursor position"
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
    88
    override def clicked(): Unit = update_request()
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    89
  }
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    90
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
    91
  private val locate_button = new GUI.Button("Locate") {
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    92
    tooltip = "Locate printed command within source text"
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
    93
    override def clicked(): Unit = print_state.locate_query()
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    94
  }
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
    95
75839
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75812
diff changeset
    96
  private val zoom = new Font_Info.Zoom { override def changed(): Unit = handle_resize() }
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
  private val controls =
66205
e9fa94f43a15 tuned signature;
wenzelm
parents: 66091
diff changeset
    99
    Wrap_Panel(
e9fa94f43a15 tuned signature;
wenzelm
parents: 66091
diff changeset
   100
      List(auto_update_button, update_button,
66206
2d2082db735a clarified defaults;
wenzelm
parents: 66205
diff changeset
   101
        locate_button, pretty_text_area.search_label, pretty_text_area.search_field, zoom))
66205
e9fa94f43a15 tuned signature;
wenzelm
parents: 66091
diff changeset
   102
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   103
  add(controls.peer, BorderLayout.NORTH)
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   104
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   105
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   106
  /* main */
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   107
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   108
  private val main =
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   109
    Session.Consumer[Any](getClass.getName) {
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   110
      case _: Session.Global_Options =>
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   111
        GUI_Thread.later { handle_resize() }
61720
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
   112
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
   113
      case changed: Session.Commands_Changed =>
61801
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
   114
        if (changed.assignment) GUI_Thread.later { auto_update() }
61720
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
   115
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
   116
      case Session.Caret_Focus =>
61801
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
   117
        GUI_Thread.later { auto_update() }
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   118
    }
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   119
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   120
  override def init(): Unit = {
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   121
    PIDE.session.global_options += main
61720
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
   122
    PIDE.session.commands_changed += main
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
   123
    PIDE.session.caret_focus += main
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   124
    handle_resize()
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   125
    print_state.activate()
61801
fa8d1cdf6518 tuned signature;
wenzelm
parents: 61726
diff changeset
   126
    auto_update()
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   127
  }
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   128
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   129
  override def exit(): Unit = {
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   130
    print_state.deactivate()
61720
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
   131
    PIDE.session.caret_focus -= main
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   132
    PIDE.session.global_options -= main
61720
a31730632e13 recovered auto update from f9aaca00be49;
wenzelm
parents: 61220
diff changeset
   133
    PIDE.session.commands_changed -= main
61208
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   134
    delay_resize.revoke()
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   135
  }
19118f9b939d separate panel for proof state output;
wenzelm
parents:
diff changeset
   136
}