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