src/Tools/jEdit/src/symbols_dockable.scala
author wenzelm
Tue, 06 May 2014 16:57:17 +0200
changeset 56879 ee2b61f37ad9
parent 56769 8649243d7e91
child 57612 990ffb84489b
permissions -rw-r--r--
renamed "Find" to "Query", with more general operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/symbols_dockable.scala
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     2
    Author:     Fabian Immler
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     3
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     4
Dockable window for Symbol Palette.
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     5
*/
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     6
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     7
package isabelle.jedit
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     8
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
     9
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    10
import isabelle._
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    11
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    12
import scala.swing.event.{SelectionChanged, ValueChanged}
53713
bb15972a644d improved layout, with special treatment for ScrollPane;
wenzelm
parents: 53316
diff changeset
    13
import scala.swing.{Action, Button, TabbedPane, TextField, BorderPanel, Label, ScrollPane}
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    14
50299
f70b3712040f renamed dockable "Prover Session" to "Theories";
wenzelm
parents: 50207
diff changeset
    15
import org.gjt.sp.jedit.View
f70b3712040f renamed dockable "Prover Session" to "Theories";
wenzelm
parents: 50207
diff changeset
    16
f70b3712040f renamed dockable "Prover Session" to "Theories";
wenzelm
parents: 50207
diff changeset
    17
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    18
class Symbols_Dockable(view: View, position: String) extends Dockable(view, position)
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    19
{
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    20
  private class Symbol_Component(val symbol: String) extends Button
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    21
  {
50187
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    22
    private val decoded = Symbol.decode(symbol)
55825
694833e3e4a0 tuned signature -- separate module Font_Info;
wenzelm
parents: 53786
diff changeset
    23
    private val font_size = Font_Info.main_size(PIDE.options.real("jedit_font_scale")).round
50191
8b5a256859af more robust default font -- user might have switched jEdit TextArea to another font that lacks glyphs;
wenzelm
parents: 50190
diff changeset
    24
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    25
    font =
50191
8b5a256859af more robust default font -- user might have switched jEdit TextArea to another font that lacks glyphs;
wenzelm
parents: 50190
diff changeset
    26
      Symbol.fonts.get(symbol) match {
51614
22d1dd43f089 separate module Isabelle_Font, to keep this out of the way of generic Isabelle_System operations, notably for non-Isabelle/jEdit applications;
wenzelm
parents: 50299
diff changeset
    27
        case None => Isabelle_Font(size = font_size)
22d1dd43f089 separate module Isabelle_Font, to keep this out of the way of generic Isabelle_System operations, notably for non-Isabelle/jEdit applications;
wenzelm
parents: 50299
diff changeset
    28
        case Some(font_family) => Isabelle_Font(family = font_family, size = font_size)
50191
8b5a256859af more robust default font -- user might have switched jEdit TextArea to another font that lacks glyphs;
wenzelm
parents: 50190
diff changeset
    29
      }
50187
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    30
    action =
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    31
      Action(decoded) {
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    32
        val text_area = view.getTextArea
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    33
        if (Token_Markup.is_control_style(decoded))
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    34
          Token_Markup.edit_control_style(text_area, decoded)
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    35
        else text_area.setSelectedText(decoded)
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    36
        text_area.requestFocus
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    37
      }
56622
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 56609
diff changeset
    38
    tooltip =
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 56609
diff changeset
    39
      GUI.tooltip_lines(
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 56609
diff changeset
    40
        cat_lines(symbol :: Symbol.abbrevs.get_list(symbol).map(a => "abbrev: " + a)))
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    41
  }
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    42
50187
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    43
  private class Reset_Component extends Button
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    44
  {
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    45
    action = Action("Reset") {
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    46
      val text_area = view.getTextArea
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    47
      Token_Markup.edit_control_style(text_area, "")
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    48
      text_area.requestFocus
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    49
    }
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    50
    tooltip = "Reset control symbols within text"
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    51
  }
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    52
56756
wenzelm
parents: 56755
diff changeset
    53
  private val group_tabs: TabbedPane = new TabbedPane {
50192
22d0f64362a5 tuned -- Symbol.groups already sorted;
wenzelm
parents: 50191
diff changeset
    54
    pages ++=
56753
40f8822d6bef misc tuning;
wenzelm
parents: 56622
diff changeset
    55
      Symbol.groups.map({ case (group, symbols) =>
50151
5f5e74365f14 capitalize lowercase groups;
immler
parents: 50146
diff changeset
    56
        new TabbedPane.Page(group,
53713
bb15972a644d improved layout, with special treatment for ScrollPane;
wenzelm
parents: 53316
diff changeset
    57
          new ScrollPane(new Wrap_Panel {
50187
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    58
            contents ++= symbols.map(new Symbol_Component(_))
b5a09812abc4 special handling of control symbols in Symbols dockable;
wenzelm
parents: 50186
diff changeset
    59
            if (group == "control") contents += new Reset_Component
53713
bb15972a644d improved layout, with special treatment for ScrollPane;
wenzelm
parents: 53316
diff changeset
    60
          }), null)
56753
40f8822d6bef misc tuning;
wenzelm
parents: 56622
diff changeset
    61
      })
40f8822d6bef misc tuning;
wenzelm
parents: 56622
diff changeset
    62
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    63
    val search_field = new TextField(10)
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    64
    val search_page =
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    65
      new TabbedPane.Page("search", new BorderPanel {
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    66
        val results_panel = new Wrap_Panel
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    67
        add(search_field, BorderPanel.Position.North)
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    68
        add(new ScrollPane(results_panel), BorderPanel.Position.Center)
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    69
  
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    70
        val search_space =
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    71
          (for (sym <- Symbol.names.keysIterator) yield (sym, Word.lowercase(sym))).toList
56769
wenzelm
parents: 56756
diff changeset
    72
        val search_delay =
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    73
          Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) {
56756
wenzelm
parents: 56755
diff changeset
    74
            val search_words = Word.explode(Word.lowercase(search_field.text))
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    75
            val search_limit = PIDE.options.int("jedit_symbols_search_limit") max 0
56756
wenzelm
parents: 56755
diff changeset
    76
            val results =
wenzelm
parents: 56755
diff changeset
    77
              for ((sym, s) <- search_space; if search_words.forall(s.contains(_))) yield sym
wenzelm
parents: 56755
diff changeset
    78
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    79
            results_panel.contents.clear
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    80
            for (sym <- results.take(search_limit))
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    81
              results_panel.contents += new Symbol_Component(sym)
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    82
            if (results.length > search_limit)
56756
wenzelm
parents: 56755
diff changeset
    83
              results_panel.contents +=
wenzelm
parents: 56755
diff changeset
    84
                new Label("...") { tooltip = "(" + (results.length - search_limit) + " more)" }
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    85
            revalidate
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    86
            repaint
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    87
          }
56769
wenzelm
parents: 56756
diff changeset
    88
          search_field.reactions += { case ValueChanged(_) => search_delay.invoke() }
56755
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    89
      }, "Search Symbols")
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    90
    pages += search_page
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    91
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    92
    listenTo(selection)
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    93
    reactions += {
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    94
      case SelectionChanged(_) if selection.page == search_page => search_field.requestFocus
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    95
    }
f29c9e7a3a80 clarified GUI focus;
wenzelm
parents: 56754
diff changeset
    96
56756
wenzelm
parents: 56755
diff changeset
    97
    for (page <- pages)
wenzelm
parents: 56755
diff changeset
    98
      page.title = Word.implode(Word.explode('_', page.title).map(Word.perhaps_capitalize(_)))
50143
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
    99
  }
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
   100
  set_content(group_tabs)
4ff5d795ed08 dockable with buttons for symbols, grouped and sorted in tabs according to ~~/etc/symbols;
immler
parents:
diff changeset
   101
}