src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Tue Sep 27 22:14:15 2011 +0200 (2011-09-27)
changeset 45099 67740480cf39
parent 45011 436ea69d5d37
child 45100 0971c3ee3cdf
permissions -rw-r--r--
observe base URL of rendered document;
added PIDE logo;
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@45099
    31
  private val readme_url = "file://" + readme_path.expand.implode
wenzelm@45099
    32
  readme.render_document(readme_url, Isabelle_System.try_read(List(readme_path)))
wenzelm@39591
    33
wenzelm@44991
    34
  val status = new ListView(Nil: List[Document.Node.Name]) {
wenzelm@44991
    35
    listenTo(mouse.clicks)
wenzelm@44991
    36
    reactions += {
wenzelm@44991
    37
      case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
wenzelm@44991
    38
        val index = peer.locationToIndex(point)
wenzelm@44991
    39
        if (index >= 0) jEdit.openFile(view, listData(index).node)
wenzelm@44991
    40
    }
wenzelm@44991
    41
  }
wenzelm@44609
    42
  status.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
wenzelm@44609
    43
  status.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@44609
    44
wenzelm@39629
    45
  private val syslog = new TextArea(Isabelle.session.syslog())
wenzelm@39591
    46
wenzelm@39591
    47
  private val tabs = new TabbedPane {
wenzelm@39591
    48
    pages += new TabbedPane.Page("README", Component.wrap(readme))
wenzelm@44609
    49
    pages += new TabbedPane.Page("Theory Status", new ScrollPane(status))
wenzelm@44609
    50
    pages += new TabbedPane.Page("System Log", new ScrollPane(syslog))
wenzelm@39638
    51
wenzelm@39638
    52
    selection.index =
wenzelm@39638
    53
    {
wenzelm@39638
    54
      val index = Isabelle.Int_Property("session-panel.selection", 0)
wenzelm@39638
    55
      if (index >= pages.length) 0 else index
wenzelm@39638
    56
    }
wenzelm@39638
    57
    listenTo(selection)
wenzelm@39638
    58
    reactions += {
wenzelm@39638
    59
      case SelectionChanged(_) =>
wenzelm@39638
    60
        Isabelle.Int_Property("session-panel.selection") = selection.index
wenzelm@39638
    61
    }
wenzelm@39591
    62
  }
wenzelm@39591
    63
wenzelm@39591
    64
  set_content(tabs)
wenzelm@39515
    65
wenzelm@39515
    66
wenzelm@39593
    67
  /* controls */
wenzelm@39593
    68
wenzelm@39635
    69
  val session_phase = new Label(Isabelle.session.phase.toString)
wenzelm@39697
    70
  session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@39697
    71
  session_phase.tooltip = "Prover status"
wenzelm@39635
    72
wenzelm@44775
    73
  private val cancel = new Button("Cancel") {
wenzelm@44864
    74
    reactions += { case ButtonClicked(_) => Isabelle.cancel_execution() }
wenzelm@44775
    75
  }
wenzelm@44865
    76
  cancel.tooltip = jEdit.getProperty("isabelle.cancel-execution.label")
wenzelm@44775
    77
wenzelm@44776
    78
  private val check = new Button("Check") {
wenzelm@44864
    79
    reactions += { case ButtonClicked(_) => Isabelle.check_buffer(view.getBuffer) }
wenzelm@44776
    80
  }
wenzelm@44865
    81
  check.tooltip = jEdit.getProperty("isabelle.check-buffer.label")
wenzelm@44776
    82
wenzelm@39702
    83
  private val logic = Isabelle.logic_selector(Isabelle.Property("logic"))
wenzelm@39702
    84
  logic.listenTo(logic.selection)
wenzelm@39702
    85
  logic.reactions += {
wenzelm@39702
    86
    case SelectionChanged(_) => Isabelle.Property("logic") = logic.selection.item.name
wenzelm@39702
    87
  }
wenzelm@39702
    88
wenzelm@44775
    89
  private val controls =
wenzelm@44776
    90
    new FlowPanel(FlowPanel.Alignment.Right)(check, cancel, session_phase, logic)
wenzelm@39593
    91
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    92
wenzelm@39593
    93
wenzelm@44609
    94
  /* component state -- owned by Swing thread */
wenzelm@44609
    95
wenzelm@44866
    96
  private var nodes_status: Map[Document.Node.Name, Isar_Document.Node_Status] = Map.empty
wenzelm@44866
    97
wenzelm@44990
    98
  private object Node_Renderer_Component extends Label
