src/Tools/jEdit/src/document_view.scala
author wenzelm
Mon Jul 04 22:11:32 2011 +0200 (2011-07-04)
changeset 43661 39fdbd814c7f
parent 43650 f00da558b78e
child 44378 81b4af4cfa5a
permissions -rw-r--r--
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/document_view.scala
wenzelm@36760
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     3
    Author:     Makarius
wenzelm@36760
     4
wenzelm@36760
     5
Document view connected to jEdit text area.
wenzelm@36760
     6
*/
wenzelm@34408
     7
immler@34403
     8
package isabelle.jedit
immler@34403
     9
wenzelm@34760
    10
wenzelm@36015
    11
import isabelle._
wenzelm@36015
    12
wenzelm@34784
    13
import scala.actors.Actor._
wenzelm@34784
    14
wenzelm@43520
    15
import java.lang.System
wenzelm@39740
    16
import java.awt.{BorderLayout, Graphics, Color, Dimension, Graphics2D, Point}
wenzelm@39741
    17
import java.awt.event.{MouseAdapter, MouseMotionAdapter, MouseEvent,
wenzelm@39741
    18
  FocusAdapter, FocusEvent, WindowEvent, WindowAdapter}
wenzelm@39741
    19
import javax.swing.{JPanel, ToolTipManager, Popup, PopupFactory, SwingUtilities, BorderFactory}
wenzelm@34784
    20
import javax.swing.event.{CaretListener, CaretEvent}
wenzelm@34734
    21
wenzelm@43404
    22
import org.gjt.sp.util.Log
wenzelm@43404
    23
wenzelm@42839
    24
import org.gjt.sp.jedit.{jEdit, OperatingSystem, Debug}
wenzelm@34709
    25
import org.gjt.sp.jedit.gui.RolloverButton
wenzelm@39176
    26
import org.gjt.sp.jedit.options.GutterOptionPane
wenzelm@34784
    27
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter}
wenzelm@43369
    28
import org.gjt.sp.jedit.syntax.{SyntaxStyle}
wenzelm@34784
    29
wenzelm@34784
    30
wenzelm@34784
    31
object Document_View
wenzelm@34784
    32
{
wenzelm@34784
    33
  /* document view of text area */
wenzelm@34784
    34
wenzelm@34784
    35
  private val key = new Object
wenzelm@34784
    36
wenzelm@39741
    37
  def apply(text_area: JEditTextArea): Option[Document_View] =
wenzelm@34784
    38
  {
wenzelm@38223
    39
    Swing_Thread.require()
wenzelm@34784
    40
    text_area.getClientProperty(key) match {
wenzelm@34784
    41
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    42
      case _ => None
wenzelm@34784
    43
    }
wenzelm@34784
    44
  }
wenzelm@34784
    45
wenzelm@39741
    46
  def exit(text_area: JEditTextArea)
wenzelm@34784
    47
  {
wenzelm@38223
    48
    Swing_Thread.require()
wenzelm@34788
    49
    apply(text_area) match {
wenzelm@39636
    50
      case None =>
wenzelm@34784
    51
      case Some(doc_view) =>
wenzelm@34784
    52
        doc_view.deactivate()
wenzelm@34784
    53
        text_area.putClientProperty(key, null)
wenzelm@34784
    54
    }
wenzelm@34784
    55
  }
wenzelm@43397
    56
wenzelm@43397
    57
  def init(model: Document_Model, text_area: JEditTextArea): Document_View =
wenzelm@43397
    58
  {
wenzelm@43397
    59
    exit(text_area)
wenzelm@43397
    60
    val doc_view = new Document_View(model, text_area)
wenzelm@43397
    61
    text_area.putClientProperty(key, doc_view)
wenzelm@43397
    62
    doc_view.activate()
wenzelm@43397
    63
    doc_view
wenzelm@43397
    64
  }
wenzelm@34784
    65
}
immler@34403
    66
wenzelm@34733
    67
