001: /*
002: * QuickNotepad.java
003: * part of the QuickNotepad plugin for the jEdit text editor
004: * Copyright (C) 2001 John Gellene
005: * jgellene@nyc.rr.com
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * $Id: QuickNotepad.java 9481 2007-05-02 00:34:44Z k_satoda $
022: */
023:
024: // {{{ imports
025: import java.awt.BorderLayout;
026: import java.awt.Dimension;
027: import java.awt.Font;
028: import java.io.BufferedReader;
029: import java.io.File;
030: import java.io.FileNotFoundException;
031: import java.io.FileReader;
032: import java.io.FileWriter;
033: import java.io.IOException;
034:
035: import javax.swing.JFileChooser;
036: import javax.swing.JPanel;
037: import javax.swing.JScrollPane;
038:
039: import org.gjt.sp.jedit.EBComponent;
040: import org.gjt.sp.jedit.EBMessage;
041: import org.gjt.sp.jedit.EditBus;
042: import org.gjt.sp.jedit.GUIUtilities;
043: import org.gjt.sp.jedit.View;
044: import org.gjt.sp.jedit.jEdit;
045: import org.gjt.sp.jedit.gui.DefaultFocusComponent;
046: import org.gjt.sp.jedit.gui.DockableWindowManager;
047: import org.gjt.sp.jedit.msg.PropertiesChanged;
048: import org.gjt.sp.util.Log;
049: import org.gjt.sp.util.StandardUtilities;
050:
051: // }}}
052:
053: // {{{ QuickNotePad class
054: /**
055: *
056: * QuickNotePad - a dockable JPanel, a demonstration of a jEdit plugin.
057: *
058: */
059: public class QuickNotepad extends JPanel implements EBComponent,
060: QuickNotepadActions, DefaultFocusComponent {
061:
062: // {{{ Instance Variables
063: private static final long serialVersionUID = 6412255692894321789L;
064:
065: private String filename;
066:
067: private String defaultFilename;
068:
069: private View view;
070:
071: private boolean floating;
072:
073: private QuickNotepadTextArea textArea;
074:
075: private QuickNotepadToolPanel toolPanel;
076:
077: // }}}
078:
079: // {{{ Constructor
080: /**
081: *
082: * @param view the current jedit window
083: * @param position a variable passed in from the script in actions.xml,
084: * which can be DockableWindowManager.FLOATING, TOP, BOTTOM, LEFT, RIGHT, etc.
085: * see @ref DockableWindowManager for possible values.
086: */
087: public QuickNotepad(View view, String position) {
088: super (new BorderLayout());
089: this .view = view;
090: this .floating = position.equals(DockableWindowManager.FLOATING);
091:
092: if (jEdit.getSettingsDirectory() != null) {
093: this .filename = jEdit
094: .getProperty(QuickNotepadPlugin.OPTION_PREFIX
095: + "filepath");
096: if (this .filename == null || this .filename.length() == 0) {
097: this .filename = new String(jEdit.getSettingsDirectory()
098: + File.separator + "qn.txt");
099: jEdit.setProperty(QuickNotepadPlugin.OPTION_PREFIX
100: + "filepath", this .filename);
101: }
102: this .defaultFilename = this .filename;
103: }
104:
105: this .toolPanel = new QuickNotepadToolPanel(this );
106: add(BorderLayout.NORTH, this .toolPanel);
107:
108: if (floating)
109: this .setPreferredSize(new Dimension(500, 250));
110:
111: textArea = new QuickNotepadTextArea();
112: textArea.setFont(QuickNotepadOptionPane.makeFont());
113:
114: JScrollPane pane = new JScrollPane(textArea);
115: add(BorderLayout.CENTER, pane);
116:
117: readFile();
118: }
119:
120: // }}}
121:
122: // {{{ Member Functions
123:
124: // {{{ focusOnDefaultComponent
125: public void focusOnDefaultComponent() {
126: textArea.requestFocus();
127: }
128:
129: // }}}
130:
131: // {{{ getFileName
132: public String getFilename() {
133: return filename;
134: }
135:
136: // }}}
137:
138: // EBComponent implementation
139:
140: // {{{ handleMessage
141: public void handleMessage(EBMessage message) {
142: if (message instanceof PropertiesChanged) {
143: propertiesChanged();
144: }
145: }
146:
147: // }}}
148:
149: // {{{ propertiesChanged
150: private void propertiesChanged() {
151: String propertyFilename = jEdit
152: .getProperty(QuickNotepadPlugin.OPTION_PREFIX
153: + "filepath");
154: if (!StandardUtilities.objectsEqual(defaultFilename,
155: propertyFilename)) {
156: saveFile();
157: toolPanel.propertiesChanged();
158: defaultFilename = propertyFilename;
159: filename = defaultFilename;
160: readFile();
161: }
162: Font newFont = QuickNotepadOptionPane.makeFont();
163: if (!newFont.equals(textArea.getFont())) {
164: textArea.setFont(newFont);
165: }
166: }
167:
168: // }}}
169:
170: // These JComponent methods provide the appropriate points
171: // to subscribe and unsubscribe this object to the EditBus.
172:
173: // {{{ addNotify
174: public void addNotify() {
175: super .addNotify();
176: EditBus.addToBus(this );
177: }
178:
179: // }}}
180:
181: // {{{ removeNotify
182: public void removeNotify() {
183: saveFile();
184: super .removeNotify();
185: EditBus.removeFromBus(this );
186: }
187:
188: // }}}
189:
190: // QuickNotepadActions implementation
191:
192: // {{{
193: public void saveFile() {
194: if (filename == null || filename.length() == 0)
195: return;
196: try {
197: FileWriter out = new FileWriter(filename);
198: out.write(textArea.getText());
199: out.close();
200: } catch (IOException ioe) {
201: Log.log(Log.ERROR, QuickNotepad.class,
202: "Could not write notepad text to " + filename);
203: }
204: }
205:
206: // }}}
207:
208: // {{{ chooseFile
209: public void chooseFile() {
210: String[] paths = GUIUtilities.showVFSFileDialog(view, null,
211: JFileChooser.OPEN_DIALOG, false);
212: if (paths != null && !paths[0].equals(filename)) {
213: saveFile();
214: filename = paths[0];
215: toolPanel.propertiesChanged();
216: readFile();
217: }
218: }
219:
220: // }}}
221:
222: // {{{ copyToBuffer
223: public void copyToBuffer() {
224: jEdit.newFile(view);
225: view.getEditPane().getTextArea().setText(textArea.getText());
226: }
227:
228: // }}}
229: // {{{ readFile()
230: /**
231: * Helper method
232: */
233: private void readFile() {
234: if (filename == null || filename.length() == 0)
235: return;
236:
237: BufferedReader bf = null;
238: try {
239: bf = new BufferedReader(new FileReader(filename));
240: StringBuffer sb = new StringBuffer(2048);
241: String str;
242: while ((str = bf.readLine()) != null) {
243: sb.append(str).append('\n');
244: }
245: bf.close();
246: textArea.setText(sb.toString());
247: } catch (FileNotFoundException fnf) {
248: Log.log(Log.ERROR, QuickNotepad.class, "notepad file "
249: + filename + " does not exist");
250: } catch (IOException ioe) {
251: Log.log(Log.ERROR, QuickNotepad.class,
252: "could not read notepad file " + filename);
253: }
254: }
255: // }}}
256: // }}}
257: }
258: // }}}
|