src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Tue Jul 30 22:43:11 2013 +0200 (2013-07-30)
changeset 52802 0b98561d0790
parent 52779 82707f95a783
child 52807 b859a180936b
permissions -rw-r--r--
more uniform border;
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@52759
    13
import scala.swing.{FlowPanel, Button, TextArea, Label, ListView, Alignment, ScrollPane, Component,
wenzelm@52759
    14
  BoxPanel, Orientation, RadioButton, ButtonGroup}
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@52759
    58
  private val execution_range = new BoxPanel(Orientation.Horizontal) {
wenzelm@52769
    59
    private def button(range: PIDE.Execution_Range.Value, tip: String): RadioButton =
wenzelm@52759
    60
      new RadioButton(range.toString) {
wenzelm@52769
    61
        tooltip = tip
wenzelm@52759
    62
        reactions += { case ButtonClicked(_) => PIDE.update_execution_range(range) }
wenzelm@52759
    63
      }
wenzelm@52769
    64
    private val label =
wenzelm@52769
    65
      new Label("Range:") { tooltip = "Execution range of continuous document processing" }
wenzelm@52779
    66
    private val b1 = button(PIDE.Execution_Range.ALL, "Check all theories (potentially slow)")
wenzelm@52769
    67
    private val b2 = button(PIDE.Execution_Range.NONE, "Check nothing")
wenzelm@52769
    68
    private val b3 = button(PIDE.Execution_Range.VISIBLE, "Check visible parts of theories")
wenzelm@52759
    69
    private val group = new ButtonGroup(b1, b2, b3)
wenzelm@52759
    70
    contents ++= List(label, b1, b2, b3)
wenzelm@52802
    71
    border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@44775
    72
wenzelm@52759
    73
    def load()
wenzelm@52759
    74
    {
wenzelm@52759
    75
      PIDE.execution_range() match {
wenzelm@52759
    76
        case PIDE.Execution_Range.ALL => group.select(b1)
wenzelm@52759
    77
        case PIDE.Execution_Range.NONE => group.select(b2)
wenzelm@52759
    78
        case PIDE.Execution_Range.VISIBLE => group.select(b3)
wenzelm@52759
    79
      }
wenzelm@52759
    80
    }
wenzelm@52763
    81
    load()
wenzelm@44776
    82
  }
wenzelm@44776
    83
wenzelm@49246
    84
  private val logic = Isabelle_Logic.logic_selector(true)
wenzelm@39702
    85
wenzelm@44775
    86
  private val controls =
wenzelm@52759
    87
    new FlowPanel(FlowPanel.Alignment.Right)(execution_range, session_phase, logic)
wenzelm@39593
    88
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    89
wenzelm@39593
    90
wenzelm@44609
    91
  /* component state -- owned by Swing thread */
wenzelm@44609
    92
wenzelm@45709
    93
  private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
wenzelm@44866
    94
wenzelm@44990
    95
  private object Node_Renderer_Component extends Label
