src/Tools/jEdit/src/document_view.scala
author wenzelm
Fri Sep 21 15:39:51 2012 +0200 (2012-09-21)
changeset 49492 2e3e7ea5ce8e
parent 49424 491363c6feb4
child 49843 afddf4e26fac
permissions -rw-r--r--
some support for hovering and sendback area;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/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@44379
    13
import scala.collection.mutable
wenzelm@45460
    14
import scala.collection.immutable.SortedMap
wenzelm@34784
    15
import scala.actors.Actor._
wenzelm@34784
    16
wenzelm@43520
    17
import java.lang.System
wenzelm@45744
    18
import java.text.BreakIterator
wenzelm@46572
    19
import java.awt.{Color, Graphics2D, Point}
wenzelm@34784
    20
import javax.swing.event.{CaretListener, CaretEvent}
wenzelm@34734
    21
wenzelm@49410
    22
import org.gjt.sp.jedit.{jEdit, Debug}
wenzelm@34709
    23
import org.gjt.sp.jedit.gui.RolloverButton
wenzelm@39176
    24
import org.gjt.sp.jedit.options.GutterOptionPane
wenzelm@47392
    25
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter}
wenzelm@43369
    26
import org.gjt.sp.jedit.syntax.{SyntaxStyle}
wenzelm@34784
    27
wenzelm@34784
    28
wenzelm@34784
    29
object Document_View
wenzelm@34784
    30
{
wenzelm@34784
    31
  /* document view of text area */
wenzelm@34784
    32
wenzelm@34784
    33
  private val key = new Object
wenzelm@34784
    34
wenzelm@39741
    35
  def apply(text_area: JEditTextArea): Option[Document_View] =
wenzelm@34784
    36
  {
wenzelm@38223
    37
    Swing_Thread.require()
wenzelm@34784
    38
    text_area.getClientProperty(key) match {
wenzelm@34784
    39
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    40
      case _ => None
wenzelm@34784
    41
    }
wenzelm@34784
    42
  }
wenzelm@34784
    43
wenzelm@39741
    44
  def exit(text_area: JEditTextArea)
wenzelm@34784
    45
  {
wenzelm@38223
    46
    Swing_Thread.require()
wenzelm@34788
    47
    apply(text_area) match {
wenzelm@39636
    48
      case None =>
wenzelm@34784
    49
      case Some(doc_view) =>
wenzelm@34784
    50
        doc_view.deactivate()
wenzelm@34784
    51
        text_area.putClientProperty(key, null)
wenzelm@34784
    52
    }
wenzelm@34784
    53
  }
wenzelm@43397
    54
wenzelm@43397
    55
  def init(model: Document_Model, text_area: JEditTextArea): Document_View =
wenzelm@43397
    56
  {
wenzelm@43397
    57
    exit(text_area)
wenzelm@43397
    58
    val doc_view = new Document_View(model, text_area)
wenzelm@43397
    59
    text_area.putClientProperty(key, doc_view)
wenzelm@43397
    60
    doc_view.activate()
wenzelm@43397
    61
    doc_view
wenzelm@43397
    62
  }
wenzelm@34784
    63
}
immler@34403
    64
wenzelm@34733
    65
wenzelm@43376
    66
