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