src/Tools/jEdit/src/jedit/document_view.scala
author wenzelm
Mon Jan 04 00:13:09 2010 +0100 (2010-01-04)
changeset 34834 df9af932e418
parent 34832 d785f72ef388
child 34855 81d0410dc3ac
permissions -rw-r--r--
slightly more uniform/robust handling of visible document;
uniform changed_delay for view updates;
misc tuning;
wenzelm@34408
     1
/*
wenzelm@34784
     2
 * Document view connected to jEdit text area
wenzelm@34408
     3
 *
wenzelm@34408
     4
 * @author Fabian Immler, TU Munich
wenzelm@34784
     5
 * @author Makarius
wenzelm@34408
     6
 */
wenzelm@34408
     7
immler@34403
     8
package isabelle.jedit
immler@34403
     9
wenzelm@34760
    10
wenzelm@34823
    11
import isabelle.proofdocument.{Command, Document, Session}
immler@34403
    12
wenzelm@34784
    13
import scala.actors.Actor._
wenzelm@34784
    14
wenzelm@34784
    15
import java.awt.event.{MouseAdapter, MouseEvent}
wenzelm@34784
    16
import java.awt.{BorderLayout, Graphics, Dimension, Color, Graphics2D}
wenzelm@34734
    17
import javax.swing.{JPanel, ToolTipManager}
wenzelm@34784
    18
import javax.swing.event.{CaretListener, CaretEvent}
wenzelm@34734
    19
wenzelm@34709
    20
import org.gjt.sp.jedit.gui.RolloverButton
wenzelm@34784
    21
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter}
wenzelm@34784
    22
wenzelm@34784
    23
wenzelm@34784
    24
object Document_View
wenzelm@34784
    25
{
wenzelm@34823
    26
  def choose_color(command: Command, doc: Document): Color =
wenzelm@34784
    27
  {
wenzelm@34832
    28
    doc.current_state(command).status match {
wenzelm@34784
    29
      case Command.Status.UNPROCESSED => new Color(255, 228, 225)
wenzelm@34784
    30
      case Command.Status.FINISHED => new Color(234, 248, 255)
wenzelm@34794
    31
      case Command.Status.FAILED => new Color(255, 193, 193)
wenzelm@34784
    32
      case _ => Color.red
wenzelm@34784
    33
    }
wenzelm@34784
    34
  }
wenzelm@34784
    35
wenzelm@34784
    36
wenzelm@34784
    37
  /* document view of text area */
wenzelm@34784
    38
wenzelm@34784
    39
  private val key = new Object
wenzelm@34784
    40
wenzelm@34784
    41
  def init(model: Document_Model, text_area: TextArea): Document_View =
wenzelm@34784
    42
  {
wenzelm@34789
    43
    Swing_Thread.assert()
wenzelm@34784
    44
    val doc_view = new Document_View(model, text_area)
wenzelm@34784
    45
    text_area.putClientProperty(key, doc_view)
wenzelm@34784
    46
    doc_view.activate()
wenzelm@34784
    47
    doc_view
wenzelm@34784
    48
  }
wenzelm@34784
    49
wenzelm@34788
    50
  def apply(text_area: TextArea): Option[Document_View] =
wenzelm@34784
    51
  {
wenzelm@34789
    52
    Swing_Thread.assert()
wenzelm@34784
    53
    text_area.getClientProperty(key) match {
wenzelm@34784
    54
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    55
      case _ => None
wenzelm@34784
    56
    }
wenzelm@34784
    57
  }
wenzelm@34784
    58
wenzelm@34784
    59
  def exit(text_area: TextArea)
wenzelm@34784
    60
  {
wenzelm@34789
    61
    Swing_Thread.assert()
wenzelm@34788
    62
    apply(text_area) match {
wenzelm@34784
    63
      case None => error("No document view for text area: " + text_area)
wenzelm@34784
    64
      case Some(doc_view) =>
wenzelm@34784
    65
        doc_view.deactivate()
wenzelm@34784
    66
        text_area.putClientProperty(key, null)
wenzelm@34784
    67
    }
wenzelm@34784
    68
  }
wenzelm@34784
    69
}
immler@34403
    70
