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