PageRenderTime 23ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/jars/QuickNotepad/QuickNotepad.java

#
Java | 262 lines | 176 code | 38 blank | 48 comment | 19 complexity | fc283fc6a86d6267d463a83fe088a14b 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 3892 2001-11-11 12:26:20Z jgellene $
 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.*;
 38//import org.gjt.sp.jedit.msg.CreateDockableWindow;
 39import org.gjt.sp.jedit.msg.PropertiesChanged;
 40import org.gjt.sp.jedit.msg.ViewUpdate;
 41import org.gjt.sp.util.Log;
 42
 43
 44public class QuickNotepad extends JPanel implements EBComponent, QuickNotepadActions
 45{
 46	private String filename;
 47	private String defaultFilename;
 48	private View view;
 49	private boolean floating;
 50
 51	private QuickNotepadTextArea textArea;
 52	private QuickNotepadToolPanel toolPanel;
 53
 54	//
 55	// Constructor
 56	//
 57
 58	public QuickNotepad(View view, String position)
 59	{
 60		super(new BorderLayout());
 61
 62		this.view = view;
 63		this.floating  = position.equals(DockableWindowManager.FLOATING);
 64
 65		this.filename = jEdit.getProperty(
 66			QuickNotepadPlugin.OPTION_PREFIX + "filepath");
 67		if(this.filename == null || this.filename.length() == 0)
 68		{
 69			this.filename = new String(jEdit.getSettingsDirectory()
 70				+ File.separator + "qn.txt");
 71			jEdit.setProperty(
 72				QuickNotepadPlugin.OPTION_PREFIX + "filepath",
 73				this.filename);
 74		}
 75		this.defaultFilename = new String(this.filename);
 76
 77		this.toolPanel = new QuickNotepadToolPanel(this);
 78		add(BorderLayout.NORTH, this.toolPanel);
 79
 80		if(floating)
 81			this.setPreferredSize(new Dimension(500, 250));
 82
 83		textArea = new QuickNotepadTextArea();
 84		textArea.setFont(QuickNotepadOptionPane.makeFont());
 85		textArea.addKeyListener(new KeyHandler());
 86		textArea.addAncestorListener(new AncestorHandler());
 87
 88		JScrollPane pane = new JScrollPane(textArea);
 89		add(BorderLayout.CENTER, pane);
 90
 91		readFile();
 92	}
 93
 94	//
 95	// Attribute methods
 96	//
 97
 98	// for toolbar display
 99	public String getFilename()
100	{
101		return filename;
102	}
103
104	//
105	// EBComponent implementation
106	//
107
108	public void handleMessage(EBMessage message)
109	{
110		if (message instanceof PropertiesChanged)
111		{
112			propertiesChanged();
113		}
114	}
115
116
117	private void propertiesChanged()
118	{
119		String propertyFilename = jEdit.getProperty(
120			QuickNotepadPlugin.OPTION_PREFIX + "filepath");
121		if(!defaultFilename.equals(propertyFilename))
122		{
123			saveFile();
124			toolPanel.propertiesChanged();
125			defaultFilename = new String(propertyFilename);
126			filename = new String(defaultFilename);
127			readFile();
128		}
129		Font newFont = QuickNotepadOptionPane.makeFont();
130		if(!newFont.equals(textArea.getFont()))
131		{
132			textArea.setFont(newFont);
133			textArea.invalidate();
134		}
135	}
136
137	// These JComponent methods provide the appropriate points
138	// to subscribe and unsubscribe this object to the EditBus
139
140	public void addNotify()
141	{
142		super.addNotify();
143		EditBus.addToBus(this);
144	}
145
146
147	public void removeNotify()
148	{
149		saveFile();
150		super.removeNotify();
151		EditBus.removeFromBus(this);
152	}
153
154
155	//
156	// QuickNotepadActions implementation
157	//
158
159	public void saveFile()
160	{
161		if(filename.length() == 0) return;
162		try
163		{
164			DataOutputStream dos = new DataOutputStream(
165			new FileOutputStream(filename));
166			dos.writeBytes(textArea.getText());
167			dos.close();
168		}
169		catch (IOException ioe)
170		{
171			Log.log(Log.ERROR, QuickNotepad.class,
172				"Could not write notepad text to " + filename);
173		}
174	}
175
176	public void chooseFile()
177	{
178		String[] paths = GUIUtilities.showVFSFileDialog(view,
179			null,JFileChooser.OPEN_DIALOG,false);
180		if(paths != null && !paths[0].equals(filename))
181		{
182			saveFile();
183			filename = paths[0];
184			toolPanel.propertiesChanged();
185			readFile();
186		}
187	}
188
189
190	public void copyToBuffer()
191	{
192		jEdit.newFile(view);
193		view.getEditPane().getTextArea().setText(textArea.getText());
194	}
195
196	//
197	// helper methods
198	//
199
200	private void readFile()
201	{
202		FileInputStream fis = null;
203		BufferedReader bf = null;
204		try
205		{
206			fis = new FileInputStream(filename);
207			bf = new BufferedReader(new InputStreamReader(fis));
208			StringBuffer sb = new StringBuffer(2048);
209			String str;
210			while((str = bf.readLine()) != null)
211			{
212				sb.append(str).append('\n');
213			}
214			bf.close();
215			fis.close();
216			textArea.setText(sb.toString());
217		}
218		catch (FileNotFoundException fnf)
219		{
220			Log.log(Log.ERROR, QuickNotepad.class,
221				"notepad file " + filename + " does not exist");
222		}
223		catch (IOException ioe)
224		{
225			Log.log(Log.ERROR, QuickNotepad.class,
226				"could not read notepad file " + filename);
227		}
228	}
229
230	//
231	// Listener objects
232	//
233
234	// <Esc> closes a floating window
235	private class KeyHandler extends KeyAdapter {
236		public void keyPressed(KeyEvent evt) {
237			if(QuickNotepad.this.floating &&
238				evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
239				evt.consume();
240				DockableWindowManager wm =
241					QuickNotepad.this.view.getDockableWindowManager();
242				wm.removeDockableWindow(QuickNotepadPlugin.NAME);
243			}
244		}
245	}
246
247	private class AncestorHandler implements AncestorListener
248	{
249		public void ancestorAdded(AncestorEvent e)
250		{
251			if(e.getSource() == QuickNotepad.this.textArea)
252			{
253				if(QuickNotepad.this.floating)
254					QuickNotepad.this.textArea.requestFocus();
255			}
256		}
257		public void ancestorMoved(AncestorEvent e) {}
258		public void ancestorRemoved(AncestorEvent e) {}
259	}
260
261}
262