src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Tue Mar 26 14:03:31 2013 +0100 (2013-03-26)
changeset 51535 f2f480bc4223
parent 51534 123bd97fcea1
child 52759 a20631db9c8a
permissions -rw-r--r--
tuned;
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@51534
    33
        if (index >= 0) Hyperlink(listData(index).node).follow(view)
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@50900
    89
          val colors = List(
wenzelm@50900
    90
            (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
wenzelm@50900
    91
            (st.running, PIDE.options.color_value("running_color")),
wenzelm@50900
    92
            (st.warned, PIDE.options.color_value("warning_color")),
wenzelm@50900
    93
            (st.failed, PIDE.options.color_value("error_color")))
wenzelm@50900
    94
wenzelm@44958
    95
          val size = peer.getSize()
wenzelm@51535
    96
          val insets = border.getBorderInsets(peer)
wenzelm@44958
    97
          val w = size.width - insets.left - insets.right
wenzelm@44958
    98
          val h = size.height - insets.top - insets.bottom
wenzelm@44958
    99
          var end = size.width - insets.right
wenzelm@50900
   100
wenzelm@50900
   101
          for { (n, color) <- colors }
wenzelm@44866
   102
          {
wenzelm@44866
   103
            gfx.setColor(color)
wenzelm@50900
   104
            val v = (n * (w - colors.length) / st.total) max (if (n > 0) 4 else 0)
wenzelm@44958
   105
            gfx.fillRect(end - v, insets.top, v, h)
wenzelm@50900
   106
            end = end - v - 1
wenzelm@44866
   107
          }
wenzelm@44866
   108
        case _ =>
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@50117
   159
        case _: Session.Global_Options => Swing_Thread.later { logic.load () }
wenzelm@49247
   160
wenzelm@45011
   161
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@44609
   162
wenzelm@50299
   163
        case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
wenzelm@39515
   164
      }
wenzelm@39515
   165
    }
wenzelm@39515
   166
  }
wenzelm@39515
   167
wenzelm@48018
   168
  override def init()
wenzelm@48018
   169
  {
wenzelm@50205
   170
    PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
wenzelm@50205
   171
    PIDE.session.global_options += main_actor
wenzelm@50205
   172
    PIDE.session.commands_changed += main_actor; handle_update()
wenzelm@39635
   173
  }
wenzelm@39635
   174
wenzelm@48018
   175
  override def exit()
wenzelm@48018
   176
  {
wenzelm@50205
   177
    PIDE.session.phase_changed -= main_actor
wenzelm@50205
   178
    PIDE.session.global_options -= main_actor
wenzelm@50205
   179
    PIDE.session.commands_changed -= main_actor
wenzelm@39635
   180
  }
wenzelm@39515
   181
}