src/Tools/jEdit/src/jedit/document_view.scala
author wenzelm
Sat May 08 21:08:30 2010 +0200 (2010-05-08)
changeset 36760 b82a698ef6c9
parent 36015 6111de7c916a
child 36990 449628c148cf
permissions -rw-r--r--
tuned headers;
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@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@34867
    28
    doc.current_state(command).map(_.status) match {
wenzelm@34867
    29
      case Some(Command.Status.UNPROCESSED) => new Color(255, 228, 225)
wenzelm@34867
    30
      case Some(Command.Status.FINISHED) => new Color(234, 248, 255)
wenzelm@34867
    31
      case Some(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@34855
   129
      val metrics = text_area.getPainter.getFontMetrics
wenzelm@34784
   130
      val saved_color = gfx.getColor
wenzelm@34855
   131
      try {
wenzelm@34855
   132
        for ((command, command_start) <-
wenzelm@34855
   133
          document.command_range(from_current(start), from_current(end)))
wenzelm@34855
   134
        {
wenzelm@34855
   135
          val begin = start max to_current(command_start)
wenzelm@34855
   136
          val finish = (end - 1) min to_current(command_start + command.length)
wenzelm@34855
   137
          encolor(gfx, y, metrics.getHeight, begin, finish,
wenzelm@34855
   138
            Document_View.choose_color(command, document), true)
wenzelm@34855
   139
        }
wenzelm@34784
   140
      }
wenzelm@34855
   141
      finally { gfx.setColor(saved_color) }
wenzelm@34784
   142
    }
wenzelm@34784
   143
wenzelm@34784
   144
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   145
    {
wenzelm@34784
   146
      val offset = model.from_current(document, text_area.xyToOffset(x, y))
wenzelm@34784
   147
      document.command_at(offset) match {
wenzelm@34855
   148
        case Some((command, command_start)) =>
wenzelm@34867
   149
          document.current_state(command).get.type_at(offset - command_start).getOrElse(null)
wenzelm@34784
   150
        case None => null
wenzelm@34784
   151
      }
wenzelm@34734
   152
    }
immler@34678
   153
  }
immler@34513
   154
wenzelm@34784
   155
wenzelm@34810
   156
  /* caret_listener */
wenzelm@34810
   157
wenzelm@34810
   158
  private var _selected_command: Command = null
wenzelm@34811
   159
  private def selected_command = _selected_command
wenzelm@34811
   160
  private def selected_command_=(cmd: Command)
wenzelm@34810
   161
  {
wenzelm@34811
   162
    _selected_command = cmd
wenzelm@34811
   163
    session.results.event(cmd)
wenzelm@34810
   164
  }
wenzelm@34810
   165
wenzelm@34810
   166
  private val caret_listener = new CaretListener
wenzelm@34810
   167
  {
wenzelm@34855
   168
    override def caretUpdate(e: CaretEvent)
wenzelm@34855
   169
    {
wenzelm@34834
   170
      document.command_at(e.getDot) match {
wenzelm@34855
   171
        case Some((command, command_start)) if (selected_command != command) =>
wenzelm@34855
   172
          selected_command = command
wenzelm@34810
   173
        case _ =>
wenzelm@34810
   174
      }
wenzelm@34810
   175
    }
wenzelm@34810
   176
  }
wenzelm@34810
   177
wenzelm@34810
   178
wenzelm@34784
   179
  /* (re)painting */
wenzelm@34784
   180
wenzelm@34784
   181
  private val update_delay = Swing_Thread.delay_first(500) { model.buffer.propertiesChanged() }
wenzelm@34784
   182
wenzelm@34784
   183
  private def update_syntax(cmd: Command)
wenzelm@34784
   184
  {
wenzelm@34834
   185
    val (line1, line2) = model.lines_of_command(document, cmd)
wenzelm@34784
   186
    if (line2 >= text_area.getFirstLine &&
wenzelm@34784
   187
      line1 <= text_area.getFirstLine + text_area.getVisibleLines)
wenzelm@34784
   188
        update_delay()
wenzelm@34784
   189
  }
wenzelm@34784
   190
wenzelm@34784
   191
  private def invalidate_line(cmd: Command) =
wenzelm@34784
   192
  {
wenzelm@34834
   193
    val (start, stop) = model.lines_of_command(document, cmd)
wenzelm@34784
   194
    text_area.invalidateLineRange(start, stop)
wenzelm@34784
   195
wenzelm@34810
   196
    if (selected_command == cmd)
wenzelm@34834
   197
      session.results.event(cmd)
wenzelm@34784
   198
  }
wenzelm@34784
   199
wenzelm@34784
   200
  private def invalidate_all() =
wenzelm@34784
   201
    text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
wenzelm@34784
   202
      text_area.getLastPhysicalLine)
wenzelm@34784
   203
wenzelm@34784
   204
  private def encolor(gfx: Graphics2D,
wenzelm@34784
   205
    y: Int, height: Int, begin: Int, finish: Int, color: Color, fill: Boolean)