wenzelm@43376
    68
class Document_View(val model: Document_Model, val text_area: JEditTextArea)
immler@34654
    69
{
wenzelm@34784
    70
  private val session = model.session
wenzelm@34784
    71
immler@34403
    72
wenzelm@43419
    73
  /* robust extension body */
wenzelm@43404
    74
wenzelm@43404
    75
  def robust_body[A](default: A)(body: => A): A =
wenzelm@43417
    76
  {
wenzelm@43417
    77
    try {
wenzelm@43417
    78
      Swing_Thread.require()
wenzelm@43417
    79
      if (model.buffer == text_area.getBuffer) body
wenzelm@43417
    80
      else {
wenzelm@43650
    81
        Log.log(Log.ERROR, this, ERROR("Inconsistent document model"))
wenzelm@43417
    82
        default
wenzelm@43404
    83
      }
wenzelm@43404
    84
    }
wenzelm@43417
    85
    catch { case t: Throwable => Log.log(Log.ERROR, this, t); default }
wenzelm@43417
    86
  }
wenzelm@43404
    87
wenzelm@43404
    88
wenzelm@38881
    89
  /* visible line ranges */
wenzelm@38881
    90
wenzelm@38883
    91
  // simplify slightly odd result of TextArea.getScreenLineEndOffset etc.
wenzelm@38881
    92
  // NB: jEdit already normalizes \r\n and \r to \n
wenzelm@38881
    93
  def proper_line_range(start: Text.Offset, end: Text.Offset): Text.Range =
wenzelm@38881
    94
  {
wenzelm@38883
    95
    val stop = if (start < end) end - 1 else end min model.buffer.getLength
wenzelm@38881
    96
    Text.Range(start, stop)
wenzelm@38881
    97
  }
wenzelm@38881
    98
wenzelm@38881
    99
  def screen_lines_range(): Text.Range =
wenzelm@38881
   100
  {
wenzelm@38881
   101
    val start = text_area.getScreenLineStartOffset(0)
wenzelm@38881
   102
    val raw_end = text_area.getScreenLineEndOffset(text_area.getVisibleLines - 1 max 0)
wenzelm@38881
   103
    proper_line_range(start, if (raw_end >= 0) raw_end else model.buffer.getLength)
wenzelm@38881
   104
  }
wenzelm@38881
   105
wenzelm@39132
   106
  def invalidate_line_range(range: Text.Range)
wenzelm@39132
   107
  {
wenzelm@39132
   108
    text_area.invalidateLineRange(
wenzelm@39132
   109
      model.buffer.getLineOfOffset(range.start),
wenzelm@39132
   110
      model.buffer.getLineOfOffset(range.stop))
wenzelm@39132
   111
  }
wenzelm@39132
   112
wenzelm@38881
   113
wenzelm@43419
   114
  /* snapshot */
wenzelm@43419
   115
wenzelm@43419
   116
  // owned by Swing thread
wenzelm@43419
   117
  @volatile private var was_outdated = false
wenzelm@43419
   118
  @volatile private var was_updated = false
wenzelm@43419
   119
wenzelm@43419
   120
  def update_snapshot(): Document.Snapshot =
wenzelm@43419
   121
  {
wenzelm@43419
   122
    Swing_Thread.require()
wenzelm@43419
   123
    val snapshot = model.snapshot()
wenzelm@43419
   124
    was_updated = was_outdated && !snapshot.is_outdated
wenzelm@43419
   125
    was_outdated = was_outdated || snapshot.is_outdated
wenzelm@43419
   126
    snapshot
wenzelm@43419
   127
  }
wenzelm@43419
   128
wenzelm@43419
   129
  def flush_snapshot(): (Boolean, Document.Snapshot) =
wenzelm@43419
   130
  {
wenzelm@43419
   131
    Swing_Thread.require()
wenzelm@43419
   132
    val snapshot = update_snapshot()
wenzelm@43419
   133
    val updated = was_updated
wenzelm@43419
   134
    if (updated) { was_outdated = false; was_updated = false }
wenzelm@43419
   135
    (updated, snapshot)
wenzelm@43419
   136
  }
wenzelm@43419
   137
wenzelm@43419
   138
wenzelm@39740
   139
  /* HTML popups */
wenzelm@39740
   140
wenzelm@39740
   141
  private var html_popup: Option[Popup] = None
wenzelm@34834
   142
wenzelm@39740
   143
  private def exit_popup() { html_popup.map(_.hide) }
wenzelm@38881
   144
wenzelm@39740
   145
  private val html_panel =
wenzelm@43661
   146
    new HTML_Panel(Isabelle.font_family(), scala.math.round(Isabelle.font_size()))
wenzelm@39741
   147
  html_panel.setBorder(BorderFactory.createLineBorder(Color.black))
wenzelm@38884
   148
wenzelm@39740
   149
  private def html_panel_resize()
wenzelm@39740
   150
  {
wenzelm@39740
   151
    Swing_Thread.now {
wenzelm@39740
   152
      html_panel.resize(Isabelle.font_family(), scala.math.round(Isabelle.font_size()))
wenzelm@39740
   153
    }
wenzelm@39740
   154
  }
wenzelm@38884
   155
wenzelm@39740
   156
  private def init_popup(snapshot: Document.Snapshot, x: Int, y: Int)
wenzelm@39740
   157
  {
wenzelm@39740
   158
    exit_popup()
wenzelm@40154
   159
/* FIXME broken
wenzelm@39740
   160
    val offset = text_area.xyToOffset(x, y)
wenzelm@39740
   161
    val p = new Point(x, y); SwingUtilities.convertPointToScreen(p, text_area.getPainter)
wenzelm@34784
   162
wenzelm@39740
   163
    // FIXME snapshot.cumulate
wenzelm@39740
   164
    snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.popup) match {
wenzelm@39740
   165
      case Text.Info(_, Some(msg)) #:: _ =>
wenzelm@39740
   166
        val popup = PopupFactory.getSharedInstance().getPopup(text_area, html_panel, p.x, p.y + 60)
wenzelm@39740
   167
        html_panel.render_sync(List(msg))
wenzelm@39742
   168
        Thread.sleep(10)  // FIXME !?
wenzelm@39740
   169
        popup.show
wenzelm@39740
   170
        html_popup = Some(popup)
wenzelm@39740
   171
      case _ =>
immler@34403
   172
    }
wenzelm@40154
   173
*/
immler@34678
   174
  }
immler@34403
   175
immler@34403
   176
wenzelm@39131
   177
  /* subexpression highlighting */
wenzelm@39131
   178
wenzelm@39178
   179
  private def subexp_range(snapshot: Document.Snapshot, x: Int, y: Int)
wenzelm@39178
   180
    : Option[(Text.Range, Color)] =
wenzelm@39132
   181
  {
wenzelm@39132
   182
    val offset = text_area.xyToOffset(x, y)
wenzelm@39178
   183
    snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.subexp) match {
wenzelm@39178
   184
      case Text.Info(_, Some((range, color))) #:: _ => Some((snapshot.convert(range), color))
wenzelm@39177
   185
      case _ => None
wenzelm@39177
   186
    }
wenzelm@39132
   187
  }