wenzelm@44866
    96
  {
wenzelm@44990
    97
    opaque = false
wenzelm@44957
    98
    xAlignment = Alignment.Leading
wenzelm@44989
    99
    border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@44957
   100
wenzelm@44957
   101
    var node_name = Document.Node.Name.empty
wenzelm@44957
   102
    override def paintComponent(gfx: Graphics2D)
wenzelm@44866
   103
    {
wenzelm@44957
   104
      nodes_status.get(node_name) match {
wenzelm@44866
   105
        case Some(st) if st.total > 0 =>
wenzelm@50900
   106
          val colors = List(
wenzelm@50900
   107
            (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
wenzelm@50900
   108
            (st.running, PIDE.options.color_value("running_color")),
wenzelm@50900
   109
            (st.warned, PIDE.options.color_value("warning_color")),
wenzelm@50900
   110
            (st.failed, PIDE.options.color_value("error_color")))
wenzelm@50900
   111
wenzelm@44958
   112
          val size = peer.getSize()
wenzelm@51535
   113
          val insets = border.getBorderInsets(peer)
wenzelm@44958
   114
          val w = size.width - insets.left - insets.right
wenzelm@44958
   115
          val h = size.height - insets.top - insets.bottom
wenzelm@44958
   116
          var end = size.width - insets.right
wenzelm@50900
   117
wenzelm@50900
   118
          for { (n, color) <- colors }
wenzelm@44866
   119
          {
wenzelm@44866
   120
            gfx.setColor(color)
wenzelm@50900
   121
            val v = (n * (w - colors.length) / st.total) max (if (n > 0) 4 else 0)
wenzelm@44958
   122
            gfx.fillRect(end - v, insets.top, v, h)
wenzelm@50900
   123
            end = end - v - 1
wenzelm@44866
   124
          }
wenzelm@44866
   125
        case _ =>
wenzelm@44866
   126
      }
wenzelm@44867
   127
      super.paintComponent(gfx)
wenzelm@44866
   128
    }
wenzelm@44866
   129
  }
wenzelm@44866
   130
wenzelm@44990
   131
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   132
  {
wenzelm@44990
   133
    def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44990
   134
      name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   135
    {
wenzelm@44990
   136
      val component = Node_Renderer_Component
wenzelm@44957
   137
      component.node_name = name
wenzelm@44989
   138
      component.text = name.theory
wenzelm@44990
   139
      component
wenzelm@44957
   140
    }
wenzelm@44957
   141
  }
wenzelm@44957
   142
  status.renderer = new Node_Renderer
wenzelm@44609
   143
wenzelm@45011
   144
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   145
  {
wenzelm@44609
   146
    Swing_Thread.now {
wenzelm@50205
   147
      val snapshot = PIDE.session.snapshot()
wenzelm@44613
   148
wenzelm@46723
   149
      val iterator =
wenzelm@46723
   150
        restriction match {
wenzelm@46723
   151
          case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@46723
   152
          case None => snapshot.version.nodes.entries
wenzelm@46723
   153
        }
wenzelm@46681
   154
      val nodes_status1 =
wenzelm@46723
   155
        (nodes_status /: iterator)({ case (status, (name, node)) =>
wenzelm@50205
   156
            if (PIDE.thy_load.loaded_theories(name.theory)) status
wenzelm@46739
   157
            else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
wenzelm@44672
   158
wenzelm@44672
   159
      if (nodes_status != nodes_status1) {
wenzelm@44672
   160
        nodes_status = nodes_status1
wenzelm@44866
   161
        status.listData =
wenzelm@46723
   162
          snapshot.version.nodes.topological_order.filter(
wenzelm@44960
   163
            (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
wenzelm@44609
   164
      }
wenzelm@44609
   165
    }
wenzelm@44609
   166
  }
wenzelm@44609
   167
wenzelm@44609
   168
wenzelm@39515
   169
  /* main actor */
wenzelm@39515
   170
wenzelm@39515
   171
  private val main_actor = actor {
wenzelm@39515
   172
    loop {
wenzelm@39515
   173
      react {
wenzelm@48018
   174
        case phase: Session.Phase => handle_phase(phase)
wenzelm@39635
   175
wenzelm@52759
   176
        case _: Session.Global_Options =>
wenzelm@52759
   177
          Swing_Thread.later {
wenzelm@52759
   178
            execution_range.load()
wenzelm@52759
   179
            logic.load ()
wenzelm@52759
   180
          }
wenzelm@49247
   181
wenzelm@45011
   182
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@44609
   183
wenzelm@50299
   184
        case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
wenzelm@39515
   185
      }
wenzelm@39515
   186
    }
wenzelm@39515
   187
  }
wenzelm@39515
   188
wenzelm@48018
   189
  override def init()
wenzelm@48018
   190
  {
wenzelm@50205
   191
    PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
wenzelm@50205
   192
    PIDE.session.global_options += main_actor
wenzelm@50205
   193
    PIDE.session.commands_changed += main_actor; handle_update()
wenzelm@39635
   194
  }
wenzelm@39635
   195
wenzelm@48018
   196
  override def exit()
wenzelm@48018
   197
  {
wenzelm@50205
   198
    PIDE.session.phase_changed -= main_actor
wenzelm@50205
   199
    PIDE.session.global_options -= main_actor
wenzelm@50205
   200
    PIDE.session.commands_changed -= main_actor
wenzelm@39635
   201
  }
wenzelm@39515
   202
}