PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. // from Java:
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.io.*;
  27. import java.util.Vector;
  28. // from Swing:
  29. import javax.swing.*;
  30. import javax.swing.event.*;
  31. // from jEdit:
  32. import org.gjt.sp.jedit.*;
  33. import org.gjt.sp.jedit.gui.*;
  34. import org.gjt.sp.jedit.io.*;
  35. //import org.gjt.sp.jedit.msg.CreateDockableWindow;
  36. import org.gjt.sp.jedit.msg.PropertiesChanged;
  37. import org.gjt.sp.jedit.msg.ViewUpdate;
  38. import org.gjt.sp.util.Log;
  39. public class QuickNotepad extends JPanel implements EBComponent, QuickNotepadActions
  40. {
  41. private String filename;
  42. private String defaultFilename;
  43. private View view;
  44. private boolean floating;
  45. private QuickNotepadTextArea textArea;
  46. private QuickNotepadToolPanel toolPanel;
  47. //
  48. // Constructor
  49. //
  50. public QuickNotepad(View view, String position)
  51. {
  52. super(new BorderLayout());
  53. this.view = view;
  54. this.floating = position.equals(DockableWindowManager.FLOATING);
  55. this.filename = jEdit.getProperty(
  56. QuickNotepadPlugin.OPTION_PREFIX + "filepath");
  57. if(this.filename == null || this.filename.length() == 0)
  58. {
  59. this.filename = new String(jEdit.getSettingsDirectory()
  60. + File.separator + "qn.txt");
  61. jEdit.setProperty(
  62. QuickNotepadPlugin.OPTION_PREFIX + "filepath",
  63. this.filename);
  64. }
  65. this.defaultFilename = new String(this.filename);
  66. this.toolPanel = new QuickNotepadToolPanel(this);
  67. add(BorderLayout.NORTH, this.toolPanel);
  68. if(floating)
  69. this.setPreferredSize(new Dimension(500, 250));
  70. textArea = new QuickNotepadTextArea();
  71. textArea.setFont(QuickNotepadOptionPane.makeFont());
  72. textArea.addKeyListener(new KeyHandler());
  73. textArea.addAncestorListener(new AncestorHandler());
  74. JScrollPane pane = new JScrollPane(textArea);
  75. add(BorderLayout.CENTER, pane);
  76. readFile();
  77. }
  78. //
  79. // Attribute methods
  80. //
  81. // for toolbar display
  82. public String getFilename()
  83. {
  84. return filename;
  85. }
  86. //
  87. // EBComponent implementation
  88. //
  89. public void handleMessage(EBMessage message)
  90. {
  91. if (message instanceof PropertiesChanged)
  92. {
  93. propertiesChanged();
  94. }
  95. }
  96. private void propertiesChanged()
  97. {
  98. String propertyFilename = jEdit.getProperty(
  99. QuickNotepadPlugin.OPTION_PREFIX + "filepath");
  100. if(!defaultFilename.equals(propertyFilename))
  101. {
  102. saveFile();
  103. toolPanel.propertiesChanged();
  104. defaultFilename = new String(propertyFilename);
  105. filename = new String(defaultFilename);
  106. readFile();
  107. }
  108. Font newFont = QuickNotepadOptionPane.makeFont();
  109. if(!newFont.equals(textArea.getFont()))
  110. {
  111. textArea.setFont(newFont);
  112. textArea.invalidate();
  113. }
  114. }
  115. // These JComponent methods provide the appropriate points
  116. // to subscribe and unsubscribe this object to the EditBus
  117. public void addNotify()
  118. {
  119. super.addNotify();
  120. EditBus.addToBus(this);
  121. }
  122. public void removeNotify()
  123. {
  124. saveFile();
  125. super.removeNotify();
  126. EditBus.removeFromBus(this);
  127. }
  128. //
  129. // QuickNotepadActions implementation
  130. //
  131. public void saveFile()
  132. {
  133. if(filename.length() == 0) return;
  134. try
  135. {
  136. DataOutputStream dos = new DataOutputStream(
  137. new FileOutputStream(filename));
  138. dos.writeBytes(textArea.getText());
  139. dos.close();
  140. }
  141. catch (IOException ioe)
  142. {
  143. Log.log(Log.ERROR, QuickNotepad.class,
  144. "Could not write notepad text to " + filename);
  145. }
  146. }
  147. public void chooseFile()
  148. {
  149. String[] paths = GUIUtilities.showVFSFileDialog(view,
  150. null,JFileChooser.OPEN_DIALOG,false);
  151. if(paths != null && !paths[0].equals(filename))
  152. {
  153. saveFile();
  154. filename = paths[0];
  155. toolPanel.propertiesChanged();
  156. readFile();
  157. }
  158. }
  159. public void copyToBuffer()
  160. {
  161. jEdit.newFile(view);
  162. view.getEditPane().getTextArea().setText(textArea.getText());
  163. }
  164. //
  165. // helper methods
  166. //
  167. private void readFile()
  168. {
  169. FileInputStream fis = null;
  170. BufferedReader bf = null;
  171. try
  172. {
  173. fis = new FileInputStream(filename);
  174. bf = new BufferedReader(new InputStreamReader(fis));
  175. StringBuffer sb = new StringBuffer(2048);
  176. String str;
  177. while((str = bf.readLine()) != null)
  178. {
  179. sb.append(str).append('\n');
  180. }
  181. bf.close();
  182. fis.close();
  183. textArea.setText(sb.toString());
  184. }
  185. catch (FileNotFoundException fnf)
  186. {
  187. Log.log(Log.ERROR, QuickNotepad.class,
  188. "notepad file " + filename + " does not exist");
  189. }
  190. catch (IOException ioe)
  191. {
  192. Log.log(Log.ERROR, QuickNotepad.class,
  193. "could not read notepad file " + filename);
  194. }
  195. }
  196. //
  197. // Listener objects
  198. //
  199. // <Esc> closes a floating window
  200. private class KeyHandler extends KeyAdapter {
  201. public void keyPressed(KeyEvent evt) {
  202. if(QuickNotepad.this.floating &&
  203. evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
  204. evt.consume();
  205. DockableWindowManager wm =
  206. QuickNotepad.this.view.getDockableWindowManager();
  207. wm.removeDockableWindow(QuickNotepadPlugin.NAME);
  208. }
  209. }
  210. }
  211. private class AncestorHandler implements AncestorListener
  212. {
  213. public void ancestorAdded(AncestorEvent e)
  214. {
  215. if(e.getSource() == QuickNotepad.this.textArea)
  216. {
  217. if(QuickNotepad.this.floating)
  218. QuickNotepad.this.textArea.requestFocus();
  219. }
  220. }
  221. public void ancestorMoved(AncestorEvent e) {}
  222. public void ancestorRemoved(AncestorEvent e) {}
  223. }
  224. }