wenzelm@34784
   206
  {
wenzelm@34784
   207
    val start = text_area.offsetToXY(begin)
wenzelm@34784
   208
    val stop =
wenzelm@34784
   209
      if (finish < model.buffer.getLength) text_area.offsetToXY(finish)
wenzelm@34784
   210
      else {
wenzelm@34784
   211
        val p = text_area.offsetToXY(finish - 1)
wenzelm@34784
   212
        val metrics = text_area.getPainter.getFontMetrics
wenzelm@34784
   213
        p.x = p.x + (metrics.charWidth(' ') max metrics.getMaxAdvance)
wenzelm@34784
   214
        p
wenzelm@34784
   215
      }
wenzelm@34784
   216
wenzelm@34784
   217
    if (start != null && stop != null) {
wenzelm@34784
   218
      gfx.setColor(color)
wenzelm@34784
   219
      if (fill) gfx.fillRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   220
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   221
    }
wenzelm@34784
   222
  }
wenzelm@34784
   223
wenzelm@34784
   224
wenzelm@34784
   225
  /* overview of command status left of scrollbar */
wenzelm@34784
   226
wenzelm@34834
   227
  private val overview = new JPanel(new BorderLayout)
wenzelm@34784
   228
  {
wenzelm@34784
   229
    private val WIDTH = 10
wenzelm@34784
   230
    private val HEIGHT = 2
wenzelm@34784
   231
wenzelm@34806
   232
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   233
wenzelm@34784
   234
    setRequestFocusEnabled(false)
wenzelm@34784
   235
wenzelm@34784
   236
    addMouseListener(new MouseAdapter {
wenzelm@34784
   237
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   238
        val line = y_to_line(event.getY)
wenzelm@34784
   239
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   240
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   241
      }
wenzelm@34784
   242
    })
wenzelm@34784
   243
wenzelm@34784
   244
    override def addNotify() {
wenzelm@34784
   245
      super.addNotify()
wenzelm@34784
   246
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   247
    }
immler@34403
   248
wenzelm@34784
   249
    override def removeNotify() {
wenzelm@34784
   250
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   251
      super.removeNotify
wenzelm@34784
   252
    }
wenzelm@34784
   253
wenzelm@34784
   254
    override def getToolTipText(event: MouseEvent): String =
wenzelm@34784
   255
    {
wenzelm@34784
   256
      val line = y_to_line(event.getY())
wenzelm@34784
   257
      if (line >= 0 && line < text_area.getLineCount) "<html><b>TODO:</b><br>Tooltip</html>"
wenzelm@34784
   258
      else ""
wenzelm@34784
   259
    }
wenzelm@34784
   260
wenzelm@34784
   261
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   262
    {
wenzelm@34784
   263
      super.paintComponent(gfx)
wenzelm@34784
   264
      val buffer = model.buffer
wenzelm@34784
   265
wenzelm@34856
   266
      for ((command, start) <- document.command_range(0)) {
wenzelm@34856
   267
        val line1 = buffer.getLineOfOffset(model.to_current(document, start))
wenzelm@34856
   268
        val line2 = buffer.getLineOfOffset(model.to_current(document, start + command.length)) + 1
wenzelm@34784
   269
        val y = line_to_y(line1)
wenzelm@34784
   270
        val height = HEIGHT * (line2 - line1)
wenzelm@34834
   271
        gfx.setColor(Document_View.choose_color(command, document))
wenzelm@34784
   272
        gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@34784
   273
      }
wenzelm@34784
   274
    }
wenzelm@34784
   275
wenzelm@34784
   276
    private def line_to_y(line: Int): Int =
wenzelm@34784
   277
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   278
wenzelm@34784
   279
    private def y_to_line(y: Int): Int =
wenzelm@34784
   280
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   281
  }
immler@34403
   282
wenzelm@34784
   283
wenzelm@34784
   284
  /* activation */
wenzelm@34784
   285
wenzelm@34808
   286
  private def activate()
wenzelm@34784
   287
  {
wenzelm@34784
   288
    text_area.getPainter.
wenzelm@34784
   289
      addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, text_area_extension)
wenzelm@34810
   290
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   291
    text_area.addLeftOfScrollBar(overview)
wenzelm@34784
   292
    session.command_change += command_change_actor
wenzelm@34784
   293
  }
wenzelm@34784
   294
wenzelm@34808
   295
  private def deactivate()
wenzelm@34784
   296
  {
wenzelm@34784
   297
    session.command_change -= command_change_actor
wenzelm@34784
   298
    command_change_actor !? Exit
wenzelm@34810
   299
    text_area.removeLeftOfScrollBar(overview)
wenzelm@34810
   300
    text_area.removeCaretListener(caret_listener)
wenzelm@34784
   301
    text_area.getPainter.removeExtension(text_area_extension)
wenzelm@34784
   302
  }
immler@34403
   303
}