PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
HTML | 157 lines | 136 code | 21 blank | 0 comment | 0 complexity | 3c6d1a5d1c0fc58e957141b160d45974 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 QuickNotepad 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-dockables.html" title="The Dockable Window Catalog"><link rel="next" href="plugin-implement-quicknotepadtoolbar.html" title="The QuickNotepadToolBar Class"></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 QuickNotepad Class</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="plugin-implement-dockables.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-quicknotepadtoolbar.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-quicknotepad"></a>The QuickNotepad Class</h2></div></div><div></div></div><p>
  2. Here is where most of the features of the plugin will be implemented.
  3. To work with the dockable window API, the top level window will be a
  4. <tt class="classname">JPanel</tt>. The visible components reflect a
  5. simple layout. Inside the top-level panel we will place a scroll pane with
  6. a text area. Above the scroll pane we will place a panel containing a small
  7. tool bar and a label displaying the path of the current notepad file.
  8. </p><p>
  9. We have identified three user actions that need
  10. implementation here: <tt class="function">chooseFile()</tt>,
  11. <tt class="function">saveFile()</tt>, and
  12. <tt class="function">copyToBuffer()</tt>. As noted earlier, we also want the
  13. text area to change its appearance in immediate response to a change in
  14. user options settings. In order to do that, the window class must
  15. respond to a <tt class="classname">PropertiesChanged</tt> message from
  16. the EditBus.
  17. </p><p>
  18. Unlike the <tt class="classname">EBPlugin</tt> class, the
  19. <tt class="classname">EBComponent</tt> interface does not deal with the
  20. component's actual subscribing and unsubscribing to the EditBus. To
  21. accomplish this, we use a pair of methods inherited from the
  22. Java platform's <tt class="classname">JComponent</tt> class
  23. that are called when the window is made visible, and when it is hidden.
  24. These two methods,
  25. <tt class="function">addNotify()</tt> and
  26. <tt class="function">removeNotify()</tt>, are overridden to add and remove
  27. the visible window from the list of EditBus subscribers.
  28. </p><p>
  29. We will provide for two minor features when the notepad is
  30. displayed in the floating window. First, when a floating plugin window
  31. is created, we will give the notepad text area input focus. Second,
  32. when the notepad if floating and has input focus, we will have the
  33. <tt class="keycap">Escape</tt> key dismiss the notepad window. An
  34. <tt class="classname">AncestorListener</tt> and a
  35. <tt class="classname">KeyListener</tt> will implement these details.
  36. </p><p>
  37. Here is the listing for the data members, the constructor, and the
  38. implementation of the <tt class="classname">EBComponent</tt> interface:
  39. </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">public class QuickNotepad extends JPanel
  40. implements EBComponent
  41. {
  42. private String filename;
  43. private String defaultFilename;
  44. private View view;
  45. private boolean floating;
  46. private QuickNotepadTextArea textArea;
  47. private QuickNotepadToolPanel toolPanel;
  48. //
  49. // Constructor
  50. //
  51. public QuickNotepad(View view, String position)
  52. {
  53. super(new BorderLayout());
  54. this.view = view;
  55. this.floating = position.equals(
  56. DockableWindowManager.FLOATING);
  57. this.filename = jEdit.getProperty(
  58. QuickNotepadPlugin.OPTION_PREFIX
  59. + "filepath");
  60. if(this.filename == null || this.filename.length() == 0)
  61. {
  62. this.filename = new String(jEdit.getSettingsDirectory()
  63. + File.separator + "qn.txt");
  64. jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
  65. + "filepath",this.filename);
  66. }
  67. this.defaultFilename = new String(this.filename);
  68. this.toolPanel = new QuickNotepadToolPanel(this);
  69. add(BorderLayout.NORTH, this.toolPanel);
  70. if(floating)
  71. this.setPreferredSize(new Dimension(500, 250));
  72. textArea = new QuickNotepadTextArea();
  73. textArea.setFont(QuickNotepadOptionPane.makeFont());
  74. textArea.addKeyListener(new KeyHandler());
  75. textArea.addAncestorListener(new AncestorHandler());
  76. JScrollPane pane = new JScrollPane(textArea);
  77. add(BorderLayout.CENTER, pane);
  78. readFile();
  79. }
  80. //
  81. // Attribute methods
  82. //
  83. // for toolBar display
  84. public String getFilename()
  85. {
  86. return filename;
  87. }
  88. //
  89. // EBComponent implementation
  90. //
  91. public void handleMessage(EBMessage message)
  92. {
  93. if (message instanceof PropertiesChanged)
  94. {
  95. propertiesChanged();
  96. }
  97. }
  98. private void propertiesChanged()
  99. {
  100. String propertyFilename = jEdit.getProperty(
  101. QuickNotepadPlugin.OPTION_PREFIX + "filepath");
  102. if(!defaultFilename.equals(propertyFilename))
  103. {
  104. saveFile();
  105. toolPanel.propertiesChanged();
  106. defaultFilename = propertyFilename.clone();
  107. filename = defaultFilename.clone();
  108. readFile();
  109. }
  110. Font newFont = QuickNotepadOptionPane.makeFont();
  111. if(!newFont.equals(textArea.getFont()))
  112. {
  113. textArea.setFont(newFont);
  114. textArea.invalidate();
  115. }
  116. }
  117. // These JComponent methods provide the appropriate points
  118. // to subscribe and unsubscribe this object to the EditBus
  119. public void addNotify()
  120. {
  121. super.addNotify();
  122. EditBus.addToBus(this);
  123. }
  124. public void removeNotify()
  125. {
  126. saveFile();
  127. super.removeNotify();
  128. EditBus.removeFromBus(this);
  129. }
  130. ...
  131. }</pre></td></tr></table></div><p>
  132. This listing refers to a <tt class="classname">QuickNotebookTextArea</tt>
  133. object. It is currently implemented as a <tt class="classname">JTextArea</tt> with
  134. word wrap and tab sizes hard-coded. Placing the object in a separate
  135. class will simply future modifications.
  136. </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plugin-implement-dockables.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-quicknotepadtoolbar.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">The Dockable Window Catalog </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> The QuickNotepadToolBar Class</td></tr></table></div></body></html>