src/Tools/jEdit/src/jedit/document_view.scala
author wenzelm
Tue Dec 15 19:50:55 2009 +0100 (2009-12-15)
changeset 34784 02959dcea756
parent 34777 src/Tools/jEdit/src/jedit/document_overview.scala@91d6089cef88
child 34788 3779c54a2d21
permissions -rw-r--r--
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
incorporate Document_Overview into Document_View;
eliminated Plugin.mapping in favour of direct association via generic properties;
support several views per model;
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@34784
    31
      case Command.Status.FAILED => new Color(255, 106, 106)
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@34784
    43
    val doc_view = new Document_View(model, text_area)
wenzelm@34784
    44
    text_area.putClientProperty(key, doc_view)
wenzelm@34784
    45
    doc_view.activate()
wenzelm@34784
    46
    doc_view
wenzelm@34784
    47
  }
wenzelm@34784
    48
wenzelm@34784
    49
  def get(text_area: TextArea): Option[Document_View] =
wenzelm@34784
    50
  {
wenzelm@34784
    51
    text_area.getClientProperty(key) match {
wenzelm@34784
    52
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    53
      case _ => None
wenzelm@34784
    54
    }
wenzelm@34784
    55
  }
wenzelm@34784
    56
wenzelm@34784
    57
  def exit(text_area: TextArea)
wenzelm@34784
    58
  {
wenzelm@34784
    59
    get(text_area) match {
wenzelm@34784
    60
      case None => error("No document view for text area: " + text_area)
wenzelm@34784
    61
      case Some(doc_view) =>
wenzelm@34784
    62
        doc_view.deactivate()
wenzelm@34784
    63
        text_area.putClientProperty(key, null)
wenzelm@34784
    64
    }
wenzelm@34784
    65
  }
wenzelm@34784
    66
}
immler@34403
    67
wenzelm@34733
    68
wenzelm@34784
    69
