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