src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Thu Dec 05 17:58:03 2013 +0100 (2013-12-05)
changeset 54671 d64a4ef26edb
parent 54652 07ee041537a5
parent 54531 8330faaeebd5
child 55618 995162143ef4
permissions -rw-r--r--
merged, resolving obvious conflicts in NEWS and src/Pure/System/isabelle_process.ML;
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@53711
    13
import scala.swing.{Button, TextArea, Label, ListView, Alignment,
wenzelm@52815
    14
  ScrollPane, Component, CheckBox, BorderPanel}
wenzelm@53715
    15
import scala.swing.event.{MouseClicked, MouseMoved}
wenzelm@39593
    16
wenzelm@43520
    17
import java.lang.System
wenzelm@53178
    18
import java.awt.{BorderLayout, Graphics2D, Color, Point, Dimension}
wenzelm@47588
    19
import javax.swing.{JList, BorderFactory}
wenzelm@52802
    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@50299
    25
class Theories_Dockable(view: View, position: String) extends Dockable(view, position)
wenzelm@39515
    26
{
wenzelm@48021
    27
  /* status */
wenzelm@39591
    28
wenzelm@48021
    29
  private val status = new ListView(Nil: List[Document.Node.Name]) {
wenzelm@44991
    30
    listenTo(mouse.clicks)
wenzelm@52817
    31
    listenTo(mouse.moves)
wenzelm@44991
    32
    reactions += {
wenzelm@52816
    33
      case MouseClicked(_, point, _, clicks, _) =>
wenzelm@44991
    34
        val index = peer.locationToIndex(point)
wenzelm@52817
    35
        if (index >= 0) {
wenzelm@52817
    36
          if (in_checkbox(peer.indexToLocation(index), point)) {
wenzelm@52817
    37
            if (clicks == 1) {
wenzelm@52817
    38
              for {
wenzelm@52817
    39
                buffer <- JEdit_Lib.jedit_buffer(listData(index).node)
wenzelm@52817
    40
                model <- PIDE.document_model(buffer)
wenzelm@52817
    41
              } model.node_required = !model.node_required
wenzelm@52817
    42
            }
wenzelm@52816
    43
          }
wenzelm@52980
    44
          else if (clicks == 2) PIDE.editor.goto(view, listData(index).node)
wenzelm@52816
    45
        }
wenzelm@52817
    46
      case MouseMoved(_, point, _) =>
wenzelm@52817
    47
        val index = peer.locationToIndex(point)
wenzelm@52817
    48
        if (index >= 0 && in_checkbox(peer.indexToLocation(index), point))
wenzelm@52817
    49
          tooltip = "Mark as required for continuous checking"
wenzelm@52817
    50
        else
wenzelm@52817
    51
          tooltip = null
wenzelm@44991
    52
    }
wenzelm@44991
    53
  }
wenzelm@50250
    54
  status.peer.setLayoutOrientation(JList.HORIZONTAL_WRAP)
wenzelm@50250
    55
  status.peer.setVisibleRowCount(0)
wenzelm@44609
    56
  status.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@44609
    57
wenzelm@49038
    58
  set_content(new ScrollPane(status))
wenzelm@39515
    59
wenzelm@39515
    60
wenzelm@39593
    61
  /* controls */
wenzelm@39593
    62
wenzelm@50299
    63
  def phase_text(phase: Session.Phase): String =
wenzelm@50299
    64
    "Prover: " + Library.lowercase(phase.toString)
wenzelm@50299
    65
wenzelm@50299
    66
  private val session_phase = new Label(phase_text(PIDE.session.phase))
wenzelm@39697
    67
  session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@50299
    68
  session_phase.tooltip = "Status of prover session"
wenzelm@39635
    69
wenzelm@48018
    70
  private def handle_phase(phase: Session.Phase)
wenzelm@48018
    71
  {
wenzelm@53177
    72
    Swing_Thread.require()
wenzelm@53177
    73
    session_phase.text = " " + phase_text(phase) + " "
wenzelm@48018
    74
  }
wenzelm@48018
    75
wenzelm@53715
    76
  private val continuous_checking = new Isabelle.Continuous_Checking
wenzelm@54652
    77
  continuous_checking.focusable = false
wenzelm@44776
    78
wenzelm@49246
    79
  private val logic = Isabelle_Logic.logic_selector(true)
wenzelm@39702
    80
wenzelm@44775
    81
  private val controls =
wenzelm@53711
    82
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(continuous_checking, session_phase, logic)
wenzelm@39593
    83
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    84
wenzelm@39593
    85
wenzelm@44609
    86
  /* component state -- owned by Swing thread */
wenzelm@44609
    87
wenzelm@45709
    88
  private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
wenzelm@52815
    89
  private var nodes_required: Set[Document.Node.Name] = Set.empty
wenzelm@44866
    90
wenzelm@52815
    91
  private def update_nodes_required()
wenzelm@52815
    92
  {
wenzelm@52815
    93
    nodes_required = Set.empty
wenzelm@52815
    94
    for {
wenzelm@52815
    95
      buffer <- JEdit_Lib.jedit_buffers
wenzelm@52815
    96
      model <- PIDE.document_model(buffer)
wenzelm@52815
    97
      if model.node_required
wenzelm@52973
    98
    } nodes_required += model.node_name
wenzelm@52815
    99
  }
wenzelm@52860
   100
  update_nodes_required()
wenzelm@52815
   101
wenzelm@52817
   102
  private def in_checkbox(loc0: Point, p: Point): Boolean =
wenzelm@52817
   103
    Node_Renderer_Component != null &&
wenzelm@52817
   104
      (Node_Renderer_Component.checkbox_geometry match {
wenzelm@52817
   105
        case Some((loc, size)) =>
wenzelm@52817
   106
          loc0.x + loc.x <= p.x && p.x < loc0.x + size.width &&
wenzelm@52817
   107
          loc0.y + loc.y <= p.y && p.y < loc0.y + size.height
wenzelm@52817
   108
        case None => false
wenzelm@52817
   109
      })
wenzelm@52817
   110
wenzelm@52815
   111
  private object Node_Renderer_Component extends BorderPanel
wenzelm@44866
   112
  {
wenzelm@44990
   113
    opaque = false
wenzelm@53176
   114
    border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@44957
   115
wenzelm@44957
   116
    var node_name = Document.Node.Name.empty
wenzelm@52816
   117
wenzelm@52816
   118
    var checkbox_geometry: Option[(Point, Dimension)] = None
wenzelm@52816
   119
    val checkbox = new CheckBox {
wenzelm@52819
   120
      opaque = false
wenzelm@52816
   121
      override def paintComponent(gfx: Graphics2D)
wenzelm@52816
   122
      {
wenzelm@52816
   123
        super.paintComponent(gfx)
wenzelm@52816
   124
        if (location != null && size != null)
wenzelm@52816
   125
          checkbox_geometry = Some((location, size))
wenzelm@52816
   126
      }
wenzelm@52816
   127
    }
wenzelm@52815
   128
wenzelm@52815
   129
    val label = new Label {
wenzelm@52815
   130
      opaque = false
wenzelm@53176
   131
      border = BorderFactory.createEmptyBorder()
wenzelm@52815
   132
      xAlignment = Alignment.Leading
wenzelm@52809
   133
wenzelm@52815
   134
      override def paintComponent(gfx: Graphics2D)
wenzelm@52815
   135
      {
wenzelm@53178
   136
        def paint_segment(x: Int, w: Int, color: Color)
wenzelm@53178
   137
        {
wenzelm@53178
   138
          gfx.setColor(color)
wenzelm@53178
   139
          gfx.fillRect(x, 0, w, size.height)
wenzelm@53178
   140
        }
wenzelm@50900
   141
wenzelm@52815
   142
        nodes_status.get(node_name) match {
wenzelm@52815
   143
          case Some(st) if st.total > 0 =>
wenzelm@53178
   144
            val segments =
wenzelm@53178
   145
              List(
wenzelm@53178
   146
                (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
wenzelm@53178
   147
                (st.running, PIDE.options.color_value("running_color")),
wenzelm@53178
   148
                (st.warned, PIDE.options.color_value("warning_color")),
wenzelm@53178
   149
                (st.failed, PIDE.options.color_value("error_color"))
wenzelm@53178
   150
              ).filter(_._1 > 0)
wenzelm@52815
   151
wenzelm@53178
   152
            (size.width /: segments)({ case (last, (n, color)) =>
wenzelm@53178
   153
              val w = (n * (size.width - segments.length) / st.total) max 4
wenzelm@53178
   154
              paint_segment(last - w, w, color)
wenzelm@53178
   155
              last - w - 1
wenzelm@53178
   156
            })
wenzelm@53178
   157
wenzelm@52815
   158
          case _ =>
wenzelm@53178
   159
            paint_segment(0, size.width, PIDE.options.color_value("unprocessed1_color"))
wenzelm@52815
   160
        }
wenzelm@52815
   161
        super.paintComponent(gfx)
wenzelm@44866
   162
      }
wenzelm@44866
   163
    }
wenzelm@52815
   164
wenzelm@52816
   165
    layout(checkbox) = BorderPanel.Position.West
wenzelm@52815
   166
    layout(label) = BorderPanel.Position.Center
wenzelm@44866
   167
  }
wenzelm@44866
   168
wenzelm@44990
   169
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   170
  {
wenzelm@44990
   171
    def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
wenzelm@44990
   172
      name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   173
    {
wenzelm@44990
   174
      val component = Node_Renderer_Component
wenzelm@44957
   175
      component.node_name = name
wenzelm@52816
   176
      component.checkbox.selected = nodes_required.contains(name)
wenzelm@52815
   177
      component.label.text = name.theory
wenzelm@44990
   178
      component
wenzelm@44957
   179
    }
wenzelm@44957
   180
  }
wenzelm@44957
   181
  status.renderer = new Node_Renderer
wenzelm@44609
   182
wenzelm@45011
   183
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   184
  {
wenzelm@53177
   185
    Swing_Thread.require()
wenzelm@53177
   186
wenzelm@53177
   187
    val snapshot = PIDE.session.snapshot()
wenzelm@44613
   188
wenzelm@53177
   189
    val iterator =
wenzelm@54531
   190
      (restriction match {
wenzelm@53177
   191
        case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
wenzelm@53177
   192
        case None => snapshot.version.nodes.entries
wenzelm@54531
   193
      }).filter(_._1.is_theory)
wenzelm@53177
   194
    val nodes_status1 =
wenzelm@53177
   195
      (nodes_status /: iterator)({ case (status, (name, node)) =>
wenzelm@53177
   196
          if (PIDE.thy_load.loaded_theories(name.theory)) status
wenzelm@53177
   197
          else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
wenzelm@44672
   198
wenzelm@53177
   199
    if (nodes_status != nodes_status1) {
wenzelm@53177
   200
      nodes_status = nodes_status1
wenzelm@53177
   201
      status.listData =
wenzelm@53177
   202
        snapshot.version.nodes.topological_order.filter(
wenzelm@53177
   203
          (name: Document.Node.Name) => nodes_status.isDefinedAt(name))
wenzelm@44609
   204
    }
wenzelm@44609
   205
  }
wenzelm@44609
   206
wenzelm@44609
   207
wenzelm@39515
   208
  /* main actor */
wenzelm@39515
   209
wenzelm@39515
   210
  private val main_actor = actor {
wenzelm@39515
   211
    loop {
wenzelm@39515
   212
      react {
wenzelm@53177
   213
        case phase: Session.Phase =>
wenzelm@53177
   214
          Swing_Thread.later { handle_phase(phase) }
wenzelm@39635
   215
wenzelm@52759
   216
        case _: Session.Global_Options =>
wenzelm@52759
   217
          Swing_Thread.later {
wenzelm@52807
   218
            continuous_checking.load()
wenzelm@52759
   219
            logic.load ()
wenzelm@52815
   220
            update_nodes_required()
wenzelm@52815
   221
            status.repaint()
wenzelm@52759
   222
          }
wenzelm@49247
   223
wenzelm@53177
   224
        case changed: Session.Commands_Changed =>
wenzelm@53177
   225
          Swing_Thread.later { handle_update(Some(changed.nodes)) }
wenzelm@44609
   226
wenzelm@50299
   227
        case bad => System.err.println("Theories_Dockable: ignoring bad message " + bad)
wenzelm@39515
   228
      }
wenzelm@39515
   229
    }
wenzelm@39515
   230
  }
wenzelm@39515
   231
wenzelm@48018
   232
  override def init()
wenzelm@48018
   233
  {
wenzelm@53177
   234
    PIDE.session.phase_changed += main_actor
wenzelm@50205
   235
    PIDE.session.global_options += main_actor
wenzelm@53177
   236
    PIDE.session.commands_changed += main_actor
wenzelm@53177
   237
wenzelm@53177
   238
    handle_phase(PIDE.session.phase)
wenzelm@53177
   239
    handle_update()
wenzelm@39635
   240
  }
wenzelm@39635
   241
wenzelm@48018
   242
  override def exit()
wenzelm@48018
   243
  {
wenzelm@50205
   244
    PIDE.session.phase_changed -= main_actor
wenzelm@50205
   245
    PIDE.session.global_options -= main_actor
wenzelm@50205
   246
    PIDE.session.commands_changed -= main_actor
wenzelm@39635
   247
  }
wenzelm@39515
   248
}