src/Tools/jEdit/src/jedit/document_view.scala
author wenzelm
Tue Dec 29 20:40:08 2009 +0100 (2009-12-29)
changeset 34808 e462572536e9
parent 34806 211ac6c4d4c9
child 34810 9ad3431a34a5
permissions -rw-r--r--
eliminated global Session.document_0 -- did not work due to hardwired id;
more precise Session.begin_document, avoid race on var prover;
replaced slightly odd Session.document_versions by Sassion.documents table (cf. src/Pure/Isar/isar_document.ML);
simplified edit_document in ML: initial empty command is identified by "";
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@34784
    11
import isabelle.proofdocument.{Command, Proof_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@34784
    26
  def choose_color(command: Command, doc: Proof_Document): Color =
wenzelm@34784
    27
  {
wenzelm@34784
    28
    command.status(doc) 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@34784
    77
  /* command change actor */
wenzelm@34784
    78
wenzelm@34784
    79
  private case object Exit
immler@34403
    80
wenzelm@34784
    81
  private val command_change_actor = actor {
wenzelm@34784
    82
    loop {
wenzelm@34784
    83
      react {
wenzelm@34784
    84
        case command: Command =>
wenzelm@34784
    85
          if (model.current_document().commands.contains(command))
wenzelm@34784
    86
            Swing_Thread.now {
wenzelm@34784
    87
              update_syntax(command)
wenzelm@34784
    88
              invalidate_line(command)
wenzelm@34784
    89
              overview.repaint()
wenzelm@34784
    90
            }
wenzelm@34784
    91
wenzelm@34784
    92
        case Exit => reply(()); exit()
wenzelm@34784
    93
wenzelm@34784
    94
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@34784
    95
      }
immler@34403
    96
    }
immler@34678
    97
  }
immler@34403
    98
immler@34403
    99
wenzelm@34784
   100
  /* text_area_extension */
wenzelm@34784
   101
wenzelm@34784
   102
  private val text_area_extension = new TextAreaExtension
immler@34678
   103
  {
wenzelm@34784
   104
    override def paintValidLine(gfx: Graphics2D,
wenzelm@34784
   105
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@34784
   106
    {
wenzelm@34784
   107
      val document = model.current_document()
wenzelm@34784
   108
      def from_current(pos: Int) = model.from_current(document, pos)
wenzelm@34784
   109
      def to_current(pos: Int) = model.to_current(document, pos)
wenzelm@34784
   110
      val saved_color = gfx.getColor
wenzelm@34784
   111
wenzelm@34784
   112
      val metrics = text_area.getPainter.getFontMetrics
immler@34403
   113
wenzelm@34784
   114
      // encolor phase
wenzelm@34784
   115
      var cmd = document.command_at(from_current(start))
wenzelm@34784
   116
      while (cmd.isDefined && cmd.get.start(document) < end) {
wenzelm@34784
   117
        val e = cmd.get
wenzelm@34784
   118
        val begin = start max to_current(e.start(document))
wenzelm@34784
   119
        val finish = (end - 1) min to_current(e.stop(document))
wenzelm@34784
   120
        encolor(gfx, y, metrics.getHeight, begin, finish,
wenzelm@34784
   121
          Document_View.choose_color(e, document), true)
wenzelm@34784
   122
        cmd = document.commands.next(e)
wenzelm@34784
   123
      }
immler@34678
   124
wenzelm@34784
   125
      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 document = model.current_document()
wenzelm@34784
   131
      val offset = model.from_current(document, text_area.xyToOffset(x, y))
wenzelm@34784
   132
      document.command_at(offset) match {
wenzelm@34784
   133
        case Some(cmd) =>
wenzelm@34784
   134
          document.token_start(cmd.tokens.first)
wenzelm@34784
   135
          cmd.type_at(document, offset - cmd.start(document)).getOrElse(null)
wenzelm@34784
   136
        case None => null
wenzelm@34784
   137
      }
wenzelm@34734
   138
    }
immler@34678
   139
  }
immler@34513
   140
wenzelm@34784
   141
wenzelm@34784
   142
  /* (re)painting */
wenzelm@34784
   143
wenzelm@34784
   144
  private val update_delay = Swing_Thread.delay_first(500) { model.buffer.propertiesChanged() }
wenzelm@34784
   145
wenzelm@34784
   146
  private def update_syntax(cmd: Command)
wenzelm@34784
   147
  {
wenzelm@34784
   148
    val (line1, line2) = model.lines_of_command(cmd)
wenzelm@34784
   149
    if (line2 >= text_area.getFirstLine &&
wenzelm@34784
   150
      line1 <= text_area.getFirstLine + text_area.getVisibleLines)
wenzelm@34784
   151
        update_delay()
wenzelm@34784
   152
  }
wenzelm@34784
   153
wenzelm@34784
   154
  private def invalidate_line(cmd: Command) =
wenzelm@34784
   155
  {
wenzelm@34784
   156
    val (start, stop) = model.lines_of_command(cmd)
wenzelm@34784
   157
    text_area.invalidateLineRange(start, stop)
wenzelm@34784
   158
wenzelm@34784
   159
    if (Isabelle.session.selected_state == cmd)
wenzelm@34784
   160
      Isabelle.session.selected_state = cmd
wenzelm@34784
   161
  }
wenzelm@34784
   162
wenzelm@34784
   163
  private def invalidate_all() =
wenzelm@34784
   164
    text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
wenzelm@34784
   165
      text_area.getLastPhysicalLine)
wenzelm@34784
   166
wenzelm@34784
   167
  private def encolor(gfx: Graphics2D,
wenzelm@34784
   168
    y: Int, height: Int, begin: Int, finish: Int, color: Color, fill: Boolean)
wenzelm@34784
   169
  {
wenzelm@34784
   170
    val start = text_area.offsetToXY(begin)
wenzelm@34784
   171
    val stop =
wenzelm@34784
   172
      if (finish < model.buffer.getLength) text_area.offsetToXY(finish)
wenzelm@34784
   173
      else {
wenzelm@34784
   174
        val p = text_area.offsetToXY(finish - 1)
wenzelm@34784
   175
        val metrics = text_area.getPainter.getFontMetrics
wenzelm@34784
   176
        p.x = p.x + (metrics.charWidth(' ') max metrics.getMaxAdvance)
wenzelm@34784
   177
        p
wenzelm@34784
   178
      }
wenzelm@34784
   179
wenzelm@34784
   180
    if (start != null && stop != null) {
wenzelm@34784
   181
      gfx.setColor(color)
wenzelm@34784
   182
      if (fill) gfx.fillRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   183
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   184
    }
wenzelm@34784
   185
  }
wenzelm@34784
   186
wenzelm@34784
   187
wenzelm@34784
   188
  /* overview of command status left of scrollbar */
wenzelm@34784
   189
wenzelm@34784
   190
  val overview = new JPanel(new BorderLayout)
wenzelm@34784
   191
  {
wenzelm@34784
   192
    private val WIDTH = 10
wenzelm@34784
   193
    private val HEIGHT = 2
wenzelm@34784
   194
wenzelm@34806
   195
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   196
wenzelm@34784
   197
    setRequestFocusEnabled(false)
wenzelm@34784
   198
wenzelm@34784
   199
    addMouseListener(new MouseAdapter {
wenzelm@34784
   200
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   201
        val line = y_to_line(event.getY)
wenzelm@34784
   202
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   203
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   204
      }
wenzelm@34784
   205
    })
wenzelm@34784
   206
wenzelm@34784
   207
    override def addNotify() {
wenzelm@34784
   208
      super.addNotify()
wenzelm@34784
   209
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   210
    }
immler@34403
   211
wenzelm@34784
   212
    override def removeNotify() {
wenzelm@34784
   213
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   214
      super.removeNotify
wenzelm@34784
   215
    }
wenzelm@34784
   216
wenzelm@34784
   217
    override def getToolTipText(event: MouseEvent): String =
wenzelm@34784
   218
    {
wenzelm@34784
   219
      val line = y_to_line(event.getY())
wenzelm@34784
   220
      if (line >= 0 && line < text_area.getLineCount) "<html><b>TODO:</b><br>Tooltip</html>"
wenzelm@34784
   221
      else ""
wenzelm@34784
   222
    }
wenzelm@34784
   223
wenzelm@34784
   224
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   225
    {
wenzelm@34784
   226
      super.paintComponent(gfx)
wenzelm@34784
   227
      val doc = model.current_document()
wenzelm@34784
   228
      val buffer = model.buffer
wenzelm@34784
   229
wenzelm@34784
   230
      for (command <- doc.commands) {
wenzelm@34784
   231
        val line1 = buffer.getLineOfOffset(model.to_current(doc, command.start(doc)))
wenzelm@34784
   232
        val line2 = buffer.getLineOfOffset(model.to_current(doc, command.stop(doc))) + 1
wenzelm@34784
   233
        val y = line_to_y(line1)
wenzelm@34784
   234
        val height = HEIGHT * (line2 - line1)
wenzelm@34784
   235
        gfx.setColor(Document_View.choose_color(command, doc))
wenzelm@34784
   236
        gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@34784
   237
      }
wenzelm@34784
   238
    }
wenzelm@34784
   239
wenzelm@34784
   240
    private def line_to_y(line: Int): Int =
wenzelm@34784
   241
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   242
wenzelm@34784
   243
    private def y_to_line(y: Int): Int =
wenzelm@34784
   244
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   245
  }
immler@34403
   246
wenzelm@34784
   247
wenzelm@34784
   248
  private val selected_state_controller = new CaretListener
wenzelm@34784
   249
  {
wenzelm@34784
   250
    override def caretUpdate(e: CaretEvent) {
wenzelm@34784
   251
      val doc = model.current_document()
wenzelm@34784
   252
      doc.command_at(e.getDot) match {
wenzelm@34784
   253
        case Some(cmd)
wenzelm@34784
   254
          if (doc.token_start(cmd.tokens.first) <= e.getDot &&
wenzelm@34784
   255
            Isabelle.session.selected_state != cmd) =>
wenzelm@34784
   256
          Isabelle.session.selected_state = cmd  // FIXME !?
wenzelm@34784
   257
        case _ =>
wenzelm@34784
   258
      }
wenzelm@34784
   259
    }
wenzelm@34784
   260
  }
wenzelm@34784
   261
wenzelm@34784
   262
wenzelm@34784
   263
  /* activation */
wenzelm@34784
   264
wenzelm@34808
   265
  private def activate()
wenzelm@34784
   266
  {
wenzelm@34784
   267
    text_area.addCaretListener(selected_state_controller)
wenzelm@34784
   268
    text_area.addLeftOfScrollBar(overview)
wenzelm@34784
   269
    text_area.getPainter.
wenzelm@34784
   270
      addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, text_area_extension)
wenzelm@34784
   271
    session.command_change += command_change_actor
wenzelm@34784
   272
  }
wenzelm@34784
   273
wenzelm@34808
   274
  private def deactivate()
wenzelm@34784
   275
  {
wenzelm@34784
   276
    session.command_change -= command_change_actor
wenzelm@34784
   277
    command_change_actor !? Exit
wenzelm@34784
   278
    text_area.getPainter.removeExtension(text_area_extension)
wenzelm@34784
   279
    text_area.removeLeftOfScrollBar(overview)
wenzelm@34784
   280
    text_area.removeCaretListener(selected_state_controller)
wenzelm@34784
   281
  }
immler@34403
   282
}