src/Tools/jEdit/src/session_dockable.scala
author wenzelm
Sun Nov 25 21:35:29 2012 +0100 (2012-11-25)
changeset 50208 1382ad6d4774
parent 50205 788c8263e634
child 50209 907373a080b9
permissions -rw-r--r--
tuned signature;
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@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@49559
    24
class Session_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@44609
    36
  status.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
wenzelm@44609
    37
  status.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@44609
    38
wenzelm@49038
    39
  set_content(new ScrollPane(status))
wenzelm@39515
    40
wenzelm@39515
    41
wenzelm@39593
    42
  /* controls */
wenzelm@39593
    43
wenzelm@50205
    44
  private val session_phase = new Label(PIDE.session.phase.toString)
wenzelm@39697
    45
  session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@39697
    46
  session_phase.tooltip = "Prover status"
wenzelm@39635
    47
wenzelm@48018
    48
  private def handle_phase(phase: Session.Phase)
wenzelm@48018
    49
  {
wenzelm@48018
    50
    Swing_Thread.later { session_phase.text = " " + phase.toString + " " }
wenzelm@48018
    51
  }
wenzelm@48018
    52
wenzelm@44775
    53
  private val cancel = new Button("Cancel") {
wenzelm@50208
    54
    reactions += { case ButtonClicked(_) => Isabelle.cancel_execution() }
wenzelm@44775
    55
  }
wenzelm@44865
    56
  cancel.tooltip = jEdit.getProperty("isabelle.cancel-execution.label")
wenzelm@44775
    57
wenzelm@44776
    58
  private val check = new Button("Check") {
wenzelm@50208
    59
    reactions += { case ButtonClicked(_) => Isabelle.check_buffer(view.getBuffer) }
wenzelm@44776
    60
  }
wenzelm@44865
    61
  check.tooltip = jEdit.getProperty("isabelle.check-buffer.label")
wenzelm@44776
    62
wenzelm@49246
    63
  private val logic = Isabelle_Logic.logic_selector(true)
wenzelm@39702
    64
wenzelm@44775
    65
  private val controls =
wenzelm@44776
    66
    new FlowPanel(FlowPanel.Alignment.Right)(check, cancel, session_phase, logic)
wenzelm@39593
    67
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    68
wenzelm@39593
    69
wenzelm@44609
    70
  /* component state -- owned by Swing thread */
wenzelm@44609
    71
wenzelm@45709
    72
  private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
wenzelm@44866
    73
wenzelm@44990
    74
  private object Node_Renderer_Component extends Label
