src/Tools/jEdit/src/jedit/document_view.scala
author wenzelm
Sat May 29 20:34:28 2010 +0200 (2010-05-29)
changeset 37188 b78ff6b4f4b3
parent 37187 dc1927a0f534
child 37201 8517a650cfdc
permissions -rw-r--r--
do not highlight ignored command spans;
tuned;
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@37187
    26
  def choose_color(document: Document, command: Command): Color =
wenzelm@34784
    27
  {
wenzelm@37187
    28
    val state = document.current_state(command)
wenzelm@37186
    29
    if (state.forks > 0) new Color(255, 228, 225)
wenzelm@37186
    30
    else if (state.forks < 0) Color.red
wenzelm@37186
    31
    else
wenzelm@37186
    32
      state.status match {
wenzelm@37186
    33
        case Command.Status.UNPROCESSED => new Color(255, 228, 225)
wenzelm@37186
    34
        case Command.Status.FINISHED => new Color(234, 248, 255)
wenzelm@37186
    35
        case Command.Status.FAILED => new Color(255, 193, 193)
wenzelm@37186
    36
        case Command.Status.UNDEFINED => Color.red
wenzelm@37186
    37
      }
wenzelm@34784
    38
  }
wenzelm@34784
    39
wenzelm@34784
    40
wenzelm@34784
    41
  /* document view of text area */
wenzelm@34784
    42
wenzelm@34784
    43
  private val key = new Object
wenzelm@34784
    44
wenzelm@34784
    45
  def init(model: Document_Model, text_area: TextArea): Document_View =
wenzelm@34784
    46
  {
wenzelm@34789
    47
    Swing_Thread.assert()
wenzelm@34784
    48
    val doc_view = new Document_View(model, text_area)
wenzelm@34784
    49
    text_area.putClientProperty(key, doc_view)
wenzelm@34784
    50
    doc_view.activate()
wenzelm@34784
    51
    doc_view
wenzelm@34784
    52
  }
wenzelm@34784
    53
wenzelm@34788
    54
  def apply(text_area: TextArea): Option[Document_View] =
wenzelm@34784
    55
  {
wenzelm@34789
    56
    Swing_Thread.assert()
wenzelm@34784
    57
    text_area.getClientProperty(key) match {
wenzelm@34784
    58
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    59
      case _ => None
wenzelm@34784
    60
    }
wenzelm@34784
    61
  }
wenzelm@34784
    62
wenzelm@34784
    63
  def exit(text_area: TextArea)
wenzelm@34784
    64
  {
wenzelm@34789
    65
    Swing_Thread.assert()
wenzelm@34788
    66
    apply(text_area) match {
wenzelm@34784
    67
      case None => error("No document view for text area: " + text_area)
wenzelm@34784
    68
      case Some(doc_view) =>
wenzelm@34784
    69
        doc_view.deactivate()
wenzelm@34784
    70
        text_area.putClientProperty(key, null)
wenzelm@34784
    71
    }
wenzelm@34784
    72
  }
wenzelm@34784
    73
}
immler@34403
    74
wenzelm@34733
    75
wenzelm@37129
    76
