src/Tools/jEdit/src/document_view.scala
author wenzelm
Tue Feb 21 23:24:49 2012 +0100 (2012-02-21)
changeset 46583 926957a621dd
parent 46572 3074685ab7ed
child 46740 852baa599351
permissions -rw-r--r--
more robust visible_range: allow empty view;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/document_view.scala
wenzelm@36760
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     3
    Author:     Makarius
wenzelm@36760
     4
wenzelm@36760
     5
Document view connected to jEdit text area.
wenzelm@36760
     6
*/
wenzelm@34408
     7
immler@34403
     8
package isabelle.jedit
immler@34403
     9
wenzelm@34760
    10
wenzelm@36015
    11
import isabelle._
wenzelm@36015
    12
wenzelm@44379
    13
import scala.collection.mutable
wenzelm@45460
    14
import scala.collection.immutable.SortedMap
wenzelm@34784
    15
import scala.actors.Actor._
wenzelm@34784
    16
wenzelm@43520
    17
import java.lang.System
wenzelm@45744
    18
import java.text.BreakIterator
wenzelm@46572
    19
import java.awt.{Color, Graphics2D, Point}
wenzelm@46572
    20
import java.awt.event.{MouseMotionAdapter, MouseEvent,
wenzelm@39741
    21
  FocusAdapter, FocusEvent, WindowEvent, WindowAdapter}
wenzelm@46572
    22
import javax.swing.{Popup, PopupFactory, SwingUtilities, BorderFactory}
wenzelm@34784
    23
import javax.swing.event.{CaretListener, CaretEvent}
wenzelm@34734
    24
wenzelm@43404
    25
import org.gjt.sp.util.Log
wenzelm@43404
    26
wenzelm@42839
    27
import org.gjt.sp.jedit.{jEdit, OperatingSystem, Debug}
wenzelm@34709
    28
import org.gjt.sp.jedit.gui.RolloverButton
wenzelm@39176
    29
import org.gjt.sp.jedit.options.GutterOptionPane
wenzelm@44436
    30
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter,
wenzelm@44436
    31
  ScrollListener}
wenzelm@43369
    32
import org.gjt.sp.jedit.syntax.{SyntaxStyle}
wenzelm@34784
    33
wenzelm@34784
    34
wenzelm@34784
    35
object Document_View
wenzelm@34784
    36
{
wenzelm@34784
    37
  /* document view of text area */
wenzelm@34784
    38
wenzelm@34784
    39
  private val key = new Object
wenzelm@34784
    40
wenzelm@39741
    41
  def apply(text_area: JEditTextArea): Option[Document_View] =
wenzelm@34784
    42
  {
wenzelm@38223
    43
    Swing_Thread.require()
wenzelm@34784
    44
    text_area.getClientProperty(key) match {
wenzelm@34784
    45
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    46
      case _ => None
wenzelm@34784
    47
    }
wenzelm@34784
    48
  }
wenzelm@34784
    49
wenzelm@39741
    50
  def exit(text_area: JEditTextArea)
wenzelm@34784
    51
  {
wenzelm@38223
    52
    Swing_Thread.require()
wenzelm@34788
    53
    apply(text_area) match {
wenzelm@39636
    54
      case None =>
wenzelm@34784
    55
      case Some(doc_view) =>
wenzelm@34784
    56
        doc_view.deactivate()
wenzelm@34784
    57
        text_area.putClientProperty(key, null)
wenzelm@34784
    58
    }
wenzelm@34784
    59
  }
wenzelm@43397
    60
wenzelm@43397
    61
  def init(model: Document_Model, text_area: JEditTextArea): Document_View =
wenzelm@43397
    62
  {
wenzelm@43397
    63
    exit(text_area)
wenzelm@43397
    64
    val doc_view = new Document_View(model, text_area)
wenzelm@43397
    65
    text_area.putClientProperty(key, doc_view)
wenzelm@43397
    66
    doc_view.activate()
wenzelm@43397
    67
    doc_view
wenzelm@43397
    68
  }
wenzelm@34784
    69
}
immler@34403
    70
wenzelm@34733
    71
wenzelm@43376
    72
