src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Sat Sep 10 14:48:06 2011 +0200 (2011-09-10)
changeset 44864 e50557cb0eb6
parent 44776 47e8c8daccae
child 44865 679f0d57e831
permissions -rw-r--r--
explicit jEdit actions -- to enable key mappings, for example;
     1 /*  Title:      Tools/jEdit/src/session_dockable.scala
     2     Author:     Makarius
     3 
     4 Dockable window for prover session management.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import scala.actors.Actor._
    13 import scala.swing.{FlowPanel, Button, TextArea, Label, ListView,
    14   ScrollPane, TabbedPane, Component, Swing}
    15 import scala.swing.event.{ButtonClicked, SelectionChanged}
    16 
    17 import java.lang.System
    18 import java.awt.BorderLayout
    19 import javax.swing.JList
    20 import javax.swing.border.{BevelBorder, SoftBevelBorder}
    21 
    22 import org.gjt.sp.jedit.View
    23 
    24 
    25 class Session_Dockable(view: View, position: String) extends Dockable(view: View, position: String)
    26 {
    27   /* main tabs */
    28 
    29   private val readme = new HTML_Panel("SansSerif", 14)
    30   readme.render_document(Isabelle_System.try_read(List(Path.explode("$JEDIT_HOME/README.html"))))
    31 
    32   val status = new ListView(Nil: List[String])
    33   status.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
    34   status.selection.intervalMode = ListView.IntervalMode.Single
    35 
    36   private val syslog = new TextArea(Isabelle.session.syslog())
    37 
    38   private val tabs = new TabbedPane {
    39     pages += new TabbedPane.Page("README", Component.wrap(readme))
    40     pages += new TabbedPane.Page("Theory Status", new ScrollPane(status))
    41     pages += new TabbedPane.Page("System Log", new ScrollPane(syslog))
    42 
    43     selection.index =
    44     {
    45       val index = Isabelle.Int_Property("session-panel.selection", 0)
    46       if (index >= pages.length) 0 else index
    47     }
    48     listenTo(selection)
    49     reactions += {
    50       case SelectionChanged(_) =>
    51         Isabelle.Int_Property("session-panel.selection") = selection.index
    52     }
    53   }
    54 
    55   set_content(tabs)
    56 
    57 
    58   /* controls */
    59 
    60   val session_phase = new Label(Isabelle.session.phase.toString)
    61   session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
    62   session_phase.tooltip = "Prover status"
    63 
    64   private val cancel = new Button("Cancel") {
    65     reactions += { case ButtonClicked(_) => Isabelle.cancel_execution() }
    66   }
    67   cancel.tooltip = "Cancel current proof checking process"
    68 
    69   private val check = new Button("Check") {
    70     reactions += { case ButtonClicked(_) => Isabelle.check_buffer(view.getBuffer) }
    71   }
    72   check.tooltip = "Commence full proof checking of current buffer"
    73 
    74   private val logic = Isabelle.logic_selector(Isabelle.Property("logic"))
    75   logic.listenTo(logic.selection)
    76   logic.reactions += {
    77     case SelectionChanged(_) => Isabelle.Property("logic") = logic.selection.item.name
    78   }
    79 
    80   private val controls =
    81     new FlowPanel(FlowPanel.Alignment.Right)(check, cancel, session_phase, logic)
    82   add(controls.peer, BorderLayout.NORTH)
    83 
    84 
    85   /* component state -- owned by Swing thread */
    86 
    87   private var nodes_status: Map[Document.Node.Name, String] = Map.empty
    88 
    89   private def handle_changed(changed_nodes: Set[Document.Node.Name])
    90   {
    91     Swing_Thread.now {
    92       // FIXME correlation to changed_nodes!?
    93       val state = Isabelle.session.current_state()
    94       val version = state.recent_stable.version.get_finished
    95 
    96       var nodes_status1 = nodes_status
    97       for {
    98         name <- changed_nodes
    99         node <- version.nodes.get(name)
   100         val status = Isar_Document.node_status(state, version, node)
   101       } nodes_status1 += (name -> status.toString)
   102 
   103       if (nodes_status != nodes_status1) {
   104         nodes_status = nodes_status1
   105         val order =
   106           Library.sort_wrt((name: Document.Node.Name) => name.theory,
   107             nodes_status.keySet.toList)
   108         status.listData = order.map(name => name.theory + " " + nodes_status(name))
   109       }
   110     }
   111   }
   112 
   113 
   114   /* main actor */
   115 
   116   private val main_actor = actor {
   117     loop {
   118       react {
   119         case result: Isabelle_Process.Result =>
   120           if (result.is_syslog)
   121             Swing_Thread.now {
   122               val text = Isabelle.session.syslog()
   123               if (text != syslog.text) {
   124                 syslog.text = text
   125               }
   126             }
   127 
   128         case phase: Session.Phase =>
   129           Swing_Thread.now { session_phase.text = " " + phase.toString + " " }
   130 
   131         case changed: Session.Commands_Changed => handle_changed(changed.nodes)
   132 
   133         case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
   134       }
   135     }
   136   }
   137 
   138   override def init() {
   139     Isabelle.session.syslog_messages += main_actor
   140     Isabelle.session.phase_changed += main_actor
   141     Isabelle.session.commands_changed += main_actor
   142   }
   143 
   144   override def exit() {
   145     Isabelle.session.syslog_messages -= main_actor
   146     Isabelle.session.phase_changed -= main_actor
   147     Isabelle.session.commands_changed -= main_actor
   148   }
   149 }