src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Sat Dec 27 15:03:32 2008 +0100 (2008-12-27)
changeset 34441 ff3b7ae2b12a
parent 34440 561a6d19bd95
child 34443 f2e13329cc49
permissions -rw-r--r--
replaced static IsabelleSystem by Isabelle.system;
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.utils.EventSource
wenzelm@34429
    18
import isabelle.prover.{Prover, Command}
wenzelm@34433
    19
import isabelle.{IsabelleSystem, Symbol}
wenzelm@34429
    20
wenzelm@34429
    21
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, EditPane, ServiceManager, View}
immler@34406
    22
import org.gjt.sp.jedit.buffer.JEditBuffer
immler@34406
    23
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@34429
    24
import org.gjt.sp.jedit.msg.{EditPaneUpdate, PropertiesChanged}
wenzelm@34429
    25
wenzelm@34318
    26
wenzelm@34440
    27
object Isabelle {
wenzelm@34433
    28
  // name
wenzelm@34318
    29
  val NAME = "Isabelle"
wenzelm@34318
    30
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34337
    31
  val VFS_PREFIX = "isabelle:"
wenzelm@34433
    32
wenzelm@34433
    33
  // properties
wenzelm@34429
    34
  def property(name: String) = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34429
    35
  def property(name: String, value: String) = 
wenzelm@34318
    36
    jEdit.setProperty(OPTION_PREFIX + name, value)
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@34433
    58
  val font_changed = new EventSource[Font]
immler@34406
    59
wenzelm@34433
    60
  def set_font(path: String, size: Float) {
wenzelm@34433
    61
    try {
wenzelm@34433
    62
      font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path)).
wenzelm@34433
    63
        deriveFont(Font.PLAIN, size)
wenzelm@34433
    64
      font_changed.fire(font)
wenzelm@34433
    65
    }
wenzelm@34433
    66
    catch {
wenzelm@34433
    67
      case e: IOException =>
wenzelm@34433
    68
    }
wenzelm@34433
    69
  }
wenzelm@34433
    70
wenzelm@34433
    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@34441
   135
    Isabelle.symbols = new Symbol.Interpretation(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
}