src/Tools/jEdit/src/document_model.scala
author wenzelm
Wed Jul 31 21:13:05 2013 +0200 (2013-07-31)
changeset 52816 c608e0ade554
parent 52815 eaad5fe7bb1b
child 52849 199e9fa5a5c2
permissions -rw-r--r--
home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
tuned signature;
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@50565
    26
  private val key = "PIDE.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)
wenzelm@47058
    45
        buffer.propertiesChanged
immler@34653
    46
    }
wenzelm@34318
    47
  }
wenzelm@43397
    48
wenzelm@44615
    49
  def init(session: Session, buffer: Buffer, name: Document.Node.Name): Document_Model =
wenzelm@43397
    50
  {
wenzelm@47058
    51
    Swing_Thread.require()
wenzelm@47058
    52
    apply(buffer).map(_.deactivate)
wenzelm@44615
    53
    val model = new Document_Model(session, buffer, name)
wenzelm@43397
    54
    buffer.setProperty(key, model)
wenzelm@43397
    55
    model.activate()
wenzelm@47058
    56
    buffer.propertiesChanged
wenzelm@43397
    57
    model
wenzelm@43397
    58
  }
wenzelm@34318
    59
}
wenzelm@34318
    60
wenzelm@38151
    61
wenzelm@44615
    62
class Document_Model(val session: Session, val buffer: Buffer, val name: Document.Node.Name)
wenzelm@34588
    63
{
wenzelm@44385
    64
  /* header */
wenzelm@38222
    65
wenzelm@48707
    66
  def node_header(): Document.Node.Header =
wenzelm@46920
    67
  {
wenzelm@46920
    68
    Swing_Thread.require()
wenzelm@49406
    69
    JEdit_Lib.buffer_lock(buffer) {
wenzelm@46748
    70
      Exn.capture {
wenzelm@50205
    71
        PIDE.thy_load.check_thy_text(name, buffer.getSegment(0, buffer.getLength))
wenzelm@48707
    72
      } match {
wenzelm@48707
    73
        case Exn.Res(header) => header
wenzelm@48707
    74
        case Exn.Exn(exn) => Document.Node.bad_header(Exn.message(exn))
wenzelm@46748
    75
      }
wenzelm@46748
    76
    }
wenzelm@46920
    77
  }
wenzelm@44385
    78
wenzelm@44385
    79
wenzelm@44385
    80
  /* perspective */
wenzelm@44385
    81
wenzelm@52816
    82
  private var _node_required = false
wenzelm@52816
    83
  def node_required: Boolean = _node_required
wenzelm@52816
    84
  def node_required_=(b: Boolean)
wenzelm@52816
    85
  {
wenzelm@52816
    86
    Swing_Thread.require()
wenzelm@52816
    87
    if (_node_required != b) {
wenzelm@52816
    88
      _node_required = b
wenzelm@52816
    89
      PIDE.options_changed()
wenzelm@52816
    90
      PIDE.flush_buffers()
wenzelm@52816
    91
    }
wenzelm@52816
    92
  }
wenzelm@52808
    93
wenzelm@52808
    94
  def node_perspective(): Document.Node.Perspective_Text =
wenzelm@44385
    95
  {
wenzelm@44385
    96
    Swing_Thread.require()
wenzelm@52759
    97
wenzelm@52808
    98
    val perspective =
wenzelm@52815
    99
      if (Isabelle.continuous_checking) {
wenzelm@52815
   100
        (node_required, Text.Perspective(
wenzelm@52808
   101
          for {
wenzelm@52808
   102
            doc_view <- PIDE.document_views(buffer)
wenzelm@52808
   103
            range <- doc_view.perspective().ranges
wenzelm@52808
   104
          } yield range))
wenzelm@52808
   105
      }
wenzelm@52808
   106
      else (false, Text.Perspective.empty)
wenzelm@52808
   107
wenzelm@52808
   108
    Document.Node.Perspective(perspective._1, perspective._2)
wenzelm@44385
   109
  }
wenzelm@44385
   110
wenzelm@44385
   111
wenzelm@50363
   112
  /* edits */
wenzelm@50344
   113
wenzelm@50344
   114
  def init_edits(): List[Document.Edit_Text] =
wenzelm@50344
   115
  {
wenzelm@50344
   116
    Swing_Thread.require()
wenzelm@52815
   117
wenzelm@50344
   118
    val header = node_header()
wenzelm@50344
   119
    val text = JEdit_Lib.buffer_text(buffer)
wenzelm@50344
   120
    val perspective = node_perspective()
wenzelm@50344
   121
wenzelm@50344
   122
    List(session.header_edit(name, header),
wenzelm@50344
   123
      name -> Document.Node.Clear(),
wenzelm@50344
   124
      name -> Document.Node.Edits(List(Text.Edit.insert(0, text))),
wenzelm@52808
   125
      name -> perspective)
wenzelm@50344
   126
  }
wenzelm@50344
   127
wenzelm@52808
   128
  def node_edits(perspective: Document.Node.Perspective_Text, text_edits: List[Text.Edit])
wenzelm@50363
   129
    : List[Document.Edit_Text] =
wenzelm@50363
   130
  {
wenzelm@50363
   131
    Swing_Thread.require()
wenzelm@52815
   132
wenzelm@50363
   133
    val header = node_header()
wenzelm@50363
   134
wenzelm@50363
   135
    List(session.header_edit(name, header),
wenzelm@50363
   136
      name -> Document.Node.Edits(text_edits),
wenzelm@52808
   137
      name -> perspective)
wenzelm@50363
   138
  }
wenzelm@50363
   139
wenzelm@50344
   140
wenzelm@50344
   141
  /* pending edits */
wenzelm@43648
   142
wenzelm@43644
   143
  private object pending_edits  // owned by Swing thread
wenzelm@38224
   144
  {
wenzelm@38425
   145
    private val pending = new mutable.ListBuffer[Text.Edit]
wenzelm@52808
   146
    private var last_perspective: Document.Node.Perspective_Text =
wenzelm@52815
   147
      Document.Node.Perspective(node_required, Text.Perspective.empty)
wenzelm@44438
   148
wenzelm@38425
   149
    def snapshot(): List[Text.Edit] = pending.toList
wenzelm@38224
   150
wenzelm@52759
   151
    def flushed_edits(): List[Document.Edit_Text] =
wenzelm@38224
   152
    {
wenzelm@38224
   153
      Swing_Thread.require()
wenzelm@44438
   154
wenzelm@47393
   155
      val edits = snapshot()
wenzelm@50344
   156
      val new_perspective = node_perspective()
wenzelm@47393
   157
      if (!edits.isEmpty || last_perspective != new_perspective) {
wenzelm@47393
   158
        pending.clear
wenzelm@47393
   159
        last_perspective = new_perspective
wenzelm@52759
   160
        node_edits(new_perspective, edits)
wenzelm@43648
   161
      }
wenzelm@52759
   162
      else Nil
wenzelm@38224
   163
    }
wenzelm@38224
   164
wenzelm@52759
   165
    def flush(): Unit = session.update(flushed_edits())
wenzelm@52759
   166
wenzelm@52767
   167
    val delay_flush =
wenzelm@50207
   168
      Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) { flush() }
wenzelm@40478
   169
wenzelm@38425
   170
    def +=(edit: Text.Edit)
wenzelm@38224
   171
    {
wenzelm@38224
   172
      Swing_Thread.require()
wenzelm@38224
   173
      pending += edit
wenzelm@49195
   174
      delay_flush.invoke()
wenzelm@38222
   175
    }
wenzelm@44436
   176
wenzelm@46740
   177
    def init()
wenzelm@46740
   178
    {
wenzelm@46740
   179
      flush()
wenzelm@50363
   180
      session.update(init_edits())
wenzelm@46740
   181
    }
wenzelm@46740
   182
wenzelm@46740
   183
    def exit()
wenzelm@46740
   184
    {
wenzelm@49195
   185
      delay_flush.revoke()
wenzelm@46740
   186
      flush()
wenzelm@44436
   187
    }
wenzelm@44436
   188
  }
