src/Tools/jEdit/src/document_view.scala
author wenzelm
Sat Dec 01 19:51:43 2012 +0100 (2012-12-01)
changeset 50306 b655d2d0406d
parent 50207 54be125d8cdc
child 50363 2f8dc9e65401
permissions -rw-r--r--
updated to jedit-5.0.0;
     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(): Rendering = Rendering(model.snapshot(), PIDE.options.value)
    71 
    72   val rich_text_area =
    73     new Rich_Text_Area(text_area.getView, text_area, get_rendering _,
    74       caret_visible = true, hovering = false)
    75 
    76 
    77   /* perspective */
    78 
    79   def perspective(): Text.Perspective =
    80   {
    81     Swing_Thread.require()
    82     val buffer_range = model.buffer_range()
    83     Text.Perspective(
    84       for {
    85         i <- 0 until text_area.getVisibleLines
    86         start = text_area.getScreenLineStartOffset(i)
    87         stop = text_area.getScreenLineEndOffset(i)
    88         if start >= 0 && stop >= 0
    89         range <- buffer_range.try_restrict(Text.Range(start, stop))
    90         if !range.is_singularity
    91       }
    92       yield range)
    93   }
    94 
    95   private def update_perspective = new TextAreaExtension
    96   {
    97     override def paintScreenLineRange(gfx: Graphics2D,
    98       first_line: Int, last_line: Int, physical_lines: Array[Int],
    99       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
   100     {
   101       // no robust_body
   102       model.update_perspective()
   103     }
   104   }
   105 
   106 
   107   /* gutter */
   108 
   109   private val gutter_painter = new TextAreaExtension
   110   {
   111     override def paintScreenLineRange(gfx: Graphics2D,
   112       first_line: Int, last_line: Int, physical_lines: Array[Int],
   113       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
   114     {
   115       rich_text_area.robust_body(()) {
   116         Swing_Thread.assert()
   117 
   118         val gutter = text_area.getGutter
   119         val width = GutterOptionPane.getSelectionAreaWidth
   120         val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
   121         val FOLD_MARKER_SIZE = 12
   122 
   123         if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
   124           val buffer = model.buffer
   125           JEdit_Lib.buffer_lock(buffer) {
   126             val rendering = get_rendering()
   127 
   128             for (i <- 0 until physical_lines.length) {
   129               if (physical_lines(i) != -1) {
   130                 val line_range = Text.Range(start(i), end(i))
   131 
   132                 // gutter icons
   133                 rendering.gutter_message(line_range) match {
   134                   case Some(icon) =>
   135                     val x0 = (FOLD_MARKER_SIZE + width - border_width - icon.getIconWidth) max 10
   136                     val y0 = y + i * line_height + (((line_height - icon.getIconHeight) / 2) max 0)
   137                     icon.paintIcon(gutter, gfx, x0, y0)
   138                   case None =>
   139                 }
   140               }
   141             }
   142           }
   143         }
   144       }
   145     }
   146   }
   147 
   148 
   149   /* caret handling */
   150 
   151   private val delay_caret_update =
   152     Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) {
   153       session.caret_focus.event(Session.Caret_Focus)
   154     }
   155 
   156   private val caret_listener = new CaretListener {
   157     override def caretUpdate(e: CaretEvent) { delay_caret_update.invoke() }
   158   }
   159 
   160 
   161   /* text status overview left of scrollbar */
   162 
   163   private object overview extends Text_Overview(this)
   164   {
   165     val delay_repaint =
   166       Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { repaint() }
   167   }
   168 
   169 
   170   /* main actor */
   171 
   172   private val main_actor = actor {
   173     loop {
   174       react {
   175         case _: Session.Raw_Edits =>
   176           Swing_Thread.later {
   177             overview.delay_repaint.postpone(PIDE.options.seconds("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 = Text.Range(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 }