src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Sat Dec 27 14:13:05 2008 +0100 (2008-12-27 ago)
changeset 34440 561a6d19bd95
parent 34433 3da749b53842
child 34441 ff3b7ae2b12a
permissions -rw-r--r--
renamed object Plugin to Isabelle;
renamed Plugin.self to Isabelle.plugin;
renamed Isabelle.plugin.symbols to Isabelle.symbols;
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@34440
    39
  // Isabelle symbols
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@34433
    57
  val font_changed = new EventSource[Font]
immler@34406
    58
wenzelm@34433
    59
  def set_font(path: String, size: Float) {
wenzelm@34433
    60
    try {
wenzelm@34433
    61
      font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path)).
wenzelm@34433
    62
        deriveFont(Font.PLAIN, size)
wenzelm@34433
    63
      font_changed.fire(font)
wenzelm@34433
    64
    }
wenzelm@34433
    65
    catch {
wenzelm@34433
    66
      case e: IOException =>
wenzelm@34433
    67
    }
wenzelm@34433
    68
  }
wenzelm@34433
    69
wenzelm@34433
    70
wenzelm@34433
    71
  // mapping buffer <-> prover
wenzelm@34433
    72
wenzelm@34433
    73
  private val mapping = new HashMap[JEditBuffer, ProverSetup]
immler@34406
    74
wenzelm@34429
    75
  def install(view: View) {
immler@34406
    76
    val buffer = view.getBuffer
immler@34406
    77
    mapping.get(buffer) match{
immler@34406
    78
      case None =>{
immler@34406
    79
          val prover_setup = new ProverSetup(buffer)
immler@34406
    80
          mapping.update(buffer, prover_setup)
immler@34406
    81
          prover_setup.activate(view)
immler@34406
    82
      }
immler@34406
    83
      case _ => System.err.println("Already installed plugin on Buffer")
immler@34406
    84
    }
wenzelm@34318
    85
  }
immler@34406
    86
wenzelm@34429
    87
  def uninstall(view: View) {
immler@34406
    88
    val buffer = view.getBuffer
immler@34406
    89
    mapping.removeKey(buffer) match{
immler@34406
    90
      case None => System.err.println("No Plugin installed on this Buffer")
immler@34406
    91
      case Some(proversetup) =>
immler@34406
    92
        proversetup.deactivate
immler@34406
    93
    }
wenzelm@34318
    94
  }
immler@34406
    95
wenzelm@34429
    96
  def prover_setup(buffer: JEditBuffer): Option[ProverSetup] = mapping.get(buffer)
wenzelm@34433
    97
  def is_active(buffer: JEditBuffer) = mapping.isDefinedAt(buffer)
wenzelm@34433
    98
wenzelm@34318
    99
  
wenzelm@34433
   100
  // main plugin plumbing
wenzelm@34433
   101
wenzelm@34429
   102
  override def handleMessage(msg: EBMessage) = msg match {
wenzelm@34433
   103
    case epu: EditPaneUpdate => epu.getWhat match {
wenzelm@34318
   104
      case EditPaneUpdate.BUFFER_CHANGED =>
immler@34406
   105
        mapping get epu.getEditPane.getBuffer match {
immler@34406
   106
          //only activate 'isabelle'-buffers!
immler@34406
   107
          case None =>
immler@34406
   108
          case Some(prover_setup) => 
immler@34406
   109
            prover_setup.theory_view.activate
wenzelm@34422
   110
            val dockable = epu.getEditPane.getView.getDockableWindowManager.getDockable("isabelle-output")
immler@34406
   111
            if(dockable != null) {
immler@34406
   112
              val output_dockable = dockable.asInstanceOf[OutputDockable]
immler@34406
   113
              if(output_dockable.getComponent(0) != prover_setup.output_text_view ) {
immler@34406
   114
                output_dockable.asInstanceOf[OutputDockable].removeAll
immler@34406
   115
                output_dockable.asInstanceOf[OutputDockable].add(new JScrollPane(prover_setup.output_text_view))
immler@34406
   116
                output_dockable.revalidate
immler@34406
   117
              }
immler@34406
   118
            }
immler@34406
   119
        }
wenzelm@34318
   120
      case EditPaneUpdate.BUFFER_CHANGING =>
immler@34406
   121
        val buffer = epu.getEditPane.getBuffer
immler@34406
   122
        if(buffer != null) mapping get buffer match {
immler@34406
   123
          //only deactivate 'isabelle'-buffers!
immler@34406
   124
          case None =>
immler@34406
   125
          case Some(prover_setup) => prover_setup.theory_view.deactivate
immler@34406
   126
        }
wenzelm@34318
   127
      case _ =>
wenzelm@34318
   128
    }
wenzelm@34318
   129
    case _ =>
wenzelm@34318
   130
  }
wenzelm@34318
   131
wenzelm@34318
   132
  override def start() {
wenzelm@34440
   133
    Isabelle.symbols = new Symbol.Interpretation
wenzelm@34440
   134
    Isabelle.plugin = this
immler@34406
   135
    
wenzelm@34440
   136
    if (Isabelle.property("font-path") != null && Isabelle.property("font-size") != null)
wenzelm@34318
   137
      try {
wenzelm@34440
   138
        set_font(Isabelle.property("font-path"), Isabelle.property("font-size").toFloat)
wenzelm@34318
   139
      }
wenzelm@34318
   140
      catch {
wenzelm@34429
   141
        case e: NumberFormatException =>
wenzelm@34318
   142
      }
wenzelm@34318
   143
  }
wenzelm@34318
   144
  
wenzelm@34318
   145
  override def stop() {
wenzelm@34429
   146
    // TODO: proper cleanup
wenzelm@34440
   147
    Isabelle.plugin = null
wenzelm@34440
   148
    Isabelle.symbols = null
wenzelm@34318
   149
  }
wenzelm@34318
   150
}