src/Tools/jEdit/src/text_overview.scala
author wenzelm
Wed Mar 14 15:37:51 2012 +0100 (2012-03-14)
changeset 46920 5f44c8bea84e
parent 46572 3074685ab7ed
child 47027 fc3bb6c02a3c
permissions -rw-r--r--
more explicit indication of swing thread context;
wenzelm@46572
     1
/*  Title:      Tools/jEdit/src/text_overview.scala
wenzelm@46572
     2
    Author:     Makarius
wenzelm@46572
     3
wenzelm@46572
     4
Swing component for text status overview.
wenzelm@46572
     5
*/
wenzelm@46572
     6
wenzelm@46572
     7
package isabelle.jedit
wenzelm@46572
     8
wenzelm@46572
     9
wenzelm@46572
    10
import isabelle._
wenzelm@46572
    11
wenzelm@46572
    12
import scala.annotation.tailrec
wenzelm@46572
    13
wenzelm@46572
    14
import java.awt.{Graphics, Graphics2D, BorderLayout, Dimension}
wenzelm@46572
    15
import java.awt.event.{MouseAdapter, MouseEvent}
wenzelm@46572
    16
import javax.swing.{JPanel, ToolTipManager}
wenzelm@46572
    17
wenzelm@46572
    18
wenzelm@46572
    19
class Text_Overview(doc_view: Document_View) extends JPanel(new BorderLayout)
wenzelm@46572
    20
{
wenzelm@46572
    21
  private val text_area = doc_view.text_area
wenzelm@46572
    22
  private val buffer = doc_view.model.buffer
wenzelm@46572
    23
wenzelm@46572
    24
  private val WIDTH = 10
wenzelm@46572
    25
  private val HEIGHT = 2
wenzelm@46572
    26
wenzelm@46572
    27
  private def lines(): Int = buffer.getLineCount max text_area.getVisibleLines
wenzelm@46572
    28
wenzelm@46572
    29
  setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@46572
    30
wenzelm@46572
    31
  setRequestFocusEnabled(false)
wenzelm@46572
    32
wenzelm@46572
    33
  addMouseListener(new MouseAdapter {
wenzelm@46572
    34
    override def mousePressed(event: MouseEvent) {
wenzelm@46572
    35
      val line = (event.getY * lines()) / getHeight
wenzelm@46572
    36
      if (line >= 0 && line < text_area.getLineCount)
wenzelm@46572
    37
        text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@46572
    38
    }
wenzelm@46572
    39
  })
wenzelm@46572
    40
wenzelm@46572
    41
  override def addNotify() {
wenzelm@46572
    42
    super.addNotify()
wenzelm@46572
    43
    ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@46572
    44
  }
wenzelm@46572
    45
wenzelm@46572
    46
  override def removeNotify() {
wenzelm@46572
    47
    ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@46572
    48
    super.removeNotify
wenzelm@46572
    49
  }
wenzelm@46572
    50
wenzelm@46572
    51
  override def paintComponent(gfx: Graphics)
wenzelm@46572
    52
  {
wenzelm@46572
    53
    super.paintComponent(gfx)
wenzelm@46572
    54
    Swing_Thread.assert()
wenzelm@46572
    55
wenzelm@46572
    56
    doc_view.robust_body(()) {
wenzelm@46572
    57
      Isabelle.buffer_lock(buffer) {
wenzelm@46572
    58
        val snapshot = doc_view.update_snapshot()
wenzelm@46572
    59
wenzelm@46572
    60
        gfx.setColor(getBackground)
wenzelm@46572
    61
        gfx.asInstanceOf[Graphics2D].fill(gfx.getClipBounds)
wenzelm@46572
    62
wenzelm@46572
    63
        val line_count = buffer.getLineCount
wenzelm@46572
    64
        val char_count = buffer.getLength
wenzelm@46572
    65
wenzelm@46572
    66
        val L = lines()
wenzelm@46572
    67
        val H = getHeight()
wenzelm@46572
    68
wenzelm@46572
    69
        @tailrec def paint_loop(l: Int, h: Int, p: Int, q: Int): Unit =
wenzelm@46572
    70
        {
wenzelm@46572
    71
          if (l < line_count && h < H) {
wenzelm@46572
    72
            val p1 = p + H
wenzelm@46572
    73
            val q1 = q + HEIGHT * L
wenzelm@46572
    74
            val (l1, h1) =
wenzelm@46572
    75
              if (p1 >= q1) (l + 1, h + (p1 - q) / L)
wenzelm@46572
    76
              else (l + (q1 - p) / H, h + HEIGHT)
wenzelm@46572
    77
wenzelm@46572
    78
            val start = buffer.getLineStartOffset(l)
wenzelm@46572
    79
            val end =
wenzelm@46572
    80
              if (l1 < line_count) buffer.getLineStartOffset(l1)
wenzelm@46572
    81
              else char_count
wenzelm@46572
    82
wenzelm@46572
    83
            Isabelle_Rendering.overview_color(snapshot, Text.Range(start, end)) match {
wenzelm@46572
    84
              case None =>
wenzelm@46572
    85
              case Some(color) =>
wenzelm@46572
    86
                gfx.setColor(color)
wenzelm@46572
    87
                gfx.fillRect(0, h, getWidth, h1 - h)
wenzelm@46572
    88
            }
wenzelm@46572
    89
            paint_loop(l1, h1, p + (l1 - l) * H, q + (h1 - h) * L)
wenzelm@46572
    90
          }
wenzelm@46572
    91
        }
wenzelm@46572
    92
        paint_loop(0, 0, 0, 0)
wenzelm@46572
    93
      }
wenzelm@46572
    94
    }
wenzelm@46572
    95
  }
wenzelm@46572
    96
}
wenzelm@46572
    97