1 24 25 import java.awt.BorderLayout ; 27 import java.awt.Dimension ; 28 import java.awt.Font ; 29 import java.io.BufferedReader ; 30 import java.io.File ; 31 import java.io.FileNotFoundException ; 32 import java.io.FileReader ; 33 import java.io.FileWriter ; 34 import java.io.IOException ; 35 36 import javax.swing.JFileChooser ; 37 import javax.swing.JPanel ; 38 import javax.swing.JScrollPane ; 39 40 import org.gjt.sp.jedit.EBComponent; 41 import org.gjt.sp.jedit.EBMessage; 42 import org.gjt.sp.jedit.EditBus; 43 import org.gjt.sp.jedit.GUIUtilities; 44 import org.gjt.sp.jedit.View; 45 import org.gjt.sp.jedit.jEdit; 46 import org.gjt.sp.jedit.gui.DefaultFocusComponent; 47 import org.gjt.sp.jedit.gui.DockableWindowManager; 48 import org.gjt.sp.jedit.msg.PropertiesChanged; 49 import org.gjt.sp.util.Log; 50 import org.gjt.sp.util.StandardUtilities; 51 53 59 public class QuickNotepad extends JPanel 60 implements EBComponent, QuickNotepadActions, DefaultFocusComponent { 61 62 private static final long serialVersionUID = 6412255692894321789L; 64 65 private String filename; 66 67 private String defaultFilename; 68 69 private View view; 70 71 private boolean floating; 72 73 private QuickNotepadTextArea textArea; 74 75 private QuickNotepadToolPanel toolPanel; 76 78 86 public QuickNotepad(View view, String position) { 87 super(new BorderLayout ()); 88 this.view = view; 89 this.floating = position.equals(DockableWindowManager.FLOATING); 90 91 if (jEdit.getSettingsDirectory() != null) { 92 this.filename = jEdit.getProperty(QuickNotepadPlugin.OPTION_PREFIX 93 + "filepath"); 94 if (this.filename == null || this.filename.length() == 0) { 95 this.filename = new String (jEdit.getSettingsDirectory() 96 + File.separator + "qn.txt"); 97 jEdit.setProperty( 98 QuickNotepadPlugin.OPTION_PREFIX + "filepath", 99 this.filename); 100 } 101 this.defaultFilename = this.filename; 102 } 103 104 this.toolPanel = new QuickNotepadToolPanel(this); 105 add(BorderLayout.NORTH, this.toolPanel); 106 107 if (floating) 108 this.setPreferredSize(new Dimension (500, 250)); 109 110 textArea = new QuickNotepadTextArea(); 111 textArea.setFont(QuickNotepadOptionPane.makeFont()); 112 113 JScrollPane pane = new JScrollPane (textArea); 114 add(BorderLayout.CENTER, pane); 115 116 readFile(); 117 } 118 120 122 public void focusOnDefaultComponent() { 124 textArea.requestFocus(); 125 } 126 128 public String getFilename() { 130 return filename; 131 } 132 134 136 public void handleMessage(EBMessage message) { 138 if (message instanceof PropertiesChanged) { 139 propertiesChanged(); 140 } 141 } 142 144 private void propertiesChanged() { 146 String propertyFilename = jEdit 147 .getProperty(QuickNotepadPlugin.OPTION_PREFIX + "filepath"); 148 if (!StandardUtilities.objectsEqual(defaultFilename, propertyFilename)) { 149 saveFile(); 150 toolPanel.propertiesChanged(); 151 defaultFilename = propertyFilename; 152 filename = defaultFilename; 153 readFile(); 154 } 155 Font newFont = QuickNotepadOptionPane.makeFont(); 156 if (!newFont.equals(textArea.getFont())) { 157 textArea.setFont(newFont); 158 } 159 } 160 162 165 public void addNotify() { 167 super.addNotify(); 168 EditBus.addToBus(this); 169 } 170 172 public void removeNotify() { 174 saveFile(); 175 super.removeNotify(); 176 EditBus.removeFromBus(this); 177 } 178 180 182 public void saveFile() { 184 if (filename == null || filename.length() == 0) 185 return; 186 try { 187 FileWriter out = new FileWriter (filename); 188 out.write(textArea.getText()); 189 out.close(); 190 } catch (IOException ioe) { 191 Log.log(Log.ERROR, QuickNotepad.class, 192 "Could not write notepad text to " + filename); 193 } 194 } 195 197 public void chooseFile() { 199 String [] paths = GUIUtilities.showVFSFileDialog(view, null, 200 JFileChooser.OPEN_DIALOG, false); 201 if (paths != null && !paths[0].equals(filename)) { 202 saveFile(); 203 filename = paths[0]; 204 toolPanel.propertiesChanged(); 205 readFile(); 206 } 207 } 208 210 public void copyToBuffer() { 212 jEdit.newFile(view); 213 view.getEditPane().getTextArea().setText(textArea.getText()); 214 } 215 220 private void readFile() { 221 if (filename == null || filename.length() == 0) 222 return; 223 224 BufferedReader bf = null; 225 try { 226 bf = new BufferedReader (new FileReader (filename)); 227 StringBuffer sb = new StringBuffer (2048); 228 String str; 229 while ((str = bf.readLine()) != null) { 230 sb.append(str).append('\n'); 231 } 232 bf.close(); 233 textArea.setText(sb.toString()); 234 } catch (FileNotFoundException fnf) { 235 Log.log(Log.ERROR, QuickNotepad.class, "notepad file " + filename 236 + " does not exist"); 237 } catch (IOException ioe) { 238 Log.log(Log.ERROR, QuickNotepad.class, 239 "could not read notepad file " + filename); 240 } 241 } 242 } 245 | Popular Tags |