wenzelm@39132
   188
wenzelm@43381
   189
  @volatile private var _highlight_range: Option[(Text.Range, Color)] = None
wenzelm@43381
   190
  def highlight_range(): Option[(Text.Range, Color)] = _highlight_range
wenzelm@39131
   191
wenzelm@39740
   192
wenzelm@39740
   193
  /* CONTROL-mouse management */
wenzelm@39740
   194
wenzelm@40338
   195
  private var control: Boolean = false
wenzelm@40338
   196
wenzelm@39740
   197
  private def exit_control()
wenzelm@39740
   198
  {
wenzelm@39740
   199
    exit_popup()
wenzelm@43381
   200
    _highlight_range = None
wenzelm@39740
   201
  }
wenzelm@39740
   202
wenzelm@39131
   203
  private val focus_listener = new FocusAdapter {
wenzelm@39742
   204
    override def focusLost(e: FocusEvent) {
wenzelm@43381
   205
      _highlight_range = None // FIXME exit_control !?
wenzelm@39742
   206
    }
wenzelm@39131
   207
  }
wenzelm@39131
   208
wenzelm@39741
   209
  private val window_listener = new WindowAdapter {
wenzelm@39741
   210
    override def windowIconified(e: WindowEvent) { exit_control() }
wenzelm@39742
   211
    override def windowDeactivated(e: WindowEvent) { exit_control() }
wenzelm@39741
   212
  }
