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