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

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

#
HTML | 90 lines | 81 code | 9 blank | 0 comment | 0 complexity | 5b8e65d6794a1cfd755e70cb8b75f0ca 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.73.2"><link rel="start" href="index.html" title="jEdit 4.3 User's Guide"><link rel="up" href="plugin-implement.html" title="Chapter 18. Implementing a Simple Plugin"><link rel="prev" 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><p>Using the default implementation provided by
  2. <code class="classname">AbstractOptionPane</code> reduces the preparation of an
  3. option pane to two principal tasks: writing a
  4. <code class="function">_init()</code> method to layout and initialize the pane,
  5. and writing a <code class="function">_save()</code> method to commit any settings
  6. changed by user input. If a button on the option pane should trigger
  7. another dialog, such as a <code class="classname">JFileChooser</code> or jEdit's
  8. own enhanced <code class="classname">VFSFileChooserDialog</code>, the option
  9. pane will also have to implement the
  10. <code class="classname">ActionListener</code> interface to display additional
  11. components.</p><p>The QuickNotepad plugin has only three options to set: the path
  12. name of the file that will store the notepad text, the visibility of the
  13. path name on the tool bar, and the notepad's display font. Using the
  14. shortcut methods of the plugin API, the implementation of
  15. <code class="function">_init()</code> looks like this:</p><div class="informalexample"><pre class="programlisting">public class QuickNotepadOptionPane extends AbstractOptionPane
  16. implements ActionListener
  17. {
  18. private JTextField pathName;
  19. private JButton pickPath;
  20. private FontSelector font;
  21. ...
  22. public void _init()
  23. {
  24. showPath = new JCheckBox(jEdit.getProperty(
  25. QuickNotepadPlugin.OPTION_PREFIX
  26. + "show-filepath.title"),
  27. jEdit.getProperty(
  28. QuickNotepadPlugin.OPTION_PREFIX + "show-filepath")
  29. .equals("true"));
  30. addComponent(showPath);
  31. pathName = new JTextField(jEdit.getProperty(
  32. QuickNotepadPlugin.OPTION_PREFIX
  33. + "filepath"));
  34. JButton pickPath = new JButton(jEdit.getProperty(
  35. QuickNotepadPlugin.OPTION_PREFIX
  36. + "choose-file"));
  37. pickPath.addActionListener(this);
  38. JPanel pathPanel = new JPanel(new BorderLayout(0, 0));
  39. pathPanel.add(pathName, BorderLayout.CENTER);
  40. pathPanel.add(pickPath, BorderLayout.EAST);
  41. addComponent(jEdit.getProperty(
  42. QuickNotepadPlugin.OPTION_PREFIX + "file"),
  43. pathPanel);
  44. font = new FontSelector(makeFont());
  45. addComponent(jEdit.getProperty(
  46. QuickNotepadPlugin.OPTION_PREFIX + "choose-font"),
  47. font);
  48. }
  49. ...
  50. }</pre></div><p>Here we adopt the vertical arrangement offered by use of the
  51. <code class="function">addComponent()</code> method with one embellishment. We
  52. want the first &#8220;<span class="quote">row</span>&#8221; of the option pane to contain a text
  53. field with the current notepad file path and a button that will trigger
  54. a file chooser dialog when pressed. To place both of them on the same
  55. line (along with an identifying label for the file option), we create a
  56. <code class="classname">JPanel</code> to contain both components and pass the
  57. configured panel to <code class="function">addComponent()</code>.</p><p>The <code class="function">_init()</code> method uses properties from the
  58. plugin's property file to provide the names of label for the components
  59. placed in the option pane. It also uses a property whose name begins
  60. with <code class="function">PROPERTY_PREFIX</code> as a persistent data item -
  61. the path of the current notepad file. The elements of the notepad's font
  62. are also extracted from properties using a static method of the option
  63. pane class.</p><p>The <code class="function">_save()</code> method extracts data from the
  64. user input components and assigns them to the plugin's properties. The
  65. implementation is straightforward:</p><div class="informalexample"><pre class="programlisting">public void _save()
  66. {
  67. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  68. + "filepath", pathName.getText());
  69. Font _font = font.getFont();
  70. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  71. + "font", _font.getFamily());
  72. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  73. + "fontsize", String.valueOf(_font.getSize()));
  74. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  75. + "fontstyle", String.valueOf(_font.getStyle()));
  76. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  77. + "show-filepath", String.valueOf(showPath.isSelected()));
  78. }</pre></div><p>The class has only two other methods, one to display a file
  79. chooser dialog in response to user action, and the other to construct a
  80. <code class="classname">Font</code> object from the plugin's font properties.
  81. They do not require discussion here.</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>