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