class Document_View(val model: Document_Model, val text_area: JEditTextArea)
immler@34654
    73
{
wenzelm@34784
    74
  private val session = model.session
wenzelm@34784
    75
immler@34403
    76
wenzelm@43419
    77
  /* robust extension body */
wenzelm@43404
    78
wenzelm@43404
    79
  def robust_body[A](default: A)(body: => A): A =
wenzelm@43417
    80
  {
wenzelm@43417
    81
    try {
wenzelm@43417
    82
      Swing_Thread.require()
wenzelm@43417
    83
      if (model.buffer == text_area.getBuffer) body
wenzelm@43417
    84
      else {
wenzelm@43650
    85
        Log.log(Log.ERROR, this, ERROR("Inconsistent document model"))
wenzelm@43417
    86
        default
wenzelm@43404
    87
      }
wenzelm@43404
    88
    }
wenzelm@43417
    89
    catch { case t: Throwable => Log.log(Log.ERROR, this, t); default }
wenzelm@43417
    90
  }
wenzelm@43404
    91
wenzelm@43404
    92
wenzelm@44378
    93
  /* visible text range */
wenzelm@38881
    94
wenzelm@38883
    95
  // simplify slightly odd result of TextArea.getScreenLineEndOffset etc.
wenzelm@38881
    96
  // NB: jEdit already normalizes \r\n and \r to \n
wenzelm@38881
    97
  def proper_line_range(start: Text.Offset, end: Text.Offset): Text.Range =
wenzelm@38881
    98
  {
wenzelm@38883
    99
    val stop = if (start < end) end - 1 else end min model.buffer.getLength
wenzelm@38881
   100
    Text.Range(start, stop)
wenzelm@38881
   101
  }
wenzelm@38881
   102
wenzelm@46583
   103
  def visible_range(): Option[Text.Range] =
wenzelm@38881
   104
  {
wenzelm@46583
   105
    val n = text_area.getVisibleLines
wenzelm@46583
   106
    if (n > 0) {
wenzelm@46583
   107
      val start = text_area.getScreenLineStartOffset(0)
wenzelm@46583
   108
      val raw_end = text_area.getScreenLineEndOffset(n - 1)
wenzelm@46583
   109
      Some(proper_line_range(start, if (raw_end >= 0) raw_end else model.buffer.getLength))
wenzelm@46583
   110
    }
wenzelm@46583
   111
    else None
wenzelm@38881
   112
  }
wenzelm@38881
   113
wenzelm@44378
   114
  def invalidate_range(range: Text.Range)
wenzelm@39132
   115
  {
wenzelm@39132
   116
    text_area.invalidateLineRange(
wenzelm@39132
   117
      model.buffer.getLineOfOffset(range.start),
wenzelm@39132
   118
      model.buffer.getLineOfOffset(range.stop))
wenzelm@39132
   119
  }
wenzelm@39132
   120
wenzelm@38881
   121
wenzelm@44379
   122
  /* perspective */
wenzelm@44379
   123
wenzelm@44379
   124
  def perspective(): Text.Perspective =
wenzelm@44379
   125
  {
wenzelm@44379
   126
    Swing_Thread.require()
wenzelm@44776
   127
    val buffer_range = model.buffer_range()
wenzelm@44473
   128
    Text.Perspective(
wenzelm@44379
   129
      for {
wenzelm@44379
   130
        i <- 0 until text_area.getVisibleLines
wenzelm@44379
   131
        val start = text_area.getScreenLineStartOffset(i)
wenzelm@44379
   132
        val stop = text_area.getScreenLineEndOffset(i)
wenzelm@44379
   133
        if start >= 0 && stop >= 0
wenzelm@44583
   134
        val range <- buffer_range.try_restrict(Text.Range(start, stop))
wenzelm@44583
   135
        if !range.is_singularity
wenzelm@44379
   136
      }
wenzelm@44583
   137
      yield range)
wenzelm@44379
   138
  }
wenzelm@44379
   139
wenzelm@44437
   140
  private def update_perspective = new TextAreaExtension
wenzelm@44437
   141
  {
wenzelm@44437
   142
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@44437
   143
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@44437
   144
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@44437
   145
    {
wenzelm@44437
   146
      model.update_perspective()
wenzelm@44437
   147
    }
wenzelm@44437
   148
  }
wenzelm@44437
   149
wenzelm@44379
   150
wenzelm@43419
   151
  /* snapshot */
wenzelm@43419
   152
wenzelm@43419
   153
  // owned by Swing thread
wenzelm@43419
   154
  @volatile private var was_outdated = false
wenzelm@43419
   155
  @volatile private var was_updated = false
wenzelm@43419
   156
wenzelm@43419
   157
  def update_snapshot(): Document.Snapshot =
wenzelm@43419
   158
  {
wenzelm@43419
   159
    Swing_Thread.require()
wenzelm@43419
   160
    val snapshot = model.snapshot()
wenzelm@43419
   161
    was_updated = was_outdated && !snapshot.is_outdated
wenzelm@43419
   162
    was_outdated = was_outdated || snapshot.is_outdated
wenzelm@43419
   163
    snapshot
wenzelm@43419
   164
  }
wenzelm@43419
   165
wenzelm@43419
   166
  def flush_snapshot(): (Boolean, Document.Snapshot) =
wenzelm@43419
   167
  {
wenzelm@43419
   168
    Swing_Thread.require()
wenzelm@43419
   169
    val snapshot = update_snapshot()
wenzelm@43419
   170
    val updated = was_updated
wenzelm@43419
   171
    if (updated) { was_outdated = false; was_updated = false }
wenzelm@43419
   172
    (updated, snapshot)
wenzelm@43419
   173
  }
wenzelm@43419
   174
wenzelm@43419
   175
wenzelm@39740
   176
  /* HTML popups */
wenzelm@39740
   177
wenzelm@39740
   178
  private var html_popup: Option[Popup] = None
wenzelm@34834
   179
wenzelm@39740
   180
  private def exit_popup() { html_popup.map(_.hide) }
wenzelm@38881
   181
wenzelm@39740
   182
  private val html_panel =
wenzelm@43661
   183
    new HTML_Panel(Isabelle.font_family(), scala.math.round(Isabelle.font_size()))
wenzelm@39741
   184
  html_panel.setBorder(BorderFactory.createLineBorder(Color.black))
wenzelm@38884
   185
wenzelm@39740
   186
  private def html_panel_resize()
wenzelm@39740
   187
  {
wenzelm@39740
   188
    Swing_Thread.now {
wenzelm@39740
   189
      html_panel.resize(Isabelle.font_family(), scala.math.round(Isabelle.font_size()))
wenzelm@39740
   190
    }
wenzelm@39740
   191
  }
wenzelm@38884
   192
wenzelm@39740
   193
  private def init_popup(snapshot: Document.Snapshot, x: Int, y: Int)
wenzelm@39740
   194
  {
wenzelm@39740
   195
    exit_popup()
wenzelm@40154
   196
/* FIXME broken
wenzelm@39740
   197
    val offset = text_area.xyToOffset(x, y)
wenzelm@39740
   198
    val p = new Point(x, y); SwingUtilities.convertPointToScreen(p, text_area.getPainter)
wenzelm@34784
   199
wenzelm@39740
   200
    // FIXME snapshot.cumulate
wenzelm@45665
   201
    snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Rendering.popup) match {
wenzelm@39740
   202
      case Text.Info(_, Some(msg)) #:: _ =>
wenzelm@39740
   203
        val popup = PopupFactory.getSharedInstance().getPopup(text_area, html_panel, p.x, p.y + 60)
wenzelm@39740
   204
        html_panel.render_sync(List(msg))
wenzelm@39742
   205
        Thread.sleep(10)  // FIXME !?
wenzelm@39740
   206
        popup.show
wenzelm@39740
   207
        html_popup = Some(popup)
wenzelm@39740
   208
      case _ =>
immler@34403
   209
    }
wenzelm@40154
   210
*/
immler@34678
   211
  }
immler@34403
   212
immler@34403
   213
wenzelm@39131
   214
  /* subexpression highlighting */
wenzelm@39131
   215
wenzelm@46205
   216
  @volatile private var _highlight_range: Option[Text.Info[Color]] = None
wenzelm@46205
   217
  def highlight_range(): Option[Text.Info[Color]] = _highlight_range
wenzelm@39740
   218
wenzelm@40338
   219
  private var control: Boolean = false
wenzelm@40338
   220
wenzelm@39740
   221
  private def exit_control()
wenzelm@39740
   222
  {
wenzelm@39740
   223
    exit_popup()
wenzelm@43381
   224
    _highlight_range = None
wenzelm@39740
   225
  }
wenzelm@39740
   226
wenzelm@39131
   227
  private val focus_listener = new FocusAdapter {
wenzelm@39742
   228
    override def focusLost(e: FocusEvent) {
wenzelm@43381
   229
      _highlight_range = None // FIXME exit_control !?
wenzelm@39742
   230
    }
wenzelm@39131
   231
  }
wenzelm@39131
   232
wenzelm@39741
   233
  private val window_listener = new WindowAdapter {
wenzelm@39741
   234
    override def windowIconified(e: WindowEvent) { exit_control() }
wenzelm@39742
   235
    override def windowDeactivated(e: WindowEvent) { exit_control() }
wenzelm@39741
   236
  }
wenzelm@39741
   237
wenzelm@39131
   238
  private val mouse_motion_listener = new MouseMotionAdapter {
wenzelm@39131
   239
    override def mouseMoved(e: MouseEvent) {
wenzelm@40338
   240
      control = if (OperatingSystem.isMacOS()) e.isMetaDown else e.isControlDown
wenzelm@39740
   241
      val x = e.getX()
wenzelm@39740
   242
      val y = e.getY()
wenzelm@39740
   243
wenzelm@39740
   244
      if (!model.buffer.isLoaded) exit_control()
wenzelm@39132
   245
      else
wenzelm@39132
   246
        Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@43419
   247
          val snapshot = update_snapshot()
wenzelm@39740
   248
wenzelm@39740
   249
          if (control) init_popup(snapshot, x, y)
wenzelm@39740
   250
wenzelm@46205
   251
          for (Text.Info(range, _) <- _highlight_range) invalidate_range(range)
wenzelm@46205
   252
          _highlight_range =
wenzelm@46205
   253
            if (control) {
wenzelm@46205
   254
              val offset = text_area.xyToOffset(x, y)
wenzelm@46205
   255
              Isabelle_Rendering.subexp(snapshot, Text.Range(offset, offset + 1))
wenzelm@46205
   256
            }
wenzelm@46205
   257
            else None
wenzelm@46205
   258
          for (Text.Info(range, _) <- _highlight_range) invalidate_range(range)
wenzelm@39132
   259
        }
wenzelm@39131
   260
    }
wenzelm@39131
   261
  }