wenzelm@39741
   213
wenzelm@39131
   214
  private val mouse_motion_listener = new MouseMotionAdapter {
wenzelm@39131
   215
    override def mouseMoved(e: MouseEvent) {
wenzelm@40338
   216
      control = if (OperatingSystem.isMacOS()) e.isMetaDown else e.isControlDown
wenzelm@39740
   217
      val x = e.getX()
wenzelm@39740
   218
      val y = e.getY()
wenzelm@39740
   219
wenzelm@39740
   220
      if (!model.buffer.isLoaded) exit_control()
wenzelm@39132
   221
      else
wenzelm@39132
   222
        Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@43419
   223
          val snapshot = update_snapshot()
wenzelm@39740
   224
wenzelm@39740
   225
          if (control) init_popup(snapshot, x, y)
wenzelm@39740
   226
wenzelm@43381
   227
          _highlight_range map { case (range, _) => invalidate_line_range(range) }
wenzelm@43381
   228
          _highlight_range = if (control) subexp_range(snapshot, x, y) else None
wenzelm@43381
   229
          _highlight_range map { case (range, _) => invalidate_line_range(range) }
wenzelm@39132
   230
        }
wenzelm@39131
   231
    }
wenzelm@39131
   232
  }
wenzelm@39131
   233
wenzelm@39131
   234
wenzelm@43381
   235
  /* text area painting */
wenzelm@39044
   236
wenzelm@43381
   237
  private val text_area_painter = new Text_Area_Painter(this)
wenzelm@39700
   238
wenzelm@43381
   239
  private val tooltip_painter = new TextAreaExtension
wenzelm@43381
   240
  {
wenzelm@34784
   241
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   242
    {
wenzelm@43404
   243
      robust_body(null: String) {
wenzelm@43419
   244
        val snapshot = update_snapshot()
wenzelm@38845
   245
        val offset = text_area.xyToOffset(x, y)
wenzelm@40338
   246
        if (control) {
wenzelm@40338
   247
          snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.tooltip) match
wenzelm@40338
   248
          {
wenzelm@40338
   249
            case Text.Info(_, Some(text)) #:: _ => Isabelle.tooltip(text)
wenzelm@40338
   250
            case _ => null
wenzelm@40338
   251
          }
wenzelm@40338
   252
        }
wenzelm@40338
   253
        else {
wenzelm@40338
   254
          // FIXME snapshot.cumulate
wenzelm@40338
   255
          snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.popup) match
wenzelm@40338
   256
          {
wenzelm@40338
   257
            case Text.Info(_, Some(text)) #:: _ => Isabelle.tooltip(text)
wenzelm@40338
   258
            case _ => null
wenzelm@40338
   259
          }
wenzelm@39174
   260
        }
wenzelm@34784
   261
      }
wenzelm@34734
   262
    }
immler@34678
   263
  }
immler@34513
   264
wenzelm@42825
   265
  private val gutter_painter = new TextAreaExtension
