src/Tools/jEdit/src/jedit/plugin.scala
author wenzelm
Tue Sep 28 23:47:45 2010 +0200 (2010-09-28)
changeset 39741 62b91eb2d39a
parent 39735 969ede84aac0
child 40474 576b88b1dce9
permissions -rw-r--r--
CONTROL-mouse management: handle windowIconified;
html_panel: tuned border;
     1 /*  Title:      Tools/jEdit/src/jedit/plugin.scala
     2     Author:     Makarius
     3 
     4 Main Isabelle/jEdit plugin setup.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import java.io.{FileInputStream, IOException}
    13 import java.awt.Font
    14 
    15 import scala.collection.mutable
    16 import scala.swing.ComboBox
    17 
    18 import org.gjt.sp.jedit.{jEdit, GUIUtilities, EBMessage, EBPlugin,
    19   Buffer, EditPane, ServiceManager, View}
    20 import org.gjt.sp.jedit.buffer.JEditBuffer
    21 import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
    22 import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
    23 import org.gjt.sp.jedit.gui.DockableWindowManager
    24 
    25 import org.gjt.sp.util.Log
    26 
    27 import scala.actors.Actor
    28 import Actor._
    29 
    30 
    31 object Isabelle
    32 {
    33   /* plugin instance */
    34 
    35   var system: Isabelle_System = null
    36   var session: Session = null
    37 
    38 
    39   /* properties */
    40 
    41   val OPTION_PREFIX = "options.isabelle."
    42 
    43   object Property
    44   {
    45     def apply(name: String): String =
    46       jEdit.getProperty(OPTION_PREFIX + name)
    47     def apply(name: String, default: String): String =
    48       jEdit.getProperty(OPTION_PREFIX + name, default)
    49     def update(name: String, value: String) =
    50       jEdit.setProperty(OPTION_PREFIX + name, value)
    51   }
    52 
    53   object Boolean_Property
    54   {
    55     def apply(name: String): Boolean =
    56       jEdit.getBooleanProperty(OPTION_PREFIX + name)
    57     def apply(name: String, default: Boolean): Boolean =
    58       jEdit.getBooleanProperty(OPTION_PREFIX + name, default)
    59     def update(name: String, value: Boolean) =
    60       jEdit.setBooleanProperty(OPTION_PREFIX + name, value)
    61   }
    62 
    63   object Int_Property
    64   {
    65     def apply(name: String): Int =
    66       jEdit.getIntegerProperty(OPTION_PREFIX + name)
    67     def apply(name: String, default: Int): Int =
    68       jEdit.getIntegerProperty(OPTION_PREFIX + name, default)
    69     def update(name: String, value: Int) =
    70       jEdit.setIntegerProperty(OPTION_PREFIX + name, value)
    71   }
    72 
    73 
    74   /* font */
    75 
    76   def font_family(): String = jEdit.getProperty("view.font")
    77 
    78   def font_size(): Float =
    79     (jEdit.getIntegerProperty("view.fontsize", 16) *
    80       Int_Property("relative-font-size", 100)).toFloat / 100
    81 
    82 
    83   /* text area ranges */
    84 
    85   case class Gfx_Range(val x: Int, val y: Int, val length: Int)
    86 
    87   def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
    88   {
    89     val p = text_area.offsetToXY(range.start)
    90     val q = text_area.offsetToXY(range.stop)
    91     if (p != null && q != null && p.y == q.y) Some(new Gfx_Range(p.x, p.y, q.x - p.x))
    92     else None
    93   }
    94 
    95 
    96   /* tooltip markup */
    97 
    98   def tooltip(text: String): String =
    99     "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
   100         Int_Property("tooltip-font-size", 10).toString + "px; \">" +  // FIXME proper scaling (!?)
   101       HTML.encode(text) + "</pre></html>"
   102 
   103   def tooltip_dismiss_delay(): Int =
   104     Int_Property("tooltip-dismiss-delay", 8000) max 500
   105 
   106   def setup_tooltips()
   107   {
   108     Swing_Thread.now {
   109       val manager = javax.swing.ToolTipManager.sharedInstance
   110       manager.setDismissDelay(tooltip_dismiss_delay())
   111     }
   112   }
   113 
   114 
   115   /* icons */
   116 
   117   def load_icon(name: String): javax.swing.Icon =
   118   {
   119     val icon = GUIUtilities.loadIcon(name)
   120     if (icon.getIconWidth < 0 || icon.getIconHeight < 0)
   121       Log.log(Log.ERROR, icon, "Bad icon: " + name)
   122     icon
   123   }
   124 
   125 
   126   /* main jEdit components */
   127 
   128   def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
   129 
   130   def jedit_views(): Iterator[View] = jEdit.getViews().iterator
   131 
   132   def jedit_text_areas(view: View): Iterator[JEditTextArea] =
   133     view.getEditPanes().iterator.map(_.getTextArea)
   134 
   135   def jedit_text_areas(): Iterator[JEditTextArea] =
   136     jedit_views().flatMap(jedit_text_areas(_))
   137 
   138   def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
   139     jedit_text_areas().filter(_.getBuffer == buffer)
   140 
   141   def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
   142   {
   143     try { buffer.readLock(); body }
   144     finally { buffer.readUnlock() }
   145   }
   146 
   147   def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
   148     Swing_Thread.now { buffer_lock(buffer) { body } }
   149 
   150 
   151   /* dockable windows */
   152 
   153   private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
   154 
   155   def docked_session(view: View): Option[Session_Dockable] =
   156     wm(view).getDockableWindow("isabelle-session") match {
   157       case dockable: Session_Dockable => Some(dockable)
   158       case _ => None
   159     }
   160 
   161   def docked_output(view: View): Option[Output_Dockable] =
   162     wm(view).getDockableWindow("isabelle-output") match {
   163       case dockable: Output_Dockable => Some(dockable)
   164       case _ => None
   165     }
   166 
   167   def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
   168     wm(view).getDockableWindow("isabelle-raw-output") match {
   169       case dockable: Raw_Output_Dockable => Some(dockable)
   170       case _ => None
   171     }
   172 
   173   def docked_protocol(view: View): Option[Protocol_Dockable] =
   174     wm(view).getDockableWindow("isabelle-protocol") match {
   175       case dockable: Protocol_Dockable => Some(dockable)
   176       case _ => None
   177     }
   178 
   179 
   180   /* logic image */
   181 
   182   def default_logic(): String =
   183   {
   184     val logic = system.getenv("JEDIT_LOGIC")
   185     if (logic != "") logic
   186     else system.getenv_strict("ISABELLE_LOGIC")
   187   }
   188 
   189   class Logic_Entry(val name: String, val description: String)
   190   {
   191     override def toString = description
   192   }
   193 
   194   def logic_selector(logic: String): ComboBox[Logic_Entry] =
   195   {
   196     val entries =
   197       new Logic_Entry("", "default (" + default_logic() + ")") ::
   198         system.find_logics().map(name => new Logic_Entry(name, name))
   199     val component = new ComboBox(entries)
   200     entries.find(_.name == logic) match {
   201       case None =>
   202       case Some(entry) => component.selection.item = entry
   203     }
   204     component.tooltip = "Isabelle logic image"
   205     component
   206   }
   207 
   208   def start_session()
   209   {
   210     val timeout = Int_Property("startup-timeout") max 1000
   211     val modes = system.getenv("JEDIT_PRINT_MODE").split(",").toList.map("-m" + _)
   212     val logic = {
   213       val logic = Property("logic")
   214       if (logic != null && logic != "") logic
   215       else Isabelle.default_logic()
   216     }
   217     session.start(timeout, modes ::: List(logic))
   218   }
   219 }
   220 
   221 
   222 class Plugin extends EBPlugin
   223 {
   224   /* session management */
   225 
   226   private def init_model(buffer: Buffer)
   227   {
   228     Isabelle.swing_buffer_lock(buffer) {
   229       val opt_model =
   230         Document_Model(buffer) match {
   231           case Some(model) => model.refresh; Some(model)
   232           case None =>
   233             Thy_Header.split_thy_path(Isabelle.system.posix_path(buffer.getPath)) match {
   234               case Some((_, thy_name)) =>
   235                 Some(Document_Model.init(Isabelle.session, buffer, thy_name))
   236               case None => None
   237             }
   238         }
   239       if (opt_model.isDefined) {
   240         for (text_area <- Isabelle.jedit_text_areas(buffer)) {
   241           if (Document_View(text_area).map(_.model) != opt_model)
   242             Document_View.init(opt_model.get, text_area)
   243         }
   244       }
   245     }
   246   }
   247 
   248   private def exit_model(buffer: Buffer)
   249   {
   250     Isabelle.swing_buffer_lock(buffer) {
   251       Isabelle.jedit_text_areas(buffer).foreach(Document_View.exit)
   252       Document_Model.exit(buffer)
   253     }
   254   }
   255 
   256   private case class Init_Model(buffer: Buffer)
   257   private case class Exit_Model(buffer: Buffer)
   258   private case class Init_View(buffer: Buffer, text_area: JEditTextArea)
   259   private case class Exit_View(buffer: Buffer, text_area: JEditTextArea)
   260 
   261   private val session_manager = actor {
   262     var ready = false
   263     loop {
   264       react {
   265         case phase: Session.Phase =>
   266           ready = phase == Session.Ready
   267           phase match {
   268             case Session.Failed =>
   269               Swing_Thread.now {
   270                 val text = new scala.swing.TextArea(Isabelle.session.syslog())
   271                 text.editable = false
   272                 Library.error_dialog(jEdit.getActiveView, "Failed to start Isabelle process", text)
   273               }
   274 
   275             case Session.Ready => Isabelle.jedit_buffers.foreach(init_model)
   276             case Session.Shutdown => Isabelle.jedit_buffers.foreach(exit_model)
   277             case _ =>
   278           }
   279 
   280         case Init_Model(buffer) =>
   281           if (ready) init_model(buffer)
   282 
   283         case Exit_Model(buffer) =>
   284           exit_model(buffer)
   285 
   286         case Init_View(buffer, text_area) =>
   287           if (ready) {
   288             Isabelle.swing_buffer_lock(buffer) {
   289               Document_Model(buffer) match {
   290                 case Some(model) => Document_View.init(model, text_area)
   291                 case None =>
   292               }
   293             }
   294           }
   295 
   296         case Exit_View(buffer, text_area) =>
   297           Isabelle.swing_buffer_lock(buffer) {
   298             Document_View.exit(text_area)
   299           }
   300 
   301         case bad => System.err.println("session_manager: ignoring bad message " + bad)
   302       }
   303     }
   304   }
   305 
   306 
   307   /* main plugin plumbing */
   308 
   309   override def handleMessage(message: EBMessage)
   310   {
   311     message match {
   312       case msg: EditorStarted
   313       if Isabelle.Boolean_Property("auto-start") => Isabelle.start_session()
   314 
   315       case msg: BufferUpdate
   316       if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
   317 
   318         val buffer = msg.getBuffer
   319         if (buffer != null) session_manager ! Init_Model(buffer)
   320 
   321       case msg: EditPaneUpdate
   322       if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
   323           msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   324           msg.getWhat == EditPaneUpdate.CREATED ||
   325           msg.getWhat == EditPaneUpdate.DESTROYED) =>
   326 
   327         val edit_pane = msg.getEditPane
   328         val buffer = edit_pane.getBuffer
   329         val text_area = edit_pane.getTextArea
   330 
   331         if (buffer != null && text_area != null) {
   332           if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   333               msg.getWhat == EditPaneUpdate.CREATED)
   334             session_manager ! Init_View(buffer, text_area)
   335           else
   336             session_manager ! Exit_View(buffer, text_area)
   337         }
   338 
   339       case msg: PropertiesChanged =>
   340         Swing_Thread.now {
   341           Isabelle.setup_tooltips()
   342           for (text_area <- Isabelle.jedit_text_areas if Document_View(text_area).isDefined)
   343             Document_View(text_area).get.extend_styles()
   344         }
   345         Isabelle.session.global_settings.event(Session.Global_Settings)
   346 
   347       case _ =>
   348     }
   349   }
   350 
   351   override def start()
   352   {
   353     Isabelle.setup_tooltips()
   354     Isabelle.system = new Isabelle_System
   355     Isabelle.system.install_fonts()
   356     Isabelle.session = new Session(Isabelle.system)
   357     Isabelle.session.phase_changed += session_manager
   358   }
   359 
   360   override def stop()
   361   {
   362     Isabelle.session.stop()
   363     Isabelle.session.phase_changed -= session_manager
   364   }
   365 }