src/Tools/jEdit/src/theories_dockable.scala
author wenzelm
Mon Jan 09 20:26:59 2017 +0100 (2017-01-09)
changeset 64854 f5aa712e6250
parent 64817 0bb6b582bb4f
child 64867 e7220f4de11f
permissions -rw-r--r--
tuned signature;
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@64817
    41
            if (clicks == 1) Document_Model.node_required(listData(index), toggle = true)
wenzelm@52816
    42
          }
wenzelm@60893
    43
          else if (clicks == 2) PIDE.editor.goto_file(true, view, listData(index).node)
wenzelm@52816
    44
        }
wenzelm@52817
    45
      case MouseMoved(_, point, _) =>
wenzelm@52817
    46
        val index = peer.locationToIndex(point)
wenzelm@52817
    47
        if (index >= 0 && in_checkbox(peer.indexToLocation(index), point))
wenzelm@52817
    48
          tooltip = "Mark as required for continuous checking"
wenzelm@52817
    49
        else
wenzelm@52817
    50
          tooltip = null
wenzelm@44991
    51
    }
wenzelm@44991
    52
  }
wenzelm@50250
    53
  status.peer.setLayoutOrientation(JList.HORIZONTAL_WRAP)
wenzelm@50250
    54
  status.peer.setVisibleRowCount(0)
wenzelm@44609
    55
  status.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@44609
    56
wenzelm@49038
    57
  set_content(new ScrollPane(status))
wenzelm@39515
    58
wenzelm@39515
    59
wenzelm@39593
    60
  /* controls */
wenzelm@39593
    61
wenzelm@50299
    62
  def phase_text(phase: Session.Phase): String =
wenzelm@56599
    63
    "Prover: " + Word.lowercase(phase.toString)
wenzelm@50299
    64
wenzelm@50299
    65
  private val session_phase = new Label(phase_text(PIDE.session.phase))
wenzelm@39697
    66
  session_phase.border = new SoftBevelBorder(BevelBorder.LOWERED)
wenzelm@50299
    67
  session_phase.tooltip = "Status of prover session"
wenzelm@39635
    68
wenzelm@48018
    69
  private def handle_phase(phase: Session.Phase)
wenzelm@48018
    70
  {
wenzelm@57612
    71
    GUI_Thread.require {}
wenzelm@53177
    72
    session_phase.text = " " + phase_text(phase) + " "
wenzelm@48018
    73
  }
wenzelm@48018
    74
wenzelm@53715
    75
  private val continuous_checking = new Isabelle.Continuous_Checking
wenzelm@54652
    76
  continuous_checking.focusable = false
wenzelm@44776
    77
wenzelm@62973
    78
  private val logic = JEdit_Sessions.logic_selector(true)
wenzelm@39702
    79
wenzelm@44775
    80
  private val controls =
wenzelm@53711
    81
    new Wrap_Panel(Wrap_Panel.Alignment.Right)(continuous_checking, session_phase, logic)
wenzelm@39593
    82
  add(controls.peer, BorderLayout.NORTH)
wenzelm@39593
    83
wenzelm@39593
    84
wenzelm@57612
    85
  /* component state -- owned by GUI thread */
wenzelm@44609
    86
wenzelm@45709
    87
  private var nodes_status: Map[Document.Node.Name, Protocol.Node_Status] = Map.empty
wenzelm@64817
    88
  private var nodes_required: Set[Document.Node.Name] = Document_Model.required_nodes()
wenzelm@52815
    89
wenzelm@52817
    90
  private def in_checkbox(loc0: Point, p: Point): Boolean =
wenzelm@52817
    91
    Node_Renderer_Component != null &&
wenzelm@52817
    92
      (Node_Renderer_Component.checkbox_geometry match {
wenzelm@52817
    93
        case Some((loc, size)) =>
wenzelm@52817
    94
          loc0.x + loc.x <= p.x && p.x < loc0.x + size.width &&
wenzelm@52817
    95
          loc0.y + loc.y <= p.y && p.y < loc0.y + size.height
wenzelm@52817
    96
        case None => false
wenzelm@52817
    97
      })
wenzelm@52817
    98
wenzelm@52815
    99
  private object Node_Renderer_Component extends BorderPanel