class Document_View(val model: Document_Model, text_area: TextArea)
immler@34654
    77
{
wenzelm@34784
    78
  private val session = model.session
wenzelm@34784
    79
immler@34403
    80
wenzelm@37129
    81
  /* commands_changed_actor */
wenzelm@34834
    82
wenzelm@37129
    83
  private val commands_changed_actor = actor {
wenzelm@34784
    84
    loop {
wenzelm@34784
    85
      react {
wenzelm@37129
    86
        case Command_Set(changed) =>
wenzelm@34834
    87
          Swing_Thread.now {
wenzelm@37187
    88
            val document = model.recent_document()
wenzelm@37129
    89
            // FIXME cover doc states as well!!?
wenzelm@37129
    90
            for (command <- changed if document.commands.contains(command)) {
wenzelm@37187
    91
              update_syntax(document, command)
wenzelm@37187
    92
              invalidate_line(document, command)
wenzelm@37129
    93
              overview.repaint()
wenzelm@37129
    94
            }
wenzelm@34834
    95
          }
wenzelm@34784
    96
wenzelm@34784
    97
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@34784
    98
      }
immler@34403
    99
    }
immler@34678
   100
  }
immler@34403
   101
immler@34403
   102
wenzelm@34784
   103
  /* text_area_extension */
wenzelm@34784
   104
wenzelm@34784
   105
  private val text_area_extension = new TextAreaExtension
immler@34678
   106
  {
wenzelm@34784
   107
    override def paintValidLine(gfx: Graphics2D,
wenzelm@34784
   108
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@34784
   109
    {
wenzelm@37187
   110
      val document = model.recent_document()
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@37188
   117
          document.command_range(from_current(start), from_current(end)) if !command.is_ignored)
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@37187
   122
            Document_View.choose_color(document, command), 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@37187
   130
      val document = model.recent_document()
wenzelm@34784
   131
      val offset = model.from_current(document, text_area.xyToOffset(x, y))
wenzelm@34784
   132
      document.command_at(offset) match {
wenzelm@34855
   133
        case Some((command, command_start)) =>
wenzelm@36990
   134
          document.current_state(command).type_at(offset - command_start) getOrElse null
wenzelm@34784
   135
        case None => null
wenzelm@34784
   136
      }
wenzelm@34734
   137
    }
immler@34678
   138
  }
immler@34513
   139
wenzelm@34784
   140
wenzelm@37129
   141
  /* caret handling */
wenzelm@34810
   142
wenzelm@37129
   143
  def selected_command: Option[Command] =
wenzelm@37187
   144
    model.recent_document().command_at(text_area.getCaretPosition) match {
wenzelm@37129
   145
      case Some((command, _)) => Some(command)
wenzelm@37129
   146
      case None => None
wenzelm@37129
   147
    }
wenzelm@34810
   148
wenzelm@34810
   149
  private val caret_listener = new CaretListener
wenzelm@34810
   150
  {
wenzelm@37129
   151
    private var last_selected_command: Option[Command] = None
wenzelm@34855
   152
    override def caretUpdate(e: CaretEvent)
wenzelm@34855
   153
    {
wenzelm@37129
   154
      val selected = selected_command
wenzelm@37129
   155
      if (selected != last_selected_command) {
wenzelm@37129
   156
        last_selected_command = selected
wenzelm@37129
   157
        if (selected.isDefined) session.indicate_command_change(selected.get)
wenzelm@34810
   158
      }
wenzelm@34810
   159
    }
wenzelm@34810
   160
  }
wenzelm@34810
   161
wenzelm@34810
   162
wenzelm@34784
   163
  /* (re)painting */
wenzelm@34784
   164
wenzelm@34784
   165
  private val update_delay = Swing_Thread.delay_first(500) { model.buffer.propertiesChanged() }
wenzelm@37132
   166
  // FIXME update_delay property
wenzelm@34784
   167
wenzelm@37187
   168
  private def update_syntax(document: Document, cmd: Command)
wenzelm@34784
   169
  {
wenzelm@34834
   170
    val (line1, line2) = model.lines_of_command(document, cmd)
wenzelm@34784
   171
    if (line2 >= text_area.getFirstLine &&
wenzelm@34784
   172
      line1 <= text_area.getFirstLine + text_area.getVisibleLines)
wenzelm@34784
   173
        update_delay()
wenzelm@34784
   174
  }
wenzelm@34784
   175
wenzelm@37187
   176
  private def invalidate_line(document: Document, cmd: Command) =
wenzelm@34784
   177
  {
wenzelm@34834
   178
    val (start, stop) = model.lines_of_command(document, cmd)
wenzelm@34784
   179
    text_area.invalidateLineRange(start, stop)
wenzelm@34784
   180
  }
wenzelm@34784
   181
wenzelm@34784
   182
  private def invalidate_all() =
wenzelm@34784
   183
    text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
wenzelm@34784
   184
      text_area.getLastPhysicalLine)
wenzelm@34784
   185
wenzelm@34784
   186
  private def encolor(gfx: Graphics2D,
wenzelm@34784
   187
    y: Int, height: Int, begin: Int, finish: Int, color: Color, fill: Boolean)
wenzelm@34784
   188
  {
wenzelm@34784
   189
    val start = text_area.offsetToXY(begin)
wenzelm@34784
   190
    val stop =
wenzelm@34784
   191
      if (finish < model.buffer.getLength) text_area.offsetToXY(finish)
wenzelm@34784
   192
      else {
wenzelm@34784
   193
        val p = text_area.offsetToXY(finish - 1)
wenzelm@34784
   194
        val metrics = text_area.getPainter.getFontMetrics
wenzelm@34784
   195
        p.x = p.x + (metrics.charWidth(' ') max metrics.getMaxAdvance)
wenzelm@34784
   196
        p
wenzelm@34784
   197
      }
wenzelm@34784
   198
wenzelm@34784
   199
    if (start != null && stop != null) {
wenzelm@34784
   200
      gfx.setColor(color)
wenzelm@34784
   201
      if (fill) gfx.fillRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   202
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   203
    }
wenzelm@34784
   204
  }