class Document_View(model: Document_Model, text_area: TextArea)
immler@34654
    70
{
wenzelm@34784
    71
  private val session = model.session
wenzelm@34784
    72
immler@34403
    73
wenzelm@34784
    74
  /* command change actor */
wenzelm@34784
    75
wenzelm@34784
    76
  private case object Exit
immler@34403
    77
wenzelm@34784
    78
  private val command_change_actor = actor {
wenzelm@34784
    79
    loop {
wenzelm@34784
    80
      react {
wenzelm@34784
    81
        case command: Command =>
wenzelm@34784
    82
          if (model.current_document().commands.contains(command))
wenzelm@34784
    83
            Swing_Thread.now {
wenzelm@34784
    84
              update_syntax(command)
wenzelm@34784
    85
              invalidate_line(command)
wenzelm@34784
    86
              overview.repaint()
wenzelm@34784
    87
            }
wenzelm@34784
    88
wenzelm@34784
    89
        case Exit => reply(()); exit()
wenzelm@34784
    90
wenzelm@34784
    91
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@34784
    92
      }
immler@34403
    93
    }
immler@34678
    94
  }
immler@34403
    95
immler@34403
    96
wenzelm@34784
    97
  /* text_area_extension */
wenzelm@34784
    98
wenzelm@34784
    99
  private val text_area_extension = new TextAreaExtension
immler@34678
   100
  {
wenzelm@34784
   101
    override def paintValidLine(gfx: Graphics2D,
wenzelm@34784
   102
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@34784
   103
    {
wenzelm@34784
   104
      val document = model.current_document()
wenzelm@34784
   105
      def from_current(pos: Int) = model.from_current(document, pos)
wenzelm@34784
   106
      def to_current(pos: Int) = model.to_current(document, pos)
wenzelm@34784
   107
      val saved_color = gfx.getColor
wenzelm@34784
   108
wenzelm@34784
   109
      val metrics = text_area.getPainter.getFontMetrics
immler@34403
   110
wenzelm@34784
   111
      // encolor phase
wenzelm@34784
   112
      var cmd = document.command_at(from_current(start))
wenzelm@34784
   113
      while (cmd.isDefined && cmd.get.start(document) < end) {
wenzelm@34784
   114
        val e = cmd.get
wenzelm@34784
   115
        val begin = start max to_current(e.start(document))
wenzelm@34784
   116
        val finish = (end - 1) min to_current(e.stop(document))
wenzelm@34784
   117
        encolor(gfx, y, metrics.getHeight, begin, finish,
wenzelm@34784
   118
          Document_View.choose_color(e, document), true)
wenzelm@34784
   119
        cmd = document.commands.next(e)
wenzelm@34784
   120
      }
immler@34678
   121
wenzelm@34784
   122
      gfx.setColor(saved_color)
wenzelm@34784
   123
    }
wenzelm@34784
   124
wenzelm@34784
   125
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   126
    {
wenzelm@34784
   127
      val document = model.current_document()
wenzelm@34784
   128
      val offset = model.from_current(document, text_area.xyToOffset(x, y))
wenzelm@34784
   129
      document.command_at(offset) match {
wenzelm@34784
   130
        case Some(cmd) =>
wenzelm@34784
   131
          document.token_start(cmd.tokens.first)
wenzelm@34784
   132
          cmd.type_at(document, offset - cmd.start(document)).getOrElse(null)
wenzelm@34784
   133
        case None => null
wenzelm@34784
   134
      }
wenzelm@34734
   135
    }
immler@34678
   136
  }
immler@34513
   137
wenzelm@34784
   138
wenzelm@34784
   139
  /* (re)painting */
wenzelm@34784
   140
wenzelm@34784
   141
  private val update_delay = Swing_Thread.delay_first(500) { model.buffer.propertiesChanged() }
wenzelm@34784
   142
wenzelm@34784
   143
  private def update_syntax(cmd: Command)
wenzelm@34784
   144
  {
wenzelm@34784
   145
    val (line1, line2) = model.lines_of_command(cmd)
wenzelm@34784
   146
    if (line2 >= text_area.getFirstLine &&
wenzelm@34784
   147
      line1 <= text_area.getFirstLine + text_area.getVisibleLines)
wenzelm@34784
   148
        update_delay()
wenzelm@34784
   149
  }
wenzelm@34784
   150
wenzelm@34784
   151
  private def invalidate_line(cmd: Command) =
wenzelm@34784
   152
  {
wenzelm@34784
   153
    val (start, stop) = model.lines_of_command(cmd)
wenzelm@34784
   154
    text_area.invalidateLineRange(start, stop)
wenzelm@34784
   155
wenzelm@34784
   156
    if (Isabelle.session.selected_state == cmd)
wenzelm@34784
   157
      Isabelle.session.selected_state = cmd
wenzelm@34784
   158
  }
wenzelm@34784
   159
wenzelm@34784
   160
  private def invalidate_all() =
wenzelm@34784
   161
    text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
wenzelm@34784
   162
      text_area.getLastPhysicalLine)
wenzelm@34784
   163
wenzelm@34784
   164
  private def encolor(gfx: Graphics2D,
wenzelm@34784
   165
    y: Int, height: Int, begin: Int, finish: Int, color: Color, fill: Boolean)
wenzelm@34784
   166
  {
wenzelm@34784
   167
    val start = text_area.offsetToXY(begin)
wenzelm@34784
   168
    val stop =
wenzelm@34784
   169
      if (finish < model.buffer.getLength) text_area.offsetToXY(finish)
wenzelm@34784
   170
      else {
wenzelm@34784
   171
        val p = text_area.offsetToXY(finish - 1)
wenzelm@34784
   172
        val metrics = text_area.getPainter.getFontMetrics
wenzelm@34784
   173
        p.x = p.x + (metrics.charWidth(' ') max metrics.getMaxAdvance)
wenzelm@34784
   174
        p
wenzelm@34784
   175
      }
wenzelm@34784
   176
wenzelm@34784
   177
    if (start != null && stop != null) {
wenzelm@34784
   178
      gfx.setColor(color)
wenzelm@34784
   179
      if (fill) gfx.fillRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   180
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
wenzelm@34784
   181
    }
wenzelm@34784
   182
  }
wenzelm@34784
   183
wenzelm@34784
   184
wenzelm@34784
   185
  /* overview of command status left of scrollbar */
wenzelm@34784
   186
wenzelm@34784
   187
  val overview = new JPanel(new BorderLayout)
wenzelm@34784
   188
  {
wenzelm@34784
   189
    private val WIDTH = 10
wenzelm@34784
   190
    private val HEIGHT = 2
wenzelm@34784
   191
wenzelm@34784
   192
    setRequestFocusEnabled(false)
wenzelm@34784
   193
wenzelm@34784
   194
    addMouseListener(new MouseAdapter {
wenzelm@34784
   195
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   196
        val line = y_to_line(event.getY)
wenzelm@34784
   197
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   198
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   199
      }
wenzelm@34784
   200
    })
wenzelm@34784
   201
wenzelm@34784
   202
    override def addNotify() {
wenzelm@34784
   203
      super.addNotify()
wenzelm@34784
   204
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   205
    }
immler@34403
   206
wenzelm@34784
   207
    override def removeNotify() {
wenzelm@34784
   208
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   209
      super.removeNotify
wenzelm@34784
   210
    }
wenzelm@34784
   211
wenzelm@34784
   212
    override def getToolTipText(event: MouseEvent): String =
wenzelm@34784
   213
    {
wenzelm@34784
   214
      val line = y_to_line(event.getY())
wenzelm@34784
   215
      if (line >= 0 && line < text_area.getLineCount) "<html><b>TODO:</b><br>Tooltip</html>"
wenzelm@34784
   216
      else ""
wenzelm@34784
   217
    }
wenzelm@34784
   218
wenzelm@34784
   219
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   220
    {
wenzelm@34784
   221
      super.paintComponent(gfx)
wenzelm@34784
   222
      val doc = model.current_document()
wenzelm@34784
   223
      val buffer = model.buffer
wenzelm@34784
   224
wenzelm@34784
   225
      for (command <- doc.commands) {
wenzelm@34784
   226
        val line1 = buffer.getLineOfOffset(model.to_current(doc, command.start(doc)))
wenzelm@34784
   227
        val line2 = buffer.getLineOfOffset(model.to_current(doc, command.stop(doc))) + 1
wenzelm@34784
   228
        val y = line_to_y(line1)
wenzelm@34784
   229
        val height = HEIGHT * (line2 - line1)
wenzelm@34784
   230
        gfx.setColor(Document_View.choose_color(command, doc))
wenzelm@34784
   231
        gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@34784
   232
      }
wenzelm@34784
   233
    }
wenzelm@34784
   234
wenzelm@34784
   235
    override def getPreferredSize = new Dimension(WIDTH, 0)
wenzelm@34784
   236
wenzelm@34784
   237
    private def line_to_y(line: Int): Int =
wenzelm@34784
   238
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   239
wenzelm@34784
   240
    private def y_to_line(y: Int): Int =
wenzelm@34784
   241
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   242
  }
