src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Sat Aug 24 12:31:24 2013 +0200 (2013-08-24)
changeset 53176 f88635e1c52e
parent 52980 28f59ca8ce78
child 53177 dcac8d837b9c
permissions -rw-r--r--
clarified border (again, see also 7ce3ebc268a1);
     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,
    14   ScrollPane, Component, CheckBox, BorderPanel}
    15 import scala.swing.event.{ButtonClicked, MouseClicked, MouseMoved}
    16 
    17 import java.lang.System
    18 import java.awt.{BorderLayout, Graphics2D, Insets, Point, Dimension}
    19 import javax.swing.{JList, BorderFactory}
    20 import javax.swing.border.{BevelBorder, SoftBevelBorder}
    21 
    22 import org.gjt.sp.jedit.{View, jEdit}
    23 
    24 
    25 class Theories_Dockable(view: View, position: String) extends Dockable(view, position)
    26 {
    27   /* status */
    28 
    29   private val status = new ListView(Nil: List[Document.Node.Name]) {
    30     listenTo(mouse.clicks)
    31     listenTo(mouse.moves)
    32     reactions += {
    33       case MouseClicked(_, point, _, clicks, _) =>
    34         val index = peer.locationToIndex(point)
    35         if (index >= 0) {
    36           if (in_checkbox(peer.indexToLocation(index), point)) {
    37             if (clicks == 1) {
    38               for {
    39                 buffer <- JEdit_Lib.jedit_buffer(listData(index).node)
    40                 model <- PIDE.document_model(buffer)
    41               } model.node_required = !model.node_required
    42             }
    43           }
    44           else if (clicks == 2) PIDE.editor.goto(view, listData(index).node)
    45         }
    46       case MouseMoved(_, point, _) =>
    47         val index = peer.locationToIndex(point)
    48         if (index >= 0 && in_checkbox(peer.indexToLocation(index), point))
    49           tooltip = "Mark as required for continuous checking"
    50         else
    51           tooltip = null
    52     }
    53   }
    54   status.peer.setLayoutOrientation(JList.HORIZONTAL_WRAP)
    55   status.peer.setVisibleRowCount(0)
    56   status.selection.intervalMode = ListView.IntervalMode.Single
    57 
    58   set_content(new ScrollPane(status))
    59 
    60 
    61   /* controls */
    62 
    63   def phase_text(phase: Session.Phase): String =
    64     "Prover: " + Library.lowercase(phase.toString)
    65 
    66   private val session_phase = new Label(phase_text(PIDE.session.phase))
    67   session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
    68   session_phase.tooltip = "Status of prover session"
    69 
    70   private def handle_phase(phase: Session.Phase)
    71   {
    72     Swing_Thread.later { session_phase.text = " " + phase_text(phase) + " " }
    73   }
    74 
    75   private val continuous_checking = new CheckBox("Continuous checking") {
    76     tooltip = "Continuous checking of proof document (visible and required parts)"
    77     reactions += { case ButtonClicked(_) => Isabelle.continuous_checking = selected }
    78     def load() { selected = Isabelle.continuous_checking }
    79     load()
    80   }
    81 
    82   private val logic = Isabelle_Logic.logic_selector(true)
    83 
    84   private val controls =
    85     new FlowPanel(FlowPanel.Alignment.Right)(continuous_checking, session_phase, logic)
    86   add(controls.peer, BorderLayout.NORTH)
    87 
    88 
    89   /* component state -- owned by Swing thread */
    90 
    91   private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
    92   private var nodes_required: Set[Document.Node.Name] = Set.empty
    93 
    94   private def update_nodes_required()
    95   {
    96     nodes_required = Set.empty
    97     for {
    98       buffer <- JEdit_Lib.jedit_buffers
    99       model <- PIDE.document_model(buffer)
   100       if model.node_required
   101     } nodes_required += model.node_name
   102   }
   103   update_nodes_required()
   104 
   105   private def in_checkbox(loc0: Point, p: Point): Boolean =
   106     Node_Renderer_Component != null &&
   107       (Node_Renderer_Component.checkbox_geometry match {
   108         case Some((loc, size)) =>
   109           loc0.x + loc.x <= p.x && p.x < loc0.x + size.width &&
   110           loc0.y + loc.y <= p.y && p.y < loc0.y + size.height
   111         case None => false
   112       })
   113 
   114   private object Node_Renderer_Component extends BorderPanel
   115   {
   116     opaque = false
   117     border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
   118 
   119     var node_name = Document.Node.Name.empty
   120 
   121     var checkbox_geometry: Option[(Point, Dimension)] = None
   122     val checkbox = new CheckBox {
   123       opaque = false
   124       override def paintComponent(gfx: Graphics2D)
   125       {
   126         super.paintComponent(gfx)
   127         if (location != null && size != null)
   128           checkbox_geometry = Some((location, size))
   129       }
   130     }
   131 
   132     val label = new Label {
   133       opaque = false
   134       border = BorderFactory.createEmptyBorder()
   135       xAlignment = Alignment.Leading
   136 
   137       override def paintComponent(gfx: Graphics2D)
   138       {
   139         val w = size.width
   140         val h = size.height
   141 
   142         nodes_status.get(node_name) match {
   143           case Some(st) if st.total > 0 =>
   144             val colors = List(
   145               (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
   146               (st.running, PIDE.options.color_value("running_color")),
   147               (st.warned, PIDE.options.color_value("warning_color")),
   148               (st.failed, PIDE.options.color_value("error_color")))
   149 
   150             var end = size.width
   151             for { (n, color) <- colors }
   152             {
   153               gfx.setColor(color)
   154               val v = (n * (w - colors.length) / st.total) max (if (n > 0) 4 else 0)
   155               gfx.fillRect(end - v, 0, v, h)
   156               end = end - v - 1
   157             }
   158           case _ =>
   159             gfx.setColor(PIDE.options.color_value("unprocessed1_color"))
   160             gfx.fillRect(0, 0, w, h)
   161         }
   162         super.paintComponent(gfx)
   163       }
   164     }
   165 
   166     layout(checkbox) = BorderPanel.Position.West
   167     layout(label) = BorderPanel.Position.Center
   168   }
   169 
   170   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   171   {
   172     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   173       name: Document.Node.Name, index: Int): Component =
   174     {
   175       val component = Node_Renderer_Component
   176       component.node_name = name
   177       component.checkbox.selected = nodes_required.contains(name)
   178       component.label.text = name.theory
   179       component
   180     }
   181   }
   182   status.renderer = new Node_Renderer
   183 
   184   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   185   {
   186     Swing_Thread.now {
   187       val snapshot = PIDE.session.snapshot()
   188 
   189       val iterator =
   190         restriction match {
   191           case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
   192           case None => snapshot.version.nodes.entries
   193         }
   194       val nodes_status1 =
   195         (nodes_status /: iterator)({ case (status, (name, node)) =>
   196             if (PIDE.thy_load.loaded_theories(name.theory)) status
   197             else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
   198 
   199       if (nodes_status != nodes_status1) {
   200         nodes_status = nodes_status1
   201         status.listData =
   202           snapshot.version.nodes.topological_order.filter(
   203             (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   204       }
   205     }
   206   }
   207 
   208 
   209   /* main actor */
   210 
   211   private val main_actor = actor {
   212     loop {
   213       react {
   214         case phase: Session.Phase => handle_phase(phase)
   215 
   216         case _: Session.Global_Options =>
   217           Swing_Thread.later {
   218             continuous_checking.load()
   219             logic.load ()
   220             update_nodes_required()
   221             status.repaint()
   222           }
   223 
   224         case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
   225 
   226         case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
   227       }
   228     }
   229   }
   230 
   231   override def init()
   232   {
   233     PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
   234     PIDE.session.global_options += main_actor
   235     PIDE.session.commands_changed += main_actor; handle_update()
   236   }
   237 
   238   override def exit()
   239   {
   240     PIDE.session.phase_changed -= main_actor
   241     PIDE.session.global_options -= main_actor
   242     PIDE.session.commands_changed -= main_actor
   243   }
   244 }