wenzelm@39175
   266
  {
wenzelm@39175
   267
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@39175
   268
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@39175
   269
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@39175
   270
    {
wenzelm@43404
   271
      robust_body(()) {
wenzelm@43404
   272
        val gutter = text_area.getGutter
wenzelm@43404
   273
        val width = GutterOptionPane.getSelectionAreaWidth
wenzelm@43404
   274
        val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
wenzelm@43404
   275
        val FOLD_MARKER_SIZE = 12
wenzelm@43419
   276
wenzelm@43404
   277
        if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
wenzelm@43404
   278
          Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@43419
   279
            val snapshot = update_snapshot()
wenzelm@43404
   280
            for (i <- 0 until physical_lines.length) {
wenzelm@43404
   281
              if (physical_lines(i) != -1) {
wenzelm@43404
   282
                val line_range = proper_line_range(start(i), end(i))
wenzelm@43419
   283
wenzelm@43404
   284
                // gutter icons
wenzelm@43404
   285
                val icons =
wenzelm@43404
   286
                  (for (Text.Info(_, Some(icon)) <- // FIXME snapshot.cumulate
wenzelm@43404
   287
                    snapshot.select_markup(line_range)(Isabelle_Markup.gutter_message).iterator)
wenzelm@43404
   288
                  yield icon).toList.sortWith(_ >= _)
wenzelm@43404
   289
                icons match {
wenzelm@43404
   290
                  case icon :: _ =>
wenzelm@43404
   291
                    val icn = icon.icon
wenzelm@43404
   292
                    val x0 = (FOLD_MARKER_SIZE + width - border_width - icn.getIconWidth) max 10
wenzelm@43404
   293
                    val y0 = y + i * line_height + (((line_height - icn.getIconHeight) / 2) max 0)
wenzelm@43404
   294
                    icn.paintIcon(gutter, gfx, x0, y0)
wenzelm@43404
   295
                  case Nil =>
wenzelm@43404
   296
                }
wenzelm@39175
   297
              }
wenzelm@39175
   298
            }
wenzelm@39175
   299
          }
wenzelm@39175
   300
        }
wenzelm@39175
   301
      }
wenzelm@39175
   302
    }
wenzelm@39175
   303
  }
wenzelm@39175
   304
wenzelm@39175
   305
wenzelm@43448
   306
  /* caret range */
wenzelm@43448
   307
wenzelm@43448
   308
  def caret_range(): Text.Range =
wenzelm@43448
   309
    Isabelle.buffer_lock(model.buffer) {
wenzelm@43448
   310
      def text(i: Text.Offset): Char = model.buffer.getText(i, 1).charAt(0)
wenzelm@43448
   311
      val caret = text_area.getCaretPosition
wenzelm@43448
   312
      try {
wenzelm@43448
   313
        val c = text(caret)
wenzelm@43448
   314
        if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(caret + 1)))
wenzelm@43448
   315
          Text.Range(caret, caret + 2)
wenzelm@43448
   316
        else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(caret - 1)))
wenzelm@43448
   317
          Text.Range(caret - 1, caret + 1)
wenzelm@43448
   318
        else Text.Range(caret, caret + 1)
wenzelm@43448
   319
      }
wenzelm@43448
   320
      catch { case _: ArrayIndexOutOfBoundsException => Text.Range(caret, caret + 1) }
wenzelm@43448
   321
    }
wenzelm@43448
   322
wenzelm@43448
   323
wenzelm@37129
   324
  /* caret handling */
wenzelm@34810
   325
wenzelm@37849
   326
  def selected_command(): Option[Command] =
wenzelm@38223
   327
  {
wenzelm@38223
   328
    Swing_Thread.require()
wenzelm@43419
   329
    update_snapshot().node.proper_command_at(text_area.getCaretPosition)
wenzelm@38223
   330
  }
wenzelm@34810
   331
wenzelm@37849
   332
  private val caret_listener = new CaretListener {
wenzelm@37849
   333
    private val delay = Swing_Thread.delay_last(session.input_delay) {
wenzelm@37849
   334
      session.perspective.event(Session.Perspective)
wenzelm@34810
   335
    }
wenzelm@37849
   336
    override def caretUpdate(e: CaretEvent) { delay() }
wenzelm@34810
   337
  }
