src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Tue Jan 27 16:16:55 2009 +0100 (2009-01-27 ago)
changeset 34497 184fda8cce04
parent 34487 b88ee385308d
child 34502 7d1d13750890
permissions -rw-r--r--
more explicit indication of mutable collections;
wenzelm@34407
     1
/*
wenzelm@34407
     2
 * Main Isabelle/jEdit plugin setup
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Johannes Hölzl, TU Munich
wenzelm@34407
     5
 * @author Fabian Immler, TU Munich
wenzelm@34407
     6
 */
wenzelm@34407
     7
wenzelm@34318
     8
package isabelle.jedit
wenzelm@34318
     9
wenzelm@34429
    10
wenzelm@34429
    11
import java.io.{FileInputStream, IOException}
wenzelm@34318
    12
import java.awt.Font
immler@34406
    13
import javax.swing.JScrollPane
wenzelm@34318
    14
wenzelm@34497
    15
import scala.collection.mutable
immler@34406
    16
wenzelm@34429
    17
import isabelle.prover.{Prover, Command}
wenzelm@34487
    18
import isabelle.IsabelleSystem
wenzelm@34429
    19
wenzelm@34429
    20
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, EditPane, ServiceManager, View}
immler@34406
    21
import org.gjt.sp.jedit.buffer.JEditBuffer
immler@34406
    22
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@34429
    23
import org.gjt.sp.jedit.msg.{EditPaneUpdate, PropertiesChanged}
wenzelm@34429
    24
wenzelm@34318
    25
wenzelm@34440
    26
object Isabelle {
wenzelm@34433
    27
  // name
wenzelm@34318
    28
  val NAME = "Isabelle"
wenzelm@34337
    29
  val VFS_PREFIX = "isabelle:"
wenzelm@34433
    30
wenzelm@34433
    31
  // properties
wenzelm@34468
    32
  object Property {
wenzelm@34468
    33
    private val OPTION_PREFIX = "options.isabelle."
wenzelm@34468
    34
    def apply(name: String) = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34468
    35
    def update(name: String, value: String) = jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34468
    36
  }
wenzelm@34433
    37
wenzelm@34487
    38
  // Isabelle system instance
wenzelm@34441
    39
  var system: IsabelleSystem = null
wenzelm@34487
    40
  def symbols = system.symbols
wenzelm@34440
    41
wenzelm@34440
    42
  // plugin instance
wenzelm@34440
    43
  var plugin: Plugin = null
wenzelm@34433
    44
wenzelm@34487
    45
  // running provers
immler@34475
    46
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer)
wenzelm@34318
    47
}
wenzelm@34318
    48
wenzelm@34429
    49
wenzelm@34318
    50
class Plugin extends EBPlugin {
wenzelm@34433
    51
wenzelm@34433
    52
  // Isabelle font
wenzelm@34433
    53
wenzelm@34433
    54
  var font: Font = null
wenzelm@34456
    55
  val font_changed = new EventBus[Font]
immler@34406
    56
wenzelm@34433
    57
  def set_font(path: String, size: Float) {
wenzelm@34456
    58
    font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path)).
wenzelm@34456
    59
      deriveFont(Font.PLAIN, size)
wenzelm@34456
    60
    font_changed.event(font)
wenzelm@34433
    61
  }
wenzelm@34433
    62
wenzelm@34433
    63
wenzelm@34449
    64
  /* unique ids */  // FIXME specific to "session" (!??)
wenzelm@34449
    65
wenzelm@34449
    66
  private var id_count: BigInt = 0
wenzelm@34449
    67
  def id() : String = synchronized { id_count += 1; "editor:" + id_count }
wenzelm@34449
    68
wenzelm@34449
    69
wenzelm@34433
    70
  // mapping buffer <-> prover
wenzelm@34433
    71
wenzelm@34497
    72
  private val mapping = new mutable.HashMap[JEditBuffer, ProverSetup]
immler@34406
    73
