src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Fri Apr 25 12:51:08 2014 +0200 (2014-04-25)
changeset 56715 52125652e82a
parent 56662 f373fb77e0a4
child 57612 990ffb84489b
permissions -rw-r--r--
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
eliminated old actors;
     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.swing.{Button, TextArea, Label, ListView, Alignment,
    13   ScrollPane, Component, CheckBox, BorderPanel}
    14 import scala.swing.event.{MouseClicked, MouseMoved}
    15 
    16 import java.awt.{BorderLayout, Graphics2D, Color, Point, Dimension}
    17 import javax.swing.{JList, BorderFactory, UIManager}
    18 import javax.swing.border.{BevelBorder, SoftBevelBorder}
    19 
    20 import org.gjt.sp.jedit.{View, jEdit}
    21 
    22 
    23 class Theories_Dockable(view: View, position: String) extends Dockable(view, position)
    24 {
    25   /* status */
    26 
    27   private val status = new ListView(Nil: List[Document.Node.Name]) {
    28     background =
    29     {
    30       // enforce default value
    31       val c = UIManager.getDefaults.getColor("Panel.background")
    32       new Color(c.getRed, c.getGreen, c.getBlue, c.getAlpha)
    33     }
    34     listenTo(mouse.clicks)
    35     listenTo(mouse.moves)
    36     reactions += {
    37       case MouseClicked(_, point, _, clicks, _) =>
    38         val index = peer.locationToIndex(point)
    39         if (index >= 0) {
    40           if (in_checkbox(peer.indexToLocation(index), point)) {
    41             if (clicks == 1) {
    42               for {
    43                 buffer <- JEdit_Lib.jedit_buffer(listData(index))
    44                 model <- PIDE.document_model(buffer)
    45               } model.node_required = !model.node_required
    46             }
    47           }
    48           else if (clicks == 2) PIDE.editor.goto_file(view, listData(index).node)
    49         }
    50       case MouseMoved(_, point, _) =>
    51         val index = peer.locationToIndex(point)
    52         if (index >= 0 && in_checkbox(peer.indexToLocation(index), point))
    53           tooltip = "Mark as required for continuous checking"
    54         else
    55           tooltip = null
    56     }
    57   }
    58   status.peer.setLayoutOrientation(JList.HORIZONTAL_WRAP)
    59   status.peer.setVisibleRowCount(0)
    60   status.selection.intervalMode = ListView.IntervalMode.Single
    61 
    62   set_content(new ScrollPane(status))
    63 
    64 
    65   /* controls */
    66 
    67   def phase_text(phase: Session.Phase): String =
    68     "Prover: " + Word.lowercase(phase.toString)
    69 
    70   private val session_phase = new Label(phase_text(PIDE.session.phase))
    71   session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
    72   session_phase.tooltip = "Status of prover session"
    73 
    74   private def handle_phase(phase: Session.Phase)
    75   {
    76     Swing_Thread.require {}
    77     session_phase.text = " " + phase_text(phase) + " "
    78   }
    79 
    80   private val continuous_checking = new Isabelle.Continuous_Checking
    81   continuous_checking.focusable = false
    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 = true
   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       background = view.getTextArea.getPainter.getBackground
   135       foreground = view.getTextArea.getPainter.getForeground
   136       border =
   137         BorderFactory.createCompoundBorder(
   138           BorderFactory.createLineBorder(foreground, 1),
   139           BorderFactory.createEmptyBorder(1, 1, 1, 1))
   140       opaque = false
   141       xAlignment = Alignment.Leading
   142 
   143       override def paintComponent(gfx: Graphics2D)
   144       {
   145         def paint_segment(x: Int, w: Int, color: Color)
   146         {
   147           gfx.setColor(color)
   148           gfx.fillRect(x, 0, w, size.height)
   149         }
   150 
   151         paint_segment(0, size.width, background)
   152         nodes_status.get(node_name) match {
   153           case Some(st) if st.total > 0 =>
   154             val segments =
   155               List(
   156                 (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
   157                 (st.running, PIDE.options.color_value("running_color")),
   158                 (st.warned, PIDE.options.color_value("warning_color")),
   159                 (st.failed, PIDE.options.color_value("error_color"))
   160               ).filter(_._1 > 0)
   161 
   162             ((size.width - 1) /: segments)({ case (last, (n, color)) =>
   163               val w = (n * ((size.width - 2) - segments.length) / st.total) max 4
   164               paint_segment(last - w, w, color)
   165               last - w - 1
   166             })
   167 
   168           case _ =>
   169             paint_segment(0, size.width, PIDE.options.color_value("unprocessed1_color"))
   170         }
   171         super.paintComponent(gfx)
   172       }
   173     }
   174 
   175     layout(checkbox) = BorderPanel.Position.West
   176     layout(label) = BorderPanel.Position.Center
   177   }
   178 
   179   private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
   180   {
   181     def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
   182       name: Document.Node.Name, index: Int): Component =
   183     {
   184       val component = Node_Renderer_Component
   185       component.node_name = name
   186       component.checkbox.selected = nodes_required.contains(name)
   187       component.label.text = name.theory
   188       component
   189     }
   190   }
   191   status.renderer = new Node_Renderer
   192 
   193   private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
   194   {
   195     Swing_Thread.require {}
   196 
   197     val snapshot = PIDE.session.snapshot()
   198 
   199     val iterator =
   200       (restriction match {
   201         case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
   202         case None => snapshot.version.nodes.iterator
   203       }).filter(_._1.is_theory)
   204     val nodes_status1 =
   205       (nodes_status /: iterator)({ case (status, (name, node)) =>
   206           if (PIDE.resources.loaded_theories(name.theory)) status
   207           else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
   208 
   209     if (nodes_status != nodes_status1) {
   210       nodes_status = nodes_status1
   211       status.listData =
   212         snapshot.version.nodes.topological_order.filter(
   213           (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
   214     }
   215   }
   216 
   217 
   218   /* main */
   219 
   220   private val main =
   221     Session.Consumer[Any](getClass.getName) {
   222       case phase: Session.Phase =>
   223         Swing_Thread.later { handle_phase(phase) }
   224 
   225       case _: Session.Global_Options =>
   226         Swing_Thread.later {
   227           continuous_checking.load()
   228           logic.load ()
   229           update_nodes_required()
   230           status.repaint()
   231         }
   232 
   233       case changed: Session.Commands_Changed =>
   234         Swing_Thread.later { handle_update(Some(changed.nodes)) }
   235     }
   236 
   237   override def init()
   238   {
   239     PIDE.session.phase_changed += main
   240     PIDE.session.global_options += main
   241     PIDE.session.commands_changed += main
   242 
   243     handle_phase(PIDE.session.phase)
   244     handle_update()
   245   }
   246 
   247   override def exit()
   248   {
   249     PIDE.session.phase_changed -= main
   250     PIDE.session.global_options -= main
   251     PIDE.session.commands_changed -= main
   252   }
   253 }