src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Wed Jul 31 12:31:10 2013 +0200 (2013-07-31)
changeset 52809 e750169a5884
parent 52807 b859a180936b
child 52815 eaad5fe7bb1b
permissions -rw-r--r--
paint unassigned/unchanged nodes as unprocessed -- relevant for editor_continuous_checking = false;
wenzelm@50299
     1
/*  Title:      Tools/jEdit/src/theories_dockable.scala
wenzelm@39515
     2
    Author:     Makarius
wenzelm@39515
     3
wenzelm@50299
     4
Dockable window for theories managed by prover.
wenzelm@39515
     5
*/
wenzelm@39515
     6
wenzelm@39515
     7
package isabelle.jedit
wenzelm@39515
     8
wenzelm@39515
     9
wenzelm@39515
    10
import isabelle._
wenzelm@39515
    11
wenzelm@39515
    12
import scala.actors.Actor._
wenzelm@52807
    13
import scala.swing.{FlowPanel, Button, TextArea, Label, ListView, Alignment,
wenzelm@52807
    14
  ScrollPane, Component, CheckBox}
wenzelm@49246
    15
import scala.swing.event.{ButtonClicked, MouseClicked}
wenzelm@39593
    16
wenzelm@43520
    17
import java.lang.System
wenzelm@52802
    18
import java.awt.{BorderLayout, Graphics2D, Insets}
wenzelm@47588
    19
import javax.swing.{JList, BorderFactory}
wenzelm@52802
    20
import javax.swing.border.{BevelBorder, SoftBevelBorder}
wenzelm@39515
    21
wenzelm@44865
    22
import org.gjt.sp.jedit.{View, jEdit}
wenzelm@39515
    23
wenzelm@39515
    24
wenzelm@50299
    25
