src/Tools/jEdit/src/document_view.scala
author wenzelm
Thu Aug 29 10:01:59 2013 +0200 (2013-08-29)
changeset 53272 0dfd78ff7696
parent 53245 301b69957af7
child 53274 1760c01f1c78
permissions -rw-r--r--
more abstract Completion_Popup.Text_Area;
more uniform font size;
     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 java.awt.event.KeyEvent
    21 import javax.swing.event.{CaretListener, CaretEvent}
    22 
    23 import org.gjt.sp.jedit.{jEdit, Debug}
    24 import org.gjt.sp.jedit.gui.RolloverButton
    25 import org.gjt.sp.jedit.options.GutterOptionPane
    26 import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter}
    27 import org.gjt.sp.jedit.syntax.SyntaxStyle
    28 
    29 
    30 object Document_View
    31 {
    32   /* document view of text area */
    33 
    34   private val key = new Object
    35 
    36   def apply(text_area: JEditTextArea): Option[Document_View] =
    37   {
    38     Swing_Thread.require()
    39     text_area.getClientProperty(key) match {
    40       case doc_view: Document_View => Some(doc_view)
    41       case _ => None
    42     }
    43   }
    44 
    45   def exit(text_area: JEditTextArea)
    46   {
    47     Swing_Thread.require()
    48     apply(text_area) match {
    49       case None =>
    50       case Some(doc_view) =>
    51         doc_view.deactivate()
    52         text_area.putClientProperty(key, null)
    53     }
    54   }
    55 
    56   def init(model: Document_Model, text_area: JEditTextArea): Document_View =
    57   {
    58     exit(text_area)
    59     val doc_view = new Document_View(model, text_area)
    60     text_area.putClientProperty(key, doc_view)
    61     doc_view.activate()
    62     doc_view
    63   }
    64 }
    65 
    66 
    67 class Document_View(val model: Document_Model, val text_area: JEditTextArea)
    68 {
    69   private val session = model.session
    70 
    71   def get_rendering(): Rendering = Rendering(model.snapshot(), PIDE.options.value)
    72 
    73   val rich_text_area =
    74     new Rich_Text_Area(text_area.getView, text_area, get_rendering _, () => (),
    75       caret_visible = true, enable_hovering = false)
    76 
    77 
    78   /* perspective */
    79 
    80   def perspective(): Text.Perspective =
    81   {
    82     Swing_Thread.require()
    83     val buffer_range = JEdit_Lib.buffer_range(model.buffer)
    84     Text.Perspective(
    85       for {
    86         i <- 0 until text_area.getVisibleLines
    87         start = text_area.getScreenLineStartOffset(i)
    88         stop = text_area.getScreenLineEndOffset(i)
    89         if start >= 0 && stop >= 0
    90         range <- buffer_range.try_restrict(Text.Range(start, stop))
    91         if !range.is_singularity
    92       }
    93       yield range)
    94   }
    95 
    96   private def update_perspective = new TextAreaExtension
    97   {
    98     override def paintScreenLineRange(gfx: Graphics2D,
    99       first_line: Int, last_line: Int, physical_lines: Array[Int],
   100       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
   101     {
   102       // no robust_body
   103       model.update_perspective()
   104     }
   105   }
   106 
   107 
   108   /* gutter */
   109 
   110   private val gutter_painter = new TextAreaExtension
   111   {
   112     override def paintScreenLineRange(gfx: Graphics2D,
   113       first_line: Int, last_line: Int, physical_lines: Array[Int],
   114       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
   115     {
   116       rich_text_area.robust_body(()) {
   117         Swing_Thread.assert()
   118 
   119         val gutter = text_area.getGutter
   120         val width = GutterOptionPane.getSelectionAreaWidth
   121         val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
   122         val FOLD_MARKER_SIZE = 12
   123 
   124         if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
   125           val buffer = model.buffer
   126           JEdit_Lib.buffer_lock(buffer) {
   127             val rendering = get_rendering()
   128 
   129             for (i <- 0 until physical_lines.length) {
   130               if (physical_lines(i) != -1) {
   131                 val line_range = Text.Range(start(i), end(i))
   132 
   133                 // gutter icons
   134                 rendering.gutter_message(line_range) match {
   135                   case Some(icon) =>
   136                     val x0 = (FOLD_MARKER_SIZE + width - border_width - icon.getIconWidth) max 10
   137                     val y0 = y + i * line_height + (((line_height - icon.getIconHeight) / 2) max 0)
   138                     icon.paintIcon(gutter, gfx, x0, y0)
   139                   case None =>
   140                 }
   141               }
   142             }
   143           }
   144         }
   145       }
   146     }
   147   }
   148 
   149 
   150   /* key listener */
   151 
   152   private val completion_popup =
   153     Completion_Popup.Text_Area(text_area, PIDE.get_recent_syntax)
   154 
   155   def dismissed_popups(): Boolean = completion_popup.dismissed()
   156 
   157   private val key_listener =
   158     JEdit_Lib.key_listener(
   159       key_pressed = (evt: KeyEvent) =>
   160         {
   161           if (evt.getKeyCode == KeyEvent.VK_ESCAPE && PIDE.dismissed_popups(text_area.getView))
   162             evt.consume
   163         },
   164       key_typed = completion_popup.input _
   165     )
   166 
   167 
   168   /* caret handling */
   169 
   170   private val delay_caret_update =
   171     Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) {
   172       session.caret_focus.event(Session.Caret_Focus)
   173     }
   174 
   175   private val caret_listener = new CaretListener {
   176     override def caretUpdate(e: CaretEvent) { delay_caret_update.invoke() }
   177   }
   178 
   179 
   180   /* text status overview left of scrollbar */
   181 
   182   private object overview extends Text_Overview(this)
   183   {
   184     val delay_repaint =
   185       Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { repaint() }
   186   }
   187 
   188 
   189   /* main actor */
   190 
   191   private val main_actor = actor {
   192     loop {
   193       react {
   194         case _: Session.Raw_Edits =>
   195           Swing_Thread.later {
   196             overview.delay_repaint.postpone(PIDE.options.seconds("editor_input_delay"))
   197           }
   198 
   199         case changed: Session.Commands_Changed =>
   200           val buffer = model.buffer
   201           Swing_Thread.later {
   202             JEdit_Lib.buffer_lock(buffer) {
   203               if (model.buffer == text_area.getBuffer) {
   204                 val snapshot = model.snapshot()
   205 
   206                 if (changed.assignment ||
   207                     (changed.nodes.contains(model.node_name) &&
   208                      changed.commands.exists(snapshot.node.commands.contains)))
   209                   Swing_Thread.later { overview.delay_repaint.invoke() }
   210 
   211                 val visible_lines = text_area.getVisibleLines
   212                 if (visible_lines > 0) {
   213                   if (changed.assignment) text_area.invalidateScreenLineRange(0, visible_lines)
   214                   else {
   215                     val visible_range = JEdit_Lib.visible_range(text_area).get
   216                     val visible_cmds =
   217                       snapshot.node.command_range(snapshot.revert(visible_range)).map(_._1)
   218                     if (visible_cmds.exists(changed.commands)) {
   219                       for {
   220                         line <- 0 until visible_lines
   221                         start = text_area.getScreenLineStartOffset(line) if start >= 0
   222                         end = text_area.getScreenLineEndOffset(line) if end >= 0
   223                         range = Text.Range(start, end)
   224                         line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
   225                         if line_cmds.exists(changed.commands)
   226                       } text_area.invalidateScreenLineRange(line, line)
   227                     }
   228                   }
   229                 }
   230               }
   231             }
   232           }
   233 
   234         case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
   235       }
   236     }
   237   }
   238 
   239 
   240   /* activation */
   241 
   242   private def activate()
   243   {
   244     val painter = text_area.getPainter
   245 
   246     painter.addExtension(TextAreaPainter.LOWEST_LAYER, update_perspective)
   247     rich_text_area.activate()
   248     text_area.getGutter.addExtension(gutter_painter)
   249     text_area.addKeyListener(key_listener)
   250     text_area.addCaretListener(caret_listener)
   251     text_area.addLeftOfScrollBar(overview)
   252     session.raw_edits += main_actor
   253     session.commands_changed += main_actor
   254   }
   255 
   256   private def deactivate()
   257   {
   258     val painter = text_area.getPainter
   259 
   260     session.raw_edits -= main_actor
   261     session.commands_changed -= main_actor
   262     text_area.removeLeftOfScrollBar(overview); overview.delay_repaint.revoke()
   263     text_area.removeCaretListener(caret_listener); delay_caret_update.revoke()
   264     text_area.removeKeyListener(key_listener)
   265     text_area.getGutter.removeExtension(gutter_painter)
   266     rich_text_area.deactivate()
   267     painter.removeExtension(update_perspective)
   268   }
   269 }