src/Tools/jEdit/src/jedit/document_view.scala
author wenzelm
Thu May 27 00:47:15 2010 +0200 (2010-05-27)
changeset 37129 4c83696b340e
parent 36990 449628c148cf
child 37132 10ef4da1c314
permissions -rw-r--r--
Command.toString: include id for debugging;
Command.consume: explicit forward, avoid dependency on Session and side-effect on event bus;
State.+ without side-effect on event bus;
Session.commands_changed: delayed command changes (outside of Swing thread), also subsumes former Session.results;
Document_View: tuned commands_changed handling and caret listening;
Document_View.selected_command: proper function, not event handler state;
Output_Dockable: directly act upon commands_changed, not caret events (via former Session.results);
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@36990
    28
    doc.current_state(command).status match {
wenzelm@36990
    29
      case Command.Status.UNPROCESSED => new Color(255, 228, 225)
wenzelm@36990
    30
      case Command.Status.FINISHED => new Color(234, 248, 255)
wenzelm@36990
    31
      case Command.Status.FAILED => new Color(255, 193, 193)
wenzelm@36990
    32
      case Command.Status.UNDEFINED => 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@37129
    72
class Document_View(val 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@37129
    82
  /* commands_changed_actor */
wenzelm@34834
    83
wenzelm@37129
    84
  private val commands_changed_actor = actor {
wenzelm@34784
    85
    loop {
wenzelm@34784
    86
      react {
wenzelm@37129
    87
        case Command_Set(changed) =>
wenzelm@34834
    88
          Swing_Thread.now {
wenzelm@37129
    89
            document = model.recent_document()
wenzelm@37129
    90
            // FIXME cover doc states as well!!?
wenzelm@37129
    91
            for (command <- changed if document.commands.contains(command)) {
wenzelm@37129
    92
              update_syntax(command)
wenzelm@37129
    93
              invalidate_line(command)
wenzelm@37129
    94
              overview.repaint()
wenzelm@37129
    95
            }
wenzelm@34834
    96
          }
wenzelm@34784
    97
wenzelm@34784
    98
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@34784
    99
      }
immler@34403
   100
    }
immler@34678
   101
  }
immler@34403
   102
immler@34403
   103
wenzelm@34784
   104
  /* text_area_extension */
wenzelm@34784
   105
wenzelm@34784
   106
  private val text_area_extension = new TextAreaExtension
immler@34678
   107
  {
wenzelm@34784
   108
    override def paintValidLine(gfx: Graphics2D,
wenzelm@34784
   109
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@34784
   110
    {
wenzelm@34784
   111
      def from_current(pos: Int) = model.from_current(document, pos)
wenzelm@34784
   112
      def to_current(pos: Int) = model.to_current(document, pos)
wenzelm@34855
   113
      val metrics = text_area.getPainter.getFontMetrics
wenzelm@34784
   114
      val saved_color = gfx.getColor
wenzelm@34855
   115
      try {
wenzelm@34855
   116
        for ((command, command_start) <-
wenzelm@34855
   117
          document.command_range(from_current(start), from_current(end)))
wenzelm@34855
   118
        {
wenzelm@34855
   119
          val begin = start max to_current(command_start)
wenzelm@34855
   120
          val finish = (end - 1) min to_current(command_start + command.length)
wenzelm@34855
   121
          encolor(gfx, y, metrics.getHeight, begin, finish,
wenzelm@34855
   122
            Document_View.choose_color(command, document), true)
wenzelm@34855
   123
        }
wenzelm@34784
   124
      }
wenzelm@34855
   125
      finally { gfx.setColor(saved_color) }
wenzelm@34784
   126
    }
wenzelm@34784
   127
wenzelm@34784
   128
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   129
    {
wenzelm@34784
   130
      val offset = model.from_current(document, text_area.xyToOffset(x, y))
wenzelm@34784
   131
      document.command_at(offset) match {
wenzelm@34855
   132
        case Some((command, command_start)) =>
wenzelm@36990
   133
          document.current_state(command).type_at(offset - command_start) getOrElse null
wenzelm@34784
   134
        case None => null
wenzelm@34784
   135
      }
wenzelm@34734
   136
    }
immler@34678
   137
  }
immler@34513
   138
wenzelm@34784
   139
wenzelm@37129
   140
  /* caret handling */
wenzelm@34810
   141
wenzelm@37129
   142
  def selected_command: Option[Command] =
wenzelm@37129
   143
    document.command_at(text_area.getCaretPosition) match {
wenzelm@37129
   144
      case Some((command, _)) => Some(command)
wenzelm@37129
   145
      case None => None
wenzelm@37129
   146
    }
wenzelm@34810
   147
wenzelm@34810
   148
  private val caret_listener = new CaretListener
wenzelm@34810
   149
  {
wenzelm@37129
   150
    private var last_selected_command: Option[Command] = None
wenzelm@34855
   151
    override def caretUpdate(e: CaretEvent)
wenzelm@34855
   152
    {
wenzelm@37129
   153
      val selected = selected_command
wenzelm@37129
   154
      if (selected != last_selected_command) {
wenzelm@37129
   155
        last_selected_command = selected
wenzelm@37129
   156
        if (selected.isDefined) session.indicate_command_change(selected.get)
wenzelm@34810
   157
      }
wenzelm@34810
   158
    }
wenzelm@34810
   159
  }
wenzelm@34810
   160
wenzelm@34810
   161
wenzelm@34784
   162
  /* (re)painting */
wenzelm@34784
   163
wenzelm@34784
   164
  private val update_delay = Swing_Thread.delay_first(500) { model.buffer.propertiesChanged() }
wenzelm@34784
   165
wenzelm@34784
   166
  private def update_syntax(cmd: Command)
wenzelm@34784
   167
  {
wenzelm@34834
   168
    val (line1, line2) = model.lines_of_command(document, cmd)
wenzelm@34784
   169
    if (line2 >= text_area.getFirstLine &&
wenzelm@34784
   170
      line1 <= text_area.getFirstLine + text_area.getVisibleLines)
wenzelm@34784
   171
        update_delay()
wenzelm@34784
   172
  }
wenzelm@34784
   173
wenzelm@34784
   174
  private def invalidate_line(cmd: Command) =
wenzelm@34784
   175
  {
wenzelm@34834
   176
    val (start, stop) = model.lines_of_command(document, cmd)
wenzelm@34784
   177
    text_area.invalidateLineRange(start, stop)
wenzelm@34784
   178
  }
wenzelm@34784
   179
wenzelm@34784
   180
  private def invalidate_all() =
wenzelm@34784
   181
    text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
wenzelm@34784
   182
      text_area.getLastPhysicalLine)
wenzelm@34784
   183
wenzelm@34784
   184
  private def encolor(gfx: Graphics2D,
wenzelm@34784
   185
    y: Int, height: Int, begin: Int, finish: Int, color: Color, fill: Boolean)
wenzelm@34784
   186
  {
wenzelm@34784
   187
    val start = text_area.offsetToXY(begin)
wenzelm@34784
   188
    val stop =
wenzelm@34784
   189
      if (finish < model.buffer.getLength) text_area.offsetToXY(finish)
wenzelm@34784
   190
      else {
wenzelm@34784
   191
        val p = text_area.offsetToXY(finish - 1)
wenzelm@34784
   192
        val metrics = text_area.getPainter.getFontMetrics
wenzelm@34784
   193
        p.x = p.x + (metrics.charWidth(' ') max metrics.getMaxAdvance)
wenzelm@34784
   194
        p
wenzelm@34784
   195
      }
wenzelm@34784
   196
wenzelm@34784
   197
    if (start != null && stop != null) {
wenzelm@34784
   198
      gfx.setColor(color)
wenzelm@34784
   199
      if (fill) gfx.fillRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   200
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   201
    }
wenzelm@34784
   202
  }
wenzelm@34784
   203
wenzelm@34784
   204
wenzelm@34784
   205
  /* overview of command status left of scrollbar */
wenzelm@34784
   206
wenzelm@34834
   207
  private val overview = new JPanel(new BorderLayout)
wenzelm@34784
   208
  {
wenzelm@34784
   209
    private val WIDTH = 10
wenzelm@34784
   210
    private val HEIGHT = 2
wenzelm@34784
   211
wenzelm@34806
   212
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   213
wenzelm@34784
   214
    setRequestFocusEnabled(false)
wenzelm@34784
   215
wenzelm@34784
   216
    addMouseListener(new MouseAdapter {
wenzelm@34784
   217
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   218
        val line = y_to_line(event.getY)
wenzelm@34784
   219
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   220
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   221
      }
wenzelm@34784
   222
    })
wenzelm@34784
   223
wenzelm@34784
   224
    override def addNotify() {
wenzelm@34784
   225
      super.addNotify()
wenzelm@34784
   226
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   227
    }
immler@34403
   228
wenzelm@34784
   229
    override def removeNotify() {
wenzelm@34784
   230
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   231
      super.removeNotify
wenzelm@34784
   232
    }
wenzelm@34784
   233
wenzelm@34784
   234
    override def getToolTipText(event: MouseEvent): String =
wenzelm@34784
   235
    {
wenzelm@34784
   236
      val line = y_to_line(event.getY())
wenzelm@34784
   237
      if (line >= 0 && line < text_area.getLineCount) "<html><b>TODO:</b><br>Tooltip</html>"
wenzelm@34784
   238
      else ""
wenzelm@34784
   239
    }
wenzelm@34784
   240
wenzelm@34784
   241
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   242
    {
wenzelm@34784
   243
      super.paintComponent(gfx)
wenzelm@34784
   244
      val buffer = model.buffer
wenzelm@34784
   245
wenzelm@34856
   246
      for ((command, start) <- document.command_range(0)) {
wenzelm@34856
   247
        val line1 = buffer.getLineOfOffset(model.to_current(document, start))
wenzelm@34856
   248
        val line2 = buffer.getLineOfOffset(model.to_current(document, start + command.length)) + 1
wenzelm@34784
   249
        val y = line_to_y(line1)
wenzelm@34784
   250
        val height = HEIGHT * (line2 - line1)
wenzelm@34834
   251
        gfx.setColor(Document_View.choose_color(command, document))
wenzelm@34784
   252
        gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@34784
   253
      }
wenzelm@34784
   254
    }
wenzelm@34784
   255
wenzelm@34784
   256
    private def line_to_y(line: Int): Int =
wenzelm@34784
   257
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   258
wenzelm@34784
   259
    private def y_to_line(y: Int): Int =
wenzelm@34784
   260
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   261
  }
immler@34403
   262
wenzelm@34784
   263
wenzelm@34784
   264
  /* activation */
wenzelm@34784
   265
wenzelm@34808
   266
  private def activate()
wenzelm@34784
   267
  {
wenzelm@34784
   268
    text_area.getPainter.
wenzelm@34784
   269
      addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, text_area_extension)
wenzelm@34810
   270
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   271
    text_area.addLeftOfScrollBar(overview)
wenzelm@37129
   272
    session.commands_changed += commands_changed_actor
wenzelm@34784
   273
  }
wenzelm@34784
   274
wenzelm@34808
   275
  private def deactivate()
wenzelm@34784
   276
  {
wenzelm@37129
   277
    session.commands_changed -= commands_changed_actor
wenzelm@34810
   278
    text_area.removeLeftOfScrollBar(overview)
wenzelm@34810
   279
    text_area.removeCaretListener(caret_listener)
wenzelm@34784
   280
    text_area.getPainter.removeExtension(text_area_extension)
wenzelm@34784
   281
  }
immler@34403
   282
}