The QuickNotepad Class
Prev | Chapter 18. Implementing a Simple Plugin | Next

## The QuickNotepad Class

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 `JPanel`. 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.

We have identified three user actions that need implementation here: `chooseFile()`, `saveFile()`, and `copyToBuffer()`. 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 `PropertiesChanged` message from the EditBus.

Unlike the `EBPlugin` class, the `EBComponent` 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 `JComponent` class that are called when the window is made visible, and when it is hidden. These two methods, `addNotify()` and `removeNotify()`, are overridden to add and remove the visible window from the list of EditBus subscribers.

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 `Escape` key dismiss the notepad window. An `AncestorListener` and a `KeyListener` will implement these details.

Here is the listing for the data members, the constructor, and the implementation of the `EBComponent` interface:

    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);
        }

        ...

    }

This listing refers to a `QuickNotebookTextArea` object. It is currently implemented as a `JTextArea` with word wrap and tab sizes hard-coded. Placing the object in a separate class will simply future modifications.

Prev | Up | Next
The Dockable Window Catalog | Home | The QuickNotepadToolBar Class