src/Tools/jEdit/src/simplifier_trace_dockable.scala
author wenzelm
Mon Jan 09 20:26:59 2017 +0100 (2017-01-09)
changeset 64854 f5aa712e6250
parent 60748 6d718fda8215
child 65195 ffab6f460a82
permissions -rw-r--r--
tuned signature;
lars@55316
     1
/*  Title:      Tools/jEdit/src/simplifier_trace_dockable.scala
lars@55316
     2
    Author:     Lars Hupel
lars@55316
     3
lars@55316
     4
Dockable window with interactive simplifier trace.
lars@55316
     5
*/
lars@55316
     6
lars@55316
     7
package isabelle.jedit
lars@55316
     8
lars@55316
     9
lars@55316
    10
import isabelle._
lars@55316
    11
lars@55316
    12
import scala.swing.{Button, CheckBox, Orientation, Separator}
lars@55316
    13
import scala.swing.event.ButtonClicked
lars@55316
    14
lars@55316
    15
import java.awt.BorderLayout
lars@55316
    16
import java.awt.event.{ComponentEvent, ComponentAdapter}
lars@55316
    17
lars@55316
    18
import org.gjt.sp.jedit.View
lars@55316
    19
wenzelm@55556
    20
lars@55316
    21
class Simplifier_Trace_Dockable(view: View, position: String) extends Dockable(view, position)
lars@55316
    22
{
wenzelm@57612
    23
  GUI_Thread.require {}
lars@55316
    24
wenzelm@60748
    25
wenzelm@57612
    26
  /* component state -- owned by GUI thread */
lars@55316
    27
lars@55316
    28
  private var current_snapshot = Document.State.init.snapshot()
wenzelm@56299
    29
  private var current_command = Command.empty
wenzelm@56299
    30
  private var current_results = Command.Results.empty
lars@55316
    31
  private var current_id = 0L
lars@55316
    32
  private var do_update = true
lars@55316
    33
lars@55316
    34
lars@55316
    35
  private val text_area = new Pretty_Text_Area(view)
lars@55316
    36
  set_content(text_area)
lars@55316
    37
wenzelm@57593
    38
  private def update_contents()
lars@55316
    39
  {
wenzelm@57593
    40
    val snapshot = current_snapshot
wenzelm@57593
    41
    val context = Simplifier_Trace.handle_results(PIDE.session, current_id, current_results)
lars@55316
    42
wenzelm@57593
    43
    answers.contents.clear()
wenzelm@57593
    44
    context.questions.values.toList match {
wenzelm@57593
    45
      case q :: _ =>
wenzelm@57593
    46
        val data = q.data
wenzelm@57593
    47
        val content = Pretty.separate(XML.Text(data.text) :: data.content)
wenzelm@57593
    48
        text_area.update(snapshot, Command.Results.empty, content)
wenzelm@57593
    49
        q.answers.foreach { answer =>
wenzelm@57593
    50
          answers.contents += new Button(answer.string) {
wenzelm@57593
    51
            reactions += {
wenzelm@57593
    52
              case ButtonClicked(_) =>
wenzelm@57593
    53
                Simplifier_Trace.send_reply(PIDE.session, data.serial, answer)
wenzelm@57593
    54
            }
wenzelm@57593
    55
          }
wenzelm@57593
    56
        }
wenzelm@57593
    57
      case Nil =>
wenzelm@57593
    58
        text_area.update(snapshot, Command.Results.empty, Nil)
wenzelm@57593
    59
    }
lars@55316
    60
wenzelm@57593
    61
    do_paint()
lars@55316
    62
  }
lars@55316
    63
lars@55316
    64
  private def show_trace()
lars@55316
    65
  {
wenzelm@56299
    66
    val trace = Simplifier_Trace.generate_trace(current_results)
lars@55316
    67
    new Simplifier_Trace_Window(view, current_snapshot, trace)
lars@55316
    68
  }
lars@55316
    69
lars@55316
    70
  private def do_paint()
lars@55316
    71
  {
wenzelm@57612
    72
    GUI_Thread.later {
wenzelm@55825
    73
      text_area.resize(Font_Info.main(PIDE.options.real("jedit_font_scale")))
lars@55316
    74
    }
lars@55316
    75
  }
lars@55316
    76
lars@55316
    77
  private def handle_resize()
lars@55316
    78
  {
lars@55316
    79
    do_paint()
lars@55316
    80
  }
lars@55316
    81
lars@55316
    82
  private def handle_update(follow: Boolean)
lars@55316
    83
  {
wenzelm@56299
    84
    val (new_snapshot, new_command, new_results, new_id) =
lars@55316
    85
      PIDE.editor.current_node_snapshot(view) match {
lars@55316
    86
        case Some(snapshot) =>
lars@55316
    87
          if (follow && !snapshot.is_outdated) {
lars@55316
    88
            PIDE.editor.current_command(view, snapshot) match {
lars@55316
    89
              case Some(cmd) =>
wenzelm@56299
    90
                (snapshot, cmd, snapshot.state.command_results(snapshot.version, cmd), cmd.id)
lars@55316
    91
              case None =>
wenzelm@56299
    92
                (Document.State.init.snapshot(), Command.empty, Command.Results.empty, 0L)
lars@55316
    93
            }
lars@55316
    94
          }
wenzelm@56299
    95
          else (current_snapshot, current_command, current_results, current_id)
wenzelm@56299
    96
        case None => (current_snapshot, current_command, current_results, current_id)
lars@55316
    97
      }
lars@55316
    98
lars@55316
    99
    current_snapshot = new_snapshot
wenzelm@56299
   100
    current_command = new_command
wenzelm@56299
   101
    current_results = new_results
lars@55316
   102
    current_id = new_id
lars@55316
   103
    update_contents()
lars@55316
   104
  }
lars@55316
   105
lars@55316
   106
wenzelm@56715
   107
  /* main */
wenzelm@56715
   108
wenzelm@56715
   109
  private val main =
wenzelm@56715
   110
    Session.Consumer[Any](getClass.getName) {
wenzelm@56715
   111
      case _: Session.Global_Options =>
wenzelm@57612
   112
        GUI_Thread.later { handle_resize() }
lars@55316
   113
wenzelm@56715
   114
      case changed: Session.Commands_Changed =>
wenzelm@57612
   115
        GUI_Thread.later { handle_update(do_update) }
wenzelm@56715
   116
wenzelm@56715
   117
      case Session.Caret_Focus =>
wenzelm@57612
   118
        GUI_Thread.later { handle_update(do_update) }
wenzelm@56715
   119
wenzelm@56715
   120
      case Simplifier_Trace.Event =>
wenzelm@57612
   121
        GUI_Thread.later { handle_update(do_update) }
lars@55316
   122
    }
lars@55316
   123
lars@55316
   124
  override def init()
lars@55316
   125
  {
wenzelm@56715
   126
    PIDE.session.global_options += main
wenzelm@56715
   127
    PIDE.session.commands_changed += main
wenzelm@56715
   128
    PIDE.session.caret_focus += main
wenzelm@56715
   129
    PIDE.session.trace_events += main
lars@55316
   130
    handle_update(true)
lars@55316
   131
  }
lars@55316
   132
lars@55316
   133
  override def exit()
lars@55316
   134
  {
wenzelm@56715
   135
    PIDE.session.global_options -= main
wenzelm@56715
   136
    PIDE.session.commands_changed -= main
wenzelm@56715
   137
    PIDE.session.caret_focus -= main
wenzelm@56715
   138
    PIDE.session.trace_events -= main
lars@55316
   139
    delay_resize.revoke()
lars@55316
   140
  }
lars@55316
   141
lars@55316
   142
lars@55316
   143
  /* resize */
lars@55316
   144
lars@55316
   145
  private val delay_resize =
wenzelm@57612
   146
    GUI_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
lars@55316
   147
lars@55316
   148
  addComponentListener(new ComponentAdapter {
lars@55316
   149
    override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
wenzelm@56770
   150
    override def componentShown(e: ComponentEvent) { delay_resize.invoke() }
lars@55316
   151
  })
lars@55316
   152
lars@55316
   153
lars@55316
   154
  /* controls */
lars@55316
   155
lars@55316
   156
  private val controls = new Wrap_Panel(Wrap_Panel.Alignment.Right)(
lars@55316
   157
    new CheckBox("Auto update") {
lars@55316
   158
      selected = do_update
lars@55316
   159
      reactions += {
lars@55316
   160
        case ButtonClicked(_) =>
lars@55316
   161
          do_update = this.selected
lars@55316
   162
          handle_update(do_update)
lars@55316
   163
      }
lars@55316
   164
    },
lars@55316
   165
    new Button("Update") {
lars@55316
   166
      reactions += {
lars@55316
   167
        case ButtonClicked(_) =>
lars@55316
   168
          handle_update(true)
lars@55316
   169
      }
lars@55316
   170
    },
lars@55316
   171
    new Separator(Orientation.Vertical),
lars@55316
   172
    new Button("Show trace") {
lars@55316
   173
      reactions += {
lars@55316
   174
        case ButtonClicked(_) =>
lars@55316
   175
          show_trace()
lars@55316
   176
      }
lars@55316
   177
    },
lars@55316
   178
    new Button("Clear memory") {
lars@55316
   179
      reactions += {
lars@55316
   180
        case ButtonClicked(_) =>
lars@55316
   181
          Simplifier_Trace.clear_memory()
lars@55316
   182
      }
lars@55316
   183
    }
lars@55316
   184
  )
lars@55316
   185
wenzelm@57593
   186
  private val answers = new Wrap_Panel(Wrap_Panel.Alignment.Left)()
wenzelm@57593
   187
lars@55316
   188
  add(controls.peer, BorderLayout.NORTH)
wenzelm@57593
   189
  add(answers.peer, BorderLayout.SOUTH)
lars@55316
   190
}