wenzelm@44866
    75
  {
wenzelm@44990
    76
    opaque = false
wenzelm@44957
    77
    xAlignment = Alignment.Leading
wenzelm@44989
    78
    border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@44957
    79
wenzelm@44957
    80
    var node_name = Document.Node.Name.empty
wenzelm@44957
    81
    override def paintComponent(gfx: Graphics2D)
wenzelm@44866
    82
    {
wenzelm@44957
    83
      nodes_status.get(node_name) match {
wenzelm@44866
    84
        case Some(st) if st.total > 0 =>
wenzelm@44958
    85
          val size = peer.getSize()
wenzelm@44989
    86
          val insets = border.getBorderInsets(this.peer)
wenzelm@44958
    87
          val w = size.width - insets.left - insets.right
wenzelm@44958
    88
          val h = size.height - insets.top - insets.bottom
wenzelm@44958
    89
          var end = size.width - insets.right
wenzelm@44866
    90
          for {
wenzelm@44866
    91
            (n, color) <- List(
wenzelm@50205
    92
              (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
wenzelm@50205
    93
              (st.running, PIDE.options.color_value("running_color")),
wenzelm@50205
    94
              (st.warned, PIDE.options.color_value("warning_color")),
wenzelm@50205
    95
              (st.failed, PIDE.options.color_value("error_color"))) }
wenzelm@44866
    96
          {
wenzelm@44866
    97
            gfx.setColor(color)
wenzelm@44866
    98
            val v = (n * w / st.total) max (if (n > 0) 2 else 0)
wenzelm@44958
    99
            gfx.fillRect(end - v, insets.top, v, h)
wenzelm@44866
   100
            end -= v
wenzelm@44866
   101
          }
wenzelm@44866
   102
        case _ =>
wenzelm@44866
   103
      }
wenzelm@44867
   104
      super.paintComponent(gfx)
wenzelm@44866
   105
    }
wenzelm@44866
   106
  }
wenzelm@44866
   107
wenzelm@44990
   108
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   109
  {
wenzelm@44990
   110
    def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44990
   111
      name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   112
    {
wenzelm@44990
   113
      val component = Node_Renderer_Component
wenzelm@44957
   114
      component.node_name = name
wenzelm@44989
   115
      component.text = name.theory
wenzelm@44990
   116
      component
wenzelm@44957
   117
    }
wenzelm@44957
   118
  }
wenzelm@44957
   119
  status.renderer = new Node_Renderer
wenzelm@44609
   120
wenzelm@45011
   121
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   122
  {
wenzelm@44609
   123
    Swing_Thread.now {
wenzelm@50205
   124
      val snapshot = PIDE.session.snapshot()
wenzelm@44613
   125
wenzelm@46723
   126
      val iterator =
wenzelm@46723
   127
        restriction match {
wenzelm@46723
   128
          case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@46723
   129
          case None => snapshot.version.nodes.entries
wenzelm@46723
   130
        }
wenzelm@46681
   131
      val nodes_status1 =
wenzelm@46723
   132
        (nodes_status /: iterator)({ case (status, (name, node)) =>
wenzelm@50205
   133
            if (PIDE.thy_load.loaded_theories(name.theory)) status
wenzelm@46739
   134
            else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
wenzelm@44672
   135
wenzelm@44672
   136
      if (nodes_status != nodes_status1) {
wenzelm@44672
   137
        nodes_status = nodes_status1
wenzelm@44866
   138
        status.listData =
wenzelm@46723
   139
          snapshot.version.nodes.topological_order.filter(
wenzelm@44960
   140
            (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
wenzelm@44609
   141
      }
wenzelm@44609
   142
    }
wenzelm@44609
   143
  }
wenzelm@44609
   144
wenzelm@44609
   145
wenzelm@39515
   146
  /* main actor */
wenzelm@39515
   147
wenzelm@39515
   148
  private val main_actor = actor {
wenzelm@39515
   149
    loop {
wenzelm@39515
   150
      react {
wenzelm@48018
   151
        case phase: Session.Phase => handle_phase(phase)
wenzelm@39635
   152
wenzelm@50117
   153
        case _: Session.Global_Options => Swing_Thread.later { logic.load () }
wenzelm@49247
   154
wenzelm@45011
   155
        case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
wenzelm@44609
   156
wenzelm@39515
   157
        case bad => System.err.println("Session_Dockable: ignoring bad message " + bad)
wenzelm@39515
   158
      }
wenzelm@39515
   159
    }
wenzelm@39515
   160
  }
wenzelm@39515
   161
wenzelm@48018
   162
  override def init()
wenzelm@48018
   163
  {
wenzelm@50205
   164
    PIDE.session.phase_changed += main_actor; handle_phase(PIDE.session.phase)
wenzelm@50205
   165
    PIDE.session.global_options += main_actor
wenzelm@50205
   166
    PIDE.session.commands_changed += main_actor; handle_update()
wenzelm@39635
   167
  }
wenzelm@39635
   168
wenzelm@48018
   169
  override def exit()
wenzelm@48018
   170
  {
wenzelm@50205
   171
    PIDE.session.phase_changed -= main_actor
wenzelm@50205
   172
    PIDE.session.global_options -= main_actor
wenzelm@50205
   173
    PIDE.session.commands_changed -= main_actor
wenzelm@39635
   174
  }
wenzelm@39515
   175
}