src/Tools/jEdit/src/jedit/Plugin.scala
author wenzelm
Sun Dec 21 20:16:43 2008 +0100 (2008-12-21)
changeset 34429 03afc73e185f
parent 34422 d5a41da986c3
child 34433 3da749b53842
permissions -rw-r--r--
tuned;
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@34429
    19
import isabelle.IsabelleSystem
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@34318
    27
object Plugin {
wenzelm@34318
    28
  val NAME = "Isabelle"
wenzelm@34318
    29
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34337
    30
  val VFS_PREFIX = "isabelle:"
wenzelm@34318
    31
  
wenzelm@34429
    32
  def property(name: String) = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34429
    33
  def property(name: String, value: String) = 
wenzelm@34318
    34
    jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34318
    35
  
wenzelm@34429
    36
  var plugin: Plugin = null
wenzelm@34429
    37
  def prover(buffer: JEditBuffer) = plugin.prover_setup(buffer).get.prover
wenzelm@34429
    38
  def prover_setup(buffer: JEditBuffer) = plugin.prover_setup(buffer).get
wenzelm@34318
    39
}
wenzelm@34318
    40
wenzelm@34429
    41
wenzelm@34318
    42
class Plugin extends EBPlugin {
wenzelm@34429
    43
  
immler@34406
    44
  private val mapping = new HashMap[JEditBuffer, ProverSetup]
immler@34406
    45
wenzelm@34429
    46
  var viewFont: Font = null
wenzelm@34318
    47
  val viewFontChanged = new EventSource[Font]
immler@34406
    48
wenzelm@34429
    49
  def install(view: View) {
immler@34406
    50
    val buffer = view.getBuffer
immler@34406
    51
    mapping.get(buffer) match{
immler@34406
    52
      case None =>{
immler@34406
    53
          val prover_setup = new ProverSetup(buffer)
immler@34406
    54
          mapping.update(buffer, prover_setup)
immler@34406
    55
          prover_setup.activate(view)
immler@34406
    56
      }
immler@34406
    57
      case _ => System.err.println("Already installed plugin on Buffer")
immler@34406
    58
    }
wenzelm@34318
    59
  }
immler@34406
    60
wenzelm@34429
    61
  def uninstall(view: View) {
immler@34406
    62
    val buffer = view.getBuffer
immler@34406
    63
    mapping.removeKey(buffer) match{
immler@34406
    64
      case None => System.err.println("No Plugin installed on this Buffer")
immler@34406
    65
      case Some(proversetup) =>
immler@34406
    66
        proversetup.deactivate
immler@34406
    67
    }
wenzelm@34318
    68
  }
immler@34406
    69
wenzelm@34429
    70
  def prover_setup(buffer: JEditBuffer): Option[ProverSetup] = mapping.get(buffer)
wenzelm@34429
    71
  def is_active(buffer: JEditBuffer) =
wenzelm@34429
    72
    mapping.get(buffer) match { case None => false case _ => true }
wenzelm@34318
    73
  
wenzelm@34429
    74
  override def handleMessage(msg: EBMessage) = msg match {
wenzelm@34429
    75
    case epu: EditPaneUpdate => epu.getWhat() match {
wenzelm@34318
    76
      case EditPaneUpdate.BUFFER_CHANGED =>
immler@34406
    77
        mapping get epu.getEditPane.getBuffer match {
immler@34406
    78
          //only activate 'isabelle'-buffers!
immler@34406
    79
          case None =>
immler@34406
    80
          case Some(prover_setup) => 
immler@34406
    81
            prover_setup.theory_view.activate
wenzelm@34422
    82
            val dockable = epu.getEditPane.getView.getDockableWindowManager.getDockable("isabelle-output")
immler@34406
    83
            if(dockable != null) {
immler@34406
    84
              val output_dockable = dockable.asInstanceOf[OutputDockable]
immler@34406
    85
              if(output_dockable.getComponent(0) != prover_setup.output_text_view ) {
immler@34406
    86
                output_dockable.asInstanceOf[OutputDockable].removeAll
immler@34406
    87
                output_dockable.asInstanceOf[OutputDockable].add(new JScrollPane(prover_setup.output_text_view))
immler@34406
    88
                output_dockable.revalidate
immler@34406
    89
              }
immler@34406
    90
            }
immler@34406
    91
        }
wenzelm@34318
    92
      case EditPaneUpdate.BUFFER_CHANGING =>
immler@34406
    93
        val buffer = epu.getEditPane.getBuffer
immler@34406
    94
        if(buffer != null) mapping get buffer match {
immler@34406
    95
          //only deactivate 'isabelle'-buffers!
immler@34406
    96
          case None =>
immler@34406
    97
          case Some(prover_setup) => prover_setup.theory_view.deactivate
immler@34406
    98
        }
wenzelm@34318
    99
      case _ =>
wenzelm@34318
   100
    }
wenzelm@34318
   101
    case _ =>
wenzelm@34318
   102
  }
wenzelm@34318
   103
wenzelm@34429
   104
  def setFont(path: String, size: Float) {
wenzelm@34318
   105
    try {
wenzelm@34318
   106
      val fontStream = new FileInputStream(path)
wenzelm@34318
   107
      val font = Font.createFont(Font.TRUETYPE_FONT, fontStream)
wenzelm@34318
   108
      viewFont = font.deriveFont(Font.PLAIN, size)
immler@34406
   109
wenzelm@34318
   110
      viewFontChanged.fire(viewFont)
wenzelm@34318
   111
    }
wenzelm@34318
   112
    catch {
wenzelm@34429
   113
      case e: IOException =>
wenzelm@34318
   114
    }
wenzelm@34318
   115
  }
wenzelm@34318
   116
  
wenzelm@34318
   117
  override def start() {
wenzelm@34429
   118
    Plugin.plugin = this
immler@34406
   119
    
wenzelm@34429
   120
    if (Plugin.property("font-path") != null && Plugin.property("font-size") != null)
wenzelm@34318
   121
      try {
wenzelm@34429
   122
        setFont(Plugin.property("font-path"), Plugin.property("font-size").toFloat)
wenzelm@34318
   123
      }
wenzelm@34318
   124
      catch {
wenzelm@34429
   125
        case e: NumberFormatException =>
wenzelm@34318
   126
      }
wenzelm@34318
   127
  }
wenzelm@34318
   128
  
wenzelm@34318
   129
  override def stop() {
wenzelm@34429
   130
    // TODO: proper cleanup
wenzelm@34429
   131
    Plugin.plugin = null
wenzelm@34318
   132
  }
wenzelm@34318
   133
}