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