src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Tue Aug 21 12:15:25 2012 +0200 (2012-08-21)
changeset 48870 4accee106f0f
parent 48021 d899be1cfe6d
child 49038 2f0530b81c45
permissions -rw-r--r--
clarified initialization of Thy_Load, Thy_Info, Session;
     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, Component}
    14 import scala.swing.event.{ButtonClicked, MouseClicked, SelectionChanged}
    15 
    16 import java.lang.System
    17 import java.awt.{BorderLayout, Graphics2D, Insets}
    18 import javax.swing.{JList, BorderFactory}
    19 import javax.swing.border.{BevelBorder, SoftBevelBorder}
    20 
    21 import org.gjt.sp.jedit.{View, jEdit}
    22 
    23 
    24 class Session_Dockable(view: View, position: String) extends Dockable(view: View, position: String)
    25 {
    26   /* status */
    27 
    28   private val status = new ListView(Nil: List[Document.Node.Name]) {
    29     listenTo(mouse.clicks)
    30     reactions += {
    31       case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
    32         val index = peer.locationToIndex(point)
    33         if (index >= 0) jEdit.openFile(view, listData(index).node)
    34     }
    35   }
    36   status.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
    37   status.selection.intervalMode = ListView.IntervalMode.Single
    38 
    39   set_content(status)
    40 
    41 
    42   /* controls */
    43 
    44   private val session_phase = new Label(Isabelle.session.phase.toString)
    45   session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
    46   session_phase.tooltip = "Prover status"
    47 
    48   private def handle_phase(phase: Session.Phase)
    49   {
    50     Swing_Thread.later { session_phase.text = " " + phase.toString + " " }
    51   }
    52 
    53   private val cancel = new Button("Cancel") {
    54     reactions += { case ButtonClicked(_) => Isabelle.cancel_execution() }
    55   }
    56   cancel.tooltip = jEdit.getProperty("isabelle.cancel-execution.label")
    57 
    58   private val check = new Button("Check") {
    59     reactions += { case ButtonClicked(_) => Isabelle.check_buffer(view.getBuffer) }
    60   }
    61   check.tooltip = jEdit.getProperty("isabelle.check-buffer.label")
    62 
    63   private val logic = Isabelle.logic_selector(Isabelle.Property("logic"))
    64   logic.listenTo(logic.selection)
    65   logic.reactions += {
    66     case SelectionChanged(_) => Isabelle.Property("logic") = logic.selection.item.name
    67   }
    68 
    69   private val controls =
    70     new FlowPanel(FlowPanel.Alignment.Right)(check, cancel, session_phase, logic)
    71   add(controls.peer, BorderLayout.NORTH)
    72 
    73 
    74   /* component state -- owned by Swing thread */
    75 
    76   private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
    77 
    78   private object Node_Renderer_Component extends Label
    79   {
    80     opaque = false
    81     xAlignment = Alignment.Leading
    82     border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
    83 
    84     var node_name = Document.Node.Name.empty
    85     override def paintComponent(gfx: Graphics2D)
    86     {
    87       nodes_status.get(node_name) match {
    88         case Some(st) if st.total > 0 =>
    89           val size = peer.getSize()
    90           val insets = border.getBorderInsets(this.peer)
    91           val w = size.width - insets.left - insets.right
    92           val h = size.height - insets.top - insets.bottom
    93           var end = size.width - insets.right
    94           for {
    95             (n, color) <- List(
    96               (st.unprocessed, Isabelle_Rendering.unprocessed1_color),
    97               (st.running, Isabelle_Rendering.running_color),
    98               (st.warned, Isabelle_Rendering.warning_color),
    99               (st.failed, Isabelle_Rendering.error_color)) }
   100           {
   101             gfx.setColor(color)
   102             val v = (n * w / st.total) max (if (n > 0) 2 else 0)
   103             gfx.fillRect(end - v, insets.top, v, h)
   104             end -= v
   105           }
   106         case _ =>
   107       }
   108       super.paintComponent(gfx)
   109     }
   110   }
   111 
   112   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   113   {
   114     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   115       name: Document.Node.Name, index: Int): Component =
   116     {
   117       val component = Node_Renderer_Component
   118       component.node_name = name
   119       component.text = name.theory
   120       component
   121     }
   122   }
   123   status.renderer = new Node_Renderer
   124 
   125   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   126   {
   127     Swing_Thread.now {
   128       val snapshot = Isabelle.session.snapshot()
   129 
   130       val iterator =
   131         restriction match {
   132           case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
   133           case None => snapshot.version.nodes.entries
   134         }
   135       val nodes_status1 =
   136         (nodes_status /: iterator)({ case (status, (name, node)) =>
   137             if (Isabelle.thy_load.loaded_theories(name.theory)) status
   138             else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
   139 
   140       if (nodes_status != nodes_status1) {
   141         nodes_status = nodes_status1
   142         status.listData =
   143           snapshot.version.nodes.topological_order.filter(
   144             (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   145       }
   146     }
   147   }
   148 
   149 
   150   /* main actor */
   151 
   152   private val main_actor = actor {
   153     loop {
   154       react {
   155         case phase: Session.Phase => handle_phase(phase)
   156 
   157         case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
   158 
   159         case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
   160       }
   161     }
   162   }
   163 
   164   override def init()
   165   {
   166     Isabelle.session.phase_changed += main_actor; handle_phase(Isabelle.session.phase)
   167     Isabelle.session.commands_changed += main_actor; handle_update()
   168   }
   169 
   170   override def exit()
   171   {
   172     Isabelle.session.phase_changed -= main_actor
   173     Isabelle.session.commands_changed -= main_actor
   174   }
   175 }