Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit a59ddaa

Browse files
committed
Part of bobbylight#211: undo buffer should be empty after TextEditorPane.load()
1 parent dbf8308 commit a59ddaa

File tree

3 files changed

+96
-7
lines changed

3 files changed

+96
-7
lines changed

src/main/java/org/fife/ui/rsyntaxtextarea/TextEditorPane.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import java.nio.charset.Charset;
1919
import java.nio.charset.UnsupportedCharsetException;
2020

21+
import javax.swing.JFrame;
22+
import javax.swing.JPanel;
23+
import javax.swing.JScrollPane;
2124
import javax.swing.event.DocumentEvent;
2225
import javax.swing.event.DocumentListener;
2326
import javax.swing.text.Document;
@@ -26,7 +29,6 @@
2629
import org.fife.io.UnicodeWriter;
2730
import org.fife.ui.rtextarea.RTextAreaEditorKit;
2831

29-
3032
/**
3133
* An extension of {@link org.fife.ui.rsyntaxtextarea.RSyntaxTextArea}
3234
* that adds information about the file being edited, such as:
@@ -482,6 +484,7 @@ public void load(FileLocation loc, String defaultEnc) throws IOException {
482484
this.loc = loc;
483485
setDirty(false);
484486
setCaretPosition(0);
487+
discardAllEdits();
485488
firePropertyChange(FULL_PATH_PROPERTY, old, getFileFullPath());
486489

487490
}
@@ -748,4 +751,22 @@ public void syncLastSaveOrLoadTimeToActualFile() {
748751
}
749752

750753

754+
public static void main(String[] args) throws Exception {
755+
try {
756+
TextEditorPane textArea = new TextEditorPane();
757+
textArea.load(FileLocation.create("d:/temp/test.txt"), "UTF-8");
758+
JPanel cp = new JPanel();
759+
cp.setPreferredSize(new java.awt.Dimension(300, 300));
760+
cp.setLayout(new java.awt.BorderLayout());
761+
cp.add(new JScrollPane(textArea));
762+
JFrame frame = new JFrame();
763+
frame.setContentPane(cp);
764+
frame.pack();
765+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
766+
frame.setLocationByPlatform(true);
767+
frame.setVisible(true);
768+
} catch (Exception e) {
769+
e.printStackTrace();
770+
}
771+
}
751772
}

src/main/java/org/fife/ui/rtextarea/RTextAreaEditorKit.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ public final String getMacroID() {
17051705

17061706
private void moveLineDown(RTextArea textArea, int line, int lineCount)
17071707
throws BadLocationException {
1708-
System.out.println("--- " + line + ", " + lineCount + ", " + textArea.getLineCount());
1708+
17091709
// If we'd be moving lines past the end of the document, stop.
17101710
// We could perhaps just decide to move the lines to the end of the
17111711
// file, but this just keeps things simple.
@@ -1730,13 +1730,11 @@ private void moveLineDown(RTextArea textArea, int line, int lineCount)
17301730
doc.remove(start, end - start);
17311731

17321732
int insertLine = Math.min(line + 1, textArea.getLineCount());
1733-
System.out.println("insertLine == " + insertLine);
17341733
boolean newlineInserted = false;
17351734
if (insertLine == textArea.getLineCount()) {
17361735
textArea.append("\n");
17371736
newlineInserted = true;
17381737
}
1739-
System.out.println("... " + newlineInserted);
17401738

17411739
int insertOffs = textArea.getLineStartOffset(insertLine);
17421740
doc.insertString(insertOffs, text, null);
@@ -1776,14 +1774,11 @@ private void moveLineUp(RTextArea textArea, int line, int moveCount)
17761774
textArea.beginAtomicEdit();
17771775
try {
17781776

1779-
System.out.println("*** " + start + ", " + (end - start));
17801777
String text = doc.getText(start, end - start);
17811778
if (movingLastLine) {
17821779
text += '\n';
17831780
}
1784-
System.out.println("*** *** '" + text + "'");
17851781
doc.remove(start, end - start);
1786-
System.out.println("*** *** *** good");
17871782

17881783
int insertOffs = textArea.getLineStartOffset(insertLine);
17891784
doc.insertString(insertOffs, text, null);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* 12/09/2016
3+
*
4+
* This library is distributed under a modified BSD license. See the included
5+
* RSyntaxTextArea.License.txt file for details.
6+
*/
7+
package org.fife.ui.rsyntaxtextarea;
8+
9+
import java.nio.charset.UnsupportedCharsetException;
10+
11+
import org.fife.ui.SwingRunner;
12+
import org.junit.Assert;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
16+
17+
/**
18+
* Unit tests for {@link TextEditorPane}.
19+
*
20+
* @author Robert Futrell
21+
* @version 1.0
22+
*/
23+
@RunWith(SwingRunner.class)
24+
public class TextEditorPaneTest {
25+
26+
27+
@Test
28+
public void testConstructor_zeroArg() {
29+
TextEditorPane textArea = new TextEditorPane();
30+
Assert.assertEquals(TextEditorPane.INSERT_MODE, textArea.getTextMode());
31+
Assert.assertFalse(textArea.getLineWrap());
32+
}
33+
34+
35+
@Test
36+
public void testConstructor_oneArg() {
37+
TextEditorPane textArea = new TextEditorPane(TextEditorPane.OVERWRITE_MODE);
38+
Assert.assertEquals(TextEditorPane.OVERWRITE_MODE, textArea.getTextMode());
39+
Assert.assertFalse(textArea.getLineWrap());
40+
}
41+
42+
43+
@Test
44+
public void testGetSetEncoding() {
45+
46+
TextEditorPane textArea = new TextEditorPane();
47+
Assert.assertFalse(textArea.isDirty());
48+
49+
textArea.setEncoding("UTF-16");
50+
Assert.assertEquals("UTF-16", textArea.getEncoding());
51+
Assert.assertTrue(textArea.isDirty());
52+
textArea.setDirty(false);
53+
54+
textArea.setEncoding("UTF-8");
55+
Assert.assertEquals("UTF-8", textArea.getEncoding());
56+
Assert.assertTrue(textArea.isDirty());
57+
58+
}
59+
60+
61+
@Test(expected = NullPointerException.class)
62+
public void testSetEncoding_invalidArg_null() {
63+
new TextEditorPane().setEncoding(null);
64+
}
65+
66+
67+
@Test(expected = UnsupportedCharsetException.class)
68+
public void testSetEncoding_invalidArg_unsupportedCharset() {
69+
new TextEditorPane().setEncoding("xxx");
70+
}
71+
72+
73+
}

0 commit comments

Comments
 (0)