KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > QuickNotepad


1 // {{{ QuickNotepad
2
/*
3  * QuickNotepad.java
4  * part of the QuickNotepad plugin for the jEdit text editor
5  * Copyright (C) 2001 John Gellene
6  * jgellene@nyc.rr.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  * $Id: QuickNotepad.java 5574 2006-07-13 06:49:00Z kpouer $
23  */

24
25 // {{{ imports
26
import java.awt.BorderLayout JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.Font JavaDoc;
29 import java.io.BufferedReader JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.FileReader JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 import javax.swing.JFileChooser JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.JScrollPane JavaDoc;
39
40 import org.gjt.sp.jedit.EBComponent;
41 import org.gjt.sp.jedit.EBMessage;
42 import org.gjt.sp.jedit.EditBus;
43 import org.gjt.sp.jedit.GUIUtilities;
44 import org.gjt.sp.jedit.View;
45 import org.gjt.sp.jedit.jEdit;
46 import org.gjt.sp.jedit.gui.DefaultFocusComponent;
47 import org.gjt.sp.jedit.gui.DockableWindowManager;
48 import org.gjt.sp.jedit.msg.PropertiesChanged;
49 import org.gjt.sp.util.Log;
50 import org.gjt.sp.util.StandardUtilities;
51 // }}}
52

53 // {{{ QuickNotePad class
54
/**
55  *
56  * QuickNotePad - a dockable JPanel, a demonstration of a jEdit plugin.
57  *
58  */

59 public class QuickNotepad extends JPanel JavaDoc
60     implements EBComponent, QuickNotepadActions, DefaultFocusComponent {
61
62     // {{{ Instance Variables
63
private static final long serialVersionUID = 6412255692894321789L;
64
65     private String JavaDoc filename;
66
67     private String JavaDoc defaultFilename;
68
69     private View view;
70
71     private boolean floating;
72
73     private QuickNotepadTextArea textArea;
74
75     private QuickNotepadToolPanel toolPanel;
76     // }}}
77

78     // {{{ Constructor
79
/**
80      *
81      * @param view the current jedit window
82      * @param position a variable passed in from the script in actions.xml,
83      * which can be DockableWindowManager.FLOATING, TOP, BOTTOM, LEFT, RIGHT, etc.
84      * see @ref DockableWindowManager for possible values.
85      */

86     public QuickNotepad(View view, String JavaDoc position) {
87         super(new BorderLayout JavaDoc());
88         this.view = view;
89         this.floating = position.equals(DockableWindowManager.FLOATING);
90
91         if (jEdit.getSettingsDirectory() != null) {
92             this.filename = jEdit.getProperty(QuickNotepadPlugin.OPTION_PREFIX
93                     + "filepath");
94             if (this.filename == null || this.filename.length() == 0) {
95                 this.filename = new String JavaDoc(jEdit.getSettingsDirectory()
96                         + File.separator + "qn.txt");
97                 jEdit.setProperty(
98                         QuickNotepadPlugin.OPTION_PREFIX + "filepath",
99                         this.filename);
100             }
101             this.defaultFilename = this.filename;
102         }
103
104         this.toolPanel = new QuickNotepadToolPanel(this);
105         add(BorderLayout.NORTH, this.toolPanel);
106
107         if (floating)
108             this.setPreferredSize(new Dimension JavaDoc(500, 250));
109
110         textArea = new QuickNotepadTextArea();
111         textArea.setFont(QuickNotepadOptionPane.makeFont());
112
113         JScrollPane JavaDoc pane = new JScrollPane JavaDoc(textArea);
114         add(BorderLayout.CENTER, pane);
115
116         readFile();
117     }
118     // }}}
119

120     // {{{ Member Functions
121

122     // {{{ focusOnDefaultComponent
123
public void focusOnDefaultComponent() {
124         textArea.requestFocus();
125     }
126     // }}}
127

128     // {{{ getFileName
129
public String JavaDoc getFilename() {
130         return filename;
131     }
132     // }}}
133

134     // EBComponent implementation
135

136     // {{{ handleMessage
137
public void handleMessage(EBMessage message) {
138         if (message instanceof PropertiesChanged) {
139             propertiesChanged();
140         }
141     }
142     // }}}
143

144     // {{{ propertiesChanged
145
private void propertiesChanged() {
146         String JavaDoc propertyFilename = jEdit
147                 .getProperty(QuickNotepadPlugin.OPTION_PREFIX + "filepath");
148         if (!StandardUtilities.objectsEqual(defaultFilename, propertyFilename)) {
149             saveFile();
150             toolPanel.propertiesChanged();
151             defaultFilename = propertyFilename;
152             filename = defaultFilename;
153             readFile();
154         }
155         Font JavaDoc newFont = QuickNotepadOptionPane.makeFont();
156         if (!newFont.equals(textArea.getFont())) {
157             textArea.setFont(newFont);
158         }
159     }
160     // }}}
161

162     // These JComponent methods provide the appropriate points
163
// to subscribe and unsubscribe this object to the EditBus.
164

165     // {{{ addNotify
166
public void addNotify() {
167         super.addNotify();
168         EditBus.addToBus(this);
169     }
170      // }}}
171

172     // {{{ removeNotify
173
public void removeNotify() {
174         saveFile();
175         super.removeNotify();
176         EditBus.removeFromBus(this);
177     }
178     // }}}
179

180     // QuickNotepadActions implementation
181

182     // {{{
183
public void saveFile() {
184         if (filename == null || filename.length() == 0)
185             return;
186         try {
187             FileWriter JavaDoc out = new FileWriter JavaDoc(filename);
188             out.write(textArea.getText());
189             out.close();
190         } catch (IOException JavaDoc ioe) {
191             Log.log(Log.ERROR, QuickNotepad.class,
192                     "Could not write notepad text to " + filename);
193         }
194     }
195     // }}}
196

197     // {{{ chooseFile
198
public void chooseFile() {
199         String JavaDoc[] paths = GUIUtilities.showVFSFileDialog(view, null,
200                 JFileChooser.OPEN_DIALOG, false);
201         if (paths != null && !paths[0].equals(filename)) {
202             saveFile();
203             filename = paths[0];
204             toolPanel.propertiesChanged();
205             readFile();
206         }
207     }
208     // }}}
209

210     // {{{ copyToBuffer
211
public void copyToBuffer() {
212         jEdit.newFile(view);
213         view.getEditPane().getTextArea().setText(textArea.getText());
214     }
215     // }}}
216
// {{{ readFile()
217
/**
218      * Helper method
219      */

220     private void readFile() {
221         if (filename == null || filename.length() == 0)
222             return;
223
224         BufferedReader JavaDoc bf = null;
225         try {
226             bf = new BufferedReader JavaDoc(new FileReader JavaDoc(filename));
227             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2048);
228             String JavaDoc str;
229             while ((str = bf.readLine()) != null) {
230                 sb.append(str).append('\n');
231             }
232             bf.close();
233             textArea.setText(sb.toString());
234         } catch (FileNotFoundException JavaDoc fnf) {
235             Log.log(Log.ERROR, QuickNotepad.class, "notepad file " + filename
236                     + " does not exist");
237         } catch (IOException JavaDoc ioe) {
238             Log.log(Log.ERROR, QuickNotepad.class,
239                     "could not read notepad file " + filename);
240         }
241     }
242     // }}}
243
// }}}
244
}
245 // }}}
246
Popular Tags