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