src/Tools/jEdit/src/jedit_options.scala
author wenzelm
Tue, 11 Sep 2012 23:26:03 +0200
changeset 49296 313369027391
parent 49289 60424f123621
child 49317 5eff42e69edb
permissions -rw-r--r--
some GUI support for color options;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_options.scala
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     3
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     4
Options for Isabelle/jEdit.
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     5
*/
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     6
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     8
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
     9
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    10
import isabelle._
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    11
49252
9e10481dd3c4 prefer global default font over IsabelleText of jEdit TextArea;
wenzelm
parents: 49248
diff changeset
    12
import javax.swing.{InputVerifier, JComponent, UIManager}
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    13
import javax.swing.text.JTextComponent
49252
9e10481dd3c4 prefer global default font over IsabelleText of jEdit TextArea;
wenzelm
parents: 49248
diff changeset
    14
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    15
import scala.swing.{Component, CheckBox, TextArea}
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    16
49296
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    17
import org.gjt.sp.jedit.gui.ColorWellButton
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    18
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    19
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    20
trait Option_Component extends Component
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    21
{
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    22
  val title: String
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    23
  def load(): Unit
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    24
  def save(): Unit
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    25
}
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    26
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    27
class JEdit_Options extends Options_Variable
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
    28
{
49296
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    29
  def make_color_component(opt: Options.Opt): Option_Component =
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    30
  {
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    31
    Swing_Thread.require()
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    32
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    33
    val opt_name = opt.name
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    34
    val opt_title = opt.title("jedit")
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    35
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    36
    val button = new ColorWellButton(Color_Value(opt.value))
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    37
    val component = new Component with Option_Component {
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    38
      override lazy val peer = button
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    39
      name = opt_name
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    40
      val title = opt_title
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    41
      def load = button.setSelectedColor(Color_Value(string(opt_name)))
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    42
      def save = string(opt_name) = Color_Value.print(button.getSelectedColor)
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    43
    }
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    44
    component.tooltip = Isabelle.tooltip(opt.print_default)
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    45
    component
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    46
  }
313369027391 some GUI support for color options;
wenzelm
parents: 49289
diff changeset
    47
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    48
  def make_component(opt: Options.Opt): Option_Component =
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    49
  {
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    50
    Swing_Thread.require()
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    51
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    52
    val opt_name = opt.name
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    53
    val opt_title = opt.title("jedit")
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    54
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    55
    val component =
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    56
      if (opt.typ == Options.Bool)
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    57
        new CheckBox with Option_Component {
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    58
          name = opt_name
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    59
          val title = opt_title
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    60
          def load = selected = bool(opt_name)
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    61
          def save = bool(opt_name) = selected
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    62
        }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    63
      else {
49252
9e10481dd3c4 prefer global default font over IsabelleText of jEdit TextArea;
wenzelm
parents: 49248
diff changeset
    64
        val default_font = UIManager.getFont("TextField.font")
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    65
        val text_area =
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    66
          new TextArea with Option_Component {
49252
9e10481dd3c4 prefer global default font over IsabelleText of jEdit TextArea;
wenzelm
parents: 49248
diff changeset
    67
            if (default_font != null) font = default_font
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    68
            name = opt_name
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    69
            val title = opt_title
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    70
            def load = text = value.check_name(opt_name).value
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    71
            def save = update(value + (opt_name, text))
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    72
          }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    73
        text_area.peer.setInputVerifier(new InputVerifier {
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    74
          def verify(jcomponent: JComponent): Boolean =
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    75
            jcomponent match {
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    76
              case text: JTextComponent =>
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    77
                try { value + (opt_name, text.getText); true }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    78
                catch { case ERROR(_) => false }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    79
              case _ => true
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    80
            }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    81
          })
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    82
        text_area
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    83
      }
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    84
    component.load()
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    85
    component.tooltip = Isabelle.tooltip(opt.print_default)
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    86
    component
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    87
  }
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    88
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    89
  def make_components(predefined: List[Option_Component], filter: String => Boolean)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    90
    : List[(String, List[Option_Component])] =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    91
  {
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    92
    def mk_component(opt: Options.Opt): List[Option_Component] =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    93
      predefined.find(opt.name == _.name) match {
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    94
        case Some(c) => List(c)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    95
        case None => if (filter(opt.name)) List(make_component(opt)) else Nil
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    96
      }
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    97
    value.sections.sortBy(_._1).map(
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    98
        { case (a, opts) => (a, opts.sortBy(_.title("jedit")).flatMap(mk_component _)) })
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
    99
      .filterNot(_._2.isEmpty)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49252
diff changeset
   100
  }
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
   101
}
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
diff changeset
   102