src/Tools/jEdit/src/document_view.scala
author wenzelm
Mon Sep 17 18:06:34 2012 +0200 (2012-09-17)
changeset 49408 3cfc114acd05
parent 49407 215ba6884bdf
child 49410 34acbcc33adf
permissions -rw-r--r--
tuned signature;
     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.{MouseMotionAdapter, MouseAdapter, MouseEvent,
    21   FocusAdapter, FocusEvent, WindowEvent, WindowAdapter}
    22 import javax.swing.event.{CaretListener, CaretEvent}
    23 
    24 import org.gjt.sp.util.Log
    25 
    26 import org.gjt.sp.jedit.{jEdit, OperatingSystem, Debug}
    27 import org.gjt.sp.jedit.gui.RolloverButton
    28 import org.gjt.sp.jedit.options.GutterOptionPane
    29 import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter}
    30 import org.gjt.sp.jedit.syntax.{SyntaxStyle}
    31 
    32 
    33 object Document_View
    34 {
    35   /* document view of text area */
    36 
    37   private val key = new Object
    38 
    39   def apply(text_area: JEditTextArea): Option[Document_View] =
    40   {
    41     Swing_Thread.require()
    42     text_area.getClientProperty(key) match {
    43       case doc_view: Document_View => Some(doc_view)
    44       case _ => None
    45     }
    46   }
    47 
    48   def exit(text_area: JEditTextArea)
    49   {
    50     Swing_Thread.require()
    51     apply(text_area) match {
    52       case None =>
    53       case Some(doc_view) =>
    54         doc_view.deactivate()
    55         text_area.putClientProperty(key, null)
    56     }
    57   }
    58 
    59   def init(model: Document_Model, text_area: JEditTextArea): Document_View =
    60   {
    61     exit(text_area)
    62     val doc_view = new Document_View(model, text_area)
    63     text_area.putClientProperty(key, doc_view)
    64     doc_view.activate()
    65     doc_view
    66   }
    67 }
    68 
    69 
    70 class Document_View(val model: Document_Model, val text_area: JEditTextArea)
    71 {
    72   private val session = model.session
    73 
    74 
    75   /* robust extension body */
    76 
    77   def robust_body[A](default: A)(body: => A): A =
    78   {
    79     try {
    80       Swing_Thread.require()
    81       if (model.buffer == text_area.getBuffer) body
    82       else {
    83         Log.log(Log.ERROR, this, ERROR("Inconsistent document model"))
    84         default
    85       }
    86     }
    87     catch { case t: Throwable => Log.log(Log.ERROR, this, t); default }
    88   }
    89 
    90 
    91   /* perspective */
    92 
    93   def perspective(): Text.Perspective =
    94   {
    95     Swing_Thread.require()
    96     val buffer_range = model.buffer_range()
    97     Text.Perspective(
    98       for {
    99         i <- 0 until text_area.getVisibleLines
   100         start = text_area.getScreenLineStartOffset(i)
   101         stop = text_area.getScreenLineEndOffset(i)
   102         if start >= 0 && stop >= 0
   103         range <- buffer_range.try_restrict(Text.Range(start, stop))
   104         if !range.is_singularity
   105       }
   106       yield range)
   107   }
   108 
   109   private def update_perspective = 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       model.update_perspective()
   116     }
   117   }
   118 
   119 
   120   /* active areas within the text */
   121 
   122   private class Active_Area[A](
   123     rendering: Isabelle_Rendering => Text.Range => Option[Text.Info[A]])
   124   {
   125     private var the_info: Option[Text.Info[A]] = None
   126 
   127     def info: Option[Text.Info[A]] = the_info
   128 
   129     def update(new_info: Option[Text.Info[A]])
   130     {
   131       val old_info = the_info
   132       if (new_info != old_info) {
   133         for { opt <- List(old_info, new_info); Text.Info(range, _) <- opt }
   134           JEdit_Lib.invalidate_range(text_area, range)
   135         the_info = new_info
   136       }
   137     }
   138 
   139     def update_rendering(r: Isabelle_Rendering, range: Text.Range)
   140     { update(rendering(r)(range)) }
   141 
   142     def reset { update(None) }
   143   }
   144 
   145   // owned by Swing thread
   146 
   147   private var control: Boolean = false
   148 
   149   private val highlight_area = new Active_Area[Color]((r: Isabelle_Rendering) => r.highlight _)
   150   def highlight_info(): Option[Text.Info[Color]] = highlight_area.info
   151 
   152   private val hyperlink_area = new Active_Area[Hyperlink]((r: Isabelle_Rendering) => r.hyperlink _)
   153   def hyperlink_info(): Option[Text.Info[Hyperlink]] = hyperlink_area.info
   154 
   155   private val active_areas = List(highlight_area, hyperlink_area)
   156   private def active_reset(): Unit = active_areas.foreach(_.reset)
   157 
   158   private val focus_listener = new FocusAdapter {
   159     override def focusLost(e: FocusEvent) { active_reset() }
   160   }
   161 
   162   private val window_listener = new WindowAdapter {
   163     override def windowIconified(e: WindowEvent) { active_reset() }
   164     override def windowDeactivated(e: WindowEvent) { active_reset() }
   165   }
   166 
   167   private val mouse_listener = new MouseAdapter {
   168     override def mouseClicked(e: MouseEvent) {
   169       hyperlink_area.info match {
   170         case Some(Text.Info(range, link)) => link.follow(text_area.getView)
   171         case None =>
   172       }
   173     }
   174   }
   175 
   176   private val mouse_motion_listener = new MouseMotionAdapter {
   177     override def mouseMoved(e: MouseEvent) {
   178       control = if (OperatingSystem.isMacOS()) e.isMetaDown else e.isControlDown
   179       if (control && model.buffer.isLoaded) {
   180         JEdit_Lib.buffer_lock(model.buffer) {
   181           val rendering = Isabelle_Rendering(model.snapshot(), Isabelle.options.value)
   182           val mouse_range =
   183             JEdit_Lib.point_range(model.buffer, text_area.xyToOffset(e.getX(), e.getY()))
   184           active_areas.foreach(_.update_rendering(rendering, mouse_range))
   185         }
   186       }
   187       else active_reset()
   188     }
   189   }
   190 
   191 
   192   /* text area painting */
   193 
   194   private val text_area_painter = new Text_Area_Painter(this)
   195 
   196   private val tooltip_painter = new TextAreaExtension
   197   {
   198     override def getToolTipText(x: Int, y: Int): String =
   199     {
   200       robust_body(null: String) {
   201         val rendering = Isabelle_Rendering(model.snapshot(), Isabelle.options.value)
   202         val offset = text_area.xyToOffset(x, y)
   203         val range = Text.Range(offset, offset + 1)
   204         val tip =
   205           if (control) rendering.tooltip(range)
   206           else rendering.tooltip_message(range)
   207         tip.map(Isabelle.tooltip(_)) getOrElse null
   208       }
   209     }
   210   }
   211 
   212   private val gutter_painter = new TextAreaExtension
   213   {
   214     override def paintScreenLineRange(gfx: Graphics2D,
   215       first_line: Int, last_line: Int, physical_lines: Array[Int],
   216       start: Array[Int], end: Array[Int], y: Int, line_height: Int)
   217     {
   218       robust_body(()) {
   219         Swing_Thread.assert()
   220 
   221         val gutter = text_area.getGutter
   222         val width = GutterOptionPane.getSelectionAreaWidth
   223         val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
   224         val FOLD_MARKER_SIZE = 12
   225 
   226         if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
   227           val buffer = model.buffer
   228           JEdit_Lib.buffer_lock(buffer) {
   229             val rendering = Isabelle_Rendering(model.snapshot(), Isabelle.options.value)
   230 
   231             for (i <- 0 until physical_lines.length) {
   232               if (physical_lines(i) != -1) {
   233                 val line_range = JEdit_Lib.proper_line_range(buffer, start(i), end(i))
   234 
   235                 // gutter icons
   236                 rendering.gutter_message(line_range) match {
   237                   case Some(icon) =>
   238                     val x0 = (FOLD_MARKER_SIZE + width - border_width - icon.getIconWidth) max 10
   239                     val y0 = y + i * line_height + (((line_height - icon.getIconHeight) / 2) max 0)
   240                     icon.paintIcon(gutter, gfx, x0, y0)
   241                   case None =>
   242                 }
   243               }
   244             }
   245           }
   246         }
   247       }
   248     }
   249   }
   250 
   251 
   252   /* caret handling */
   253 
   254   private val delay_caret_update =
   255     Swing_Thread.delay_last(Time.seconds(Isabelle.options.real("editor_input_delay"))) {
   256       session.caret_focus.event(Session.Caret_Focus)
   257     }
   258 
   259   private val caret_listener = new CaretListener {
   260     override def caretUpdate(e: CaretEvent) { delay_caret_update.invoke() }
   261   }
   262 
   263 
   264   /* text status overview left of scrollbar */
   265 
   266   private object overview extends Text_Overview(this)
   267   {
   268     val delay_repaint =
   269       Swing_Thread.delay_first(
   270         Time.seconds(Isabelle.options.real("editor_update_delay"))) { repaint() }
   271   }
   272 
   273 
   274   /* main actor */
   275 
   276   private val main_actor = actor {
   277     loop {
   278       react {
   279         case _: Session.Raw_Edits =>
   280           Swing_Thread.later {
   281             overview.delay_repaint.postpone(
   282               Time.seconds(Isabelle.options.real("editor_input_delay")))
   283           }
   284 
   285         case changed: Session.Commands_Changed =>
   286           val buffer = model.buffer
   287           Swing_Thread.later {
   288             JEdit_Lib.buffer_lock(buffer) {
   289               if (model.buffer == text_area.getBuffer) {
   290                 val snapshot = model.snapshot()
   291 
   292                 if (changed.assignment ||
   293                     (changed.nodes.contains(model.name) &&
   294                      changed.commands.exists(snapshot.node.commands.contains)))
   295                   Swing_Thread.later { overview.delay_repaint.invoke() }
   296 
   297                 JEdit_Lib.visible_range(text_area) match {
   298                   case Some(visible) =>
   299                     if (changed.assignment) JEdit_Lib.invalidate_range(text_area, visible)
   300                     else {
   301                       val visible_cmds =
   302                         snapshot.node.command_range(snapshot.revert(visible)).map(_._1)
   303                       if (visible_cmds.exists(changed.commands)) {
   304                         for {
   305                           line <- 0 until text_area.getVisibleLines
   306                           start = text_area.getScreenLineStartOffset(line) if start >= 0
   307                           end = text_area.getScreenLineEndOffset(line) if end >= 0
   308                           range = JEdit_Lib.proper_line_range(buffer, start, end)
   309                           line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
   310                           if line_cmds.exists(changed.commands)
   311                         } text_area.invalidateScreenLineRange(line, line)
   312                       }
   313                     }
   314                   case None =>
   315                 }
   316               }
   317             }
   318           }
   319 
   320         case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
   321       }
   322     }
   323   }
   324 
   325 
   326   /* activation */
   327 
   328   private def activate()
   329   {
   330     val painter = text_area.getPainter
   331     painter.addExtension(TextAreaPainter.LOWEST_LAYER, update_perspective)
   332     painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, tooltip_painter)
   333     text_area_painter.activate()
   334     text_area.getGutter.addExtension(gutter_painter)
   335     text_area.addFocusListener(focus_listener)
   336     text_area.getView.addWindowListener(window_listener)
   337     painter.addMouseListener(mouse_listener)
   338     painter.addMouseMotionListener(mouse_motion_listener)
   339     text_area.addCaretListener(caret_listener)
   340     text_area.addLeftOfScrollBar(overview)
   341     session.raw_edits += main_actor
   342     session.commands_changed += main_actor
   343   }
   344 
   345   private def deactivate()
   346   {
   347     val painter = text_area.getPainter
   348     session.raw_edits -= main_actor
   349     session.commands_changed -= main_actor
   350     text_area.removeFocusListener(focus_listener)
   351     text_area.getView.removeWindowListener(window_listener)
   352     painter.removeMouseMotionListener(mouse_motion_listener)
   353     painter.removeMouseListener(mouse_listener)
   354     text_area.removeCaretListener(caret_listener); delay_caret_update.revoke()
   355     text_area.removeLeftOfScrollBar(overview); overview.delay_repaint.revoke()
   356     text_area.getGutter.removeExtension(gutter_painter)
   357     text_area_painter.deactivate()
   358     painter.removeExtension(tooltip_painter)
   359     painter.removeExtension(update_perspective)
   360   }
   361 }