immler@34463
    74
  private def install(view: View) {
immler@34406
    75
    val buffer = view.getBuffer
immler@34463
    76
    val prover_setup = new ProverSetup(buffer)
immler@34463
    77
    mapping.update(buffer, prover_setup)
immler@34463
    78
    prover_setup.activate(view)
wenzelm@34318
    79
  }
immler@34406
    80
immler@34463
    81
  private def uninstall(view: View) =
immler@34463
    82
    mapping.removeKey(view.getBuffer).get.deactivate
immler@34463
    83
immler@34463
    84
  def switch_active (view : View) =
immler@34463
    85
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
immler@34463
    86
    else install(view)
immler@34406
    87
immler@34406
    88
  def prover_setup (buffer : JEditBuffer) : Option[ProverSetup] = mapping.get(buffer)
immler@34462
    89
  def is_active (buffer : JEditBuffer) = mapping.isDefinedAt(buffer)
immler@34463
    90
  
wenzelm@34318
    91
  
wenzelm@34433
    92
  // main plugin plumbing
wenzelm@34433
    93
wenzelm@34429
    94
  override def handleMessage(msg: EBMessage) = msg match {
wenzelm@34433
    95
    case epu: EditPaneUpdate => epu.getWhat match {
wenzelm@34318
    96
      case EditPaneUpdate.BUFFER_CHANGED =>
immler@34406
    97
        mapping get epu.getEditPane.getBuffer match {
immler@34406
    98
          //only activate 'isabelle'-buffers!
immler@34406
    99
          case None =>
immler@34406
   100
          case Some(prover_setup) => 
immler@34406
   101
            prover_setup.theory_view.activate
wenzelm@34422
   102
            val dockable = epu.getEditPane.getView.getDockableWindowManager.getDockable("isabelle-output")
immler@34406
   103
            if(dockable != null) {
immler@34406
   104
              val output_dockable = dockable.asInstanceOf[OutputDockable]
immler@34406
   105
              if(output_dockable.getComponent(0) != prover_setup.output_text_view ) {
immler@34406
   106
                output_dockable.asInstanceOf[OutputDockable].removeAll
immler@34406
   107
                output_dockable.asInstanceOf[OutputDockable].add(new JScrollPane(prover_setup.output_text_view))
immler@34406
   108
                output_dockable.revalidate
immler@34406
   109
              }
immler@34406
   110
            }
immler@34406
   111
        }
wenzelm@34318
   112
      case EditPaneUpdate.BUFFER_CHANGING =>
immler@34406
   113
        val buffer = epu.getEditPane.getBuffer
immler@34406
   114
        if(buffer != null) mapping get buffer match {
immler@34406
   115
          //only deactivate 'isabelle'-buffers!
immler@34406
   116
          case None =>
immler@34406
   117
          case Some(prover_setup) => prover_setup.theory_view.deactivate
immler@34406
   118
        }
wenzelm@34318
   119
      case _ =>
wenzelm@34318
   120
    }
wenzelm@34318
   121
    case _ =>
wenzelm@34318
   122
  }
wenzelm@34318
   123
wenzelm@34318
   124
  override def start() {
wenzelm@34441
   125
    Isabelle.system = new IsabelleSystem
wenzelm@34440
   126
    Isabelle.plugin = this
immler@34406
   127
    
wenzelm@34468
   128
    if (Isabelle.Property("font-path") != null && Isabelle.Property("font-size") != null)
wenzelm@34318
   129
      try {
wenzelm@34468
   130
        set_font(Isabelle.Property("font-path"), Isabelle.Property("font-size").toFloat)
wenzelm@34318
   131
      }
wenzelm@34318
   132
      catch {
wenzelm@34429
   133
        case e: NumberFormatException =>
wenzelm@34318
   134
      }
wenzelm@34318
   135
  }
wenzelm@34318
   136
  
wenzelm@34318
   137
  override def stop() {
wenzelm@34429
   138
    // TODO: proper cleanup
wenzelm@34441
   139
    Isabelle.system = null
wenzelm@34440
   140
    Isabelle.plugin = null
wenzelm@34318
   141
  }
wenzelm@34318
   142
}