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