src/Tools/jEdit/src/document_model.scala
author wenzelm
Sun Jul 03 19:42:32 2011 +0200 (2011-07-03)
changeset 43648 e32de528b5ef
parent 43644 ea08ce1c314b
child 43661 39fdbd814c7f
permissions -rw-r--r--
more explicit edit_node vs. init_node;
some support for master_dir and header;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/document_model.scala
wenzelm@36760
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     3
    Author:     Makarius
wenzelm@36760
     4
wenzelm@38222
     5
Document model connected to jEdit buffer -- single node in theory graph.
wenzelm@36760
     6
*/
wenzelm@34407
     7
wenzelm@34784
     8
package isabelle.jedit
wenzelm@34760
     9
wenzelm@34318
    10
wenzelm@36015
    11
import isabelle._
wenzelm@36015
    12
wenzelm@34693
    13
import scala.collection.mutable
wenzelm@34446
    14
wenzelm@34784
    15
import org.gjt.sp.jedit.Buffer
wenzelm@34783
    16
import org.gjt.sp.jedit.buffer.{BufferAdapter, BufferListener, JEditBuffer}
wenzelm@38158
    17
import org.gjt.sp.jedit.textarea.TextArea
wenzelm@38158
    18
wenzelm@38158
    19
import java.awt.font.TextAttribute
wenzelm@34318
    20
wenzelm@34760
    21
wenzelm@34784
    22
object Document_Model
wenzelm@34588
    23
{
wenzelm@34784
    24
  /* document model of buffer */
wenzelm@34784
    25
wenzelm@34784
    26
  private val key = "isabelle.document_model"
wenzelm@34784
    27
wenzelm@34788
    28
  def apply(buffer: Buffer): Option[Document_Model] =
wenzelm@34784
    29
  {
wenzelm@38223
    30
    Swing_Thread.require()
wenzelm@34784
    31
    buffer.getProperty(key) match {
wenzelm@34784
    32
      case model: Document_Model => Some(model)
wenzelm@34784
    33
      case _ => None
wenzelm@34784
    34
    }
wenzelm@34784
    35
  }
wenzelm@34784
    36
wenzelm@34784
    37
  def exit(buffer: Buffer)
wenzelm@34784
    38
  {
wenzelm@38223
    39
    Swing_Thread.require()
wenzelm@34788
    40
    apply(buffer) match {
wenzelm@39636
    41
      case None =>
wenzelm@34784
    42
      case Some(model) =>
wenzelm@34784
    43
        model.deactivate()
wenzelm@34784
    44
        buffer.unsetProperty(key)
immler@34653
    45
    }
wenzelm@34318
    46
  }
wenzelm@43397
    47
wenzelm@43648
    48
  def init(session: Session, buffer: Buffer, master_dir: String, thy_name: String): Document_Model =
wenzelm@43397
    49
  {
wenzelm@43397
    50
    exit(buffer)
wenzelm@43648
    51
    val model = new Document_Model(session, buffer, master_dir, thy_name)
wenzelm@43397
    52
    buffer.setProperty(key, model)
wenzelm@43397
    53
    model.activate()
wenzelm@43397
    54
    model
wenzelm@43397
    55
  }
wenzelm@34318
    56
}
wenzelm@34318
    57
wenzelm@38151
    58
wenzelm@43648
    59
class Document_Model(val session: Session,
wenzelm@43648
    60
  val buffer: Buffer, val master_dir: String, val thy_name: String)