wenzelm@34810
   338
wenzelm@34810
   339
wenzelm@34784
   340
  /* overview of command status left of scrollbar */
wenzelm@34784
   341
wenzelm@34834
   342
  private val overview = new JPanel(new BorderLayout)
wenzelm@34784
   343
  {
wenzelm@34784
   344
    private val WIDTH = 10
wenzelm@34784
   345
    private val HEIGHT = 2
wenzelm@34784
   346
wenzelm@34806
   347
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   348
wenzelm@34784
   349
    setRequestFocusEnabled(false)
wenzelm@34784
   350
wenzelm@34784
   351
    addMouseListener(new MouseAdapter {
wenzelm@34784
   352
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   353
        val line = y_to_line(event.getY)
wenzelm@34784
   354
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   355
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   356
      }
wenzelm@34784
   357
    })
wenzelm@34784
   358
wenzelm@34784
   359
    override def addNotify() {
wenzelm@34784
   360
      super.addNotify()
wenzelm@34784
   361
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   362
    }
immler@34403
   363
wenzelm@34784
   364
    override def removeNotify() {
wenzelm@34784
   365
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   366
      super.removeNotify
wenzelm@34784
   367
    }
wenzelm@34784
   368
wenzelm@34784
   369
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   370
    {
wenzelm@34784
   371
      super.paintComponent(gfx)
wenzelm@38640
   372
      Swing_Thread.assert()
wenzelm@39737
   373
wenzelm@34784
   374
      val buffer = model.buffer
wenzelm@38843
   375
      Isabelle.buffer_lock(buffer) {
wenzelm@43419
   376
        val snapshot = update_snapshot()
wenzelm@39737
   377
wenzelm@39737
   378
        def line_range(command: Command, start: Text.Offset): Option[(Int, Int)] =
wenzelm@39737
   379
        {
wenzelm@39737
   380
          try {
wenzelm@39737
   381
            val line1 = buffer.getLineOfOffset(snapshot.convert(start))
wenzelm@39737
   382
            val line2 = buffer.getLineOfOffset(snapshot.convert(start + command.length)) + 1
wenzelm@39737
   383
            Some((line1, line2))
wenzelm@39737
   384
          }
wenzelm@39737
   385
          catch { case e: ArrayIndexOutOfBoundsException => None }
wenzelm@39737
   386
        }
wenzelm@39182
   387
        for {
wenzelm@39182
   388
          (command, start) <- snapshot.node.command_starts
wenzelm@39182
   389
          if !command.is_ignored
wenzelm@39737
   390
          (line1, line2) <- line_range(command, start)
wenzelm@39182
   391
          val y = line_to_y(line1)
wenzelm@39182
   392
          val height = HEIGHT * (line2 - line1)
wenzelm@39182
   393
          color <- Isabelle_Markup.overview_color(snapshot, command)
wenzelm@39182
   394
        } {
wenzelm@39182
   395
          gfx.setColor(color)
wenzelm@39182
   396
          gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@37188
   397
        }
wenzelm@34784
   398
      }
wenzelm@34784
   399
    }
wenzelm@34784
   400
wenzelm@34784
   401
    private def line_to_y(line: Int): Int =
wenzelm@34784
   402
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   403
wenzelm@34784
   404
    private def y_to_line(y: Int): Int =
wenzelm@34784
   405
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   406
  }
immler@34403
   407
wenzelm@34784
   408
wenzelm@39740
   409
  /* main actor */
wenzelm@39740
   410
