src/Tools/jEdit/src/jedit/Plugin.scala
author immler@in.tum.de
Thu Dec 18 01:10:20 2008 +0100 (2008-12-18 ago)
changeset 34406 f81cd75ae331
parent 34337 5d5b69f2956b
child 34407 aad6834ba380
child 34460 cc5b9f02fbea
permissions -rw-r--r--
restructured: independent provers in different buffers
wenzelm@34318
     1
package isabelle.jedit
wenzelm@34318
     2
wenzelm@34318
     3
import java.awt.Font
wenzelm@34318
     4
import java.io.{ FileInputStream, IOException }
immler@34406
     5
import javax.swing.JScrollPane
wenzelm@34318
     6
wenzelm@34318
     7
import isabelle.utils.EventSource
wenzelm@34318
     8
wenzelm@34318
     9
import isabelle.prover.{ Prover, Command }
wenzelm@34318
    10
wenzelm@34318
    11
import isabelle.IsabelleSystem
wenzelm@34318
    12
immler@34406
    13
import scala.collection.mutable.HashMap
immler@34406
    14
immler@34406
    15
import org.gjt.sp.jedit.{ jEdit, EBMessage, EBPlugin, Buffer, EditPane, ServiceManager, View }
immler@34406
    16
import org.gjt.sp.jedit.buffer.JEditBuffer
immler@34406
    17
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@34318
    18
import org.gjt.sp.jedit.msg.{ EditPaneUpdate, PropertiesChanged }
wenzelm@34318
    19
wenzelm@34318
    20
object Plugin {
wenzelm@34318
    21
  val NAME = "Isabelle"
wenzelm@34318
    22
  val OPTION_PREFIX = "options.isabelle."
wenzelm@34337
    23
  val VFS_PREFIX = "isabelle:"
wenzelm@34318
    24
  
wenzelm@34318
    25
  def property(name : String) = jEdit.getProperty(OPTION_PREFIX + name)
wenzelm@34318
    26
  def property(name : String, value : String) = 
wenzelm@34318
    27
    jEdit.setProperty(OPTION_PREFIX + name, value)
wenzelm@34318
    28
  
wenzelm@34318
    29
  var plugin : Plugin = null
immler@34406
    30
  def prover(buffer : JEditBuffer) = plugin.prover_setup(buffer).get.prover
immler@34406
    31
  def prover_setup(buffer : JEditBuffer) = plugin.prover_setup(buffer).get
immler@34406
    32
wenzelm@34318
    33
}
wenzelm@34318
    34
wenzelm@34318
    35
