PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/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
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 4740 2003-05-29 19:02:57Z 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. DataOutputStream dos = new DataOutputStream(
  141. new FileOutputStream(filename));
  142. dos.writeBytes(textArea.getText());
  143. dos.close();
  144. }
  145. catch (IOException ioe)
  146. {
  147. Log.log(Log.ERROR, QuickNotepad.class,
  148. "Could not write notepad text to " + filename);
  149. }
  150. }
  151. public void chooseFile()
  152. {
  153. String[] paths = GUIUtilities.showVFSFileDialog(view,
  154. null,JFileChooser.OPEN_DIALOG,false);
  155. if(paths != null && !paths[0].equals(filename))
  156. {
  157. saveFile();
  158. filename = paths[0];
  159. toolPanel.propertiesChanged();
  160. readFile();
  161. }
  162. }
  163. public void copyToBuffer()
  164. {
  165. jEdit.newFile(view);
  166. view.getEditPane().getTextArea().setText(textArea.getText());
  167. }
  168. //
  169. // helper methods
  170. //
  171. private void readFile()
  172. {
  173. if(filename == null || filename.length() == 0) return;
  174. FileInputStream fis = null;
  175. BufferedReader bf = null;
  176. try
  177. {
  178. fis = new FileInputStream(filename);
  179. bf = new BufferedReader(new InputStreamReader(fis));
  180. StringBuffer sb = new StringBuffer(2048);
  181. String str;
  182. while((str = bf.readLine()) != null)
  183. {
  184. sb.append(str).append('\n');
  185. }
  186. bf.close();
  187. fis.close();
  188. textArea.setText(sb.toString());
  189. }
  190. catch (FileNotFoundException fnf)
  191. {
  192. Log.log(Log.ERROR, QuickNotepad.class,
  193. "notepad file " + filename + " does not exist");
  194. }
  195. catch (IOException ioe)
  196. {
  197. Log.log(Log.ERROR, QuickNotepad.class,
  198. "could not read notepad file " + filename);
  199. }
  200. }
  201. //
  202. // Listener objects
  203. //
  204. // <Esc> closes a floating window
  205. private class KeyHandler extends KeyAdapter {
  206. public void keyPressed(KeyEvent evt) {
  207. if(QuickNotepad.this.floating &&
  208. evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
  209. evt.consume();
  210. DockableWindowManager wm =
  211. QuickNotepad.this.view.getDockableWindowManager();
  212. wm.removeDockableWindow(QuickNotepadPlugin.NAME);
  213. }
  214. }
  215. }
  216. }