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