src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Tue Mar 26 14:03:31 2013 +0100 (2013-03-26)
changeset 51535 f2f480bc4223
parent 51534 123bd97fcea1
child 52759 a20631db9c8a
permissions -rw-r--r--
tuned;
     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) Hyperlink(listData(index).node).follow(view)
    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 = "Cancel current checking process"
    61 
    62   private val check = new Button("Check") {
    63     reactions += { case ButtonClicked(_) => PIDE.check_buffer(view.getBuffer) }
    64   }
    65   check.tooltip = "Commence full checking of current buffer"
    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 colors = List(
    90             (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
    91             (st.running, PIDE.options.color_value("running_color")),
    92             (st.warned, PIDE.options.color_value("warning_color")),
    93             (st.failed, PIDE.options.color_value("error_color")))
    94 
    95           val size = peer.getSize()
    96           val insets = border.getBorderInsets(peer)
    97           val w = size.width - insets.left - insets.right
    98           val h = size.height - insets.top - insets.bottom
    99           var end = size.width - insets.right
   100 
   101           for { (n, color) <- colors }
   102           {
   103             gfx.setColor(color)
   104             val v = (n * (w - colors.length) / st.total) max (if (n > 0) 4 else 0)
   105             gfx.fillRect(end - v, insets.top, v, h)
   106             end = end - v - 1
   107           }
   108         case _ =>
   109       }
   110       super.paintComponent(gfx)
   111     }
   112   }
   113 
   114   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   115   {
   116     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   117       name: Document.Node.Name, index: Int): Component =
   118     {
   119       val component = Node_Renderer_Component
   120       component.node_name = name
   121       component.text = name.theory
   122       component
   123     }
   124   }
   125   status.renderer = new Node_Renderer
   126 
   127   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   128   {
   129     Swing_Thread.now {
   130       val snapshot = PIDE.session.snapshot()
   131 
   132       val iterator =
   133         restriction match {
   134           case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
   135           case None => snapshot.version.nodes.entries
   136         }
   137       val nodes_status1 =
   138         (nodes_status /: iterator)({ case (status, (name, node)) =>
   139             if (PIDE.thy_load.loaded_theories(name.theory)) status
   140             else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
   141 
   142       if (nodes_status != nodes_status1) {
   143         nodes_status = nodes_status1
   144         status.listData =
   145           snapshot.version.nodes.topological_order.filter(
   146             (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   147       }
   148     }
   149   }
   150 
   151 
   152   /* main actor */
   153 
   154   private val main_actor = actor {
   155     loop {
   156       react {
   157         case phase: Session.Phase => handle_phase(phase)
   158 
   159         case _: Session.Global_Options => Swing_Thread.later { logic.load () }
   160 
   161         case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
   162 
   163         case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
   164       }
   165     }
   166   }
   167 
   168   override def init()
   169   {
   170     PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
   171     PIDE.session.global_options += main_actor
   172     PIDE.session.commands_changed += main_actor; handle_update()
   173   }
   174 
   175   override def exit()
   176   {
   177     PIDE.session.phase_changed -= main_actor
   178     PIDE.session.global_options -= main_actor
   179     PIDE.session.commands_changed -= main_actor
   180   }
   181 }