src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Sun Sep 18 21:15:31 2011 +0200 (2011-09-18)
changeset 44981 2bec3b7514cf
parent 44980 ad5883642a83
child 44989 5450ab3c677e
permissions -rw-r--r--
additional space for borderless UI;
     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, SelectionChanged}
    16 
    17 import java.lang.System
    18 import java.awt.{BorderLayout, Graphics2D, Insets}
    19 import javax.swing.{JList, DefaultListCellRenderer, UIManager}
    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   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 = jEdit.getProperty("isabelle.cancel-execution.label")
    68 
    69   private val check = new Button("Check") {
    70     reactions += { case ButtonClicked(_) => Isabelle.check_buffer(view.getBuffer) }
    71   }
    72   check.tooltip = jEdit.getProperty("isabelle.check-buffer.label")
    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, Isar_Document.Node_Status] = Map.empty
    88 
    89   private class Node_Renderer_Component extends Label
    90   {
    91     xAlignment = Alignment.Leading
    92     border = UIManager.getBorder("List.cellNoFocusBorder")
    93     val empty_insets = new Insets(0, 0, 0, 0)
    94 
    95     var node_name = Document.Node.Name.empty
    96     override def paintComponent(gfx: Graphics2D)
    97     {
    98       nodes_status.get(node_name) match {
    99         case Some(st) if st.total > 0 =>
   100           val size = peer.getSize()
   101           val insets = if (border == null) empty_insets else border.getBorderInsets(this.peer)
   102           val w = size.width - insets.left - insets.right
   103           val h = size.height - insets.top - insets.bottom
   104           var end = size.width - insets.right
   105           for {
   106             (n, color) <- List(
   107               (st.unprocessed, Isabelle_Markup.unprocessed1_color),
   108               (st.running, Isabelle_Markup.running_color),
   109               (st.failed, Isabelle_Markup.error_color)) }
   110           {
   111             gfx.setColor(color)
   112             val v = (n * w / st.total) max (if (n > 0) 2 else 0)
   113             gfx.fillRect(end - v, insets.top, v, h)
   114             end -= v
   115           }
   116         case _ =>
   117       }
   118       super.paintComponent(gfx)
   119     }
   120   }
   121 
   122   private class Node_Renderer extends
   123     ListView.AbstractRenderer[Document.Node.Name, Node_Renderer_Component](
   124       new Node_Renderer_Component)
   125   {
   126     def configure(list: ListView[_], isSelected: Boolean, focused: Boolean,
   127       name: Document.Node.Name, index: Int)
   128     {
   129       component.opaque = false
   130       component.background = list.background
   131       component.foreground = list.foreground
   132       component.node_name = name
   133       component.text = name.theory + " "
   134     }
   135   }
   136   status.renderer = new Node_Renderer
   137 
   138   private def handle_changed(changed_nodes: Set[Document.Node.Name])
   139   {
   140     Swing_Thread.now {
   141       // FIXME correlation to changed_nodes!?
   142       val snapshot = Isabelle.session.snapshot()
   143 
   144       var nodes_status1 = nodes_status
   145       for {
   146         name <- changed_nodes
   147         node <- snapshot.version.nodes.get(name)
   148         val status = Isar_Document.node_status(snapshot.state, snapshot.version, node)
   149       } nodes_status1 += (name -> status)
   150 
   151       if (nodes_status != nodes_status1) {
   152         nodes_status = nodes_status1
   153         status.listData =
   154           snapshot.version.topological_order.filter(
   155             (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   156       }
   157     }
   158   }
   159 
   160 
   161   /* main actor */
   162 
   163   private val main_actor = actor {
   164     loop {
   165       react {
   166         case result: Isabelle_Process.Result =>
   167           if (result.is_syslog)
   168             Swing_Thread.now {
   169               val text = Isabelle.session.syslog()
   170               if (text != syslog.text) {
   171                 syslog.text = text
   172               }
   173             }
   174 
   175         case phase: Session.Phase =>
   176           Swing_Thread.now { session_phase.text = " " + phase.toString + " " }
   177 
   178         case changed: Session.Commands_Changed => handle_changed(changed.nodes)
   179 
   180         case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
   181       }
   182     }
   183   }
   184 
   185   override def init() {
   186     Isabelle.session.syslog_messages += main_actor
   187     Isabelle.session.phase_changed += main_actor
   188     Isabelle.session.commands_changed += main_actor
   189   }
   190 
   191   override def exit() {
   192     Isabelle.session.syslog_messages -= main_actor
   193     Isabelle.session.phase_changed -= main_actor
   194     Isabelle.session.commands_changed -= main_actor
   195   }
   196 }