src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Sat Sep 17 22:13:15 2011 +0200 (2011-09-17)
changeset 44958 86e4916825ee
parent 44957 098dd95349e7
child 44960 640c2b957f16
permissions -rw-r--r--
more precise painting;
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@39638
    15
import scala.swing.event.{ButtonClicked, SelectionChanged}
wenzelm@39593
    16
wenzelm@43520
    17
import java.lang.System
wenzelm@44957
    18
import java.awt.{BorderLayout, Graphics2D}
wenzelm@44957
    19
import javax.swing.{JList, DefaultListCellRenderer, UIManager}
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@43661
    30
  readme.render_document(Isabelle_System.try_read(List(Path.explode("$JEDIT_HOME/README.html"))))
wenzelm@39591
    31
wenzelm@44957
    32
  val status = new ListView(Nil: List[Document.Node.Name])
wenzelm@44609
    33
  status.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
wenzelm@44609
    34
  status.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@44609
    35
wenzelm@39629
    36
  private val syslog = new TextArea(Isabelle.session.syslog())
wenzelm@39591
    37
wenzelm@39591
    38
  private val tabs = new TabbedPane {
wenzelm@39591
    39
    pages += new TabbedPane.Page("README", Component.wrap(readme))
wenzelm@44609
    40
    pages += new TabbedPane.Page("Theory Status", new ScrollPane(status))
wenzelm@44609
    41
    pages += new TabbedPane.Page("System Log", new ScrollPane(syslog))
wenzelm@39638
    42
wenzelm@39638
    43
    selection.index =
wenzelm@39638
    44
    {
wenzelm@39638
    45
      val index = Isabelle.Int_Property("session-panel.selection", 0)
wenzelm@39638
    46
      if (index >= pages.length) 0 else index
wenzelm@39638
    47
    }
wenzelm@39638
    48
    listenTo(selection)
wenzelm@39638
    49
    reactions += {
wenzelm@39638
    50
      case SelectionChanged(_) =>
wenzelm@39638
    51
        Isabelle.Int_Property("session-panel.selection") = selection.index
wenzelm@39638
    52
    }
wenzelm@39591
    53
  }
wenzelm@39591
    54
wenzelm@39591
    55
  set_content(tabs)
wenzelm@39515
    56
wenzelm@39515
    57
wenzelm@39593
    58
  /* controls */
wenzelm@39593
    59
wenzelm@39635
    60
  val session_phase = new Label(Isabelle.session.phase.toString)
wenzelm@39697
    61
  session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@39697
    62
  session_phase.tooltip = "Prover status"
wenzelm@39635
    63
wenzelm@44775
    64
  private val cancel = new Button("Cancel") {
wenzelm@44864
    65
    reactions += { case ButtonClicked(_) => Isabelle.cancel_execution() }
wenzelm@44775
    66
  }
wenzelm@44865
    67
  cancel.tooltip = jEdit.getProperty("isabelle.cancel-execution.label")
wenzelm@44775
    68
wenzelm@44776
    69
  private val check = new Button("Check") {
wenzelm@44864
    70
    reactions += { case ButtonClicked(_) => Isabelle.check_buffer(view.getBuffer) }
wenzelm@44776
    71
  }
wenzelm@44865
    72
  check.tooltip = jEdit.getProperty("isabelle.check-buffer.label")
wenzelm@44776
    73
wenzelm@39702
    74
  private val logic = Isabelle.logic_selector(Isabelle.Property("logic"))
wenzelm@39702
    75
  logic.listenTo(logic.selection)
wenzelm@39702
    76
  logic.reactions += {
wenzelm@39702
    77
    case SelectionChanged(_) => Isabelle.Property("logic") = logic.selection.item.name
wenzelm@39702
    78
  }
wenzelm@39702
    79
wenzelm@44775
    80
  private val controls =
wenzelm@44776
    81
    new FlowPanel(FlowPanel.Alignment.Right)(check, cancel, session_phase, logic)
wenzelm@39593
    82
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    83
wenzelm@39593
    84
wenzelm@44609
    85
  /* component state -- owned by Swing thread */
wenzelm@44609
    86
wenzelm@44866
    87
  private var nodes_status: Map[Document.Node.Name, Isar_Document.Node_Status] = Map.empty
wenzelm@44866
    88
wenzelm@44957
    89
  private class Node_Renderer_Component extends Label