immler@34403
   243
wenzelm@34784
   244
wenzelm@34784
   245
  private val selected_state_controller = new CaretListener
wenzelm@34784
   246
  {
wenzelm@34784
   247
    override def caretUpdate(e: CaretEvent) {
wenzelm@34784
   248
      val doc = model.current_document()
wenzelm@34784
   249
      doc.command_at(e.getDot) match {
wenzelm@34784
   250
        case Some(cmd)
wenzelm@34784
   251
          if (doc.token_start(cmd.tokens.first) <= e.getDot &&
wenzelm@34784
   252
            Isabelle.session.selected_state != cmd) =>
wenzelm@34784
   253
          Isabelle.session.selected_state = cmd  // FIXME !?
wenzelm@34784
   254
        case _ =>
wenzelm@34784
   255
      }
wenzelm@34784
   256
    }
wenzelm@34784
   257
  }
wenzelm@34784
   258
wenzelm@34784
   259
wenzelm@34784
   260
  /* activation */
wenzelm@34784
   261
wenzelm@34784
   262
  def activate()
wenzelm@34784
   263
  {
wenzelm@34784
   264
    text_area.addCaretListener(selected_state_controller)
wenzelm@34784
   265
    text_area.addLeftOfScrollBar(overview)
wenzelm@34784
   266
    text_area.getPainter.
wenzelm@34784
   267
      addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, text_area_extension)
wenzelm@34784
   268
    session.command_change += command_change_actor
wenzelm@34784
   269
  }
wenzelm@34784
   270
wenzelm@34784
   271
  def deactivate()
wenzelm@34784
   272
  {
wenzelm@34784
   273
    session.command_change -= command_change_actor
wenzelm@34784
   274
    command_change_actor !? Exit
wenzelm@34784
   275
    text_area.getPainter.removeExtension(text_area_extension)
wenzelm@34784
   276
    text_area.removeLeftOfScrollBar(overview)
wenzelm@34784
   277
    text_area.removeCaretListener(selected_state_controller)
wenzelm@34784
   278
  }
immler@34403
   279
}