wenzelm@39131
   262
wenzelm@39131
   263
wenzelm@43381
   264
  /* text area painting */
wenzelm@39044
   265
wenzelm@43381
   266
  private val text_area_painter = new Text_Area_Painter(this)
wenzelm@39700
   267
wenzelm@43381
   268
  private val tooltip_painter = new TextAreaExtension
wenzelm@43381
   269
  {
wenzelm@34784
   270
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   271
    {
wenzelm@43404
   272
      robust_body(null: String) {
wenzelm@43419
   273
        val snapshot = update_snapshot()
wenzelm@38845
   274
        val offset = text_area.xyToOffset(x, y)
wenzelm@45460
   275
        val range = Text.Range(offset, offset + 1)
wenzelm@46178
   276
        val tip =
wenzelm@46178
   277
          if (control) Isabelle_Rendering.tooltip(snapshot, range)
wenzelm@46178
   278
          else Isabelle_Rendering.tooltip_message(snapshot, range)
wenzelm@46178
   279
        tip.map(Isabelle.tooltip(_)) getOrElse null
wenzelm@34784
   280
      }
wenzelm@34734
   281
    }
immler@34678
   282
  }
immler@34513
   283
wenzelm@42825
   284
  private val gutter_painter = new TextAreaExtension
wenzelm@39175
   285
  {
wenzelm@39175
   286
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@39175
   287
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@39175
   288
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@39175
   289
    {
wenzelm@43404
   290
      robust_body(()) {
wenzelm@43404
   291
        val gutter = text_area.getGutter
wenzelm@43404
   292
        val width = GutterOptionPane.getSelectionAreaWidth
wenzelm@43404
   293
        val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
wenzelm@43404
   294
        val FOLD_MARKER_SIZE = 12
wenzelm@43419
   295
wenzelm@43404
   296
        if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
wenzelm@43404
   297
          Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@43419
   298
            val snapshot = update_snapshot()
wenzelm@43404
   299
            for (i <- 0 until physical_lines.length) {
wenzelm@43404
   300
              if (physical_lines(i) != -1) {
wenzelm@43404
   301
                val line_range = proper_line_range(start(i), end(i))
wenzelm@43419
   302
wenzelm@43404
   303
                // gutter icons
wenzelm@46178
   304
                Isabelle_Rendering.gutter_message(snapshot, line_range) match {
wenzelm@46178
   305
                  case Some(icon) =>
wenzelm@46227
   306
                    val x0 = (FOLD_MARKER_SIZE + width - border_width - icon.getIconWidth) max 10
wenzelm@46227
   307
                    val y0 = y + i * line_height + (((line_height - icon.getIconHeight) / 2) max 0)
wenzelm@46227
   308
                    icon.paintIcon(gutter, gfx, x0, y0)
wenzelm@46178
   309
                  case None =>
wenzelm@43404
   310
                }
wenzelm@39175
   311
              }
wenzelm@39175
   312
            }
wenzelm@39175
   313
          }
wenzelm@39175
   314
        }
wenzelm@39175
   315
      }