class Theories_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@39515
    26
{
wenzelm@48021
    27
  /* status */
wenzelm@39591
    28
wenzelm@48021
    29
  private val status = new ListView(Nil: List[Document.Node.Name]) {
wenzelm@44991
    30
    listenTo(mouse.clicks)
wenzelm@44991
    31
    reactions += {
wenzelm@44991
    32
      case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
wenzelm@44991
    33
        val index = peer.locationToIndex(point)
wenzelm@51534
    34
        if (index >= 0) Hyperlink(listData(index).node).follow(view)
wenzelm@44991
    35
    }
wenzelm@44991
    36
  }
wenzelm@50250
    37
  status.peer.setLayoutOrientation(JList.HORIZONTAL_WRAP)
wenzelm@50250
    38
  status.peer.setVisibleRowCount(0)
wenzelm@44609
    39
  status.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@44609
    40
wenzelm@49038
    41
  set_content(new ScrollPane(status))
wenzelm@39515
    42
wenzelm@39515
    43
wenzelm@39593
    44
  /* controls */
wenzelm@39593
    45
wenzelm@50299
    46
  def phase_text(phase: Session.Phase): String =
wenzelm@50299
    47
    "Prover: " + Library.lowercase(phase.toString)
wenzelm@50299
    48
wenzelm@50299
    49
  private val session_phase = new Label(phase_text(PIDE.session.phase))
wenzelm@39697
    50
  session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@50299
    51
  session_phase.tooltip = "Status of prover session"
wenzelm@39635
    52
wenzelm@48018
    53
  private def handle_phase(phase: Session.Phase)
wenzelm@48018
    54
  {
wenzelm@50299
    55
    Swing_Thread.later { session_phase.text = " " + phase_text(phase) + " " }
wenzelm@48018
    56
  }
wenzelm@48018
    57
wenzelm@52807
    58
  private val continuous_checking = new CheckBox("Continuous checking") {
wenzelm@52807
    59
    tooltip = "Continuous checking of proof document (visible and required parts)"
wenzelm@52807
    60
    reactions += { case ButtonClicked(_) => PIDE.continuous_checking = selected }
wenzelm@52807
    61
    def load() { selected = PIDE.continuous_checking }
wenzelm@52763
    62
    load()
wenzelm@44776
    63
  }
wenzelm@44776
    64
wenzelm@49246
    65
  private val logic = Isabelle_Logic.logic_selector(true)
wenzelm@39702
    66
wenzelm@44775
    67
  private val controls =
wenzelm@52807
    68
    new FlowPanel(FlowPanel.Alignment.Right)(continuous_checking, session_phase, logic)
wenzelm@39593
    69
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    70
wenzelm@39593
    71
wenzelm@44609
    72
  /* component state -- owned by Swing thread */
wenzelm@44609
    73
wenzelm@45709
    74
  private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
wenzelm@44866
    75
wenzelm@44990
    76
  private object Node_Renderer_Component extends Label
wenzelm@44866
    77
  {
wenzelm@44990
    78
    opaque = false
wenzelm@44957
    79
    xAlignment = Alignment.Leading
wenzelm@44989
    80
    border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@44957
    81
wenzelm@44957
    82
    var node_name = Document.Node.Name.empty
wenzelm@44957
    83
    override def paintComponent(gfx: Graphics2D)
wenzelm@44866
    84
    {
wenzelm@52809
    85
      val size = peer.getSize()
wenzelm@52809
    86
      val insets = border.getBorderInsets(peer)
wenzelm@52809
    87
      val w = size.width - insets.left - insets.right
wenzelm@52809
    88
      val h = size.height - insets.top - insets.bottom
wenzelm@52809
    89
wenzelm@44957
    90
      nodes_status.get(node_name) match {
wenzelm@44866
    91
        case Some(st) if st.total > 0 =>
wenzelm@50900
    92
          val colors = List(
wenzelm@50900
    93
            (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
wenzelm@50900
    94
            (st.running, PIDE.options.color_value("running_color")),
wenzelm@50900
    95
            (st.warned, PIDE.options.color_value("warning_color")),
wenzelm@50900
    96
            (st.failed, PIDE.options.color_value("error_color")))
wenzelm@50900
    97
wenzelm@44958
    98
          var end = size.width - insets.right
wenzelm@50900
    99
          for { (n, color) <- colors }
wenzelm@44866
   100
          {
wenzelm@44866
   101
            gfx.setColor(color)
wenzelm@50900
   102
            val v = (n * (w - colors.length) / st.total) max (if (n > 0) 4 else 0)
wenzelm@44958
   103
            gfx.fillRect(end - v, insets.top, v, h)
wenzelm@50900
   104
            end = end - v - 1
wenzelm@44866
   105
          }
wenzelm@44866
   106
        case _ =>
wenzelm@52809
   107
          gfx.setColor(PIDE.options.color_value("unprocessed1_color"))
wenzelm@52809
   108
          gfx.fillRect(insets.left, insets.top, w, h)
wenzelm@44866
   109
      }
wenzelm@44867
   110
      super.paintComponent(gfx)
wenzelm@44866
   111
    }
wenzelm@44866
   112
  }
wenzelm@44866
   113
wenzelm@44990
   114
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   115
  {
wenzelm@44990
   116
    def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44990
   117
      name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   118
    {
wenzelm@44990
   119
      val component = Node_Renderer_Component
wenzelm@44957
   120
      component.node_name = name
wenzelm@44989
   121
      component.text = name.theory
wenzelm@44990
   122
      component
wenzelm@44957
   123
    }
wenzelm@44957
   124
  }
wenzelm@44957
   125
  status.renderer = new Node_Renderer
wenzelm@44609
   126
wenzelm@45011
   127
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   128
  {
wenzelm@44609
   129
    Swing_Thread.now {
wenzelm@50205
   130
      val snapshot = PIDE.session.snapshot()
wenzelm@44613
   131
wenzelm@46723
   132
      val iterator =
wenzelm@46723
   133
        restriction match {
wenzelm@46723
   134
          case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@46723
   135
          case None => snapshot.version.nodes.entries
wenzelm@46723
   136
        }
wenzelm@46681
   137
      val nodes_status1 =
wenzelm@46723
   138
        (nodes_status /: iterator)({ case (status, (name, node)) =>
wenzelm@50205
   139
            if (PIDE.thy_load.loaded_theories(name.theory)) status
wenzelm@46739
   140
            else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
wenzelm@44672
   141
wenzelm@44672
   142
      if (nodes_status != nodes_status1) {
wenzelm@44672
   143
        nodes_status = nodes_status1
wenzelm@44866
   144
        status.listData =
wenzelm@46723
   145
          snapshot.version.nodes.topological_order.filter(
wenzelm@44960
   146
            (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
wenzelm@44609
   147
      }
wenzelm@44609
   148
    }
wenzelm@44609
   149
  }
wenzelm@44609
   150
wenzelm@44609
   151
wenzelm@39515
   152
  /* main actor */
wenzelm@39515
   153
wenzelm@39515
   154
  private val main_actor = actor {
wenzelm@39515
   155
    loop {
wenzelm@39515
   156
      react {
wenzelm@48018
   157
        case phase: Session.Phase => handle_phase(phase)
wenzelm@39635
   158
wenzelm@52759
   159
        case _: Session.Global_Options =>
wenzelm@52759
   160
          Swing_Thread.later {
wenzelm@52807
   161
            continuous_checking.load()
wenzelm@52759
   162
            logic.load ()
wenzelm@52759
   163
          }
wenzelm@49247
   164
wenzelm@45011
   165
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@44609
   166
wenzelm@50299
   167
        case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
wenzelm@39515
   168
      }
wenzelm@39515
   169
    }
wenzelm@39515
   170
  }
wenzelm@39515
   171
wenzelm@48018
   172
  override def init()
wenzelm@48018
   173
  {
wenzelm@50205
   174
    PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
wenzelm@50205
   175
    PIDE.session.global_options += main_actor
wenzelm@50205
   176
    PIDE.session.commands_changed += main_actor; handle_update()
wenzelm@39635
   177
  }
wenzelm@39635
   178
wenzelm@48018
   179
  override def exit()
wenzelm@48018
   180
  {
wenzelm@50205
   181
    PIDE.session.phase_changed -= main_actor
wenzelm@50205
   182
    PIDE.session.global_options -= main_actor
wenzelm@50205
   183
    PIDE.session.commands_changed -= main_actor
wenzelm@39635
   184
  }
wenzelm@39515
   185
}