src/Tools/jEdit/src/sledgehammer_dockable.scala
author wenzelm
Wed Jul 23 11:19:24 2014 +0200 (2014-07-23)
changeset 57612 990ffb84489b
parent 57044 042d6e58cb40
child 60202 a95023a21725
permissions -rw-r--r--
clarified module name: facilitate alternative GUI frameworks;
     1 /*  Title:      Tools/jEdit/src/sledgehammer_dockable.scala
     2     Author:     Makarius
     3 
     4 Dockable window for Sledgehammer.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import scala.swing.{Button, Component, Label, TextField, CheckBox}
    13 import scala.swing.event.ButtonClicked
    14 
    15 import java.awt.BorderLayout
    16 import java.awt.event.{ComponentEvent, ComponentAdapter, KeyEvent}
    17 
    18 import org.gjt.sp.jedit.View
    19 import org.gjt.sp.jedit.gui.HistoryTextField
    20 
    21 
    22 class Sledgehammer_Dockable(view: View, position: String) extends Dockable(view, position)
    23 {
    24   val pretty_text_area = new Pretty_Text_Area(view)
    25   set_content(pretty_text_area)
    26 
    27   override def detach_operation = pretty_text_area.detach_operation
    28 
    29 
    30   /* query operation */
    31 
    32   private val process_indicator = new Process_Indicator
    33 
    34   private def consume_status(status: Query_Operation.Status.Value)
    35   {
    36     status match {
    37       case Query_Operation.Status.WAITING =>
    38         process_indicator.update("Waiting for evaluation of context ...", 5)
    39       case Query_Operation.Status.RUNNING =>
    40         process_indicator.update("Sledgehammering ...", 15)
    41       case Query_Operation.Status.FINISHED =>
    42         process_indicator.update(null, 0)
    43     }
    44   }
    45 
    46   private val sledgehammer =
    47     new Query_Operation(PIDE.editor, view, "sledgehammer", consume_status _,
    48       (snapshot, results, body) =>
    49         pretty_text_area.update(snapshot, results, Pretty.separate(body)))
    50 
    51 
    52   /* resize */
    53 
    54   private val zoom = new Font_Info.Zoom_Box { def changed = handle_resize() }
    55 
    56   private def handle_resize()
    57   {
    58     GUI_Thread.require {}
    59 
    60     pretty_text_area.resize(
    61       Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom.factor / 100))
    62   }
    63 
    64   private val delay_resize =
    65     GUI_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() }
    66 
    67   addComponentListener(new ComponentAdapter {
    68     override def componentResized(e: ComponentEvent) { delay_resize.invoke() }
    69   })
    70 
    71 
    72   /* controls */
    73 
    74   private def clicked {
    75     PIDE.options.string("sledgehammer_provers") = provers.getText
    76     sledgehammer.apply_query(List(provers.getText, isar_proofs.selected.toString))
    77   }
    78 
    79   private val provers_label = new Label("Provers:") {
    80     tooltip =
    81       GUI.tooltip_lines(
    82         "Automatic provers as space-separated list, e.g.\ne spass remote_vampire")
    83   }
    84 
    85   private val provers = new HistoryTextField("isabelle-sledgehammer-provers") {
    86     override def processKeyEvent(evt: KeyEvent)
    87     {
    88       if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) clicked
    89       super.processKeyEvent(evt)
    90     }
    91     setToolTipText(provers_label.tooltip)
    92     setColumns(30)
    93   }
    94 
    95   private def update_provers()
    96   {
    97     val new_provers = PIDE.options.string("sledgehammer_provers")
    98     if (new_provers != provers.getText) {
    99       provers.setText(new_provers)
   100       if (provers.getCaret != null)
   101         provers.getCaret.setDot(0)
   102     }
   103   }
   104 
   105   private val isar_proofs = new CheckBox("Isar proofs") {
   106     tooltip = "Specify whether Isar proofs should be output in addition to metis line"
   107     selected = false
   108   }
   109 
   110   private val apply_query = new Button("<html><b>Apply</b></html>") {
   111     tooltip = "Search for first-order proof using automatic theorem provers"
   112     reactions += { case ButtonClicked(_) => clicked }
   113   }
   114 
   115   private val cancel_query = new Button("Cancel") {
   116     tooltip = "Interrupt unfinished sledgehammering"
   117     reactions += { case ButtonClicked(_) => sledgehammer.cancel_query() }
   118   }
   119 
   120   private val locate_query = new Button("Locate") {
   121     tooltip = "Locate context of current query within source text"
   122     reactions += { case ButtonClicked(_) => sledgehammer.locate_query() }
   123   }
   124 
   125   private val controls =
   126     new Wrap_Panel(Wrap_Panel.Alignment.Right)(
   127       provers_label, Component.wrap(provers), isar_proofs,
   128       process_indicator.component, apply_query, cancel_query, locate_query, zoom)
   129   add(controls.peer, BorderLayout.NORTH)
   130 
   131   override def focusOnDefaultComponent { provers.requestFocus }
   132 
   133 
   134   /* main */
   135 
   136   private val main =
   137     Session.Consumer[Session.Global_Options](getClass.getName) {
   138       case _: Session.Global_Options => GUI_Thread.later { update_provers(); handle_resize() }
   139     }
   140 
   141   override def init()
   142   {
   143     PIDE.session.global_options += main
   144     update_provers()
   145     handle_resize()
   146     sledgehammer.activate()
   147   }
   148 
   149   override def exit()
   150   {
   151     sledgehammer.deactivate()
   152     PIDE.session.global_options -= main
   153     delay_resize.revoke()
   154   }
   155 }