wenzelm@44866
   100
  {
wenzelm@56389
   101
    opaque = true
wenzelm@53176
   102
    border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
wenzelm@44957
   103
wenzelm@44957
   104
    var node_name = Document.Node.Name.empty
wenzelm@52816
   105
wenzelm@52816
   106
    var checkbox_geometry: Option[(Point, Dimension)] = None
wenzelm@52816
   107
    val checkbox = new CheckBox {
wenzelm@52819
   108
      opaque = false
wenzelm@52816
   109
      override def paintComponent(gfx: Graphics2D)
wenzelm@52816
   110
      {
wenzelm@52816
   111
        super.paintComponent(gfx)
wenzelm@52816
   112
        if (location != null && size != null)
wenzelm@52816
   113
          checkbox_geometry = Some((location, size))
wenzelm@52816
   114
      }
wenzelm@52816
   115
    }
wenzelm@52815
   116
wenzelm@52815
   117
    val label = new Label {
wenzelm@56389
   118
      background = view.getTextArea.getPainter.getBackground
wenzelm@56389
   119
      foreground = view.getTextArea.getPainter.getForeground
wenzelm@56389
   120
      border =
wenzelm@56389
   121
        BorderFactory.createCompoundBorder(
wenzelm@56389
   122
          BorderFactory.createLineBorder(foreground, 1),
wenzelm@56389
   123
          BorderFactory.createEmptyBorder(1, 1, 1, 1))
wenzelm@52815
   124
      opaque = false
wenzelm@52815
   125
      xAlignment = Alignment.Leading
wenzelm@52809
   126
wenzelm@52815
   127
      override def paintComponent(gfx: Graphics2D)
wenzelm@52815
   128
      {
wenzelm@53178
   129
        def paint_segment(x: Int, w: Int, color: Color)
wenzelm@53178
   130
        {
wenzelm@53178
   131
          gfx.setColor(color)
wenzelm@53178
   132
          gfx.fillRect(x, 0, w, size.height)
wenzelm@53178
   133
        }
wenzelm@50900
   134
wenzelm@56389
   135
        paint_segment(0, size.width, background)
wenzelm@52815
   136
        nodes_status.get(node_name) match {
wenzelm@52815
   137
          case Some(st) if st.total > 0 =>
wenzelm@53178
   138
            val segments =
wenzelm@53178
   139
              List(
wenzelm@53178
   140
                (st.unprocessed, PIDE.options.color_value("unprocessed1_color")),
wenzelm@53178
   141
                (st.running, PIDE.options.color_value("running_color")),
wenzelm@53178
   142
                (st.warned, PIDE.options.color_value("warning_color")),
wenzelm@53178
   143
                (st.failed, PIDE.options.color_value("error_color"))
wenzelm@53178
   144
              ).filter(_._1 > 0)
wenzelm@52815
   145
wenzelm@56407
   146
            ((size.width - 1) /: segments)({ case (last, (n, color)) =>
wenzelm@56407
   147
              val w = (n * ((size.width - 2) - segments.length) / st.total) max 4
wenzelm@53178
   148
              paint_segment(last - w, w, color)
wenzelm@53178
   149
              last - w - 1
wenzelm@53178
   150
            })
wenzelm@53178
   151
wenzelm@52815
   152
          case _ =>
wenzelm@53178
   153
            paint_segment(0, size.width, PIDE.options.color_value("unprocessed1_color"))
wenzelm@52815
   154
        }
wenzelm@52815
   155
        super.paintComponent(gfx)
wenzelm@44866
   156
      }
wenzelm@44866
   157
    }
wenzelm@52815
   158
wenzelm@52816
   159
    layout(checkbox) = BorderPanel.Position.West
wenzelm@52815
   160
    layout(label) = BorderPanel.Position.Center
wenzelm@44866
   161
  }
wenzelm@44866
   162
wenzelm@44990
   163
  private class Node_Renderer extends ListView.Renderer[Document.Node.Name]