wenzelm@39175
   316
    }
wenzelm@39175
   317
  }
wenzelm@39175
   318
wenzelm@39175
   319
wenzelm@43448
   320
  /* caret range */
wenzelm@43448
   321
wenzelm@43448
   322
  def caret_range(): Text.Range =
wenzelm@46229
   323
    Isabelle.buffer_lock(model.buffer) {
wenzelm@46229
   324
      def text(i: Text.Offset): Char = model.buffer.getText(i, 1).charAt(0)
wenzelm@46229
   325
      val caret = text_area.getCaretPosition
wenzelm@46229
   326
      try {
wenzelm@46229
   327
        val c = text(caret)
wenzelm@46229
   328
        if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(caret + 1)))
wenzelm@46229
   329
          Text.Range(caret, caret + 2)
wenzelm@46229
   330
        else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(caret - 1)))
wenzelm@46229
   331
          Text.Range(caret - 1, caret + 1)
wenzelm@46229
   332
        else Text.Range(caret, caret + 1)
wenzelm@46229
   333
      }
wenzelm@46229
   334
      catch { case _: ArrayIndexOutOfBoundsException => Text.Range(caret, caret + 1) }
wenzelm@43448
   335
    }
wenzelm@43448
   336
wenzelm@43448
   337
wenzelm@37129
   338
  /* caret handling */
