src/Tools/jEdit/src/output_dockable.scala
author wenzelm
Wed Jul 23 11:19:24 2014 +0200 (2014-07-23 ago)
changeset 57612 990ffb84489b
parent 57044 042d6e58cb40
child 60750 7694aa52ad56
permissions -rw-r--r--
clarified module name: facilitate alternative GUI frameworks;
wenzelm@49494
     1
/*  Title:      Tools/jEdit/src/output_dockable.scala
wenzelm@49398
     2
    Author:     Makarius
wenzelm@49398
     3
wenzelm@49398
     4
Dockable window with result message output.
wenzelm@49398
     5
*/
wenzelm@49398
     6
wenzelm@49398
     7
package isabelle.jedit
wenzelm@49398
     8
wenzelm@49398
     9
wenzelm@49398
    10
import isabelle._
wenzelm@49398
    11
wenzelm@53711
    12
import scala.swing.{Button, CheckBox}
wenzelm@49398
    13
import scala.swing.event.ButtonClicked
wenzelm@49398
    14
wenzelm@49398
    15
import java.awt.BorderLayout
wenzelm@49398
    16
import java.awt.event.{ComponentEvent, ComponentAdapter}
wenzelm@49398
    17
wenzelm@49398
    18
import org.gjt.sp.jedit.View
wenzelm@49398
    19
wenzelm@49398
    20
wenzelm@49494
    21
class Output_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@49398
    22
{
wenzelm@57612
    23
  /* component state -- owned by GUI thread */
wenzelm@49398
    24
wenzelm@49398
    25
  private var do_update = true
wenzelm@52972
    26
  private var current_snapshot = Document.Snapshot.init
wenzelm@56299
    27
  private var current_command = Command.empty
wenzelm@56299
    28
  private var current_results = Command.Results.empty
wenzelm@49415
    29
  private var current_output: List[XML.Tree] = Nil
wenzelm@49398
    30
wenzelm@49398
    31
wenzelm@49726
    32
  /* pretty text area */
wenzelm@49398
    33
wenzelm@50541
    34
  val pretty_text_area = new Pretty_Text_Area(view)
wenzelm@49398
    35
  set_content(pretty_text_area)
wenzelm@49398
    36
wenzelm@56918
    37
  override def detach_operation = pretty_text_area.detach_operation
wenzelm@56906
    38
wenzelm@49398
    39
wenzelm@57044
    40
  private val zoom = new Font_Info.Zoom_Box { def changed = handle_resize() }
wenzelm@57044
    41
wenzelm@49398
    42
  private def handle_resize()
wenzelm@49398
    43
  {
wenzelm@57612
    44
    GUI_Thread.require {}
wenzelm@49398
    45
wenzelm@55825
    46
    pretty_text_area.resize(
wenzelm@57044
    47
      Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom.factor / 100))
wenzelm@49398
    48
  }
wenzelm@49398
    49
wenzelm@49398
    50
  private def handle_update(follow: Boolean, restriction: Option[Set[Command]])
wenzelm@49398
    51
  {
wenzelm@57612
    52
    GUI_Thread.require {}
wenzelm@49398
    53
wenzelm@56299
    54
    val (new_snapshot, new_command, new_results) =
wenzelm@52978
    55
      PIDE.editor.current_node_snapshot(view) match {
wenzelm@52978
    56
        case Some(snapshot) =>
wenzelm@49419
    57
          if (follow && !snapshot.is_outdated) {
wenzelm@52978
    58
            PIDE.editor.current_command(view, snapshot) match {
wenzelm@53844
    59
              case Some(cmd) =>
wenzelm@56299
    60
                (snapshot, cmd, snapshot.state.command_results(snapshot.version, cmd))
wenzelm@49419
    61
              case None =>
wenzelm@56299
    62
                (Document.Snapshot.init, Command.empty, Command.Results.empty)
wenzelm@49398
    63
            }
wenzelm@49419
    64
          }
wenzelm@56299
    65
          else (current_snapshot, current_command, current_results)
wenzelm@56299
    66
        case None => (current_snapshot, current_command, current_results)
wenzelm@49398
    67
      }
wenzelm@49398
    68
wenzelm@50409
    69
    val new_output =
wenzelm@56299
    70
      if (!restriction.isDefined || restriction.get.contains(new_command)) {
wenzelm@50500
    71
        val rendering = Rendering(new_snapshot, PIDE.options.value)
wenzelm@56299
    72
        rendering.output_messages(new_results)
wenzelm@50499
    73
      }
wenzelm@50409
    74
      else current_output
wenzelm@49398
    75
wenzelm@49415
    76
    if (new_output != current_output)
wenzelm@56299
    77
      pretty_text_area.update(new_snapshot, new_results, Pretty.separate(new_output))
wenzelm@49398
    78
wenzelm@49419
    79
    current_snapshot = new_snapshot
wenzelm@56299
    80
    current_command = new_command
wenzelm@56299
    81
    current_results = new_results
wenzelm@49415
    82
    current_output = new_output
wenzelm@49398
    83
  }
