src/Tools/jEdit/src/document_view.scala
changeset 56715 52125652e82a
parent 56662 f373fb77e0a4
child 56770 e160ae47db94
equal deleted inserted replaced
56714:061f83259922 56715:52125652e82a
     7 
     7 
     8 package isabelle.jedit
     8 package isabelle.jedit
     9 
     9 
    10 
    10 
    11 import isabelle._
    11 import isabelle._
    12 
       
    13 import scala.actors.Actor._
       
    14 
    12 
    15 import java.awt.Graphics2D
    13 import java.awt.Graphics2D
    16 import java.awt.event.KeyEvent
    14 import java.awt.event.KeyEvent
    17 import javax.swing.event.{CaretListener, CaretEvent}
    15 import javax.swing.event.{CaretListener, CaretEvent}
    18 
    16 
   174 
   172 
   175   /* caret handling */
   173   /* caret handling */
   176 
   174 
   177   private val delay_caret_update =
   175   private val delay_caret_update =
   178     Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) {
   176     Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) {
   179       session.caret_focus.event(Session.Caret_Focus)
   177       session.caret_focus.post(Session.Caret_Focus)
   180     }
   178     }
   181 
   179 
   182   private val caret_listener = new CaretListener {
   180   private val caret_listener = new CaretListener {
   183     override def caretUpdate(e: CaretEvent) { delay_caret_update.invoke() }
   181     override def caretUpdate(e: CaretEvent) { delay_caret_update.invoke() }
   184   }
   182   }
   191     val delay_repaint =
   189     val delay_repaint =
   192       Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { repaint() }
   190       Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { repaint() }
   193   }
   191   }
   194 
   192 
   195 
   193 
   196   /* main actor */
   194   /* main */
   197 
   195 
   198   private val main_actor = actor {
   196   private val main =
   199     loop {
   197     Session.Consumer[Any](getClass.getName) {
   200       react {
   198       case _: Session.Raw_Edits =>
   201         case _: Session.Raw_Edits =>
   199         Swing_Thread.later {
   202           Swing_Thread.later {
   200           overview.delay_repaint.postpone(PIDE.options.seconds("editor_input_delay"))
   203             overview.delay_repaint.postpone(PIDE.options.seconds("editor_input_delay"))
   201         }
   204           }
   202 
   205 
   203       case changed: Session.Commands_Changed =>
   206         case changed: Session.Commands_Changed =>
   204         val buffer = model.buffer
   207           val buffer = model.buffer
   205         Swing_Thread.later {
   208           Swing_Thread.later {
   206           JEdit_Lib.buffer_lock(buffer) {
   209             JEdit_Lib.buffer_lock(buffer) {
   207             if (model.buffer == text_area.getBuffer) {
   210               if (model.buffer == text_area.getBuffer) {
   208               val snapshot = model.snapshot()
   211                 val snapshot = model.snapshot()
   209 
   212 
   210               val load_changed =
   213                 val load_changed =
   211                 snapshot.load_commands.exists(changed.commands.contains)
   214                   snapshot.load_commands.exists(changed.commands.contains)
   212 
   215 
   213               if (changed.assignment || load_changed ||
   216                 if (changed.assignment || load_changed ||
   214                   (changed.nodes.contains(model.node_name) &&
   217                     (changed.nodes.contains(model.node_name) &&
   215                    changed.commands.exists(snapshot.node.commands.contains)))
   218                      changed.commands.exists(snapshot.node.commands.contains)))
   216                 Swing_Thread.later { overview.delay_repaint.invoke() }
   219                   Swing_Thread.later { overview.delay_repaint.invoke() }
   217 
   220 
   218               val visible_lines = text_area.getVisibleLines
   221                 val visible_lines = text_area.getVisibleLines
   219               if (visible_lines > 0) {
   222                 if (visible_lines > 0) {
   220                 if (changed.assignment || load_changed)
   223                   if (changed.assignment || load_changed)
   221                   text_area.invalidateScreenLineRange(0, visible_lines)
   224                     text_area.invalidateScreenLineRange(0, visible_lines)
   222                 else {
   225                   else {
   223                   val visible_range = JEdit_Lib.visible_range(text_area).get
   226                     val visible_range = JEdit_Lib.visible_range(text_area).get
   224                   val visible_iterator =
   227                     val visible_iterator =
   225                     snapshot.node.command_iterator(snapshot.revert(visible_range)).map(_._1)
   228                       snapshot.node.command_iterator(snapshot.revert(visible_range)).map(_._1)
   226                   if (visible_iterator.exists(changed.commands)) {
   229                     if (visible_iterator.exists(changed.commands)) {
   227                     for {
   230                       for {
   228                       line <- (0 until visible_lines).iterator
   231                         line <- (0 until visible_lines).iterator
   229                       start = text_area.getScreenLineStartOffset(line) if start >= 0
   232                         start = text_area.getScreenLineStartOffset(line) if start >= 0
   230                       end = text_area.getScreenLineEndOffset(line) if end >= 0
   233                         end = text_area.getScreenLineEndOffset(line) if end >= 0
   231                       range = Text.Range(start, end)
   234                         range = Text.Range(start, end)
   232                       line_cmds = snapshot.node.command_iterator(snapshot.revert(range)).map(_._1)
   235                         line_cmds = snapshot.node.command_iterator(snapshot.revert(range)).map(_._1)
   233                       if line_cmds.exists(changed.commands)
   236                         if line_cmds.exists(changed.commands)
   234                     } text_area.invalidateScreenLineRange(line, line)
   237                       } text_area.invalidateScreenLineRange(line, line)
       
   238                     }
       
   239                   }
   235                   }
   240                 }
   236                 }
   241               }
   237               }
   242             }
   238             }
   243           }
   239           }
   244 
   240         }
   245         case bad =>
   241     }
   246           System.err.println("command_change_actor: ignoring bad message " + bad)
       
   247       }
       
   248     }
       
   249   }
       
   250 
   242 
   251 
   243 
   252   /* activation */
   244   /* activation */
   253 
   245 
   254   private def activate()
   246   private def activate()
   259     rich_text_area.activate()
   251     rich_text_area.activate()
   260     text_area.getGutter.addExtension(gutter_painter)
   252     text_area.getGutter.addExtension(gutter_painter)
   261     text_area.addKeyListener(key_listener)
   253     text_area.addKeyListener(key_listener)
   262     text_area.addCaretListener(caret_listener)
   254     text_area.addCaretListener(caret_listener)
   263     text_area.addLeftOfScrollBar(overview)
   255     text_area.addLeftOfScrollBar(overview)
   264     session.raw_edits += main_actor
   256     session.raw_edits += main
   265     session.commands_changed += main_actor
   257     session.commands_changed += main
   266   }
   258   }
   267 
   259 
   268   private def deactivate()
   260   private def deactivate()
   269   {
   261   {
   270     val painter = text_area.getPainter
   262     val painter = text_area.getPainter
   271 
   263 
   272     session.raw_edits -= main_actor
   264     session.raw_edits -= main
   273     session.commands_changed -= main_actor
   265     session.commands_changed -= main
   274     text_area.removeLeftOfScrollBar(overview); overview.delay_repaint.revoke()
   266     text_area.removeLeftOfScrollBar(overview); overview.delay_repaint.revoke()
   275     text_area.removeCaretListener(caret_listener); delay_caret_update.revoke()
   267     text_area.removeCaretListener(caret_listener); delay_caret_update.revoke()
   276     text_area.removeKeyListener(key_listener)
   268     text_area.removeKeyListener(key_listener)
   277     text_area.getGutter.removeExtension(gutter_painter)
   269     text_area.getGutter.removeExtension(gutter_painter)
   278     rich_text_area.deactivate()
   270     rich_text_area.deactivate()