wenzelm@34588
    61
{
wenzelm@38224
    62
  /* pending text edits */
wenzelm@38222
    63
wenzelm@43648
    64
  private def capture_header(): Exn.Result[Thy_Header.Header] =
wenzelm@43648
    65
    Exn.capture {
wenzelm@43648
    66
      session.thy_header.check(thy_name, buffer.getSegment(0, buffer.getLength))
wenzelm@43648
    67
    }
wenzelm@43648
    68
wenzelm@43644
    69
  private object pending_edits  // owned by Swing thread
wenzelm@38224
    70
  {
wenzelm@38425
    71
    private val pending = new mutable.ListBuffer[Text.Edit]
wenzelm@38425
    72
    def snapshot(): List[Text.Edit] = pending.toList
wenzelm@38224
    73
wenzelm@43648
    74
    def flush()
wenzelm@38224
    75
    {
wenzelm@38224
    76
      Swing_Thread.require()
wenzelm@43648
    77
      snapshot() match {
wenzelm@43648
    78
        case Nil =>
wenzelm@43648
    79
        case edits =>
wenzelm@43648
    80
          pending.clear
wenzelm@43648
    81
          session.edit_node(master_dir + "/" + thy_name, capture_header(), edits)
wenzelm@43648
    82
      }
wenzelm@38224
    83
    }
wenzelm@38224
    84
wenzelm@40478
    85
    def init()
wenzelm@40478
    86
    {
wenzelm@43648
    87
      flush()
wenzelm@43648
    88
      session.init_node(master_dir + "/" + thy_name, capture_header(), Isabelle.buffer_text(buffer))
wenzelm@40478
    89
    }
wenzelm@40478
    90
wenzelm@40478
    91
    private val delay_flush =
wenzelm@40478
    92
      Swing_Thread.delay_last(session.input_delay) { flush() }
wenzelm@40478
    93
wenzelm@38425
    94
    def +=(edit: Text.Edit)
wenzelm@38224
    95
    {
wenzelm@38224
    96
      Swing_Thread.require()
wenzelm@38224
    97
      pending += edit
wenzelm@38224
    98
      delay_flush()
wenzelm@38222
    99
    }
wenzelm@38222
   100
  }
wenzelm@38152
   101
wenzelm@38152
   102
wenzelm@38152
   103
  /* snapshot */
wenzelm@34731
   104
wenzelm@38417
   105
  def snapshot(): Document.Snapshot =
wenzelm@38417
   106
  {
wenzelm@38223
   107
    Swing_Thread.require()
wenzelm@43648
   108
    session.snapshot(master_dir + "/" + thy_name, pending_edits.snapshot())
wenzelm@38223
   109
  }
wenzelm@34828
   110
wenzelm@34828
   111
wenzelm@34828
   112
  /* buffer listener */
wenzelm@34828
   113
wenzelm@34828
   114
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   115
  {
wenzelm@40478
   116
    override def bufferLoaded(buffer: JEditBuffer)
wenzelm@40478
   117
    {
wenzelm@40478
   118
      pending_edits.init()
wenzelm@40478
   119
    }
wenzelm@40478
   120
wenzelm@34828
   121
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   122
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   123
    {
wenzelm@40478
   124
      if (!buffer.isLoading)
wenzelm@40478
   125
        pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
wenzelm@34828
   126
    }
wenzelm@34828
   127
wenzelm@34828
   128
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@38426
   129
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   130
    {
wenzelm@40478
   131
      if (!buffer.isLoading)
wenzelm@40478
   132
        pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
wenzelm@34828
   133
    }
wenzelm@34828
   134
  }
wenzelm@34828
   135
wenzelm@34828
   136
wenzelm@38158
   137
  /* activation */
wenzelm@37557
   138
wenzelm@43512
   139
  private def activate()
wenzelm@34784
   140
  {
wenzelm@34784
   141
    buffer.addBufferListener(buffer_listener)
wenzelm@43512
   142
    pending_edits.init()
wenzelm@34784
   143
    buffer.propertiesChanged()
immler@34680
   144
  }
immler@34680
   145
wenzelm@43512
   146
  private def deactivate()
immler@34680
   147
  {
wenzelm@40478
   148
    pending_edits.flush()
wenzelm@34784
   149
    buffer.removeBufferListener(buffer_listener)
wenzelm@43512
   150
    buffer.propertiesChanged()
immler@34680
   151
  }
wenzelm@34447
   152
}