PageRenderTime 40ms CodeModel.GetById 22ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 255 lines | 169 code | 39 blank | 47 comment | 24 complexity | 161906d5d67ffa1ee33ae26e6167106d 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 4740 2003-05-29 19:02:57Z 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			DataOutputStream dos = new DataOutputStream(
170			new FileOutputStream(filename));
171			dos.writeBytes(textArea.getText());
172			dos.close();
173		}
174		catch (IOException ioe)
175		{
176			Log.log(Log.ERROR, QuickNotepad.class,
177				"Could not write notepad text to " + filename);
178		}
179	}
180
181	public void chooseFile()
182	{
183		String[] paths = GUIUtilities.showVFSFileDialog(view,
184			null,JFileChooser.OPEN_DIALOG,false);
185		if(paths != null && !paths[0].equals(filename))
186		{
187			saveFile();
188			filename = paths[0];
189			toolPanel.propertiesChanged();
190			readFile();
191		}
192	}
193
194
195	public void copyToBuffer()
196	{
197		jEdit.newFile(view);
198		view.getEditPane().getTextArea().setText(textArea.getText());
199	}
200
201	//
202	// helper methods
203	//
204
205	private void readFile()
206	{
207		if(filename == null || filename.length() == 0) return;
208
209		FileInputStream fis = null;
210		BufferedReader bf = null;
211		try
212		{
213			fis = new FileInputStream(filename);
214			bf = new BufferedReader(new InputStreamReader(fis));
215			StringBuffer sb = new StringBuffer(2048);
216			String str;
217			while((str = bf.readLine()) != null)
218			{
219				sb.append(str).append('\n');
220			}
221			bf.close();
222			fis.close();
223			textArea.setText(sb.toString());
224		}
225		catch (FileNotFoundException fnf)
226		{
227			Log.log(Log.ERROR, QuickNotepad.class,
228				"notepad file " + filename + " does not exist");
229		}
230		catch (IOException ioe)
231		{
232			Log.log(Log.ERROR, QuickNotepad.class,
233				"could not read notepad file " + filename);
234		}
235	}
236
237	//
238	// Listener objects
239	//
240
241	// <Esc> closes a floating window
242	private class KeyHandler extends KeyAdapter {
243		public void keyPressed(KeyEvent evt) {
244			if(QuickNotepad.this.floating &&
245				evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
246				evt.consume();
247				DockableWindowManager wm =
248					QuickNotepad.this.view.getDockableWindowManager();
249				wm.removeDockableWindow(QuickNotepadPlugin.NAME);
250			}
251		}
252	}
253
254}
255