src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Fri Nov 30 21:30:24 2012 +0100 (2012-11-30)
changeset 50299 f70b3712040f
parent 50250 src/Tools/jEdit/src/session_dockable.scala@267bd685a69f
child 50300 6658097758ba
permissions -rw-r--r--
renamed dockable "Prover Session" to "Theories";
more uniform Library.lowercase/uppercase;
     1 /*  Title:      Tools/jEdit/src/theories_dockable.scala
     2     Author:     Makarius
     3 
     4 Dockable window for theories managed by prover.
     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 Theories_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.HORIZONTAL_WRAP)
    37   status.peer.setVisibleRowCount(0)
    38   status.selection.intervalMode = ListView.IntervalMode.Single
    39 
    40   set_content(new ScrollPane(status))
    41 
    42 
    43   /* controls */
    44 
    45   def phase_text(phase: Session.Phase): String =
    46     "Prover: " + Library.lowercase(phase.toString)
    47 
    48   private val session_phase = new Label(phase_text(PIDE.session.phase))
    49   session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
    50   session_phase.tooltip = "Status of prover session"
    51 
    52   private def handle_phase(phase: Session.Phase)
    53   {
    54     Swing_Thread.later { session_phase.text = " " + phase_text(phase) + " " }
    55   }
    56 
    57   private val cancel = new Button("Cancel") {
    58     reactions += { case ButtonClicked(_) => PIDE.cancel_execution() }
    59   }
    60   cancel.tooltip = jEdit.getProperty("isabelle.cancel-execution.label")
    61 
    62   private val check = new Button("Check") {
    63     reactions += { case ButtonClicked(_) => PIDE.check_buffer(view.getBuffer) }
    64   }
    65   check.tooltip = jEdit.getProperty("isabelle.check-buffer.label")
    66 
    67   private val logic = Isabelle_Logic.logic_selector(true)
    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, PIDE.options.color_value("unprocessed1_color")),
    97               (st.running, PIDE.options.color_value("running_color")),
    98               (st.warned, PIDE.options.color_value("warning_color")),
    99               (st.failed, PIDE.options.color_value("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 = PIDE.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 (PIDE.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 _: Session.Global_Options => Swing_Thread.later { logic.load () }
   158 
   159         case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
   160 
   161         case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
   162       }
   163     }
   164   }
   165 
   166   override def init()
   167   {
   168     PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
   169     PIDE.session.global_options += main_actor
   170     PIDE.session.commands_changed += main_actor; handle_update()
   171   }
   172 
   173   override def exit()
   174   {
   175     PIDE.session.phase_changed -= main_actor
   176     PIDE.session.global_options -= main_actor
   177     PIDE.session.commands_changed -= main_actor
   178   }
   179 }