class Plugin extends EBPlugin {
wenzelm@34318
    36
  import Plugin._
immler@34406
    37
immler@34406
    38
  private val mapping = new HashMap[JEditBuffer, ProverSetup]
immler@34406
    39
immler@34406
    40
  var viewFont : Font = null
wenzelm@34318
    41
  val viewFontChanged = new EventSource[Font]
immler@34406
    42
immler@34406
    43
  def install(view : View) {
immler@34406
    44
    val buffer = view.getBuffer
immler@34406
    45
    mapping.get(buffer) match{
immler@34406
    46
      case None =>{
immler@34406
    47
          val prover_setup = new ProverSetup(buffer)
immler@34406
    48
          mapping.update(buffer, prover_setup)
immler@34406
    49
          prover_setup.activate(view)
immler@34406
    50
      }
immler@34406
    51
      case _ => System.err.println("Already installed plugin on Buffer")
immler@34406
    52
    }
wenzelm@34318
    53
  }
immler@34406
    54
immler@34406
    55
  def uninstall(view : View) {
immler@34406
    56
    val buffer = view.getBuffer
immler@34406
    57
    mapping.removeKey(buffer) match{
immler@34406
    58
      case None => System.err.println("No Plugin installed on this Buffer")
immler@34406
    59
      case Some(proversetup) =>
immler@34406
    60
        proversetup.deactivate
immler@34406
    61
    }
wenzelm@34318
    62
  }
immler@34406
    63
immler@34406
    64
  def prover_setup (buffer : JEditBuffer) : Option[ProverSetup] = mapping.get(buffer)
immler@34406
    65
  def is_active (buffer : JEditBuffer) = mapping.get(buffer) match { case None => false case _ => true}
wenzelm@34318
    66
  
wenzelm@34318
    67
  override def handleMessage(msg : EBMessage) = msg match {
wenzelm@34318
    68
    case epu : EditPaneUpdate => epu.getWhat() match {
wenzelm@34318
    69
      case EditPaneUpdate.BUFFER_CHANGED =>
immler@34406
    70
        mapping get epu.getEditPane.getBuffer match {
immler@34406
    71
          //only activate 'isabelle'-buffers!
immler@34406
    72
          case None =>
immler@34406
    73
          case Some(prover_setup) => 
immler@34406
    74
            prover_setup.theory_view.activate
immler@34406
    75
            val dockable = epu.getEditPane.getView.getDockableWindowManager.getDockable("Isabelle_output")
immler@34406
    76
            if(dockable != null) {
immler@34406
    77
              val output_dockable = dockable.asInstanceOf[OutputDockable]
immler@34406
    78
              if(output_dockable.getComponent(0) != prover_setup.output_text_view ) {
immler@34406
    79
                output_dockable.asInstanceOf[OutputDockable].removeAll
immler@34406
    80
                output_dockable.asInstanceOf[OutputDockable].add(new JScrollPane(prover_setup.output_text_view))
immler@34406
    81
                output_dockable.revalidate
immler@34406
    82
              }
immler@34406
    83
            }
immler@34406
    84
        }
wenzelm@34318
    85
      case EditPaneUpdate.BUFFER_CHANGING =>
immler@34406
    86
        val buffer = epu.getEditPane.getBuffer
immler@34406
    87
        if(buffer != null) mapping get buffer match {
immler@34406
    88
          //only deactivate 'isabelle'-buffers!
immler@34406
    89
          case None =>
immler@34406
    90
          case Some(prover_setup) => prover_setup.theory_view.deactivate
immler@34406
    91
        }
wenzelm@34318
    92
      case _ =>
wenzelm@34318
    93
    }
wenzelm@34318
    94
      
wenzelm@34318
    95
    case _ =>
wenzelm@34318
    96
  }
wenzelm@34318
    97
wenzelm@34318
    98
  def setFont(path : String, size : Float) {
wenzelm@34318
    99
    try {
wenzelm@34318
   100
      val fontStream = new FileInputStream(path)
wenzelm@34318
   101
      val font = Font.createFont(Font.TRUETYPE_FONT, fontStream)
wenzelm@34318
   102
      viewFont = font.deriveFont(Font.PLAIN, size)
immler@34406
   103
wenzelm@34318
   104
      viewFontChanged.fire(viewFont)
wenzelm@34318
   105
    }
wenzelm@34318
   106
    catch {
wenzelm@34318
   107
      case e : IOException =>
wenzelm@34318
   108
    }
wenzelm@34318
   109
  }
wenzelm@34318
   110
  
wenzelm@34318
   111
  override def start() {
wenzelm@34318
   112
    plugin = this
immler@34406
   113
    
wenzelm@34318
   114
    if (property("font-path") != null && property("font-size") != null)
wenzelm@34318
   115
      try {
wenzelm@34318
   116
        setFont(property("font-path"), property("font-size").toFloat)
wenzelm@34318
   117
      }
wenzelm@34318
   118
      catch {
wenzelm@34318
   119
        case e : NumberFormatException =>
wenzelm@34318
   120
      }
wenzelm@34318
   121
  }
wenzelm@34318
   122
  
wenzelm@34318
   123
  override def stop() {
wenzelm@34318
   124
    // TODO: implement cleanup
wenzelm@34318
   125
  }
wenzelm@34318
   126
}