wenzelm@34810
   339
wenzelm@37849
   340
  def selected_command(): Option[Command] =
wenzelm@38223
   341
  {
wenzelm@38223
   342
    Swing_Thread.require()
wenzelm@43419
   343
    update_snapshot().node.proper_command_at(text_area.getCaretPosition)
wenzelm@38223
   344
  }
wenzelm@34810
   345
wenzelm@37849
   346
  private val caret_listener = new CaretListener {
wenzelm@37849
   347
    private val delay = Swing_Thread.delay_last(session.input_delay) {
wenzelm@44805
   348
      session.caret_focus.event(Session.Caret_Focus)
wenzelm@34810
   349
    }
wenzelm@37849
   350
    override def caretUpdate(e: CaretEvent) { delay() }
wenzelm@34810
   351
  }
wenzelm@34810
   352
wenzelm@34810
   353
wenzelm@46572
   354
  /* text status overview left of scrollbar */
wenzelm@34784
   355
wenzelm@46572
   356
  private val overview = new Text_Overview(this)
wenzelm@46572
   357
  {
wenzelm@46571
   358
    val delay_repaint =
wenzelm@46571
   359
      Swing_Thread.delay_first(Isabelle.session.update_delay) { repaint() }
wenzelm@34784
   360
  }
immler@34403
   361
wenzelm@34784
   362
wenzelm@39740
   363
  /* main actor */
wenzelm@39740
   364
