src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Wed Mar 14 15:37:51 2012 +0100 (2012-03-14)
changeset 46920 5f44c8bea84e
parent 46918 1752164d916b
child 47588 1f8f1c2045fd
permissions -rw-r--r--
more explicit indication of swing thread context;
     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.current_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.warned, Isabelle_Rendering.warning_color),
   120               (st.failed, Isabelle_Rendering.error_color)) }
   121           {
   122             gfx.setColor(color)
   123             val v = (n * w / st.total) max (if (n > 0) 2 else 0)
   124             gfx.fillRect(end - v, insets.top, v, h)
   125             end -= v
   126           }
   127         case _ =>
   128       }
   129       super.paintComponent(gfx)
   130     }
   131   }
   132 
   133   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   134   {
   135     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   136       name: Document.Node.Name, index: Int): Component =
   137     {
   138       val component = Node_Renderer_Component
   139       component.node_name = name
   140       component.text = name.theory
   141       component
   142     }
   143   }
   144   status.renderer = new Node_Renderer
   145 
   146   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   147   {
   148     Swing_Thread.now {
   149       val snapshot = Isabelle.session.snapshot()
   150 
   151       val iterator =
   152         restriction match {
   153           case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
   154           case None => snapshot.version.nodes.entries
   155         }
   156       val nodes_status1 =
   157         (nodes_status /: iterator)({ case (status, (name, node)) =>
   158             if (Isabelle.thy_load.is_loaded(name.theory)) status
   159             else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
   160 
   161       if (nodes_status != nodes_status1) {
   162         nodes_status = nodes_status1
   163         status.listData =
   164           snapshot.version.nodes.topological_order.filter(
   165             (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   166       }
   167     }
   168   }
   169 
   170 
   171   /* main actor */
   172 
   173   private val main_actor = actor {
   174     loop {
   175       react {
   176         case output: Isabelle_Process.Output =>
   177           if (output.is_syslog)
   178             Swing_Thread.later {
   179               val text = Isabelle.session.current_syslog()
   180               if (text != syslog.text) syslog.text = text
   181             }
   182 
   183         case phase: Session.Phase =>
   184           Swing_Thread.later { session_phase.text = " " + phase.toString + " " }
   185 
   186         case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
   187 
   188         case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
   189       }
   190     }
   191   }
   192 
   193   override def init() {
   194     Isabelle.session.syslog_messages += main_actor
   195     Isabelle.session.phase_changed += main_actor
   196     Isabelle.session.commands_changed += main_actor
   197   }
   198 
   199   override def exit() {
   200     Isabelle.session.syslog_messages -= main_actor
   201     Isabelle.session.phase_changed -= main_actor
   202     Isabelle.session.commands_changed -= main_actor
   203   }
   204 
   205   handle_update()
   206 }