PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/www/tags/NOV_07_2009/htdocs/42docs/users-guide/plugin-implement-options.html

#
HTML | 103 lines | 94 code | 9 blank | 0 comment | 0 complexity | b7c97906d7573b27b6a3b94365a31151 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>The QuickNotepadOptionPane Class</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="jEdit 4.2 User's Guide"><link rel="up" href="plugin-implement.html" title="Chapter 18. Implementing a Simple Plugin"><link rel="previous" href="plugin-implement-quicknotepadtoolbar.html" title="The QuickNotepadToolBar Class"><link rel="next" href="plugin-implement-docs.html" title="Plugin Documentation"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">The QuickNotepadOptionPane Class</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="plugin-implement-quicknotepadtoolbar.html">Prev</a> </td><th width="60%" align="center">Chapter 18. Implementing a Simple Plugin</th><td width="20%" align="right"> <a accesskey="n" href="plugin-implement-docs.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="plugin-implement-options"></a>The QuickNotepadOptionPane Class</h2></div></div><div></div></div><p>
  2. Using the default implementation provided by
  3. <tt class="classname">AbstractOptionPane</tt> reduces the preparation of an
  4. option pane to two principal tasks: writing a
  5. <tt class="function">_init()</tt> method to layout and initialize the pane,
  6. and writing a <tt class="function">_save()</tt> method to commit any settings
  7. changed by user input. If a button on the option pane should trigger
  8. another dialog, such as a <tt class="classname">JFileChooser</tt> or jEdit's
  9. own enhanced <tt class="classname">VFSFileChooserDialog</tt>, the option
  10. pane will also have to implement the
  11. <tt class="classname">ActionListener</tt> interface to display additional
  12. components.
  13. </p><p>
  14. The QuickNotepad plugin has only three options to set: the path name of
  15. the file that will store the notepad text, the visibility of the
  16. path name on the tool bar, and the notepad's display font.
  17. Using the shortcut methods of the plugin API, the implementation of
  18. <tt class="function">_init()</tt> looks like this:
  19. </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">public class QuickNotepadOptionPane extends AbstractOptionPane
  20. implements ActionListener
  21. {
  22. private JTextField pathName;
  23. private JButton pickPath;
  24. private FontSelector font;
  25. ...
  26. public void _init()
  27. {
  28. showPath = new JCheckBox(jEdit.getProperty(
  29. QuickNotepadPlugin.OPTION_PREFIX
  30. + "show-filepath.title"),
  31. jEdit.getProperty(
  32. QuickNotepadPlugin.OPTION_PREFIX + "show-filepath")
  33. .equals("true"));
  34. addComponent(showPath);
  35. pathName = new JTextField(jEdit.getProperty(
  36. QuickNotepadPlugin.OPTION_PREFIX
  37. + "filepath"));
  38. JButton pickPath = new JButton(jEdit.getProperty(
  39. QuickNotepadPlugin.OPTION_PREFIX
  40. + "choose-file"));
  41. pickPath.addActionListener(this);
  42. JPanel pathPanel = new JPanel(new BorderLayout(0, 0));
  43. pathPanel.add(pathName, BorderLayout.CENTER);
  44. pathPanel.add(pickPath, BorderLayout.EAST);
  45. addComponent(jEdit.getProperty(
  46. QuickNotepadPlugin.OPTION_PREFIX + "file"),
  47. pathPanel);
  48. font = new FontSelector(makeFont());
  49. addComponent(jEdit.getProperty(
  50. QuickNotepadPlugin.OPTION_PREFIX + "choose-font"),
  51. font);
  52. }
  53. ...
  54. }</pre></td></tr></table></div><p>
  55. Here we adopt the vertical arrangement offered by use of the
  56. <tt class="function">addComponent()</tt> method with one embellishment.
  57. We want the first &#8220;<span class="quote">row</span>&#8221; of the option pane to contain
  58. a text field with the current notepad file path and a button that will
  59. trigger a file chooser dialog when pressed. To place both of them on
  60. the same line (along with an identifying label for the file option),
  61. we create a <tt class="classname">JPanel</tt> to contain both components and
  62. pass the configured panel to <tt class="function">addComponent()</tt>.
  63. </p><p>
  64. The <tt class="function">_init()</tt> method uses properties from the plugin's
  65. property file to provide the names of label for the components placed
  66. in the option pane. It also uses a property whose name begins with
  67. <tt class="function">PROPERTY_PREFIX</tt> as a persistent data item - the
  68. path of the current notepad file. The elements of the notepad's font
  69. are also extracted from properties using a static method of the option
  70. pane class.
  71. </p><p>
  72. The <tt class="function">_save()</tt> method extracts data from the user
  73. input components and
  74. assigns them to the plugin's properties. The implementation is
  75. straightforward:
  76. </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">public void _save()
  77. {
  78. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  79. + "filepath", pathName.getText());
  80. Font _font = font.getFont();
  81. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  82. + "font", _font.getFamily());
  83. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  84. + "fontsize", String.valueOf(_font.getSize()));
  85. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  86. + "fontstyle", String.valueOf(_font.getStyle()));
  87. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  88. + "show-filepath", String.valueOf(showPath.isSelected()));
  89. }</pre></td></tr></table></div><p>
  90. The class has only two other methods, one to display a file chooser
  91. dialog in response to user action, and the other
  92. to construct a <tt class="classname">Font</tt> object from the plugin's font
  93. properties. They do not require discussion here.
  94. </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plugin-implement-quicknotepadtoolbar.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plugin-implement.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plugin-implement-docs.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">The QuickNotepadToolBar Class </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Plugin Documentation</td></tr></table></div></body></html>