src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Sun Nov 25 21:35:29 2012 +0100 (2012-11-25)
changeset 50208 1382ad6d4774
parent 50205 788c8263e634
child 50209 907373a080b9
permissions -rw-r--r--
tuned signature;
     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, ScrollPane, Component}
    14 import scala.swing.event.{ButtonClicked, MouseClicked}
    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, position)
    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(new ScrollPane(status))
    40 
    41 
    42   /* controls */
    43 
    44   private val session_phase = new Label(PIDE.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.logic_selector(true)
    64 
    65   private val controls =
    66     new FlowPanel(FlowPanel.Alignment.Right)(check, cancel, session_phase, logic)
    67   add(controls.peer, BorderLayout.NORTH)
    68 
    69 
    70   /* component state -- owned by Swing thread */
    71 
    72   private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
    73 
    74   private object Node_Renderer_Component extends Label
    75   {
    76     opaque = false
    77     xAlignment = Alignment.Leading
    78     border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
    79 
    80     var node_name = Document.Node.Name.empty
    81     override def paintComponent(gfx: Graphics2D)
    82     {
    83       nodes_status.get(node_name) match {
    84         case Some(st) if st.total > 0 =>
    85           val size = peer.getSize()
    86           val insets = border.getBorderInsets(this.peer)
    87           val w = size.width - insets.left - insets.right
    88           val h = size.height - insets.top - insets.bottom
    89           var end = size.width - insets.right
    90           for {
    91             (n, color) <- List(
    92               (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
    93               (st.running, PIDE.options.color_value("running_color")),
    94               (st.warned, PIDE.options.color_value("warning_color")),
    95               (st.failed, PIDE.options.color_value("error_color"))) }
    96           {
    97             gfx.setColor(color)
    98             val v = (n * w / st.total) max (if (n > 0) 2 else 0)
    99             gfx.fillRect(end - v, insets.top, v, h)
   100             end -= v
   101           }
   102         case _ =>
   103       }
   104       super.paintComponent(gfx)
   105     }
   106   }
   107 
   108   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   109   {
   110     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   111       name: Document.Node.Name, index: Int): Component =
   112     {
   113       val component = Node_Renderer_Component
   114       component.node_name = name
   115       component.text = name.theory
   116       component
   117     }
   118   }
   119   status.renderer = new Node_Renderer
   120 
   121   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   122   {
   123     Swing_Thread.now {
   124       val snapshot = PIDE.session.snapshot()
   125 
   126       val iterator =
   127         restriction match {
   128           case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
   129           case None => snapshot.version.nodes.entries
   130         }
   131       val nodes_status1 =
   132         (nodes_status /: iterator)({ case (status, (name, node)) =>
   133             if (PIDE.thy_load.loaded_theories(name.theory)) status
   134             else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
   135 
   136       if (nodes_status != nodes_status1) {
   137         nodes_status = nodes_status1
   138         status.listData =
   139           snapshot.version.nodes.topological_order.filter(
   140             (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   141       }
   142     }
   143   }
   144 
   145 
   146   /* main actor */
   147 
   148   private val main_actor = actor {
   149     loop {
   150       react {
   151         case phase: Session.Phase => handle_phase(phase)
   152 
   153         case _: Session.Global_Options => Swing_Thread.later { logic.load () }
   154 
   155         case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
   156 
   157         case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
   158       }
   159     }
   160   }
   161 
   162   override def init()
   163   {
   164     PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
   165     PIDE.session.global_options += main_actor
   166     PIDE.session.commands_changed += main_actor; handle_update()
   167   }
   168 
   169   override def exit()
   170   {
   171     PIDE.session.phase_changed -= main_actor
   172     PIDE.session.global_options -= main_actor
   173     PIDE.session.commands_changed -= main_actor
   174   }
   175 }