PageRenderTime 19ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/jars/QuickNotepad/QuickNotepad.java

#
Java | 251 lines | 165 code | 39 blank | 47 comment | 24 complexity | 3c41221175df6cf9209c1b8113a3c705 MD5 | raw file
  1/*
  2 * QuickNotepad.java
  3 * part of the QuickNotepad plugin for the jEdit text editor
  4 * Copyright (C) 2001 John Gellene
  5 * jgellene@nyc.rr.com
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * as published by the Free Software Foundation; either version 2
 10 * of the License, or any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20 *
 21 * $Id: QuickNotepad.java 5030 2004-04-30 20:23:08Z spestov $
 22 */
 23
 24// from Java:
 25import java.awt.*;
 26import java.awt.event.*;
 27import java.io.*;
 28import java.util.Vector;
 29
 30// from Swing:
 31import javax.swing.*;
 32import javax.swing.event.*;
 33
 34// from jEdit:
 35import org.gjt.sp.jedit.*;
 36import org.gjt.sp.jedit.gui.*;
 37import org.gjt.sp.jedit.io.*;
 38import org.gjt.sp.jedit.msg.PropertiesChanged;
 39import org.gjt.sp.jedit.msg.ViewUpdate;
 40import org.gjt.sp.util.Log;
 41
 42
 43public class QuickNotepad extends JPanel implements EBComponent, QuickNotepadActions, DefaultFocusComponent
 44{
 45	private String filename;
 46	private String defaultFilename;
 47	private View view;
 48	private boolean floating;
 49
 50	private QuickNotepadTextArea textArea;
 51	private QuickNotepadToolPanel toolPanel;
 52
 53	//
 54	// Constructor
 55	//
 56
 57	public QuickNotepad(View view, String position)
 58	{
 59		super(new BorderLayout());
 60
 61		this.view = view;
 62		this.floating  = position.equals(DockableWindowManager.FLOATING);
 63
 64		if(jEdit.getSettingsDirectory() != null)
 65		{
 66			this.filename = jEdit.getProperty(
 67				QuickNotepadPlugin.OPTION_PREFIX + "filepath");
 68			if(this.filename == null || this.filename.length() == 0)
 69			{
 70				this.filename = new String(jEdit.getSettingsDirectory()
 71					+ File.separator + "qn.txt");
 72				jEdit.setProperty(
 73					QuickNotepadPlugin.OPTION_PREFIX + "filepath",
 74					this.filename);
 75			}
 76			this.defaultFilename = this.filename;
 77		}
 78
 79		this.toolPanel = new QuickNotepadToolPanel(this);
 80		add(BorderLayout.NORTH, this.toolPanel);
 81
 82		if(floating)
 83			this.setPreferredSize(new Dimension(500, 250));
 84
 85		textArea = new QuickNotepadTextArea();
 86		textArea.setFont(QuickNotepadOptionPane.makeFont());
 87		textArea.addKeyListener(new KeyHandler());
 88
 89		JScrollPane pane = new JScrollPane(textArea);
 90		add(BorderLayout.CENTER, pane);
 91
 92		readFile();
 93	}
 94
 95	public void focusOnDefaultComponent()
 96	{
 97		textArea.requestFocus();
 98	}
 99
100	//
101	// Attribute methods
102	//
103
104	// for toolbar display
105	public String getFilename()
106	{
107		return filename;
108	}
109
110	//
111	// EBComponent implementation
112	//
113
114	public void handleMessage(EBMessage message)
115	{
116		if (message instanceof PropertiesChanged)
117		{
118			propertiesChanged();
119		}
120	}
121
122
123	private void propertiesChanged()
124	{
125		String propertyFilename = jEdit.getProperty(
126			QuickNotepadPlugin.OPTION_PREFIX + "filepath");
127		if(!MiscUtilities.objectsEqual(defaultFilename,propertyFilename))
128		{
129			saveFile();
130			toolPanel.propertiesChanged();
131			defaultFilename = propertyFilename;
132			filename = defaultFilename;
133			readFile();
134		}
135		Font newFont = QuickNotepadOptionPane.makeFont();
136		if(!newFont.equals(textArea.getFont()))
137		{
138			textArea.setFont(newFont);
139		}
140	}
141
142	// These JComponent methods provide the appropriate points
143	// to subscribe and unsubscribe this object to the EditBus
144
145	public void addNotify()
146	{
147		super.addNotify();
148		EditBus.addToBus(this);
149	}
150
151
152	public void removeNotify()
153	{
154		saveFile();
155		super.removeNotify();
156		EditBus.removeFromBus(this);
157	}
158
159
160	//
161	// QuickNotepadActions implementation
162	//
163
164	public void saveFile()
165	{
166		if(filename == null || filename.length() == 0) return;
167		try
168		{
169			FileWriter out = new FileWriter(filename);
170			out.write(textArea.getText());
171			out.close();
172		}
173		catch (IOException ioe)
174		{
175			Log.log(Log.ERROR, QuickNotepad.class,
176				"Could not write notepad text to " + filename);
177		}
178	}
179
180	public void chooseFile()
181	{
182		String[] paths = GUIUtilities.showVFSFileDialog(view,
183			null,JFileChooser.OPEN_DIALOG,false);
184		if(paths != null && !paths[0].equals(filename))
185		{
186			saveFile();
187			filename = paths[0];
188			toolPanel.propertiesChanged();
189			readFile();
190		}
191	}
192
193
194	public void copyToBuffer()
195	{
196		jEdit.newFile(view);
197		view.getEditPane().getTextArea().setText(textArea.getText());
198	}
199
200	//
201	// helper methods
202	//
203
204	private void readFile()
205	{
206		if(filename == null || filename.length() == 0) return;
207
208		BufferedReader bf = null;
209		try
210		{
211			bf = new BufferedReader(new FileReader(filename));
212			StringBuffer sb = new StringBuffer(2048);
213			String str;
214			while((str = bf.readLine()) != null)
215			{
216				sb.append(str).append('\n');
217			}
218			bf.close();
219			textArea.setText(sb.toString());
220		}
221		catch (FileNotFoundException fnf)
222		{
223			Log.log(Log.ERROR, QuickNotepad.class,
224				"notepad file " + filename + " does not exist");
225		}
226		catch (IOException ioe)
227		{
228			Log.log(Log.ERROR, QuickNotepad.class,
229				"could not read notepad file " + filename);
230		}
231	}
232
233	//
234	// Listener objects
235	//
236
237	// <Esc> closes a floating window
238	private class KeyHandler extends KeyAdapter {
239		public void keyPressed(KeyEvent evt) {
240			if(QuickNotepad.this.floating &&
241				evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
242				evt.consume();
243				DockableWindowManager wm =
244					QuickNotepad.this.view.getDockableWindowManager();
245				wm.removeDockableWindow(QuickNotepadPlugin.NAME);
246			}
247		}
248	}
249
250}
251