src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Tue Mar 18 17:39:03 2014 +0100 (2014-03-18)
changeset 56208 06cc31dff138
parent 55877 65c9968286d5
child 56357 8a58a8c5a1c0
permissions -rw-r--r--
clarifed module name;
     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.awt.{BorderLayout, Graphics2D, Color, Point, Dimension}
    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     listenTo(mouse.moves)
    31     reactions += {
    32       case MouseClicked(_, point, _, clicks, _) =>
    33         val index = peer.locationToIndex(point)
    34         if (index >= 0) {
    35           if (in_checkbox(peer.indexToLocation(index), point)) {
    36             if (clicks == 1) {
    37               for {
    38                 buffer <- JEdit_Lib.jedit_buffer(listData(index).node)
    39                 model <- PIDE.document_model(buffer)
    40               } model.node_required = !model.node_required
    41             }
    42           }
    43           else if (clicks == 2) PIDE.editor.goto_file(view, listData(index).node)
    44         }
    45       case MouseMoved(_, point, _) =>
    46         val index = peer.locationToIndex(point)
    47         if (index >= 0 && in_checkbox(peer.indexToLocation(index), point))
    48           tooltip = "Mark as required for continuous checking"
    49         else
    50           tooltip = null
    51     }
    52   }
    53   status.peer.setLayoutOrientation(JList.HORIZONTAL_WRAP)
    54   status.peer.setVisibleRowCount(0)
    55   status.selection.intervalMode = ListView.IntervalMode.Single
    56 
    57   set_content(new ScrollPane(status))
    58 
    59 
    60   /* controls */
    61 
    62   def phase_text(phase: Session.Phase): String =
    63     "Prover: " + Library.lowercase(phase.toString)
    64 
    65   private val session_phase = new Label(phase_text(PIDE.session.phase))
    66   session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
    67   session_phase.tooltip = "Status of prover session"
    68 
    69   private def handle_phase(phase: Session.Phase)
    70   {
    71     Swing_Thread.require()
    72     session_phase.text = " " + phase_text(phase) + " "
    73   }
    74 
    75   private val continuous_checking = new Isabelle.Continuous_Checking
    76   continuous_checking.focusable = false
    77 
    78   private val logic = Isabelle_Logic.logic_selector(true)
    79 
    80   private val controls =
    81     new Wrap_Panel(Wrap_Panel.Alignment.Right)(continuous_checking, session_phase, logic)
    82   add(controls.peer, BorderLayout.NORTH)
    83 
    84 
    85   /* component state -- owned by Swing thread */
    86 
    87   private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
    88   private var nodes_required: Set[Document.Node.Name] = Set.empty
    89 
    90   private def update_nodes_required()
    91   {
    92     nodes_required = Set.empty
    93     for {
    94       buffer <- JEdit_Lib.jedit_buffers
    95       model <- PIDE.document_model(buffer)
    96       if model.node_required
    97     } nodes_required += model.node_name
    98   }
    99   update_nodes_required()
   100 
   101   private def in_checkbox(loc0: Point, p: Point): Boolean =
   102     Node_Renderer_Component != null &&
   103       (Node_Renderer_Component.checkbox_geometry match {
   104         case Some((loc, size)) =>
   105           loc0.x + loc.x <= p.x && p.x < loc0.x + size.width &&
   106           loc0.y + loc.y <= p.y && p.y < loc0.y + size.height
   107         case None => false
   108       })
   109 
   110   private object Node_Renderer_Component extends BorderPanel
   111   {
   112     opaque = false
   113     border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
   114 
   115     var node_name = Document.Node.Name.empty
   116 
   117     var checkbox_geometry: Option[(Point, Dimension)] = None
   118     val checkbox = new CheckBox {
   119       opaque = false
   120       override def paintComponent(gfx: Graphics2D)
   121       {
   122         super.paintComponent(gfx)
   123         if (location != null && size != null)
   124           checkbox_geometry = Some((location, size))
   125       }
   126     }
   127 
   128     val label = new Label {
   129       opaque = false
   130       border = BorderFactory.createEmptyBorder()
   131       xAlignment = Alignment.Leading
   132 
   133       override def paintComponent(gfx: Graphics2D)
   134       {
   135         def paint_segment(x: Int, w: Int, color: Color)
   136         {
   137           gfx.setColor(color)
   138           gfx.fillRect(x, 0, w, size.height)
   139         }
   140 
   141         nodes_status.get(node_name) match {
   142           case Some(st) if st.total > 0 =>
   143             val segments =
   144               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               ).filter(_._1 > 0)
   150 
   151             (size.width /: segments)({ case (last, (n, color)) =>
   152               val w = (n * (size.width - segments.length) / st.total) max 4
   153               paint_segment(last - w, w, color)
   154               last - w - 1
   155             })
   156 
   157           case _ =>
   158             paint_segment(0, size.width, PIDE.options.color_value("unprocessed1_color"))
   159         }
   160         super.paintComponent(gfx)
   161       }
   162     }
   163 
   164     layout(checkbox) = BorderPanel.Position.West
   165     layout(label) = BorderPanel.Position.Center
   166   }
   167 
   168   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   169   {
   170     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   171       name: Document.Node.Name, index: Int): Component =
   172     {
   173       val component = Node_Renderer_Component
   174       component.node_name = name
   175       component.checkbox.selected = nodes_required.contains(name)
   176       component.label.text = name.theory
   177       component
   178     }
   179   }
   180   status.renderer = new Node_Renderer
   181 
   182   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   183   {
   184     Swing_Thread.require()
   185 
   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       }).filter(_._1.is_theory)
   193     val nodes_status1 =
   194       (nodes_status /: iterator)({ case (status, (name, node)) =>
   195           if (PIDE.resources.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   /* main actor */
   208 
   209   private val main_actor = actor {
   210     loop {
   211       react {
   212         case phase: Session.Phase =>
   213           Swing_Thread.later { 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 =>
   224           Swing_Thread.later { 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
   234     PIDE.session.global_options += main_actor
   235     PIDE.session.commands_changed += main_actor
   236 
   237     handle_phase(PIDE.session.phase)
   238     handle_update()
   239   }
   240 
   241   override def exit()
   242   {
   243     PIDE.session.phase_changed -= main_actor
   244     PIDE.session.global_options -= main_actor
   245     PIDE.session.commands_changed -= main_actor
   246   }
   247 }