src/Tools/jEdit/src/output_dockable.scala
author wenzelm
Thu May 08 00:14:06 2014 +0200 (2014-05-08)
changeset 56906 408b526911f7
parent 56886 87ebb99786ed
child 56907 0f3c375fd27c
permissions -rw-r--r--
some odd tricks to provide "Detach" menu item, via "PIDE" docking framework;
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@49398
    23
  /* component state -- owned by Swing thread */
wenzelm@49398
    24
wenzelm@49398
    25
  private var zoom_factor = 100
wenzelm@49398
    26
  private var do_update = true
wenzelm@52972
    27
  private var current_snapshot = Document.Snapshot.init
wenzelm@56299
    28
  private var current_command = Command.empty
wenzelm@56299
    29
  private var current_results = Command.Results.empty
wenzelm@49415
    30
  private var current_output: List[XML.Tree] = Nil
wenzelm@49398
    31
wenzelm@49398
    32
wenzelm@49726
    33
  /* pretty text area */
wenzelm@49398
    34
wenzelm@50541
    35
  val pretty_text_area = new Pretty_Text_Area(view)
wenzelm@49398
    36
  set_content(pretty_text_area)
wenzelm@49398
    37
wenzelm@56906
    38
  override val detach_operation = Some(() => pretty_text_area.detach)
wenzelm@56906
    39
wenzelm@49398
    40
wenzelm@49398
    41
  private def handle_resize()
wenzelm@49398
    42
  {
wenzelm@56662
    43
    Swing_Thread.require {}
wenzelm@49398
    44
wenzelm@55825
    45
    pretty_text_area.resize(
wenzelm@55825
    46
      Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom_factor / 100))
wenzelm@49398
    47
  }
wenzelm@49398
    48
wenzelm@49398
    49
  private def handle_update(follow: Boolean, restriction: Option[Set[Command]])
wenzelm@49398
    50
  {
wenzelm@56662
    51
    Swing_Thread.require {}
wenzelm@49398
    52
wenzelm@56299
    53
    val (new_snapshot, new_command, new_results) =
wenzelm@52978
    54
      PIDE.editor.current_node_snapshot(view) match {
wenzelm@52978
    55
        case Some(snapshot) =>
wenzelm@49419
    56
          if (follow && !snapshot.is_outdated) {
wenzelm@52978
    57
            PIDE.editor.current_command(view, snapshot) match {
wenzelm@53844
    58
              case Some(cmd) =>
wenzelm@56299
    59
                (snapshot, cmd, snapshot.state.command_results(snapshot.version, cmd))
wenzelm@49419
    60
              case None =>
wenzelm@56299
    61
                (Document.Snapshot.init, Command.empty, Command.Results.empty)
wenzelm@49398
    62
            }
wenzelm@49419
    63
          }
wenzelm@56299
    64
          else (current_snapshot, current_command, current_results)
wenzelm@56299
    65
        case None => (current_snapshot, current_command, current_results)
wenzelm@49398
    66
      }
wenzelm@49398
    67
wenzelm@50409
    68
    val new_output =
wenzelm@56299
    69
      if (!restriction.isDefined || restriction.get.contains(new_command)) {
wenzelm@50500
    70
        val rendering = Rendering(new_snapshot, PIDE.options.value)
wenzelm@56299
    71
        rendering.output_messages(new_results)
wenzelm@50499
    72
      }
wenzelm@50409
    73
      else current_output
wenzelm@49398
    74
wenzelm@49415
    75
    if (new_output != current_output)
wenzelm@56299
    76
      pretty_text_area.update(new_snapshot, new_results, Pretty.separate(new_output))
wenzelm@49398
    77
wenzelm@49419
    78
    current_snapshot = new_snapshot
wenzelm@56299
    79
    current_command = new_command
wenzelm@56299
    80
    current_results = new_results
wenzelm@49415
    81
    current_output = new_output
wenzelm@49398
    82
  }
wenzelm@49398
    83
wenzelm@49398
    84
wenzelm@56715
    85
  /* main */
wenzelm@49398
    86
wenzelm@56715
    87
  private val main =
wenzelm@56715
    88
    Session.Consumer[Any](getClass.getName) {
wenzelm@56715
    89
      case _: Session.Global_Options =>
wenzelm@56715
    90
        Swing_Thread.later { handle_resize() }
wenzelm@53177
    91
wenzelm@56715
    92
      case changed: Session.Commands_Changed =>
wenzelm@56715
    93
        val restriction = if (changed.assignment) None else Some(changed.commands)
wenzelm@56715
    94
        Swing_Thread.later { handle_update(do_update, restriction) }
wenzelm@53177
    95
wenzelm@56715
    96
      case Session.Caret_Focus =>
wenzelm@56715
    97
        Swing_Thread.later { handle_update(do_update, None) }
wenzelm@49398
    98
    }
wenzelm@49398
    99
wenzelm@49398
   100
  override def init()
wenzelm@49398
   101
  {
wenzelm@56715
   102
    PIDE.session.global_options += main
wenzelm@56715
   103
    PIDE.session.commands_changed += main
wenzelm@56715
   104
    PIDE.session.caret_focus += main
wenzelm@49398
   105
    handle_update(true, None)
wenzelm@49398
   106
  }
wenzelm@49398
   107
wenzelm@49398
   108
  override def exit()
wenzelm@49398
   109
  {
wenzelm@56715
   110
    PIDE.session.global_options -= main
wenzelm@56715
   111
    PIDE.session.commands_changed -= main
wenzelm@56715
   112
    PIDE.session.caret_focus -= main
wenzelm@49398
   113
    delay_resize.revoke()
wenzelm@49398
   114
  }
wenzelm@49398
   115
wenzelm@49398
   116
wenzelm@49398
   117
  /* resize */
wenzelm@49398
   118
wenzelm@49398
   119
  private val delay_resize =
wenzelm@50207
   120
    Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
wenzelm@49398
   121
wenzelm@49398
   122
  addComponentListener(new ComponentAdapter {
wenzelm@49398
   123
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@49398
   124
  })
wenzelm@49398
   125
wenzelm@49398
   126
wenzelm@49398
   127
  /* controls */
wenzelm@49398
   128
wenzelm@52845
   129
  private val zoom = new GUI.Zoom_Box(factor => { zoom_factor = factor; handle_resize() }) {
wenzelm@52845
   130
    tooltip = "Zoom factor for output font size"
wenzelm@52845
   131
  }
wenzelm@49398
   132
wenzelm@49398
   133
  private val auto_update = new CheckBox("Auto update") {
wenzelm@52845
   134
    tooltip = "Indicate automatic update following cursor movement"
wenzelm@49398
   135
    reactions += {
wenzelm@49398
   136
      case ButtonClicked(_) => do_update = this.selected; handle_update(do_update, None) }
wenzelm@52845
   137
    selected = do_update
wenzelm@49398
   138
  }
wenzelm@49398
   139
wenzelm@49398
   140
  private val update = new Button("Update") {
wenzelm@52845
   141
    tooltip = "Update display according to the command at cursor position"
wenzelm@49398
   142
    reactions += { case ButtonClicked(_) => handle_update(true, None) }
wenzelm@49398
   143
  }
wenzelm@49398
   144
wenzelm@53711
   145
  private val controls =
wenzelm@56883
   146
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(
wenzelm@56886
   147
      auto_update, update, pretty_text_area.detach_button,
wenzelm@56886
   148
      pretty_text_area.search_label, pretty_text_area.search_pattern, zoom)
wenzelm@49398
   149
  add(controls.peer, BorderLayout.NORTH)
wenzelm@49398
   150
}