src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Thu Dec 01 14:29:14 2011 +0100 (2011-12-01 ago)
changeset 45709 87017fcbad83
parent 45672 a497c5d4a523
child 46681 c083a3f621c0
permissions -rw-r--r--
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/session_dockable.scala
wenzelm@39515
     2
    Author:     Makarius
wenzelm@39515
     3
wenzelm@39515
     4
Dockable window for prover session management.
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@44957
    13
import scala.swing.{FlowPanel, Button, TextArea, Label, ListView, Alignment,
wenzelm@44609
    14
  ScrollPane, TabbedPane, Component, Swing}
wenzelm@44991
    15
import scala.swing.event.{ButtonClicked, MouseClicked, SelectionChanged}
wenzelm@39593
    16
wenzelm@43520
    17
import java.lang.System
wenzelm@44980
    18
import java.awt.{BorderLayout, Graphics2D, Insets}
wenzelm@44989
    19
import javax.swing.{JList, DefaultListCellRenderer, BorderFactory}
wenzelm@39697
    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@39515
    25
class Session_Dockable(view: View, position: String) extends Dockable(view: View, position: String)
wenzelm@39515
    26
{
wenzelm@39591
    27
  /* main tabs */
wenzelm@39591
    28
wenzelm@43661
    29
  private val readme = new HTML_Panel("SansSerif", 14)
wenzelm@45099
    30
  private val readme_path = Path.explode("$JEDIT_HOME/README.html")
wenzelm@45100
    31
  readme.render_document(
wenzelm@45100
    32
    Isabelle_System.platform_file_url(readme_path),
wenzelm@45100
    33
    Isabelle_System.try_read(List(readme_path)))
wenzelm@39591
    34
wenzelm@44991
    35
  val status = new ListView(Nil: List[Document.Node.Name]) {
wenzelm@44991
    36
    listenTo(mouse.clicks)
wenzelm@44991
    37
    reactions += {
wenzelm@44991
    38
      case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
wenzelm@44991
    39
        val index = peer.locationToIndex(point)
wenzelm@44991
    40
        if (index >= 0) jEdit.openFile(view, listData(index).node)
wenzelm@44991
    41
    }
wenzelm@44991
    42
  }
wenzelm@44609
    43
  status.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
wenzelm@44609
    44
  status.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@44609
    45
wenzelm@39629
    46
  private val syslog = new TextArea(Isabelle.session.syslog())
wenzelm@39591
    47
wenzelm@39591
    48
  private val tabs = new TabbedPane {
wenzelm@39591
    49
    pages += new TabbedPane.Page("README", Component.wrap(readme))
wenzelm@44609
    50
    pages += new TabbedPane.Page("Theory Status", new ScrollPane(status))
wenzelm@44609
    51
    pages += new TabbedPane.Page("System Log", new ScrollPane(syslog))
wenzelm@39638
    52
wenzelm@39638
    53
    selection.index =
wenzelm@39638
    54
    {
wenzelm@39638
    55
      val index = Isabelle.Int_Property("session-panel.selection", 0)
wenzelm@39638
    56
      if (index >= pages.length) 0 else index
wenzelm@39638
    57
    }
wenzelm@39638
    58
    listenTo(selection)
wenzelm@39638
    59
    reactions += {
wenzelm@39638
    60
      case SelectionChanged(_) =>
wenzelm@39638
    61
        Isabelle.Int_Property("session-panel.selection") = selection.index
wenzelm@39638
    62
    }
wenzelm@39591
    63
  }
wenzelm@39591
    64
wenzelm@39591
    65
  set_content(tabs)
wenzelm@39515
    66
wenzelm@39515
    67
wenzelm@39593
    68
  /* controls */
wenzelm@39593
    69
wenzelm@39635
    70
  val session_phase = new Label(Isabelle.session.phase.toString)
wenzelm@39697
    71
  session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@39697
    72
  session_phase.tooltip = "Prover status"
wenzelm@39635
    73
wenzelm@44775
    74
  private val cancel = new Button("Cancel") {
wenzelm@44864
    75
    reactions += { case ButtonClicked(_) => Isabelle.cancel_execution() }
wenzelm@44775
    76
  }
wenzelm@44865
    77
  cancel.tooltip = jEdit.getProperty("isabelle.cancel-execution.label")
wenzelm@44775
    78
wenzelm@44776
    79
  private val check = new Button("Check") {
wenzelm@44864
    80
    reactions += { case ButtonClicked(_) => Isabelle.check_buffer(view.getBuffer) }
wenzelm@44776
    81
  }
wenzelm@44865
    82
  check.tooltip = jEdit.getProperty("isabelle.check-buffer.label")
wenzelm@44776
    83
wenzelm@39702
    84
  private val logic = Isabelle.logic_selector(Isabelle.Property("logic"))
wenzelm@39702
    85
  logic.listenTo(logic.selection)
wenzelm@39702
    86
  logic.reactions += {
wenzelm@39702
    87
    case SelectionChanged(_) => Isabelle.Property("logic") = logic.selection.item.name
wenzelm@39702
    88
  }
wenzelm@39702
    89
wenzelm@44775
    90
  private val controls =
wenzelm@44776
    91
    new FlowPanel(FlowPanel.Alignment.Right)(check, cancel, session_phase, logic)
wenzelm@39593
    92
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    93
wenzelm@39593
    94
wenzelm@44609
    95
  /* component state -- owned by Swing thread */
wenzelm@44609
    96
wenzelm@45709
    97
  private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
wenzelm@44866
    98
wenzelm@44990
    99
  private object Node_Renderer_Component extends Label
wenzelm@44866
   100
  {
wenzelm@44990
   101
    opaque = false
wenzelm@44957
   102
    xAlignment = Alignment.Leading
wenzelm@44989
   103
    border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@44957
   104
wenzelm@44957
   105
    var node_name = Document.Node.Name.empty
wenzelm@44957
   106
    override def paintComponent(gfx: Graphics2D)
wenzelm@44866
   107
    {
wenzelm@44957
   108
      nodes_status.get(node_name) match {
wenzelm@44866
   109
        case Some(st) if st.total > 0 =>
wenzelm@44958
   110
          val size = peer.getSize()
wenzelm@44989
   111
          val insets = border.getBorderInsets(this.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@44866
   115
          for {
wenzelm@44866
   116
            (n, color) <- List(
wenzelm@45665
   117
              (st.unprocessed, Isabelle_Rendering.unprocessed1_color),
wenzelm@45665
   118
              (st.running, Isabelle_Rendering.running_color),
wenzelm@45665
   119
              (st.failed, Isabelle_Rendering.error_color)) }
wenzelm@44866
   120
          {
wenzelm@44866
   121
            gfx.setColor(color)
wenzelm@44866
   122
            val v = (n * w / st.total) max (if (n > 0) 2 else 0)
wenzelm@44958
   123
            gfx.fillRect(end - v, insets.top, v, h)
wenzelm@44866
   124
            end -= v
wenzelm@44866
   125
          }
wenzelm@44866
   126
        case _ =>
wenzelm@44866
   127
      }
wenzelm@44867
   128
      super.paintComponent(gfx)
wenzelm@44866
   129
    }
wenzelm@44866
   130
  }
wenzelm@44866
   131
wenzelm@44990
   132
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   133
  {
wenzelm@44990
   134
    def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44990
   135
      name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   136
    {
wenzelm@44990
   137
      val component = Node_Renderer_Component
wenzelm@44957
   138
      component.node_name = name
wenzelm@44989
   139
      component.text = name.theory
wenzelm@44990
   140
      component
wenzelm@44957
   141
    }
wenzelm@44957
   142
  }
wenzelm@44957
   143
  status.renderer = new Node_Renderer
wenzelm@44609
   144
wenzelm@45011
   145
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   146
  {
wenzelm@44609
   147
    Swing_Thread.now {
wenzelm@44960
   148
      val snapshot = Isabelle.session.snapshot()
wenzelm@45011
   149
      val nodes = restriction getOrElse snapshot.version.nodes.keySet
wenzelm@44613
   150
wenzelm@44672
   151
      var nodes_status1 = nodes_status
wenzelm@44672
   152
      for {
wenzelm@45011
   153
        name <- nodes
wenzelm@44960
   154
        node <- snapshot.version.nodes.get(name)
wenzelm@45709
   155
        val status = Protocol.node_status(snapshot.state, snapshot.version, node)
wenzelm@44866
   156
      } nodes_status1 += (name -> status)
wenzelm@44672
   157
wenzelm@44672
   158
      if (nodes_status != nodes_status1) {
wenzelm@44672
   159
        nodes_status = nodes_status1
wenzelm@44866
   160
        status.listData =
wenzelm@44960
   161
          snapshot.version.topological_order.filter(
wenzelm@44960
   162
            (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
wenzelm@44609
   163
      }
wenzelm@44609
   164
    }
wenzelm@44609
   165
  }
wenzelm@44609
   166
wenzelm@44609
   167
wenzelm@39515
   168
  /* main actor */
wenzelm@39515
   169
wenzelm@39515
   170
  private val main_actor = actor {
wenzelm@39515
   171
    loop {
wenzelm@39515
   172
      react {
wenzelm@39589
   173
        case result: Isabelle_Process.Result =>
wenzelm@39625
   174
          if (result.is_syslog)
wenzelm@39625
   175
            Swing_Thread.now {
wenzelm@39629
   176
              val text = Isabelle.session.syslog()
wenzelm@39626
   177
              if (text != syslog.text) {
wenzelm@39626
   178
                syslog.text = text
wenzelm@39626
   179
              }
wenzelm@39625
   180
            }
wenzelm@39589
   181
wenzelm@39701
   182
        case phase: Session.Phase =>
wenzelm@39696
   183
          Swing_Thread.now { session_phase.text = " " + phase.toString + " " }
wenzelm@39635
   184
wenzelm@45011
   185
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@44609
   186
wenzelm@39515
   187
        case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
wenzelm@39515
   188
      }
wenzelm@39515
   189
    }
wenzelm@39515
   190
  }
wenzelm@39515
   191
wenzelm@39635
   192
  override def init() {
wenzelm@44734
   193
    Isabelle.session.syslog_messages += main_actor
wenzelm@39635
   194
    Isabelle.session.phase_changed += main_actor
wenzelm@44609
   195
    Isabelle.session.commands_changed += main_actor
wenzelm@39635
   196
  }
wenzelm@39635
   197
wenzelm@39635
   198
  override def exit() {
wenzelm@44734
   199
    Isabelle.session.syslog_messages -= main_actor
wenzelm@39635
   200
    Isabelle.session.phase_changed -= main_actor
wenzelm@44609
   201
    Isabelle.session.commands_changed -= main_actor
wenzelm@39635
   202
  }
wenzelm@45011
   203
wenzelm@45011
   204
  handle_update()
wenzelm@39515
   205
}