wenzelm@44866
    90
  {
wenzelm@44957
    91
    xAlignment = Alignment.Leading
wenzelm@44957
    92
    border = UIManager.getBorder("List.cellNoFocusBorder")
wenzelm@44957
    93
wenzelm@44957
    94
    var node_name = Document.Node.Name.empty
wenzelm@44957
    95
    override def paintComponent(gfx: Graphics2D)
wenzelm@44866
    96
    {
wenzelm@44957
    97
      nodes_status.get(node_name) match {
wenzelm@44866
    98
        case Some(st) if st.total > 0 =>
wenzelm@44958
    99
          val size = peer.getSize()
wenzelm@44958
   100
          val insets = border.getBorderInsets(this.peer)
wenzelm@44958
   101
          val w = size.width - insets.left - insets.right
wenzelm@44958
   102
          val h = size.height - insets.top - insets.bottom
wenzelm@44958
   103
          var end = size.width - insets.right
wenzelm@44866
   104
          for {
wenzelm@44866
   105
            (n, color) <- List(
wenzelm@44866
   106
              (st.unprocessed, Isabelle_Markup.unprocessed1_color),
wenzelm@44867
   107
              (st.running, Isabelle_Markup.running_color),
wenzelm@44867
   108
              (st.failed, Isabelle_Markup.error_color)) }
wenzelm@44866
   109
          {
wenzelm@44866
   110
            gfx.setColor(color)
wenzelm@44866
   111
            val v = (n * w / st.total) max (if (n > 0) 2 else 0)
wenzelm@44958
   112
            gfx.fillRect(end - v, insets.top, v, h)
wenzelm@44866
   113
            end -= v
wenzelm@44866
   114
          }
wenzelm@44866
   115
        case _ =>
wenzelm@44866
   116
      }
wenzelm@44867
   117
      super.paintComponent(gfx)
wenzelm@44866
   118
    }
wenzelm@44866
   119
  }
wenzelm@44866
   120
wenzelm@44957
   121
  private class Node_Renderer extends
wenzelm@44957
   122
    ListView.AbstractRenderer[Document.Node.Name, Node_Renderer_Component](
wenzelm@44957
   123
      new Node_Renderer_Component)
wenzelm@44957
   124
  {
wenzelm@44957
   125
    def configure(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44957
   126
      name: Document.Node.Name, index: Int)
wenzelm@44957
   127
    {
wenzelm@44957
   128
      component.opaque = false
wenzelm@44958
   129
      component.background = list.background
wenzelm@44958
   130
      component.foreground = list.foreground
wenzelm@44957
   131
      component.node_name = name
wenzelm@44957
   132
      component.text = name.theory
wenzelm@44957
   133
    }
wenzelm@44957
   134
  }
wenzelm@44957
   135
  status.renderer = new Node_Renderer
wenzelm@44609
   136
wenzelm@44615
   137
  private def handle_changed(changed_nodes: Set[Document.Node.Name])
wenzelm@44609
   138
  {
wenzelm@44609
   139
    Swing_Thread.now {
wenzelm@44613
   140
      // FIXME correlation to changed_nodes!?
wenzelm@44613
   141
      val state = Isabelle.session.current_state()
wenzelm@44672
   142
      val version = state.recent_stable.version.get_finished
wenzelm@44613
   143
wenzelm@44672
   144
      var nodes_status1 = nodes_status
wenzelm@44672
   145
      for {
wenzelm@44672
   146
        name <- changed_nodes
wenzelm@44672
   147
        node <- version.nodes.get(name)
wenzelm@44672
   148
        val status = Isar_Document.node_status(state, version, node)
wenzelm@44866
   149
      } nodes_status1 += (name -> status)
wenzelm@44672
   150
wenzelm@44672
   151
      if (nodes_status != nodes_status1) {
wenzelm@44672
   152
        nodes_status = nodes_status1
wenzelm@44866
   153
        status.listData =
wenzelm@44866
   154
          Library.sort_wrt((name: Document.Node.Name) => name.node, nodes_status.keySet.toList)
wenzelm@44609
   155
      }
wenzelm@44609
   156
    }
wenzelm@44609
   157
  }
wenzelm@44609
   158
wenzelm@44609
   159
wenzelm@39515
   160
  /* main actor */
wenzelm@39515
   161
wenzelm@39515
   162
  private val main_actor = actor {
wenzelm@39515
   163
    loop {
wenzelm@39515
   164
      react {
wenzelm@39589
   165
        case result: Isabelle_Process.Result =>
wenzelm@39625
   166
          if (result.is_syslog)
wenzelm@39625
   167
            Swing_Thread.now {
wenzelm@39629
   168
              val text = Isabelle.session.syslog()
wenzelm@39626
   169
              if (text != syslog.text) {
wenzelm@39626
   170
                syslog.text = text
wenzelm@39626
   171
              }
wenzelm@39625
   172
            }
wenzelm@39589
   173
wenzelm@39701
   174
        case phase: Session.Phase =>
wenzelm@39696
   175
          Swing_Thread.now { session_phase.text = " " + phase.toString + " " }
wenzelm@39635
   176
wenzelm@44609
   177
        case changed: Session.Commands_Changed => handle_changed(changed.nodes)
wenzelm@44609
   178
wenzelm@39515
   179
        case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
wenzelm@39515
   180
      }
wenzelm@39515
   181
    }
wenzelm@39515
   182
  }
wenzelm@39515
   183
wenzelm@39635
   184
  override def init() {
wenzelm@44734
   185
    Isabelle.session.syslog_messages += main_actor
wenzelm@39635
   186
    Isabelle.session.phase_changed += main_actor
wenzelm@44609
   187
    Isabelle.session.commands_changed += main_actor
wenzelm@39635
   188
  }
wenzelm@39635
   189
wenzelm@39635
   190
  override def exit() {
wenzelm@44734
   191
    Isabelle.session.syslog_messages -= main_actor
wenzelm@39635
   192
    Isabelle.session.phase_changed -= main_actor
wenzelm@44609
   193
    Isabelle.session.commands_changed -= main_actor
wenzelm@39635
   194
  }
wenzelm@39515
   195
}