wenzelm@44436
   189
wenzelm@52759
   190
  def flushed_edits(): List[Document.Edit_Text] = pending_edits.flushed_edits()
wenzelm@52767
   191
wenzelm@52767
   192
  def update_perspective(): Unit = pending_edits.delay_flush.invoke()
wenzelm@44776
   193
wenzelm@38152
   194
wenzelm@38152
   195
  /* snapshot */
wenzelm@34731
   196
wenzelm@38417
   197
  def snapshot(): Document.Snapshot =
wenzelm@38417
   198
  {
wenzelm@38223
   199
    Swing_Thread.require()
wenzelm@44615
   200
    session.snapshot(name, pending_edits.snapshot())
wenzelm@38223
   201
  }
wenzelm@34828
   202
wenzelm@34828
   203
wenzelm@34828
   204
  /* buffer listener */
wenzelm@34828
   205
wenzelm@34828
   206
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   207
  {
wenzelm@40478
   208
    override def bufferLoaded(buffer: JEditBuffer)
wenzelm@40478
   209
    {
wenzelm@40478
   210
      pending_edits.init()
wenzelm@40478
   211
    }
wenzelm@40478
   212
wenzelm@34828
   213
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   214
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   215
    {
wenzelm@40478
   216
      if (!buffer.isLoading)
wenzelm@40478
   217
        pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
wenzelm@34828
   218
    }
wenzelm@34828
   219
wenzelm@34828
   220
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@38426
   221
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   222
    {
wenzelm@40478
   223
      if (!buffer.isLoading)
wenzelm@40478
   224
        pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
wenzelm@34828
   225
    }
wenzelm@34828
   226
  }
wenzelm@34828
   227
wenzelm@34828
   228
wenzelm@38158
   229
  /* activation */
wenzelm@37557
   230
wenzelm@43512
   231
  private def activate()
wenzelm@34784
   232
  {
wenzelm@34784
   233
    buffer.addBufferListener(buffer_listener)
wenzelm@50344
   234
    pending_edits.flush()
wenzelm@44358
   235
    Token_Markup.refresh_buffer(buffer)
immler@34680
   236
  }
immler@34680
   237
wenzelm@43512
   238
  private def deactivate()
immler@34680
   239
  {
wenzelm@46740
   240
    pending_edits.exit()
wenzelm@34784
   241
    buffer.removeBufferListener(buffer_listener)
wenzelm@44358
   242
    Token_Markup.refresh_buffer(buffer)
immler@34680
   243
  }
wenzelm@34447
   244
}