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