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