wenzelm@34784
   205
wenzelm@34784
   206
wenzelm@34784
   207
  /* overview of command status left of scrollbar */
wenzelm@34784
   208
wenzelm@34834
   209
  private val overview = new JPanel(new BorderLayout)
wenzelm@34784
   210
  {
wenzelm@34784
   211
    private val WIDTH = 10
wenzelm@34784
   212
    private val HEIGHT = 2
wenzelm@34784
   213
wenzelm@34806
   214
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   215
wenzelm@34784
   216
    setRequestFocusEnabled(false)
wenzelm@34784
   217
wenzelm@34784
   218
    addMouseListener(new MouseAdapter {
wenzelm@34784
   219
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   220
        val line = y_to_line(event.getY)
wenzelm@34784
   221
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   222
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   223
      }
wenzelm@34784
   224
    })
wenzelm@34784
   225
wenzelm@34784
   226
    override def addNotify() {
wenzelm@34784
   227
      super.addNotify()
wenzelm@34784
   228
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   229
    }
immler@34403
   230
wenzelm@34784
   231
    override def removeNotify() {
wenzelm@34784
   232
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   233
      super.removeNotify
wenzelm@34784
   234
    }
wenzelm@34784
   235
wenzelm@34784
   236
    override def getToolTipText(event: MouseEvent): String =
wenzelm@34784
   237
    {
wenzelm@34784
   238
      val line = y_to_line(event.getY())
wenzelm@34784
   239
      if (line >= 0 && line < text_area.getLineCount) "<html><b>TODO:</b><br>Tooltip</html>"
wenzelm@34784
   240
      else ""
wenzelm@34784
   241
    }
wenzelm@34784
   242
wenzelm@34784
   243
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   244
    {
wenzelm@34784
   245
      super.paintComponent(gfx)
wenzelm@34784
   246
      val buffer = model.buffer
wenzelm@37187
   247
      val document = model.recent_document()
wenzelm@37188
   248
      def to_current(pos: Int) = model.to_current(document, pos)
wenzelm@37188
   249
      val saved_color = gfx.getColor
wenzelm@37188
   250
      try {
wenzelm@37188
   251
        for ((command, start) <- document.command_range(0) if !command.is_ignored) {
wenzelm@37188
   252
          val line1 = buffer.getLineOfOffset(to_current(start))
wenzelm@37188
   253
          val line2 = buffer.getLineOfOffset(to_current(start + command.length)) + 1
wenzelm@37188
   254
          val y = line_to_y(line1)
wenzelm@37188
   255
          val height = HEIGHT * (line2 - line1)
wenzelm@37188
   256
          gfx.setColor(Document_View.choose_color(document, command))
wenzelm@37188
   257
          gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@37188
   258
        }
wenzelm@34784
   259
      }
wenzelm@37188
   260
      finally { gfx.setColor(saved_color) }
wenzelm@34784
   261
    }
wenzelm@34784
   262
wenzelm@34784
   263
    private def line_to_y(line: Int): Int =
wenzelm@34784
   264
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   265
wenzelm@34784
   266
    private def y_to_line(y: Int): Int =
wenzelm@34784
   267
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   268
  }
immler@34403
   269
wenzelm@34784
   270
wenzelm@34784
   271
  /* activation */
wenzelm@34784
   272
wenzelm@34808
   273
  private def activate()
wenzelm@34784
   274
  {
wenzelm@34784
   275
    text_area.getPainter.
wenzelm@34784
   276
      addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, text_area_extension)
wenzelm@34810
   277
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   278
    text_area.addLeftOfScrollBar(overview)
wenzelm@37129
   279
    session.commands_changed += commands_changed_actor
wenzelm@34784
   280
  }
wenzelm@34784
   281
wenzelm@34808
   282
  private def deactivate()
wenzelm@34784
   283
  {
wenzelm@37129
   284
    session.commands_changed -= commands_changed_actor
wenzelm@34810
   285
    text_area.removeLeftOfScrollBar(overview)
wenzelm@34810
   286
    text_area.removeCaretListener(caret_listener)
wenzelm@34784
   287
    text_area.getPainter.removeExtension(text_area_extension)
wenzelm@34784
   288
  }
immler@34403
   289
}