wenzelm@44957
   164
  {
wenzelm@64443
   165
    def componentFor(list: ListView[_], isSelected: Boolean,
wenzelm@64442
   166
      focused: Boolean, name: Document.Node.Name, index: Int): Component =
wenzelm@44957
   167
    {
wenzelm@44990
   168
      val component = Node_Renderer_Component
wenzelm@44957
   169
      component.node_name = name
wenzelm@52816
   170
      component.checkbox.selected = nodes_required.contains(name)
wenzelm@52815
   171
      component.label.text = name.theory
wenzelm@44990
   172
      component
wenzelm@44957
   173
    }
wenzelm@44957
   174
  }
wenzelm@44957
   175
  status.renderer = new Node_Renderer
wenzelm@44609
   176
wenzelm@45011
   177
  private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
wenzelm@44609
   178
  {
wenzelm@62052
   179
    GUI_Thread.require {}
wenzelm@53177
   180
wenzelm@62052
   181
    val snapshot = PIDE.session.snapshot()
wenzelm@57619
   182
    val nodes = snapshot.version.nodes
wenzelm@44613
   183
wenzelm@53177
   184
    val iterator =
wenzelm@57615
   185
      restriction match {
wenzelm@57619
   186
        case Some(names) => names.iterator.map(name => (name, nodes(name)))
wenzelm@57619
   187
        case None => nodes.iterator
wenzelm@57615
   188
      }
wenzelm@53177
   189
    val nodes_status1 =
wenzelm@62052
   190
      (nodes_status /: iterator)({ case (status, (name, node)) =>
wenzelm@64854
   191
          if (!name.is_theory || PIDE.resources.base.loaded_theories(name.theory) || node.is_empty)
wenzelm@57619
   192
            status
wenzelm@53177
   193
          else status + (name -> Protocol.node_status(snapshot.state, snapshot.version, node)) })
wenzelm@44672
   194
wenzelm@57619
   195
    val nodes_status2 =
wenzelm@57619
   196
      nodes_status1 -- nodes_status1.keysIterator.filter(nodes.is_hidden(_))
wenzelm@57619
   197
wenzelm@62052
   198
    if (nodes_status != nodes_status2) {
wenzelm@62052
   199
      nodes_status = nodes_status2
wenzelm@62052
   200
      status.listData = nodes.topological_order.filter(nodes_status.isDefinedAt(_))
wenzelm@62052
   201
    }
wenzelm@44609
   202
  }
wenzelm@44609
   203
wenzelm@44609
   204
wenzelm@56715
   205
  /* main */
wenzelm@39515
   206
wenzelm@56715
   207
  private val main =
wenzelm@56715
   208
    Session.Consumer[Any](getClass.getName) {
wenzelm@56715
   209
      case phase: Session.Phase =>
wenzelm@57612
   210
        GUI_Thread.later { handle_phase(phase) }
wenzelm@39635
   211
wenzelm@56715
   212
      case _: Session.Global_Options =>
wenzelm@57612
   213
        GUI_Thread.later {
wenzelm@56715
   214
          continuous_checking.load()
wenzelm@56715
   215
          logic.load ()
wenzelm@64817
   216
          nodes_required = Document_Model.required_nodes()
wenzelm@56715
   217
          status.repaint()
wenzelm@56715
   218
        }
wenzelm@49247
   219
wenzelm@56715
   220
      case changed: Session.Commands_Changed =>
wenzelm@62052
   221
        GUI_Thread.later { handle_update(Some(changed.nodes)) }
wenzelm@39515
   222
    }
wenzelm@39515
   223
wenzelm@48018
   224
  override def init()
wenzelm@48018
   225
  {
wenzelm@56715
   226
    PIDE.session.phase_changed += main
wenzelm@56715
   227
    PIDE.session.global_options += main
wenzelm@56715
   228
    PIDE.session.commands_changed += main
wenzelm@53177
   229
wenzelm@53177
   230
    handle_phase(PIDE.session.phase)
wenzelm@53177
   231
    handle_update()
wenzelm@39635
   232
  }
wenzelm@39635
   233
wenzelm@48018
   234
  override def exit()
wenzelm@48018
   235
  {
wenzelm@56715
   236
    PIDE.session.phase_changed -= main
wenzelm@56715
   237
    PIDE.session.global_options -= main
wenzelm@56715
   238
    PIDE.session.commands_changed -= main
wenzelm@39635
   239
  }
wenzelm@39515
   240
}