wenzelm@34733
    71
wenzelm@34784
    72
class Document_View(model: Document_Model, text_area: TextArea)
immler@34654
    73
{
wenzelm@34784
    74
  private val session = model.session
wenzelm@34784
    75
immler@34403
    76
wenzelm@34834
    77
  /* visible document -- owned by Swing thread */
wenzelm@34834
    78
wenzelm@34834
    79
  @volatile private var document = model.recent_document()
wenzelm@34834
    80
wenzelm@34834
    81
wenzelm@34834
    82
  /* buffer of changed commands -- owned by Swing thread */
wenzelm@34834
    83
wenzelm@34834
    84
  @volatile private var changed_commands: Set[Command] = Set()
wenzelm@34834
    85
wenzelm@34834
    86
  private val changed_delay = Swing_Thread.delay_first(100) {
wenzelm@34834
    87
    if (!changed_commands.isEmpty) {
wenzelm@34834
    88
      document = model.recent_document()
wenzelm@34834
    89
      for (cmd <- changed_commands if document.commands.contains(cmd)) { // FIXME cover doc states as well!!?
wenzelm@34834
    90
        update_syntax(cmd)
wenzelm@34834
    91
        invalidate_line(cmd)
wenzelm@34834
    92
        overview.repaint()
wenzelm@34834
    93
      }
wenzelm@34834
    94
      changed_commands = Set()
wenzelm@34834
    95
    }
wenzelm@34834
    96
  }
wenzelm@34834
    97
wenzelm@34834
    98
wenzelm@34784
    99
  /* command change actor */
wenzelm@34784
   100
wenzelm@34784
   101
  private case object Exit
immler@34403
   102
wenzelm@34784
   103
  private val command_change_actor = actor {
wenzelm@34784
   104
    loop {
wenzelm@34784
   105
      react {
wenzelm@34834
   106
        case command: Command =>  // FIXME rough filtering according to document family!?
wenzelm@34834
   107
          Swing_Thread.now {
wenzelm@34834
   108
            changed_commands += command
wenzelm@34834
   109
            changed_delay()
wenzelm@34834
   110
          }
wenzelm@34784
   111
wenzelm@34784
   112
        case Exit => reply(()); exit()
wenzelm@34784
   113
wenzelm@34784
   114
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@34784
   115
      }
immler@34403
   116
    }
immler@34678
   117
  }
immler@34403
   118
immler@34403
   119
wenzelm@34784
   120
  /* text_area_extension */
wenzelm@34784
   121
wenzelm@34784
   122
  private val text_area_extension = new TextAreaExtension
immler@34678
   123
  {
wenzelm@34784
   124
    override def paintValidLine(gfx: Graphics2D,
wenzelm@34784
   125
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@34784
   126
    {
wenzelm@34784
   127
      def from_current(pos: Int) = model.from_current(document, pos)
wenzelm@34784
   128
      def to_current(pos: Int) = model.to_current(document, pos)
wenzelm@34784
   129
      val saved_color = gfx.getColor
wenzelm@34784
   130
wenzelm@34784
   131
      val metrics = text_area.getPainter.getFontMetrics
immler@34403
   132
wenzelm@34784
   133
      // encolor phase
wenzelm@34784
   134
      var cmd = document.command_at(from_current(start))
wenzelm@34784
   135
      while (cmd.isDefined && cmd.get.start(document) < end) {
wenzelm@34784
   136
        val e = cmd.get
wenzelm@34784
   137
        val begin = start max to_current(e.start(document))
wenzelm@34784
   138
        val finish = (end - 1) min to_current(e.stop(document))
wenzelm@34784
   139
        encolor(gfx, y, metrics.getHeight, begin, finish,
wenzelm@34784
   140
          Document_View.choose_color(e, document), true)
wenzelm@34784
   141
        cmd = document.commands.next(e)
wenzelm@34784
   142
      }
immler@34678
   143
wenzelm@34784
   144
      gfx.setColor(saved_color)
wenzelm@34784
   145
    }
wenzelm@34784
   146
wenzelm@34784
   147
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   148
    {
wenzelm@34784
   149
      val offset = model.from_current(document, text_area.xyToOffset(x, y))
wenzelm@34784
   150
      document.command_at(offset) match {
wenzelm@34784
   151
        case Some(cmd) =>
wenzelm@34784
   152
          document.token_start(cmd.tokens.first)
wenzelm@34832
   153
          document.current_state(cmd).type_at(offset - cmd.start(document)).getOrElse(null)
wenzelm@34784
   154
        case None => null
wenzelm@34784
   155
      }
wenzelm@34734
   156
    }
immler@34678
   157
  }
immler@34513
   158
wenzelm@34784
   159
wenzelm@34810
   160
  /* caret_listener */
wenzelm@34810
   161
wenzelm@34810
   162
  private var _selected_command: Command = null
wenzelm@34811
   163
  private def selected_command = _selected_command
wenzelm@34811
   164
  private def selected_command_=(cmd: Command)
wenzelm@34810
   165
  {
wenzelm@34811
   166
    _selected_command = cmd
wenzelm@34811
   167
    session.results.event(cmd)
wenzelm@34810
   168
  }
wenzelm@34810
   169
wenzelm@34810
   170
  private val caret_listener = new CaretListener
wenzelm@34810
   171
  {
wenzelm@34810
   172
    override def caretUpdate(e: CaretEvent) {
wenzelm@34834
   173
      document.command_at(e.getDot) match {
wenzelm@34810
   174
        case Some(cmd)
wenzelm@34834
   175
          if (document.token_start(cmd.tokens.first) <= e.getDot &&
wenzelm@34810
   176
            selected_command != cmd) =>
wenzelm@34810
   177
          selected_command = cmd  // FIXME !?
wenzelm@34810
   178
        case _ =>
wenzelm@34810
   179
      }
wenzelm@34810
   180
    }
wenzelm@34810
   181
  }
wenzelm@34810
   182
wenzelm@34810
   183
wenzelm@34784
   184
  /* (re)painting */
wenzelm@34784
   185
wenzelm@34784
   186
  private val update_delay = Swing_Thread.delay_first(500) { model.buffer.propertiesChanged() }
wenzelm@34784
   187
wenzelm@34784
   188
  private def update_syntax(cmd: Command)
wenzelm@34784
   189
  {
wenzelm@34834
   190
    val (line1, line2) = model.lines_of_command(document, cmd)
wenzelm@34784
   191
    if (line2 >= text_area.getFirstLine &&
wenzelm@34784
   192
      line1 <= text_area.getFirstLine + text_area.getVisibleLines)
wenzelm@34784
   193
        update_delay()
wenzelm@34784
   194
  }
wenzelm@34784
   195
wenzelm@34784
   196
  private def invalidate_line(cmd: Command) =
wenzelm@34784
   197
  {
wenzelm@34834
   198
    val (start, stop) = model.lines_of_command(document, cmd)
wenzelm@34784
   199
    text_area.invalidateLineRange(start, stop)
wenzelm@34784
   200
wenzelm@34810
   201
    if (selected_command == cmd)
wenzelm@34834
   202
      session.results.event(cmd)
wenzelm@34784
   203
  }
wenzelm@34784
   204
wenzelm@34784
   205
  private def invalidate_all() =
wenzelm@34784
   206
    text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
wenzelm@34784
   207
      text_area.getLastPhysicalLine)
wenzelm@34784
   208
wenzelm@34784
   209
  private def encolor(gfx: Graphics2D,
wenzelm@34784
   210
    y: Int, height: Int, begin: Int, finish: Int, color: Color, fill: Boolean)
wenzelm@34784
   211
  {
wenzelm@34784
   212
    val start = text_area.offsetToXY(begin)
wenzelm@34784
   213
    val stop =
wenzelm@34784
   214
      if (finish < model.buffer.getLength) text_area.offsetToXY(finish)
wenzelm@34784
   215
      else {
wenzelm@34784
   216
        val p = text_area.offsetToXY(finish - 1)
wenzelm@34784
   217
        val metrics = text_area.getPainter.getFontMetrics
wenzelm@34784
   218
        p.x = p.x + (metrics.charWidth(' ') max metrics.getMaxAdvance)
wenzelm@34784
   219
        p
wenzelm@34784
   220
      }
wenzelm@34784
   221
wenzelm@34784
   222
    if (start != null && stop != null) {
wenzelm@34784
   223
      gfx.setColor(color)
wenzelm@34784
   224
      if (fill) gfx.fillRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   225
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   226
    }
wenzelm@34784
   227
  }
wenzelm@34784
   228
wenzelm@34784
   229
wenzelm@34784
   230
  /* overview of command status left of scrollbar */
wenzelm@34784
   231
wenzelm@34834
   232
  private val overview = new JPanel(new BorderLayout)
wenzelm@34784
   233
  {
wenzelm@34784
   234
    private val WIDTH = 10
wenzelm@34784
   235
    private val HEIGHT = 2
wenzelm@34784
   236
wenzelm@34806
   237
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   238
wenzelm@34784
   239
    setRequestFocusEnabled(false)
wenzelm@34784
   240
wenzelm@34784
   241
    addMouseListener(new MouseAdapter {
wenzelm@34784
   242
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   243
        val line = y_to_line(event.getY)
wenzelm@34784
   244
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   245
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   246
      }
wenzelm@34784
   247
    })
wenzelm@34784
   248
wenzelm@34784
   249
    override def addNotify() {
wenzelm@34784
   250
      super.addNotify()
wenzelm@34784
   251
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   252
    }
immler@34403
   253
wenzelm@34784
   254
    override def removeNotify() {
wenzelm@34784
   255
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   256
      super.removeNotify
wenzelm@34784
   257
    }
wenzelm@34784
   258
wenzelm@34784
   259
    override def getToolTipText(event: MouseEvent): String =
wenzelm@34784
   260
    {
wenzelm@34784
   261
      val line = y_to_line(event.getY())
wenzelm@34784
   262
      if (line >= 0 && line < text_area.getLineCount) "<html><b>TODO:</b><br>Tooltip</html>"
wenzelm@34784
   263
      else ""
wenzelm@34784
   264
    }
wenzelm@34784
   265
wenzelm@34784
   266
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   267
    {
wenzelm@34784
   268
      super.paintComponent(gfx)
wenzelm@34784
   269
      val buffer = model.buffer
wenzelm@34784
   270
wenzelm@34834
   271
      for (command <- document.commands) {
wenzelm@34834
   272
        val line1 = buffer.getLineOfOffset(model.to_current(document, command.start(document)))
wenzelm@34834
   273
        val line2 = buffer.getLineOfOffset(model.to_current(document, command.stop(document))) + 1
wenzelm@34784
   274
        val y = line_to_y(line1)
wenzelm@34784
   275
        val height = HEIGHT * (line2 - line1)
wenzelm@34834
   276
        gfx.setColor(Document_View.choose_color(command, document))
wenzelm@34784
   277
        gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@34784
   278
      }
wenzelm@34784
   279
    }
wenzelm@34784
   280
wenzelm@34784
   281
    private def line_to_y(line: Int): Int =
wenzelm@34784
   282
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   283
wenzelm@34784
   284
    private def y_to_line(y: Int): Int =
wenzelm@34784
   285
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   286
  }
immler@34403
   287
wenzelm@34784
   288
wenzelm@34784
   289
  /* activation */
wenzelm@34784
   290
wenzelm@34808
   291
  private def activate()
wenzelm@34784
   292
  {
wenzelm@34784
   293
    text_area.getPainter.
wenzelm@34784
   294
      addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, text_area_extension)
wenzelm@34810
   295
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   296
    text_area.addLeftOfScrollBar(overview)
wenzelm@34784
   297
    session.command_change += command_change_actor
wenzelm@34784
   298
  }
wenzelm@34784
   299
wenzelm@34808
   300
  private def deactivate()
wenzelm@34784
   301
  {
wenzelm@34784
   302
    session.command_change -= command_change_actor
wenzelm@34784
   303
    command_change_actor !? Exit
wenzelm@34810
   304
    text_area.removeLeftOfScrollBar(overview)
wenzelm@34810
   305
    text_area.removeCaretListener(caret_listener)
wenzelm@34784
   306
    text_area.getPainter.removeExtension(text_area_extension)
wenzelm@34784
   307
  }
immler@34403
   308
}