wenzelm@44866
    99
  {
wenzelm@44990
   100
    opaque = false
wenzelm@44957
   101
    xAlignment = Alignment.Leading
wenzelm@44989
   102
    border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@44957
   103
wenzelm@44957
   104
    var node_name = Document.Node.Name.empty
wenzelm@44957
   105
    override def paintComponent(gfx: Graphics2D)
wenzelm@44866
   106
    {
wenzelm@44957
   107
      nodes_status.get(node_name) match {
wenzelm@44866
   108
        case Some(st) if st.total > 0 =>
wenzelm@44958
   109
          val size = peer.getSize()
wenzelm@44989
   110
          val insets = border.getBorderInsets(this.peer)
wenzelm@44958
   111
          val w = size.width - insets.left - insets.right
wenzelm@44958
   112
          val h = size.height - insets.top - insets.bottom
wenzelm@44958
   113
          var end = size.width - insets.right
wenzelm@44866
   114
          for {
wenzelm@44866
   115
            (n, color) <- List(
wenzelm@44866
   116
              (st.unprocessed, Isabelle_Markup.unprocessed1_color),
wenzelm@44867
   117
              (st.running, Isabelle_Markup.running_color),
wenzelm@44867
   118
              (st.failed, Isabelle_Markup.error_color)) }
wenzelm@44866
   119
          {
wenzelm@44866
   120
            gfx.setColor(color)
wenzelm@44866
   121
            val v = (n * w / st.total) max (if (n > 0) 2 else 0)
wenzelm@44958
   122
            gfx.fillRect(end - v, insets.top, v, h)
wenzelm@44866
   123
            end -= v
wenzelm@44866
   124
          }
wenzelm@44866
   125
        case _ =>
wenzelm@44866
   126
      }
wenzelm@44867
   127
      super.paintComponent(gfx)
wenzelm@44866
   128
    }
wenzelm@44866
   129
  }
wenzelm@44866
   130
wenzelm@44990
   131
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   132
  {
wenzelm@44990
   133
    def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44990
   134
      name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   135
    {
wenzelm@44990
   136
      val component = Node_Renderer_Component
wenzelm@44957
   137
      component.node_name = name
wenzelm@44989
   138
      component.text = name.theory
wenzelm@44990
   139
      component
wenzelm@44957
   140
    }
wenzelm@44957
   141
  }
wenzelm@44957
   142
  status.renderer = new Node_Renderer
wenzelm@44609
   143
wenzelm@45011
   144
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   145
  {
wenzelm@44609
   146
    Swing_Thread.now {
wenzelm@44960
   147
      val snapshot = Isabelle.session.snapshot()
wenzelm@45011
   148
      val nodes = restriction getOrElse snapshot.version.nodes.keySet
wenzelm@44613
   149
wenzelm@44672
   150
      var nodes_status1 = nodes_status
wenzelm@44672
   151
      for {
wenzelm@45011
   152
        name <- nodes
wenzelm@44960
   153
        node <- snapshot.version.nodes.get(name)
wenzelm@44960
   154
        val status = Isar_Document.node_status(snapshot.state, snapshot.version, node)
wenzelm@44866
   155
      } nodes_status1 += (name -> status)
wenzelm@44672
   156
wenzelm@44672
   157
      if (nodes_status != nodes_status1) {
wenzelm@44672
   158
        nodes_status = nodes_status1
wenzelm@44866
   159
        status.listData =
wenzelm@44960
   160
          snapshot.version.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@39589
   172
        case result: Isabelle_Process.Result =>
wenzelm@39625
   173
          if (result.is_syslog)
wenzelm@39625
   174
            Swing_Thread.now {
wenzelm@39629
   175
              val text = Isabelle.session.syslog()
wenzelm@39626
   176
              if (text != syslog.text) {
wenzelm@39626
   177
                syslog.text = text
wenzelm@39626
   178
              }
wenzelm@39625
   179
            }
wenzelm@39589
   180
wenzelm@39701
   181
        case phase: Session.Phase =>
wenzelm@39696
   182
          Swing_Thread.now { session_phase.text = " " + phase.toString + " " }
wenzelm@39635
   183
wenzelm@45011
   184
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@44609
   185
wenzelm@39515
   186
        case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
wenzelm@39515
   187
      }
wenzelm@39515
   188
    }
wenzelm@39515
   189
  }
wenzelm@39515
   190
wenzelm@39635
   191
  override def init() {
wenzelm@44734
   192
    Isabelle.session.syslog_messages += main_actor
wenzelm@39635
   193
    Isabelle.session.phase_changed += main_actor
wenzelm@44609
   194
    Isabelle.session.commands_changed += main_actor
wenzelm@39635
   195
  }
wenzelm@39635
   196
wenzelm@39635
   197
  override def exit() {
wenzelm@44734
   198
    Isabelle.session.syslog_messages -= main_actor
wenzelm@39635
   199
    Isabelle.session.phase_changed -= main_actor
wenzelm@44609
   200
    Isabelle.session.commands_changed -= main_actor
wenzelm@39635
   201
  }
wenzelm@45011
   202
wenzelm@45011
   203
  handle_update()
wenzelm@39515
   204
}