String oldEncoding = getEncoding(); UnicodeReader ur = new UnicodeReader(loc.getInputStream(), oldEncoding); String encoding = ur.getEncoding(); BufferedReader r = new BufferedReader(ur); try { read(r, null); // Dumps old contents. } finally { r.close(); setEncoding(encoding); setDirty(false); syncLastSaveOrLoadTimeToActualFile(); discardAllEdits(); // Prevent user from being able to undo the reload
this.charSet = defaultEnc!=null ? defaultEnc : getDefaultEncoding(); this.loc = loc; setText(null); discardAllEdits(); setDirty(false); return; Document doc = getDocument(); doc.removeDocumentListener(this); BufferedReader r = new BufferedReader(ur); try { read(r, null); } finally { doc.addDocumentListener(this); String old = getFileFullPath(); this.loc = loc; setDirty(false); setCaretPosition(0); discardAllEdits(); firePropertyChange(FULL_PATH_PROPERTY, old, getFileFullPath());
/** * Returns whether the text file has been modified outside of this editor * since the last load or save operation. Note that if this is a remote * file, this method will always return <code>false</code>.<p> * * This method may be used by applications to implement a reloading * feature, where the user is prompted to reload a file if it has been * modified since their last open or save. * * @return Whether the text file has been modified outside of this * editor. * @see #getLastSaveOrLoadTime() */ public boolean isModifiedOutsideEditor() { return loc.getActualLastModified()>getLastSaveOrLoadTime(); }
/** * Saves the file in its current encoding.<p> * * The text area's "dirty" state is set to <code>false</code>, and if * this is a local file, its "last modified" time is updated. * * @throws IOException If an IO error occurs. * @see #saveAs(FileLocation) * @see #load(FileLocation, String) */ public void save() throws IOException { saveImpl(loc); setDirty(false); syncLastSaveOrLoadTimeToActualFile(); }
/** * Creates a new <code>TextEditorPane</code>. * * @param textMode Either <code>INSERT_MODE</code> or * <code>OVERWRITE_MODE</code>. * @param wordWrapEnabled Whether or not to use word wrap in this pane. * @param loc The location of the text file being edited. If this value * is <code>null</code>, a file named "Untitled.txt" in the current * directory is used. This file is displayed as empty even if it * actually exists. * @param defaultEnc The default encoding to use when opening the file, * if the file is not Unicode. If this value is <code>null</code>, * a system default value is used. * @throws IOException If an IO error occurs reading the file at * <code>loc</code>. This of course won't happen if * <code>loc</code> is <code>null</code>. */ public TextEditorPane(int textMode, boolean wordWrapEnabled, FileLocation loc, String defaultEnc) throws IOException { super(textMode); setLineWrap(wordWrapEnabled); init(loc, defaultEnc); }
/** * Saves this file in a new local location. This method fires a property * change event of type {@link #FULL_PATH_PROPERTY}. * * @param loc The location to save to. * @throws IOException If an IO error occurs. * @see #save() * @see #load(FileLocation, String) */ public void saveAs(FileLocation loc) throws IOException { saveImpl(loc); // No exception thrown - we can "rename" the file. String old = getFileFullPath(); this.loc = loc; setDirty(false); lastSaveOrLoadTime = loc.getActualLastModified(); firePropertyChange(FULL_PATH_PROPERTY, old, getFileFullPath()); }
charSet = defaultEnc==null ? getDefaultEncoding() : defaultEnc; setLineSeparator(System.getProperty("line.separator")); load(loc, defaultEnc); // Sets this.loc File file = new File(this.loc.getFileFullPath()); lastSaveOrLoadTime = file.lastModified(); setReadOnly(!file.canWrite()); setReadOnly(false); setDirty(false);
@Test public void testGetSetEncoding() { TextEditorPane textArea = new TextEditorPane(); Assert.assertFalse(textArea.isDirty()); textArea.setEncoding("UTF-16"); Assert.assertEquals("UTF-16", textArea.getEncoding()); Assert.assertTrue(textArea.isDirty()); textArea.setDirty(false); textArea.setEncoding("UTF-8"); Assert.assertEquals("UTF-8", textArea.getEncoding()); Assert.assertTrue(textArea.isDirty()); }
/** * Saves the text in this editor to the specified location. * * @param loc The location to save to. * @throws IOException If an IO error occurs. */ private void saveImpl(FileLocation loc) throws IOException { OutputStream out = loc.getOutputStream(); BufferedWriter w = new BufferedWriter( new UnicodeWriter(out, getEncoding())); try { write(w); } finally { w.close(); } }
throw new IllegalArgumentException("Invalid line terminator"); Document doc = getDocument(); Object old = doc.getProperty( RTextAreaEditorKit.EndOfLineStringProperty); separator); if (setDirty) { setDirty(true);
this.charSet = defaultEnc!=null ? defaultEnc : getDefaultEncoding(); return; Document doc = getDocument(); doc.removeDocumentListener(this); BufferedReader r = new BufferedReader(ur); try { read(r, null); } finally { doc.addDocumentListener(this);
/** * Callback for when text is inserted into the document. * * @param e Information on the insertion. */ @Override public void insertUpdate(DocumentEvent e) { if (!dirty) { setDirty(true); } }
/** * Sets the encoding to use when reading or writing this file. This * method sets the editor's dirty flag when the encoding is changed, and * fires a property change event of type {@link #ENCODING_PROPERTY}. * * @param encoding The new encoding. * @throws UnsupportedCharsetException If the encoding is not supported. * @throws NullPointerException If <code>encoding</code> is * <code>null</code>. * @see #getEncoding() */ public void setEncoding(String encoding) { if (encoding==null) { throw new NullPointerException("encoding cannot be null"); } else if (!Charset.isSupported(encoding)) { throw new UnsupportedCharsetException(encoding); } if (charSet==null || !charSet.equals(encoding)) { String oldEncoding = charSet; charSet = encoding; firePropertyChange(ENCODING_PROPERTY, oldEncoding, charSet); setDirty(true); } }
/** * Sets whether or not this text area should be treated as read-only. * This fires a property change event of type {@link #READ_ONLY_PROPERTY}. * * @param readOnly Whether or not the document is read-only. * @see #isReadOnly() */ public void setReadOnly(boolean readOnly) { if (this.readOnly!=readOnly) { this.readOnly = readOnly; firePropertyChange(READ_ONLY_PROPERTY, !readOnly, readOnly); } }
@Test(expected = UnsupportedCharsetException.class) public void testSetEncoding_invalidArg_unsupportedCharset() { new TextEditorPane().setEncoding("xxx"); }
/** * Returns the line separator used when writing this file (e.g. * "<code>\n</code>", "<code>\r\n</code>", or "<code>\r</code>").<p> * * Note that this value is an <code>Object</code> and not a * <code>String</code> as that is the way the {@link Document} interface * defines its property values. If you always use * {@link #setLineSeparator(String)} to modify this value, then the value * returned from this method will always be a <code>String</code>. * * @return The line separator. If this value is <code>null</code>, then * the system default line separator is used (usually the value * of <code>System.getProperty("line.separator")</code>). * @see #setLineSeparator(String) * @see #setLineSeparator(String, boolean) */ public Object getLineSeparator() { return getDocument().getProperty( RTextAreaEditorKit.EndOfLineStringProperty); }
/** * Returns the location of the <code>.jshintrc</code> file to use if using * JsHint as your error parser. * * @param textArea The text component. * @return The <code>.jshintrc</code> file, or <code>null</code> if none; * in that case, the JsHint defaults will be used. */ public File getJsHintRCFile(RSyntaxTextArea textArea) { // First, get the .jshintrc file in the current file's folder // hierarchy, if it exists. if (textArea instanceof TextEditorPane) { TextEditorPane tep = (TextEditorPane)textArea; File file = new File(tep.getFileFullPath()); File parent = file.getParentFile(); while (parent != null) { File possibleJsHintRc = new File(parent, ".jshintrc"); if (possibleJsHintRc.isFile()) { return possibleJsHintRc; } parent = parent.getParentFile(); } } // If no .jshintrc is found, use the specified fallback. return langSupport.getDefaultJsHintRCFile(); }
charSet = defaultEnc==null ? getDefaultEncoding() : defaultEnc; setLineSeparator(System.getProperty("line.separator")); load(loc, defaultEnc); // Sets this.loc File file = new File(this.loc.getFileFullPath()); lastSaveOrLoadTime = file.lastModified(); setReadOnly(!file.canWrite()); setReadOnly(false); setDirty(false);
/** * Saves this file in a new local location. This method fires a property * change event of type {@link #FULL_PATH_PROPERTY}. * * @param loc The location to save to. * @throws IOException If an IO error occurs. * @see #save() * @see #load(FileLocation, String) */ public void saveAs(FileLocation loc) throws IOException { saveImpl(loc); // No exception thrown - we can "rename" the file. String old = getFileFullPath(); this.loc = loc; setDirty(false); lastSaveOrLoadTime = loc.getActualLastModified(); firePropertyChange(FULL_PATH_PROPERTY, old, getFileFullPath()); }
/** * Saves the file in its current encoding.<p> * * The text area's "dirty" state is set to <code>false</code>, and if * this is a local file, its "last modified" time is updated. * * @throws IOException If an IO error occurs. * @see #saveAs(FileLocation) * @see #load(FileLocation, String) */ public void save() throws IOException { saveImpl(loc); setDirty(false); syncLastSaveOrLoadTimeToActualFile(); }