src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Mon Aug 12 17:11:27 2013 +0200 (2013-08-12)
changeset 52980 28f59ca8ce78
parent 52973 d5f7fa1498b7
child 53176 f88635e1c52e
permissions -rw-r--r--
manage hyperlinks via PIDE editor interface;
     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 
   118     var node_name = Document.Node.Name.empty
   119 
   120     var checkbox_geometry: Option[(Point, Dimension)] = None
   121     val checkbox = new CheckBox {
   122       opaque = false
   123       override def paintComponent(gfx: Graphics2D)
   124       {
   125         super.paintComponent(gfx)
   126         if (location != null && size != null)
   127           checkbox_geometry = Some((location, size))
   128       }
   129     }
   130 
   131     val label = new Label {
   132       opaque = false
   133       xAlignment = Alignment.Leading
   134 
   135       override def paintComponent(gfx: Graphics2D)
   136       {
   137         border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
   138         val size = peer.getSize()
   139         val insets = border.getBorderInsets(peer)
   140         val w = size.width - insets.left - insets.right
   141         val h = size.height - insets.top - insets.bottom
   142 
   143         nodes_status.get(node_name) match {
   144           case Some(st) if st.total > 0 =>
   145             val colors = List(
   146               (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
   147               (st.running, PIDE.options.color_value("running_color")),
   148               (st.warned, PIDE.options.color_value("warning_color")),
   149               (st.failed, PIDE.options.color_value("error_color")))
   150 
   151             var end = size.width - insets.right
   152             for { (n, color) <- colors }
   153             {
   154               gfx.setColor(color)
   155               val v = (n * (w - colors.length) / st.total) max (if (n > 0) 4 else 0)
   156               gfx.fillRect(end - v, insets.top, v, h)
   157               end = end - v - 1
   158             }
   159           case _ =>
   160             gfx.setColor(PIDE.options.color_value("unprocessed1_color"))
   161             gfx.fillRect(insets.left, insets.top, w, h)
   162         }
   163         super.paintComponent(gfx)
   164       }
   165     }
   166 
   167     layout(checkbox) = BorderPanel.Position.West
   168     layout(label) = BorderPanel.Position.Center
   169   }
   170 
   171   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   172   {
   173     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   174       name: Document.Node.Name, index: Int): Component =
   175     {
   176       val component = Node_Renderer_Component
   177       component.node_name = name
   178       component.checkbox.selected = nodes_required.contains(name)
   179       component.label.text = name.theory
   180       component
   181     }
   182   }
   183   status.renderer = new Node_Renderer
   184 
   185   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   186   {
   187     Swing_Thread.now {
   188       val snapshot = PIDE.session.snapshot()
   189 
   190       val iterator =
   191         restriction match {
   192           case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
   193           case None => snapshot.version.nodes.entries
   194         }
   195       val nodes_status1 =
   196         (nodes_status /: iterator)({ case (status, (name, node)) =>
   197             if (PIDE.thy_load.loaded_theories(name.theory)) status
   198             else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
   199 
   200       if (nodes_status != nodes_status1) {
   201         nodes_status = nodes_status1
   202         status.listData =
   203           snapshot.version.nodes.topological_order.filter(
   204             (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   205       }
   206     }
   207   }
   208 
   209 
   210   /* main actor */
   211 
   212   private val main_actor = actor {
   213     loop {
   214       react {
   215         case phase: Session.Phase => handle_phase(phase)
   216 
   217         case _: Session.Global_Options =>
   218           Swing_Thread.later {
   219             continuous_checking.load()
   220             logic.load ()
   221             update_nodes_required()
   222             status.repaint()
   223           }
   224 
   225         case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
   226 
   227         case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
   228       }
   229     }
   230   }
   231 
   232   override def init()
   233   {
   234     PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
   235     PIDE.session.global_options += main_actor
   236     PIDE.session.commands_changed += main_actor; handle_update()
   237   }
   238 
   239   override def exit()
   240   {
   241     PIDE.session.phase_changed -= main_actor
   242     PIDE.session.global_options -= main_actor
   243     PIDE.session.commands_changed -= main_actor
   244   }
   245 }