src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Wed Sep 18 15:09:15 2013 +0200 (2013-09-18)
changeset 53711 8ce7795256e1
parent 53178 0a3a5f606b2a
child 53715 68c664737d04
permissions -rw-r--r--
improved FlowLayout for wrapping of components over multiple lines;
     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.{ButtonClicked, 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 CheckBox("Continuous checking") {
    77     tooltip = "Continuous checking of proof document (visible and required parts)"
    78     reactions += { case ButtonClicked(_) => Isabelle.continuous_checking = selected }
    79     def load() { selected = Isabelle.continuous_checking }
    80     load()
    81   }
    82 
    83   private val logic = Isabelle_Logic.logic_selector(true)
    84 
    85   private val controls =
    86     new Wrap_Panel(Wrap_Panel.Alignment.Right)(continuous_checking, session_phase, logic)
    87   add(controls.peer, BorderLayout.NORTH)
    88 
    89 
    90   /* component state -- owned by Swing thread */
    91 
    92   private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
    93   private var nodes_required: Set[Document.Node.Name] = Set.empty
    94 
    95   private def update_nodes_required()
    96   {
    97     nodes_required = Set.empty
    98     for {
    99       buffer <- JEdit_Lib.jedit_buffers
   100       model <- PIDE.document_model(buffer)
   101       if model.node_required
   102     } nodes_required += model.node_name
   103   }
   104   update_nodes_required()
   105 
   106   private def in_checkbox(loc0: Point, p: Point): Boolean =
   107     Node_Renderer_Component != null &&
   108       (Node_Renderer_Component.checkbox_geometry match {
   109         case Some((loc, size)) =>
   110           loc0.x + loc.x <= p.x && p.x < loc0.x + size.width &&
   111           loc0.y + loc.y <= p.y && p.y < loc0.y + size.height
   112         case None => false
   113       })
   114 
   115   private object Node_Renderer_Component extends BorderPanel
   116   {
   117     opaque = false
   118     border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
   119 
   120     var node_name = Document.Node.Name.empty
   121 
   122     var checkbox_geometry: Option[(Point, Dimension)] = None
   123     val checkbox = new CheckBox {
   124       opaque = false
   125       override def paintComponent(gfx: Graphics2D)
   126       {
   127         super.paintComponent(gfx)
   128         if (location != null && size != null)
   129           checkbox_geometry = Some((location, size))
   130       }
   131     }
   132 
   133     val label = new Label {
   134       opaque = false
   135       border = BorderFactory.createEmptyBorder()
   136       xAlignment = Alignment.Leading
   137 
   138       override def paintComponent(gfx: Graphics2D)
   139       {
   140         def paint_segment(x: Int, w: Int, color: Color)
   141         {
   142           gfx.setColor(color)
   143           gfx.fillRect(x, 0, w, size.height)
   144         }
   145 
   146         nodes_status.get(node_name) match {
   147           case Some(st) if st.total > 0 =>
   148             val segments =
   149               List(
   150                 (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
   151                 (st.running, PIDE.options.color_value("running_color")),
   152                 (st.warned, PIDE.options.color_value("warning_color")),
   153                 (st.failed, PIDE.options.color_value("error_color"))
   154               ).filter(_._1 > 0)
   155 
   156             (size.width /: segments)({ case (last, (n, color)) =>
   157               val w = (n * (size.width - segments.length) / st.total) max 4
   158               paint_segment(last - w, w, color)
   159               last - w - 1
   160             })
   161 
   162           case _ =>
   163             paint_segment(0, size.width, PIDE.options.color_value("unprocessed1_color"))
   164         }
   165         super.paintComponent(gfx)
   166       }
   167     }
   168 
   169     layout(checkbox) = BorderPanel.Position.West
   170     layout(label) = BorderPanel.Position.Center
   171   }
   172 
   173   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   174   {
   175     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   176       name: Document.Node.Name, index: Int): Component =
   177     {
   178       val component = Node_Renderer_Component
   179       component.node_name = name
   180       component.checkbox.selected = nodes_required.contains(name)
   181       component.label.text = name.theory
   182       component
   183     }
   184   }
   185   status.renderer = new Node_Renderer
   186 
   187   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   188   {
   189     Swing_Thread.require()
   190 
   191     val snapshot = PIDE.session.snapshot()
   192 
   193     val iterator =
   194       restriction match {
   195         case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
   196         case None => snapshot.version.nodes.entries
   197       }
   198     val nodes_status1 =
   199       (nodes_status /: iterator)({ case (status, (name, node)) =>
   200           if (PIDE.thy_load.loaded_theories(name.theory)) status
   201           else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
   202 
   203     if (nodes_status != nodes_status1) {
   204       nodes_status = nodes_status1
   205       status.listData =
   206         snapshot.version.nodes.topological_order.filter(
   207           (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   208     }
   209   }
   210 
   211 
   212   /* main actor */
   213 
   214   private val main_actor = actor {
   215     loop {
   216       react {
   217         case phase: Session.Phase =>
   218           Swing_Thread.later { handle_phase(phase) }
   219 
   220         case _: Session.Global_Options =>
   221           Swing_Thread.later {
   222             continuous_checking.load()
   223             logic.load ()
   224             update_nodes_required()
   225             status.repaint()
   226           }
   227 
   228         case changed: Session.Commands_Changed =>
   229           Swing_Thread.later { handle_update(Some(changed.nodes)) }
   230 
   231         case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
   232       }
   233     }
   234   }
   235 
   236   override def init()
   237   {
   238     PIDE.session.phase_changed += main_actor
   239     PIDE.session.global_options += main_actor
   240     PIDE.session.commands_changed += main_actor
   241 
   242     handle_phase(PIDE.session.phase)
   243     handle_update()
   244   }
   245 
   246   override def exit()
   247   {
   248     PIDE.session.phase_changed -= main_actor
   249     PIDE.session.global_options -= main_actor
   250     PIDE.session.commands_changed -= main_actor
   251   }
   252 }