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