wenzelm@49398
    84
wenzelm@49398
    85
wenzelm@56715
    86
  /* main */
wenzelm@49398
    87
wenzelm@56715
    88
  private val main =
wenzelm@56715
    89
    Session.Consumer[Any](getClass.getName) {
wenzelm@56715
    90
      case _: Session.Global_Options =>
wenzelm@57612
    91
        GUI_Thread.later { handle_resize() }
wenzelm@53177
    92
wenzelm@56715
    93
      case changed: Session.Commands_Changed =>
wenzelm@56715
    94
        val restriction = if (changed.assignment) None else Some(changed.commands)
wenzelm@57612
    95
        GUI_Thread.later { handle_update(do_update, restriction) }
wenzelm@53177
    96
wenzelm@56715
    97
      case Session.Caret_Focus =>
wenzelm@57612
    98
        GUI_Thread.later { handle_update(do_update, None) }
wenzelm@49398
    99
    }
wenzelm@49398
   100
wenzelm@49398
   101
  override def init()
wenzelm@49398
   102
  {
wenzelm@56715
   103
    PIDE.session.global_options += main
wenzelm@56715
   104
    PIDE.session.commands_changed += main
wenzelm@56715
   105
    PIDE.session.caret_focus += main
wenzelm@49398
   106
    handle_update(true, None)
wenzelm@49398
   107
  }
wenzelm@49398
   108
wenzelm@49398
   109
  override def exit()
wenzelm@49398
   110
  {
wenzelm@56715
   111
    PIDE.session.global_options -= main
wenzelm@56715
   112
    PIDE.session.commands_changed -= main
wenzelm@56715
   113
    PIDE.session.caret_focus -= main
wenzelm@49398
   114
    delay_resize.revoke()
wenzelm@49398
   115
  }
wenzelm@49398
   116
wenzelm@49398
   117
wenzelm@49398
   118
  /* resize */
wenzelm@49398
   119
wenzelm@49398
   120
  private val delay_resize =
wenzelm@57612
   121
    GUI_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@49398
   122
wenzelm@49398
   123
  addComponentListener(new ComponentAdapter {
wenzelm@49398
   124
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@49398
   125
  })
wenzelm@49398
   126
wenzelm@49398
   127
wenzelm@49398
   128
  /* controls */
wenzelm@49398
   129
wenzelm@49398
   130
  private val auto_update = new CheckBox("Auto update") {
wenzelm@52845
   131
    tooltip = "Indicate automatic update following cursor movement"
wenzelm@49398
   132
    reactions += {
wenzelm@49398
   133
      case ButtonClicked(_) => do_update = this.selected; handle_update(do_update, None) }
wenzelm@52845
   134
    selected = do_update
wenzelm@49398
   135
  }
wenzelm@49398
   136
wenzelm@49398
   137
  private val update = new Button("Update") {
wenzelm@52845
   138
    tooltip = "Update display according to the command at cursor position"
wenzelm@49398
   139
    reactions += { case ButtonClicked(_) => handle_update(true, None) }
wenzelm@49398
   140
  }
wenzelm@49398
   141
wenzelm@53711
   142
  private val controls =
wenzelm@56907
   143
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(auto_update, update,
wenzelm@57042
   144
      pretty_text_area.search_label, pretty_text_area.search_field, zoom)
wenzelm@49398
   145
  add(controls.peer, BorderLayout.NORTH)
wenzelm@49398
   146
}