src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Sun Jan 11 19:32:26 2009 +0100 (2009-01-11 ago)
changeset 34468 9d4b4f290676
parent 34463 b510b7d88de2
child 34475 f963335dbc6b
permissions -rw-r--r--
maintain Isabelle properties via object Isabelle.Property with apply/update methods;
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
immler@34406
    15
import scala.collection.mutable.HashMap
immler@34406
    16
wenzelm@34429
    17
import isabelle.prover.{Prover, Command}
wenzelm@34433
    18
import isabelle.{IsabelleSystem, Symbol}
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@34440
    38
wenzelm@34441
    39
  // Isabelle system
wenzelm@34441
    40
  var system: IsabelleSystem = null
wenzelm@34440
    41
  var symbols: Symbol.Interpretation = null
wenzelm@34440
    42
wenzelm@34440
    43
wenzelm@34440
    44
  // plugin instance
wenzelm@34440
    45
  var plugin: Plugin = null
wenzelm@34433
    46
wenzelm@34433
    47
  // provers
wenzelm@34440
    48
  def prover(buffer: JEditBuffer) = plugin.prover_setup(buffer).get.prover
wenzelm@34440
    49
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer).get
wenzelm@34318
    50
}
wenzelm@34318
    51
wenzelm@34429
    52
wenzelm@34318
    53
class Plugin extends EBPlugin {
wenzelm@34433
    54
wenzelm@34433
    55
  // Isabelle font
wenzelm@34433
    56
wenzelm@34433
    57
  var font: Font = null
wenzelm@34456
    58
  val font_changed = new EventBus[Font]
immler@34406
    59
wenzelm@34433
    60
  def set_font(path: String, size: Float) {
wenzelm@34456
    61
    font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path)).
wenzelm@34456
    62
      deriveFont(Font.PLAIN, size)
wenzelm@34456
    63
    font_changed.event(font)
wenzelm@34433
    64
  }
wenzelm@34433
    65
wenzelm@34433
    66
wenzelm@34449
    67
  /* unique ids */  // FIXME specific to "session" (!??)
wenzelm@34449
    68
wenzelm@34449
    69
  private var id_count: BigInt = 0
wenzelm@34449
    70
  def id() : String = synchronized { id_count += 1; "editor:" + id_count }
wenzelm@34449
    71
wenzelm@34449
    72
wenzelm@34433
    73
  // mapping buffer <-> prover
wenzelm@34433
    74
wenzelm@34433
    75
  private val mapping = new HashMap[JEditBuffer, ProverSetup]
immler@34406
    76
immler@34463
    77
  private def install(view: View) {
immler@34406
    78
    val buffer = view.getBuffer
immler@34463
    79
    val prover_setup = new ProverSetup(buffer)
immler@34463
    80
    mapping.update(buffer, prover_setup)
immler@34463
    81
    prover_setup.activate(view)
wenzelm@34318
    82
  }
immler@34406
    83
immler@34463
    84
  private def uninstall(view: View) =
immler@34463
    85
    mapping.removeKey(view.getBuffer).get.deactivate
immler@34463
    86
immler@34463
    87
  def switch_active (view : View) =
immler@34463
    88
    if (mapping.isDefinedAt(view.getBuffer)) uninstall(view)
immler@34463
    89
    else install(view)
immler@34406
    90
immler@34406
    91
  def prover_setup (buffer : JEditBuffer) : Option[ProverSetup] = mapping.get(buffer)
immler@34462
    92
  def is_active (buffer : JEditBuffer) = mapping.isDefinedAt(buffer)
immler@34463
    93
  
wenzelm@34318
    94
  
wenzelm@34433
    95
  // main plugin plumbing
wenzelm@34433
    96
wenzelm@34429
    97
  override def handleMessage(msg: EBMessage) = msg match {
wenzelm@34433
    98
    case epu: EditPaneUpdate => epu.getWhat match {
wenzelm@34318
    99
      case EditPaneUpdate.BUFFER_CHANGED =>
immler@34406
   100
        mapping get epu.getEditPane.getBuffer match {
immler@34406
   101
          //only activate 'isabelle'-buffers!
immler@34406
   102
          case None =>
immler@34406
   103
          case Some(prover_setup) => 
immler@34406
   104
            prover_setup.theory_view.activate
wenzelm@34422
   105
            val dockable = epu.getEditPane.getView.getDockableWindowManager.getDockable("isabelle-output")
immler@34406
   106
            if(dockable != null) {
immler@34406
   107
              val output_dockable = dockable.asInstanceOf[OutputDockable]
immler@34406
   108
              if(output_dockable.getComponent(0) != prover_setup.output_text_view ) {
immler@34406
   109
                output_dockable.asInstanceOf[OutputDockable].removeAll
immler@34406
   110
                output_dockable.asInstanceOf[OutputDockable].add(new JScrollPane(prover_setup.output_text_view))
immler@34406
   111
                output_dockable.revalidate
immler@34406
   112
              }
immler@34406
   113
            }
immler@34406
   114
        }
wenzelm@34318
   115
      case EditPaneUpdate.BUFFER_CHANGING =>
immler@34406
   116
        val buffer = epu.getEditPane.getBuffer
immler@34406
   117
        if(buffer != null) mapping get buffer match {
immler@34406
   118
          //only deactivate 'isabelle'-buffers!
immler@34406
   119
          case None =>
immler@34406
   120
          case Some(prover_setup) => prover_setup.theory_view.deactivate
immler@34406
   121
        }
wenzelm@34318
   122
      case _ =>
wenzelm@34318
   123
    }
wenzelm@34318
   124
    case _ =>
wenzelm@34318
   125
  }
wenzelm@34318
   126
wenzelm@34318
   127
  override def start() {
wenzelm@34441
   128
    Isabelle.system = new IsabelleSystem
wenzelm@34443
   129
    Isabelle.symbols = new Symbol.Interpretation(Isabelle.system)
wenzelm@34440
   130
    Isabelle.plugin = this
immler@34406
   131
    
wenzelm@34468
   132
    if (Isabelle.Property("font-path") != null && Isabelle.Property("font-size") != null)
wenzelm@34318
   133
      try {
wenzelm@34468
   134
        set_font(Isabelle.Property("font-path"), Isabelle.Property("font-size").toFloat)
wenzelm@34318
   135
      }
wenzelm@34318
   136
      catch {
wenzelm@34429
   137
        case e: NumberFormatException =>
wenzelm@34318
   138
      }
wenzelm@34318
   139
  }
wenzelm@34318
   140
  
wenzelm@34318
   141
  override def stop() {
wenzelm@34429
   142
    // TODO: proper cleanup
wenzelm@34441
   143
    Isabelle.symbols = null
wenzelm@34441
   144
    Isabelle.system = null
wenzelm@34440
   145
    Isabelle.plugin = null
wenzelm@34318
   146
  }
wenzelm@34318
   147
}