File: java9-fix

package info (click to toggle)
libjsyntaxpane-java 0.9.6~r156-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,424 kB
  • sloc: java: 6,108; xml: 41; sh: 14; makefile: 11
file content (100 lines) | stat: -rw-r--r-- 3,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Description: Fix issue with java9
Author: Jacob Nordfalk <jacob.nordfalk@gmail.com>
Origin: https://github.com/nordfalk/jsyntaxpane/commit/5fc75594f8bc4df6e8f7096d4a440490b768fd46
Forwarded: not-needed
Reviewed-by: Felix Natter <fnatter@gmx.net>
Last-Update: 2018-04-24
--- a/src/main/java/jsyntaxpane/CompoundUndoMan.java
+++ b/src/main/java/jsyntaxpane/CompoundUndoMan.java
@@ -20,6 +20,7 @@
 import javax.swing.undo.CompoundEdit;
 import javax.swing.undo.UndoManager;
 import javax.swing.undo.UndoableEdit;
+import javax.swing.undo.*;
 
 /**
  * A revised UndoManager that groups undos based on positions.  If the change is relatively next to the
@@ -37,6 +38,8 @@
  */
 public class CompoundUndoMan extends UndoManager {
 
+     private final SyntaxDocument doc;
+    
 	private CompoundEdit compoundEdit;
 	// This allows us to start combining operations.
 	// it will be reset after the first change.
@@ -46,6 +49,7 @@
 	private int	lastLine = -1;
 
 	public CompoundUndoMan(SyntaxDocument doc) {
+        	    this.doc = doc;
 		doc.addUndoableEditListener(this);
 		lastLine = doc.getStartPosition().getOffset();
 	}
@@ -57,8 +61,8 @@
 	@Override
 	public void undoableEditHappened(UndoableEditEvent e) {
 		//  Start a new compound edit
-
-		AbstractDocument.DefaultDocumentEvent docEvt = (DefaultDocumentEvent) e.getEdit();
+	    
+	        //AbstractDocument.DefaultDocumentEvent docEvt = (DefaultDocumentEvent) e.getEdit();
 
 		if (compoundEdit == null) {
 			compoundEdit = startCompoundEdit(e.getEdit());
@@ -66,6 +70,7 @@
 			return;
 		}
 
+		/*
 		int editLine = ((SyntaxDocument)docEvt.getDocument()).getLineNumberAt(docEvt.getOffset());
 
 		//  Check for an incremental edit or backspace.
@@ -79,6 +84,38 @@
 
 		//  Not incremental edit, end previous edit and start a new one
 		lastLine = editLine;
+		*/
+
+		 if (e.getEdit() instanceof DefaultDocumentEvent) {
+		     // Java 6 to 8
+		     AbstractDocument.DefaultDocumentEvent docEvt = (DefaultDocumentEvent) e.getEdit();
+
+		     int editLine = doc.getLineNumberAt(docEvt.getOffset());
+
+		     //  Check for an incremental edit or backspace.
+		     //  The Change in Caret position and Document length should both be
+		     //  either 1 or -1.
+		     if ((startCombine || Math.abs(docEvt.getLength()) == 1) && editLine == lastLine) {
+			 compoundEdit.addEdit(e.getEdit());
+			 startCombine = false;
+			 //updateDirty();
+			 return;
+		     }
+
+		     //  Not incremental edit, end previous edit and start a new one
+		     lastLine = editLine;
+
+		 } else {
+		     // Java 9: It seems that all the edits are wrapped and we cannot get line number!
+		     // See https://github.com/netroby/jdk9-dev/blob/master/jdk/src/java.desktop/share/classes/javax/swing/text/AbstractDocument.java#L279
+		     // AbstractDocument.DefaultDocumentEventUndoableWrapper docEvt = e.getEdit();
+		     if (startCombine && !e.getEdit().isSignificant()) {
+			 compoundEdit.addEdit(e.getEdit());
+			 startCombine = false;
+			 //updateDirty();
+			 return;
+		     }
+		 }
 
 		compoundEdit.end();
 		compoundEdit = startCompoundEdit(e.getEdit());
@@ -90,7 +127,7 @@
 	 */
 	private CompoundEdit startCompoundEdit(UndoableEdit anEdit) {
 		//  Track Caret and Document information of this compound edit
-		AbstractDocument.DefaultDocumentEvent docEvt = (DefaultDocumentEvent) anEdit;
+		//AbstractDocument.DefaultDocumentEvent docEvt = (DefaultDocumentEvent) anEdit;
 
 		//  The compound edit is used to store incremental edits