src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Thu Dec 01 14:29:14 2011 +0100 (2011-12-01 ago)
changeset 45709 87017fcbad83
parent 45672 a497c5d4a523
child 46681 c083a3f621c0
permissions -rw-r--r--
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
     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, Alignment,
    14   ScrollPane, TabbedPane, Component, Swing}
    15 import scala.swing.event.{ButtonClicked, MouseClicked, SelectionChanged}
    16 
    17 import java.lang.System
    18 import java.awt.{BorderLayout, Graphics2D, Insets}
    19 import javax.swing.{JList, DefaultListCellRenderer, BorderFactory}
    20 import javax.swing.border.{BevelBorder, SoftBevelBorder}
    21 
    22 import org.gjt.sp.jedit.{View, jEdit}
    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   private val readme_path = Path.explode("$JEDIT_HOME/README.html")
    31   readme.render_document(
    32     Isabelle_System.platform_file_url(readme_path),
    33     Isabelle_System.try_read(List(readme_path)))
    34 
    35   val status = new ListView(Nil: List[Document.Node.Name]) {
    36     listenTo(mouse.clicks)
    37     reactions += {
    38       case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
    39         val index = peer.locationToIndex(point)
    40         if (index >= 0) jEdit.openFile(view, listData(index).node)
    41     }
    42   }
    43   status.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
    44   status.selection.intervalMode = ListView.IntervalMode.Single
    45 
    46   private val syslog = new TextArea(Isabelle.session.syslog())
    47 
    48   private val tabs = new TabbedPane {
    49     pages += new TabbedPane.Page("README", Component.wrap(readme))
    50     pages += new TabbedPane.Page("Theory Status", new ScrollPane(status))
    51     pages += new TabbedPane.Page("System Log", new ScrollPane(syslog))
    52 
    53     selection.index =
    54     {
    55       val index = Isabelle.Int_Property("session-panel.selection", 0)
    56       if (index >= pages.length) 0 else index
    57     }
    58     listenTo(selection)
    59     reactions += {
    60       case SelectionChanged(_) =>
    61         Isabelle.Int_Property("session-panel.selection") = selection.index
    62     }
    63   }
    64 
    65   set_content(tabs)
    66 
    67 
    68   /* controls */
    69 
    70   val session_phase = new Label(Isabelle.session.phase.toString)
    71   session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
    72   session_phase.tooltip = "Prover status"
    73 
    74   private val cancel = new Button("Cancel") {
    75     reactions += { case ButtonClicked(_) => Isabelle.cancel_execution() }
    76   }
    77   cancel.tooltip = jEdit.getProperty("isabelle.cancel-execution.label")
    78 
    79   private val check = new Button("Check") {
    80     reactions += { case ButtonClicked(_) => Isabelle.check_buffer(view.getBuffer) }
    81   }
    82   check.tooltip = jEdit.getProperty("isabelle.check-buffer.label")
    83 
    84   private val logic = Isabelle.logic_selector(Isabelle.Property("logic"))
    85   logic.listenTo(logic.selection)
    86   logic.reactions += {
    87     case SelectionChanged(_) => Isabelle.Property("logic") = logic.selection.item.name
    88   }
    89 
    90   private val controls =
    91     new FlowPanel(FlowPanel.Alignment.Right)(check, cancel, session_phase, logic)
    92   add(controls.peer, BorderLayout.NORTH)
    93 
    94 
    95   /* component state -- owned by Swing thread */
    96 
    97   private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
    98 
    99   private object Node_Renderer_Component extends Label
   100   {
   101     opaque = false
   102     xAlignment = Alignment.Leading
   103     border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
   104 
   105     var node_name = Document.Node.Name.empty
   106     override def paintComponent(gfx: Graphics2D)
   107     {
   108       nodes_status.get(node_name) match {
   109         case Some(st) if st.total > 0 =>
   110           val size = peer.getSize()
   111           val insets = border.getBorderInsets(this.peer)
   112           val w = size.width - insets.left - insets.right
   113           val h = size.height - insets.top - insets.bottom
   114           var end = size.width - insets.right
   115           for {
   116             (n, color) <- List(
   117               (st.unprocessed, Isabelle_Rendering.unprocessed1_color),
   118               (st.running, Isabelle_Rendering.running_color),
   119               (st.failed, Isabelle_Rendering.error_color)) }
   120           {
   121             gfx.setColor(color)
   122             val v = (n * w / st.total) max (if (n > 0) 2 else 0)
   123             gfx.fillRect(end - v, insets.top, v, h)
   124             end -= v
   125           }
   126         case _ =>
   127       }
   128       super.paintComponent(gfx)
   129     }
   130   }
   131 
   132   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   133   {
   134     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   135       name: Document.Node.Name, index: Int): Component =
   136     {
   137       val component = Node_Renderer_Component
   138       component.node_name = name
   139       component.text = name.theory
   140       component
   141     }
   142   }
   143   status.renderer = new Node_Renderer
   144 
   145   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   146   {
   147     Swing_Thread.now {
   148       val snapshot = Isabelle.session.snapshot()
   149       val nodes = restriction getOrElse snapshot.version.nodes.keySet
   150 
   151       var nodes_status1 = nodes_status
   152       for {
   153         name <- nodes
   154         node <- snapshot.version.nodes.get(name)
   155         val status = Protocol.node_status(snapshot.state, snapshot.version, node)
   156       } nodes_status1 += (name -> status)
   157 
   158       if (nodes_status != nodes_status1) {
   159         nodes_status = nodes_status1
   160         status.listData =
   161           snapshot.version.topological_order.filter(
   162             (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   163       }
   164     }
   165   }
   166 
   167 
   168   /* main actor */
   169 
   170   private val main_actor = actor {
   171     loop {
   172       react {
   173         case result: Isabelle_Process.Result =>
   174           if (result.is_syslog)
   175             Swing_Thread.now {
   176               val text = Isabelle.session.syslog()
   177               if (text != syslog.text) {
   178                 syslog.text = text
   179               }
   180             }
   181 
   182         case phase: Session.Phase =>
   183           Swing_Thread.now { session_phase.text = " " + phase.toString + " " }
   184 
   185         case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
   186 
   187         case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
   188       }
   189     }
   190   }
   191 
   192   override def init() {
   193     Isabelle.session.syslog_messages += main_actor
   194     Isabelle.session.phase_changed += main_actor
   195     Isabelle.session.commands_changed += main_actor
   196   }
   197 
   198   override def exit() {
   199     Isabelle.session.syslog_messages -= main_actor
   200     Isabelle.session.phase_changed -= main_actor
   201     Isabelle.session.commands_changed -= main_actor
   202   }
   203 
   204   handle_update()
   205 }