src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Mon Dec 29 20:43:04 2008 +0100 (2008-12-29)
changeset 34456 14367c0715e8
parent 34449 57fce8528a22
child 34462 fefbd0421e4e
permissions -rw-r--r--
replaced EventSource by EventBus;
misc tuning;
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@34318
    29
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34337
    30
  val VFS_PREFIX = "isabelle:"
wenzelm@34433
    31
wenzelm@34433
    32
  // properties
wenzelm@34429
    33
  def property(name: String) = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34429
    34
  def property(name: String, value: String) = 
wenzelm@34318
    35
    jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34433
    36
wenzelm@34440
    37
wenzelm@34441
    38
  // Isabelle system
wenzelm@34441
    39
  var system: IsabelleSystem = null
wenzelm@34440
    40
  var symbols: Symbol.Interpretation = null
wenzelm@34440
    41
wenzelm@34440
    42
wenzelm@34440
    43
  // plugin instance
wenzelm@34440
    44
  var plugin: Plugin = null
wenzelm@34433
    45
wenzelm@34433
    46
  // provers
wenzelm@34440
    47
  def prover(buffer: JEditBuffer) = plugin.prover_setup(buffer).get.prover
wenzelm@34440
    48
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer).get
wenzelm@34318
    49
}
wenzelm@34318
    50
wenzelm@34429
    51
wenzelm@34318
    52
class Plugin extends EBPlugin {
wenzelm@34433
    53
wenzelm@34433
    54
  // Isabelle font
wenzelm@34433
    55
wenzelm@34433
    56
  var font: Font = null
wenzelm@34456
    57
  val font_changed = new EventBus[Font]
immler@34406
    58
wenzelm@34433
    59
  def set_font(path: String, size: Float) {
wenzelm@34456
    60
    font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path)).
wenzelm@34456
    61
      deriveFont(Font.PLAIN, size)
wenzelm@34456
    62
    font_changed.event(font)
wenzelm@34433
    63
  }
wenzelm@34433
    64
wenzelm@34433
    65
wenzelm@34449
    66
  /* unique ids */  // FIXME specific to "session" (!??)
wenzelm@34449
    67
wenzelm@34449
    68
  private var id_count: BigInt = 0
wenzelm@34449
    69
  def id() : String = synchronized { id_count += 1; "editor:" + id_count }
wenzelm@34449
    70
wenzelm@34449
    71
wenzelm@34433
    72
  // mapping buffer <-> prover
wenzelm@34433
    73
wenzelm@34433
    74
  private val mapping = new HashMap[JEditBuffer, ProverSetup]
immler@34406
    75
wenzelm@34429
    76
  def install(view: View) {
immler@34406
    77
    val buffer = view.getBuffer
immler@34406
    78
    mapping.get(buffer) match{
immler@34406
    79
      case None =>{
immler@34406
    80
          val prover_setup = new ProverSetup(buffer)
immler@34406
    81
          mapping.update(buffer, prover_setup)
immler@34406
    82
          prover_setup.activate(view)
immler@34406
    83
      }
immler@34406
    84
      case _ => System.err.println("Already installed plugin on Buffer")
immler@34406
    85
    }
wenzelm@34318
    86
  }
immler@34406
    87
wenzelm@34429
    88
  def uninstall(view: View) {
immler@34406
    89
    val buffer = view.getBuffer
immler@34406
    90
    mapping.removeKey(buffer) match{
immler@34406
    91
      case None => System.err.println("No Plugin installed on this Buffer")
immler@34406
    92
      case Some(proversetup) =>
immler@34406
    93
        proversetup.deactivate
immler@34406
    94
    }
wenzelm@34318
    95
  }
immler@34406
    96
wenzelm@34429
    97
  def prover_setup(buffer: JEditBuffer): Option[ProverSetup] = mapping.get(buffer)
wenzelm@34433
    98
  def is_active(buffer: JEditBuffer) = mapping.isDefinedAt(buffer)
wenzelm@34433
    99
wenzelm@34318
   100
  
wenzelm@34433
   101
  // main plugin plumbing
wenzelm@34433
   102
wenzelm@34429
   103
  override def handleMessage(msg: EBMessage) = msg match {
wenzelm@34433
   104
    case epu: EditPaneUpdate => epu.getWhat match {
wenzelm@34318
   105
      case EditPaneUpdate.BUFFER_CHANGED =>
immler@34406
   106
        mapping get epu.getEditPane.getBuffer match {
immler@34406
   107
          //only activate 'isabelle'-buffers!
immler@34406
   108
          case None =>
immler@34406
   109
          case Some(prover_setup) => 
immler@34406
   110
            prover_setup.theory_view.activate
wenzelm@34422
   111
            val dockable = epu.getEditPane.getView.getDockableWindowManager.getDockable("isabelle-output")
immler@34406
   112
            if(dockable != null) {
immler@34406
   113
              val output_dockable = dockable.asInstanceOf[OutputDockable]
immler@34406
   114
              if(output_dockable.getComponent(0) != prover_setup.output_text_view ) {
immler@34406
   115
                output_dockable.asInstanceOf[OutputDockable].removeAll
immler@34406
   116
                output_dockable.asInstanceOf[OutputDockable].add(new JScrollPane(prover_setup.output_text_view))
immler@34406
   117
                output_dockable.revalidate
immler@34406
   118
              }
immler@34406
   119
            }
immler@34406
   120
        }
wenzelm@34318
   121
      case EditPaneUpdate.BUFFER_CHANGING =>
immler@34406
   122
        val buffer = epu.getEditPane.getBuffer
immler@34406
   123
        if(buffer != null) mapping get buffer match {
immler@34406
   124
          //only deactivate 'isabelle'-buffers!
immler@34406
   125
          case None =>
immler@34406
   126
          case Some(prover_setup) => prover_setup.theory_view.deactivate
immler@34406
   127
        }
wenzelm@34318
   128
      case _ =>
wenzelm@34318
   129
    }
wenzelm@34318
   130
    case _ =>
wenzelm@34318
   131
  }
wenzelm@34318
   132
wenzelm@34318
   133
  override def start() {
wenzelm@34441
   134
    Isabelle.system = new IsabelleSystem
wenzelm@34443
   135
    Isabelle.symbols = new Symbol.Interpretation(Isabelle.system)
wenzelm@34440
   136
    Isabelle.plugin = this
immler@34406
   137
    
wenzelm@34440
   138
    if (Isabelle.property("font-path") != null && Isabelle.property("font-size") != null)
wenzelm@34318
   139
      try {
wenzelm@34440
   140
        set_font(Isabelle.property("font-path"), Isabelle.property("font-size").toFloat)
wenzelm@34318
   141
      }
wenzelm@34318
   142
      catch {
wenzelm@34429
   143
        case e: NumberFormatException =>
wenzelm@34318
   144
      }
wenzelm@34318
   145
  }
wenzelm@34318
   146
  
wenzelm@34318
   147
  override def stop() {
wenzelm@34429
   148
    // TODO: proper cleanup
wenzelm@34441
   149
    Isabelle.symbols = null
wenzelm@34441
   150
    Isabelle.system = null
wenzelm@34440
   151
    Isabelle.plugin = null
wenzelm@34318
   152
  }
wenzelm@34318
   153
}