class Document_View(val model: Document_Model, val text_area: JEditTextArea)
immler@34654
    67
{
wenzelm@34784
    68
  private val session = model.session
wenzelm@34784
    69
wenzelm@49411
    70
  def get_rendering(): Isabelle_Rendering =
wenzelm@49411
    71
    Isabelle_Rendering(model.snapshot(), Isabelle.options.value)
wenzelm@49411
    72
wenzelm@49492
    73
  val rich_text_area = new Rich_Text_Area(text_area.getView, text_area, get_rendering _, false)
wenzelm@49411
    74
immler@34403
    75
wenzelm@44379
    76
  /* perspective */
wenzelm@44379
    77
wenzelm@44379
    78
  def perspective(): Text.Perspective =
wenzelm@44379
    79
  {
wenzelm@44379
    80
    Swing_Thread.require()
wenzelm@44776
    81
    val buffer_range = model.buffer_range()
wenzelm@44473
    82
    Text.Perspective(
wenzelm@44379
    83
      for {
wenzelm@44379
    84
        i <- 0 until text_area.getVisibleLines
wenzelm@46997
    85
        start = text_area.getScreenLineStartOffset(i)
wenzelm@46997
    86
        stop = text_area.getScreenLineEndOffset(i)
wenzelm@44379
    87
        if start >= 0 && stop >= 0
wenzelm@46997
    88
        range <- buffer_range.try_restrict(Text.Range(start, stop))
wenzelm@44583
    89
        if !range.is_singularity
wenzelm@44379
    90
      }
wenzelm@44583
    91
      yield range)
wenzelm@44379
    92
  }
wenzelm@44379
    93
wenzelm@44437
    94
  private def update_perspective = new TextAreaExtension
wenzelm@44437
    95
  {
wenzelm@44437
    96
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@44437
    97
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@44437
    98
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@44437
    99
    {
wenzelm@49424
   100
      // no robust_body
wenzelm@44437
   101
      model.update_perspective()
wenzelm@44437
   102
    }
wenzelm@44437
   103
  }
wenzelm@44437
   104
wenzelm@44379
   105
wenzelm@49411
   106
  /* gutter */
immler@34513
   107
wenzelm@42825
   108
  private val gutter_painter = new TextAreaExtension
wenzelm@39175
   109
  {
wenzelm@39175
   110
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@39175
   111
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@39175
   112
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@39175
   113
    {
wenzelm@49411
   114
      rich_text_area.robust_body(()) {
wenzelm@46920
   115
        Swing_Thread.assert()
wenzelm@46920
   116
wenzelm@43404
   117
        val gutter = text_area.getGutter
wenzelm@43404
   118
        val width = GutterOptionPane.getSelectionAreaWidth
wenzelm@43404
   119
        val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
wenzelm@43404
   120
        val FOLD_MARKER_SIZE = 12
wenzelm@43419
   121
wenzelm@43404
   122
        if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
wenzelm@49408
   123
          val buffer = model.buffer
wenzelm@49408
   124
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@49410
   125
            val rendering = get_rendering()
wenzelm@49356
   126
wenzelm@43404
   127
            for (i <- 0 until physical_lines.length) {
wenzelm@43404
   128
              if (physical_lines(i) != -1) {
wenzelm@49408
   129
                val line_range = JEdit_Lib.proper_line_range(buffer, start(i), end(i))
wenzelm@43419
   130
wenzelm@43404
   131
                // gutter icons
wenzelm@49356
   132
                rendering.gutter_message(line_range) match {
wenzelm@46178
   133
                  case Some(icon) =>
wenzelm@46227
   134
                    val x0 = (FOLD_MARKER_SIZE + width - border_width - icon.getIconWidth) max 10
wenzelm@46227
   135
                    val y0 = y + i * line_height + (((line_height - icon.getIconHeight) / 2) max 0)
wenzelm@46227
   136
                    icon.paintIcon(gutter, gfx, x0, y0)
wenzelm@46178
   137
                  case None =>
wenzelm@43404
   138
                }
wenzelm@39175
   139
              }
wenzelm@39175
   140
            }
wenzelm@39175
   141
          }
wenzelm@39175
   142
        }
wenzelm@39175
   143
      }
wenzelm@39175
   144
    }
wenzelm@39175
   145
  }
wenzelm@39175
   146
wenzelm@39175
   147
wenzelm@37129
   148
  /* caret handling */
wenzelm@34810
   149
wenzelm@46740
   150
  private val delay_caret_update =
wenzelm@49288
   151
    Swing_Thread.delay_last(Time.seconds(Isabelle.options.real("editor_input_delay"))) {
wenzelm@44805
   152
      session.caret_focus.event(Session.Caret_Focus)
wenzelm@34810
   153
    }
wenzelm@46740
   154
wenzelm@46740
   155
  private val caret_listener = new CaretListener {
wenzelm@49195
   156
    override def caretUpdate(e: CaretEvent) { delay_caret_update.invoke() }
wenzelm@34810
   157
  }
wenzelm@34810
   158
wenzelm@34810
   159
wenzelm@46572
   160
  /* text status overview left of scrollbar */
wenzelm@34784
   161
wenzelm@47993
   162
  private object overview extends Text_Overview(this)
wenzelm@46572
   163
  {
wenzelm@46571
   164
    val delay_repaint =
wenzelm@49288
   165
      Swing_Thread.delay_first(
wenzelm@49288
   166
        Time.seconds(Isabelle.options.real("editor_update_delay"))) { repaint() }
wenzelm@34784
   167
  }
immler@34403
   168
wenzelm@34784
   169
wenzelm@39740
   170
  /* main actor */
wenzelm@39740
   171