wenzelm@39740
   411
  private val main_actor = actor {
wenzelm@39740
   412
    loop {
wenzelm@39740
   413
      react {
wenzelm@39740
   414
        case Session.Commands_Changed(changed) =>
wenzelm@39740
   415
          val buffer = model.buffer
wenzelm@39740
   416
          Isabelle.swing_buffer_lock(buffer) {
wenzelm@43419
   417
            val (updated, snapshot) = flush_snapshot()
wenzelm@43419
   418
            val visible_range = screen_lines_range()
wenzelm@39740
   419
wenzelm@43419
   420
            if (updated || changed.exists(snapshot.node.commands.contains))
wenzelm@39740
   421
              overview.repaint()
wenzelm@39740
   422
wenzelm@43419
   423
            if (updated) invalidate_line_range(visible_range)
wenzelm@43419
   424
            else {
wenzelm@43419
   425
              val visible_cmds =
wenzelm@43419
   426
                snapshot.node.command_range(snapshot.revert(visible_range)).map(_._1)
wenzelm@43419
   427
              if (visible_cmds.exists(changed)) {
wenzelm@43419
   428
                for {
wenzelm@43419
   429
                  line <- 0 until text_area.getVisibleLines
wenzelm@43419
   430
                  val start = text_area.getScreenLineStartOffset(line) if start >= 0
wenzelm@43419
   431
                  val end = text_area.getScreenLineEndOffset(line) if end >= 0
wenzelm@43419
   432
                  val range = proper_line_range(start, end)
wenzelm@43419
   433
                  val line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
wenzelm@43419
   434
                  if line_cmds.exists(changed)
wenzelm@43419
   435
                } text_area.invalidateScreenLineRange(line, line)
wenzelm@43419
   436
              }
wenzelm@39740
   437
            }
wenzelm@39740
   438
          }
wenzelm@39740
   439
wenzelm@39740
   440
        case Session.Global_Settings => html_panel_resize()
wenzelm@39740
   441
wenzelm@39740
   442
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@39740
   443
      }
wenzelm@39740
   444
    }
wenzelm@39740
   445
  }
wenzelm@39740
   446
wenzelm@39740
   447
wenzelm@34784
   448
  /* activation */
wenzelm@34784
   449
wenzelm@34808
   450
  private def activate()
wenzelm@34784
   451
  {
wenzelm@42825
   452
    val painter = text_area.getPainter
wenzelm@43387
   453
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, tooltip_painter)
wenzelm@43381
   454
    text_area_painter.activate()
wenzelm@42825
   455
    text_area.getGutter.addExtension(gutter_painter)
wenzelm@39131
   456
    text_area.addFocusListener(focus_listener)
wenzelm@39741
   457
    text_area.getView.addWindowListener(window_listener)
wenzelm@42825
   458
    painter.addMouseMotionListener(mouse_motion_listener)
wenzelm@34810
   459
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   460
    text_area.addLeftOfScrollBar(overview)
wenzelm@39740
   461
    session.commands_changed += main_actor
wenzelm@39740
   462
    session.global_settings += main_actor
wenzelm@34784
   463
  }
wenzelm@34784
   464
wenzelm@34808
   465
  private def deactivate()
wenzelm@34784
   466
  {
wenzelm@42825
   467
    val painter = text_area.getPainter
wenzelm@39740
   468
    session.commands_changed -= main_actor
wenzelm@39740
   469
    session.global_settings -= main_actor
wenzelm@39131
   470
    text_area.removeFocusListener(focus_listener)
wenzelm@39741
   471
    text_area.getView.removeWindowListener(window_listener)
wenzelm@42825
   472
    painter.removeMouseMotionListener(mouse_motion_listener)
wenzelm@39131
   473
    text_area.removeCaretListener(caret_listener)
wenzelm@34810
   474
    text_area.removeLeftOfScrollBar(overview)
wenzelm@42825
   475
    text_area.getGutter.removeExtension(gutter_painter)
wenzelm@43381
   476
    text_area_painter.deactivate()
wenzelm@43381
   477
    painter.removeExtension(tooltip_painter)
wenzelm@39740
   478
    exit_popup()
wenzelm@34784
   479
  }
wenzelm@42825
   480
}