wenzelm@39740
   365
  private val main_actor = actor {
wenzelm@39740
   366
    loop {
wenzelm@39740
   367
      react {
wenzelm@44608
   368
        case changed: Session.Commands_Changed =>
wenzelm@39740
   369
          val buffer = model.buffer
wenzelm@39740
   370
          Isabelle.swing_buffer_lock(buffer) {
wenzelm@43419
   371
            val (updated, snapshot) = flush_snapshot()
wenzelm@39740
   372
wenzelm@44608
   373
            if (updated ||
wenzelm@44615
   374
                (changed.nodes.contains(model.name) &&
wenzelm@44608
   375
                 changed.commands.exists(snapshot.node.commands.contains)))
wenzelm@46571
   376
              overview.delay_repaint()
wenzelm@39740
   377
wenzelm@46583
   378
            visible_range() match {
wenzelm@46583
   379
              case None =>
wenzelm@46583
   380
              case Some(visible) =>
wenzelm@46583
   381
                if (updated) invalidate_range(visible)
wenzelm@46583
   382
                else {
wenzelm@46583
   383
                  val visible_cmds =
wenzelm@46583
   384
                    snapshot.node.command_range(snapshot.revert(visible)).map(_._1)
wenzelm@46583
   385
                  if (visible_cmds.exists(changed.commands)) {
wenzelm@46583
   386
                    for {
wenzelm@46583
   387
                      line <- 0 until text_area.getVisibleLines
wenzelm@46583
   388
                      val start = text_area.getScreenLineStartOffset(line) if start >= 0
wenzelm@46583
   389
                      val end = text_area.getScreenLineEndOffset(line) if end >= 0
wenzelm@46583
   390
                      val range = proper_line_range(start, end)
wenzelm@46583
   391
                      val line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
wenzelm@46583
   392
                      if line_cmds.exists(changed.commands)
wenzelm@46583
   393
                    } text_area.invalidateScreenLineRange(line, line)
wenzelm@46583
   394
                  }
wenzelm@46583
   395
                }
wenzelm@39740
   396
            }
wenzelm@39740
   397
          }
wenzelm@39740
   398
wenzelm@39740
   399
        case Session.Global_Settings => html_panel_resize()
wenzelm@39740
   400
wenzelm@39740
   401
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@39740
   402
      }
wenzelm@39740
   403
    }
wenzelm@39740
   404
  }
wenzelm@39740
   405
wenzelm@39740
   406
wenzelm@34784
   407
  /* activation */
wenzelm@34784
   408
wenzelm@34808
   409
  private def activate()
wenzelm@34784
   410
  {
wenzelm@42825
   411
    val painter = text_area.getPainter
wenzelm@44437
   412
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, update_perspective)
wenzelm@43387
   413
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, tooltip_painter)
wenzelm@43381
   414
    text_area_painter.activate()
wenzelm@42825
   415
    text_area.getGutter.addExtension(gutter_painter)
wenzelm@39131
   416
    text_area.addFocusListener(focus_listener)
wenzelm@39741
   417
    text_area.getView.addWindowListener(window_listener)
wenzelm@42825
   418
    painter.addMouseMotionListener(mouse_motion_listener)
wenzelm@34810
   419
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   420
    text_area.addLeftOfScrollBar(overview)
wenzelm@39740
   421
    session.commands_changed += main_actor
wenzelm@39740
   422
    session.global_settings += main_actor
wenzelm@34784
   423
  }
wenzelm@34784
   424
wenzelm@34808
   425
  private def deactivate()
wenzelm@34784
   426
  {
wenzelm@42825
   427
    val painter = text_area.getPainter
wenzelm@39740
   428
    session.commands_changed -= main_actor
wenzelm@39740
   429
    session.global_settings -= main_actor
wenzelm@39131
   430
    text_area.removeFocusListener(focus_listener)
wenzelm@39741
   431
    text_area.getView.removeWindowListener(window_listener)
wenzelm@42825
   432
    painter.removeMouseMotionListener(mouse_motion_listener)
wenzelm@39131
   433
    text_area.removeCaretListener(caret_listener)
wenzelm@34810
   434
    text_area.removeLeftOfScrollBar(overview)
wenzelm@42825
   435
    text_area.getGutter.removeExtension(gutter_painter)
wenzelm@43381
   436
    text_area_painter.deactivate()
wenzelm@43381
   437
    painter.removeExtension(tooltip_painter)
wenzelm@44437
   438
    painter.removeExtension(update_perspective)
wenzelm@39740
   439
    exit_popup()
wenzelm@34784
   440
  }
wenzelm@42825
   441
}