PageRenderTime 96ms CodeModel.GetById 86ms app.highlight 6ms RepoModel.GetById 1ms 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
  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
 47    private QuickNotepadTextArea textArea;
 48    private QuickNotepadToolPanel toolPanel;
 49
 50    //
 51    // Constructor
 52    //
 53
 54    public QuickNotepad(View view, String position)
 55    {
 56        super(new BorderLayout());
 57
 58        this.view = view;
 59        this.floating = position.equals(
 60            DockableWindowManager.FLOATING);
 61
 62        this.filename = jEdit.getProperty(
 63            QuickNotepadPlugin.OPTION_PREFIX
 64            + "filepath");
 65        if(this.filename == null || this.filename.length() == 0)
 66        {
 67            this.filename = new String(jEdit.getSettingsDirectory()
 68                + File.separator + "qn.txt");
 69            jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
 70                + "filepath",this.filename);
 71        }
 72        this.defaultFilename = new String(this.filename);
 73
 74        this.toolPanel = new QuickNotepadToolPanel(this);
 75        add(BorderLayout.NORTH, this.toolPanel);
 76
 77        if(floating)
 78            this.setPreferredSize(new Dimension(500, 250));
 79
 80        textArea = new QuickNotepadTextArea();
 81        textArea.setFont(QuickNotepadOptionPane.makeFont());
 82        textArea.addKeyListener(new KeyHandler());
 83        textArea.addAncestorListener(new AncestorHandler());
 84        JScrollPane pane = new JScrollPane(textArea);
 85        add(BorderLayout.CENTER, pane);
 86
 87        readFile();
 88    }
 89
 90    //
 91    // Attribute methods
 92    //
 93
 94    // for toolBar display
 95    public String getFilename()
 96    {
 97        return filename;
 98    }
 99
100    //
101    // EBComponent implementation
102    //
103
104    public void handleMessage(EBMessage message)
105    {
106        if (message instanceof PropertiesChanged)
107        {
108            propertiesChanged();
109        }
110    }
111
112
113    private void propertiesChanged()
114    {
115        String propertyFilename = jEdit.getProperty(
116            QuickNotepadPlugin.OPTION_PREFIX + "filepath");
117        if(!defaultFilename.equals(propertyFilename))
118        {
119            saveFile();
120            toolPanel.propertiesChanged();
121            defaultFilename = propertyFilename.clone();
122            filename = defaultFilename.clone();
123            readFile();
124        }
125        Font newFont = QuickNotepadOptionPane.makeFont();
126        if(!newFont.equals(textArea.getFont()))
127        {
128            textArea.setFont(newFont);
129            textArea.invalidate();
130        }
131    }
132
133    // These JComponent methods provide the appropriate points
134    // to subscribe and unsubscribe this object to the EditBus
135
136    public void addNotify()
137    {
138        super.addNotify();
139        EditBus.addToBus(this);
140    }
141
142
143    public void removeNotify()
144    {
145        saveFile();
146        super.removeNotify();
147        EditBus.removeFromBus(this);
148    }
149
150    ...
151
152}</pre></td></tr></table></div><p>
153  This listing refers to a <tt class="classname">QuickNotebookTextArea</tt>
154  object. It is currently implemented as a <tt class="classname">JTextArea</tt> with
155  word wrap and tab sizes hard-coded.  Placing the object in a separate
156  class will simply future modifications.
157</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>