wenzelm@39740
   172
  private val main_actor = actor {
wenzelm@39740
   173
    loop {
wenzelm@39740
   174
      react {
wenzelm@49196
   175
        case _: Session.Raw_Edits =>
wenzelm@49196
   176
          Swing_Thread.later {
wenzelm@49288
   177
            overview.delay_repaint.postpone(
wenzelm@49288
   178
              Time.seconds(Isabelle.options.real("editor_input_delay")))
wenzelm@49196
   179
          }
wenzelm@49196
   180
wenzelm@44608
   181
        case changed: Session.Commands_Changed =>
wenzelm@39740
   182
          val buffer = model.buffer
wenzelm@46918
   183
          Swing_Thread.later {
wenzelm@49406
   184
            JEdit_Lib.buffer_lock(buffer) {
wenzelm@46918
   185
              if (model.buffer == text_area.getBuffer) {
wenzelm@47027
   186
                val snapshot = model.snapshot()
wenzelm@39740
   187
wenzelm@47027
   188
                if (changed.assignment ||
wenzelm@46918
   189
                    (changed.nodes.contains(model.name) &&
wenzelm@46918
   190
                     changed.commands.exists(snapshot.node.commands.contains)))
wenzelm@49196
   191
                  Swing_Thread.later { overview.delay_repaint.invoke() }
wenzelm@39740
   192
wenzelm@49408
   193
                JEdit_Lib.visible_range(text_area) match {
wenzelm@46918
   194
                  case Some(visible) =>
wenzelm@49408
   195
                    if (changed.assignment) JEdit_Lib.invalidate_range(text_area, visible)
wenzelm@46918
   196
                    else {
wenzelm@46918
   197
                      val visible_cmds =
wenzelm@46918
   198
                        snapshot.node.command_range(snapshot.revert(visible)).map(_._1)
wenzelm@46918
   199
                      if (visible_cmds.exists(changed.commands)) {
wenzelm@46918
   200
                        for {
wenzelm@46918
   201
                          line <- 0 until text_area.getVisibleLines
wenzelm@46997
   202
                          start = text_area.getScreenLineStartOffset(line) if start >= 0
wenzelm@46997
   203
                          end = text_area.getScreenLineEndOffset(line) if end >= 0
wenzelm@49408
   204
                          range = JEdit_Lib.proper_line_range(buffer, start, end)
wenzelm@46997
   205
                          line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
wenzelm@46918
   206
                          if line_cmds.exists(changed.commands)
wenzelm@46918
   207
                        } text_area.invalidateScreenLineRange(line, line)
wenzelm@46918
   208
                      }
wenzelm@46918
   209
                    }
wenzelm@46918
   210
                  case None =>
wenzelm@46583
   211
                }
wenzelm@46918
   212
              }
wenzelm@39740
   213
            }
wenzelm@39740
   214
          }
wenzelm@39740
   215
wenzelm@39740
   216
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@39740
   217
      }
wenzelm@39740
   218
    }
wenzelm@39740
   219
  }
wenzelm@39740
   220
wenzelm@39740
   221
wenzelm@34784
   222
  /* activation */
wenzelm@34784
   223
wenzelm@34808
   224
  private def activate()
wenzelm@34784
   225
  {
wenzelm@42825
   226
    val painter = text_area.getPainter
wenzelm@49410
   227
wenzelm@44437
   228
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, update_perspective)
wenzelm@49411
   229
    rich_text_area.activate()
wenzelm@42825
   230
    text_area.getGutter.addExtension(gutter_painter)
wenzelm@34810
   231
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   232
    text_area.addLeftOfScrollBar(overview)
wenzelm@49196
   233
    session.raw_edits += main_actor
wenzelm@39740
   234
    session.commands_changed += main_actor
wenzelm@34784
   235
  }
wenzelm@34784
   236
wenzelm@34808
   237
  private def deactivate()
wenzelm@34784
   238
  {
wenzelm@42825
   239
    val painter = text_area.getPainter
wenzelm@49410
   240
wenzelm@49196
   241
    session.raw_edits -= main_actor
wenzelm@39740
   242
    session.commands_changed -= main_actor
wenzelm@49195
   243
    text_area.removeCaretListener(caret_listener); delay_caret_update.revoke()
wenzelm@49195
   244
    text_area.removeLeftOfScrollBar(overview); overview.delay_repaint.revoke()
wenzelm@42825
   245
    text_area.getGutter.removeExtension(gutter_painter)
wenzelm@49411
   246
    rich_text_area.deactivate()
wenzelm@44437
   247
    painter.removeExtension(update_perspective)
wenzelm@34784
   248
  }
wenzelm@42825
   249
}