src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Sat Nov 30 16:07:00 2013 +0100 (2013-11-30)
changeset 54652 07ee041537a5
parent 53715 68c664737d04
child 54671 d64a4ef26edb
permissions -rw-r--r--
non-focusable button, to avoid second interpretation of SPACE in C+e SPACE (isabelle.set-node-required);
     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.{Button, TextArea, Label, ListView, Alignment,
    14   ScrollPane, Component, CheckBox, BorderPanel}
    15 import scala.swing.event.{MouseClicked, MouseMoved}
    16 
    17 import java.lang.System
    18 import java.awt.{BorderLayout, Graphics2D, Color, 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.require()
    73     session_phase.text = " " + phase_text(phase) + " "
    74   }
    75 
    76   private val continuous_checking = new Isabelle.Continuous_Checking
    77   continuous_checking.focusable = false
    78 
    79   private val logic = Isabelle_Logic.logic_selector(true)
    80 
    81   private val controls =
    82     new Wrap_Panel(Wrap_Panel.Alignment.Right)(continuous_checking, session_phase, logic)
    83   add(controls.peer, BorderLayout.NORTH)
    84 
    85 
    86   /* component state -- owned by Swing thread */
    87 
    88   private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
    89   private var nodes_required: Set[Document.Node.Name] = Set.empty
    90 
    91   private def update_nodes_required()
    92   {
    93     nodes_required = Set.empty
    94     for {
    95       buffer <- JEdit_Lib.jedit_buffers
    96       model <- PIDE.document_model(buffer)
    97       if model.node_required
    98     } nodes_required += model.node_name
    99   }
   100   update_nodes_required()
   101 
   102   private def in_checkbox(loc0: Point, p: Point): Boolean =
   103     Node_Renderer_Component != null &&
   104       (Node_Renderer_Component.checkbox_geometry match {
   105         case Some((loc, size)) =>
   106           loc0.x + loc.x <= p.x && p.x < loc0.x + size.width &&
   107           loc0.y + loc.y <= p.y && p.y < loc0.y + size.height
   108         case None => false
   109       })
   110 
   111   private object Node_Renderer_Component extends BorderPanel
   112   {
   113     opaque = false
   114     border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
   115 
   116     var node_name = Document.Node.Name.empty
   117 
   118     var checkbox_geometry: Option[(Point, Dimension)] = None
   119     val checkbox = new CheckBox {
   120       opaque = false
   121       override def paintComponent(gfx: Graphics2D)
   122       {
   123         super.paintComponent(gfx)
   124         if (location != null && size != null)
   125           checkbox_geometry = Some((location, size))
   126       }
   127     }
   128 
   129     val label = new Label {
   130       opaque = false
   131       border = BorderFactory.createEmptyBorder()
   132       xAlignment = Alignment.Leading
   133 
   134       override def paintComponent(gfx: Graphics2D)
   135       {
   136         def paint_segment(x: Int, w: Int, color: Color)
   137         {
   138           gfx.setColor(color)
   139           gfx.fillRect(x, 0, w, size.height)
   140         }
   141 
   142         nodes_status.get(node_name) match {
   143           case Some(st) if st.total > 0 =>
   144             val segments =
   145               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               ).filter(_._1 > 0)
   151 
   152             (size.width /: segments)({ case (last, (n, color)) =>
   153               val w = (n * (size.width - segments.length) / st.total) max 4
   154               paint_segment(last - w, w, color)
   155               last - w - 1
   156             })
   157 
   158           case _ =>
   159             paint_segment(0, size.width, PIDE.options.color_value("unprocessed1_color"))
   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.require()
   186 
   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   /* main actor */
   209 
   210   private val main_actor = actor {
   211     loop {
   212       react {
   213         case phase: Session.Phase =>
   214           Swing_Thread.later { 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 =>
   225           Swing_Thread.later { 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
   235     PIDE.session.global_options += main_actor
   236     PIDE.session.commands_changed += main_actor
   237 
   238     handle_phase(PIDE.session.phase)
   239     handle_update()
   240   }
   241 
   242   override def exit()
   243   {
   244     PIDE.session.phase_changed -= main_actor
   245     PIDE.session.global_options -= main_actor
   246     PIDE.session.commands_changed -= main_actor
   247   }
   248 }