src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Wed Sep 07 11:00:39 2011 +0200 (2011-09-07)
changeset 44775 27930cf6f0f7
parent 44734 7313e2db3d39
child 44776 47e8c8daccae
permissions -rw-r--r--
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
     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.session.cancel_execution }
    66   }
    67   cancel.tooltip = "Cancel current proof checking process"
    68 
    69   private val logic = Isabelle.logic_selector(Isabelle.Property("logic"))
    70   logic.listenTo(logic.selection)
    71   logic.reactions += {
    72     case SelectionChanged(_) => Isabelle.Property("logic") = logic.selection.item.name
    73   }
    74 
    75   private val controls =
    76     new FlowPanel(FlowPanel.Alignment.Right)(session_phase, cancel, logic)
    77   add(controls.peer, BorderLayout.NORTH)
    78 
    79 
    80   /* component state -- owned by Swing thread */
    81 
    82   private var nodes_status: Map[Document.Node.Name, String] = Map.empty
    83 
    84   private def handle_changed(changed_nodes: Set[Document.Node.Name])
    85   {
    86     Swing_Thread.now {
    87       // FIXME correlation to changed_nodes!?
    88       val state = Isabelle.session.current_state()
    89       val version = state.recent_stable.version.get_finished
    90 
    91       var nodes_status1 = nodes_status
    92       for {
    93         name <- changed_nodes
    94         node <- version.nodes.get(name)
    95         val status = Isar_Document.node_status(state, version, node)
    96       } nodes_status1 += (name -> status.toString)
    97 
    98       if (nodes_status != nodes_status1) {
    99         nodes_status = nodes_status1
   100         val order =
   101           Library.sort_wrt((name: Document.Node.Name) => name.theory,
   102             nodes_status.keySet.toList)
   103         status.listData = order.map(name => name.theory + " " + nodes_status(name))
   104       }
   105     }
   106   }
   107 
   108 
   109   /* main actor */
   110 
   111   private val main_actor = actor {
   112     loop {
   113       react {
   114         case result: Isabelle_Process.Result =>
   115           if (result.is_syslog)
   116             Swing_Thread.now {
   117               val text = Isabelle.session.syslog()
   118               if (text != syslog.text) {
   119                 syslog.text = text
   120               }
   121             }
   122 
   123         case phase: Session.Phase =>
   124           Swing_Thread.now { session_phase.text = " " + phase.toString + " " }
   125 
   126         case changed: Session.Commands_Changed => handle_changed(changed.nodes)
   127 
   128         case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
   129       }
   130     }
   131   }
   132 
   133   override def init() {
   134     Isabelle.session.syslog_messages += main_actor
   135     Isabelle.session.phase_changed += main_actor
   136     Isabelle.session.commands_changed += main_actor
   137   }
   138 
   139   override def exit() {
   140     Isabelle.session.syslog_messages -= main_actor
   141     Isabelle.session.phase_changed -= main_actor
   142     Isabelle.session.commands_changed -= main_actor
   143   }
   144 }