/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
- <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>
- Here is where most of the features of the plugin will be implemented.
- To work with the dockable window API, the top level window will be a
- <tt class="classname">JPanel</tt>. The visible components reflect a
- simple layout. Inside the top-level panel we will place a scroll pane with
- a text area. Above the scroll pane we will place a panel containing a small
- tool bar and a label displaying the path of the current notepad file.
- </p><p>
- We have identified three user actions that need
- implementation here: <tt class="function">chooseFile()</tt>,
- <tt class="function">saveFile()</tt>, and
- <tt class="function">copyToBuffer()</tt>. As noted earlier, we also want the
- text area to change its appearance in immediate response to a change in
- user options settings. In order to do that, the window class must
- respond to a <tt class="classname">PropertiesChanged</tt> message from
- the EditBus.
- </p><p>
- Unlike the <tt class="classname">EBPlugin</tt> class, the
- <tt class="classname">EBComponent</tt> interface does not deal with the
- component's actual subscribing and unsubscribing to the EditBus. To
- accomplish this, we use a pair of methods inherited from the
- Java platform's <tt class="classname">JComponent</tt> class
- that are called when the window is made visible, and when it is hidden.
- These two methods,
- <tt class="function">addNotify()</tt> and
- <tt class="function">removeNotify()</tt>, are overridden to add and remove
- the visible window from the list of EditBus subscribers.
- </p><p>
- We will provide for two minor features when the notepad is
- displayed in the floating window. First, when a floating plugin window
- is created, we will give the notepad text area input focus. Second,
- when the notepad if floating and has input focus, we will have the
- <tt class="keycap">Escape</tt> key dismiss the notepad window. An
- <tt class="classname">AncestorListener</tt> and a
- <tt class="classname">KeyListener</tt> will implement these details.
- </p><p>
- Here is the listing for the data members, the constructor, and the
- implementation of the <tt class="classname">EBComponent</tt> interface:
- </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">public class QuickNotepad extends JPanel
- implements EBComponent
- {
- private String filename;
- private String defaultFilename;
- private View view;
- private boolean floating;
- private QuickNotepadTextArea textArea;
- private QuickNotepadToolPanel toolPanel;
- //
- // Constructor
- //
- public QuickNotepad(View view, String position)
- {
- super(new BorderLayout());
- this.view = view;
- this.floating = position.equals(
- DockableWindowManager.FLOATING);
- this.filename = jEdit.getProperty(
- QuickNotepadPlugin.OPTION_PREFIX
- + "filepath");
- if(this.filename == null || this.filename.length() == 0)
- {
- this.filename = new String(jEdit.getSettingsDirectory()
- + File.separator + "qn.txt");
- jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
- + "filepath",this.filename);
- }
- this.defaultFilename = new String(this.filename);
- this.toolPanel = new QuickNotepadToolPanel(this);
- add(BorderLayout.NORTH, this.toolPanel);
- if(floating)
- this.setPreferredSize(new Dimension(500, 250));
- textArea = new QuickNotepadTextArea();
- textArea.setFont(QuickNotepadOptionPane.makeFont());
- textArea.addKeyListener(new KeyHandler());
- textArea.addAncestorListener(new AncestorHandler());
- JScrollPane pane = new JScrollPane(textArea);
- add(BorderLayout.CENTER, pane);
- readFile();
- }
- //
- // Attribute methods
- //
- // for toolBar display
- public String getFilename()
- {
- return filename;
- }
- //
- // EBComponent implementation
- //
- public void handleMessage(EBMessage message)
- {
- if (message instanceof PropertiesChanged)
- {
- propertiesChanged();
- }
- }
- private void propertiesChanged()
- {
- String propertyFilename = jEdit.getProperty(
- QuickNotepadPlugin.OPTION_PREFIX + "filepath");
- if(!defaultFilename.equals(propertyFilename))
- {
- saveFile();
- toolPanel.propertiesChanged();
- defaultFilename = propertyFilename.clone();
- filename = defaultFilename.clone();
- readFile();
- }
- Font newFont = QuickNotepadOptionPane.makeFont();
- if(!newFont.equals(textArea.getFont()))
- {
- textArea.setFont(newFont);
- textArea.invalidate();
- }
- }
- // These JComponent methods provide the appropriate points
- // to subscribe and unsubscribe this object to the EditBus
- public void addNotify()
- {
- super.addNotify();
- EditBus.addToBus(this);
- }
- public void removeNotify()
- {
- saveFile();
- super.removeNotify();
- EditBus.removeFromBus(this);
- }
- ...
- }</pre></td></tr></table></div><p>
- This listing refers to a <tt class="classname">QuickNotebookTextArea</tt>
- object. It is currently implemented as a <tt class="classname">JTextArea</tt> with
- word wrap and tab sizes hard-coded. Placing the object in a separate
- class will simply future modifications.
- </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>