summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/gui/CodeReader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/CodeReader.java')
-rw-r--r--src/main/java/cuchaz/enigma/gui/CodeReader.java89
1 files changed, 38 insertions, 51 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/CodeReader.java b/src/main/java/cuchaz/enigma/gui/CodeReader.java
index 601e5b9..8225d8f 100644
--- a/src/main/java/cuchaz/enigma/gui/CodeReader.java
+++ b/src/main/java/cuchaz/enigma/gui/CodeReader.java
@@ -19,8 +19,6 @@ import java.awt.event.ActionListener;
19import javax.swing.JEditorPane; 19import javax.swing.JEditorPane;
20import javax.swing.SwingUtilities; 20import javax.swing.SwingUtilities;
21import javax.swing.Timer; 21import javax.swing.Timer;
22import javax.swing.event.CaretEvent;
23import javax.swing.event.CaretListener;
24import javax.swing.text.BadLocationException; 22import javax.swing.text.BadLocationException;
25import javax.swing.text.Highlighter.HighlightPainter; 23import javax.swing.text.Highlighter.HighlightPainter;
26 24
@@ -38,15 +36,15 @@ public class CodeReader extends JEditorPane {
38 36
39 private static final long serialVersionUID = 3673180950485748810L; 37 private static final long serialVersionUID = 3673180950485748810L;
40 38
41 private static final Object m_lock = new Object(); 39 private static final Object lock = new Object();
42 40
43 public interface SelectionListener { 41 public interface SelectionListener {
44 void onSelect(EntryReference<Entry, Entry> reference); 42 void onSelect(EntryReference<Entry, Entry> reference);
45 } 43 }
46 44
47 private SelectionHighlightPainter m_selectionHighlightPainter; 45 private SelectionHighlightPainter selectionHighlightPainter;
48 private SourceIndex m_sourceIndex; 46 private SourceIndex sourceIndex;
49 private SelectionListener m_selectionListener; 47 private SelectionListener selectionListener;
50 48
51 public CodeReader() { 49 public CodeReader() {
52 50
@@ -58,38 +56,34 @@ public class CodeReader extends JEditorPane {
58 kit.toggleComponent(this, "de.sciss.syntaxpane.components.TokenMarker"); 56 kit.toggleComponent(this, "de.sciss.syntaxpane.components.TokenMarker");
59 57
60 // hook events 58 // hook events
61 addCaretListener(new CaretListener() { 59 addCaretListener(event ->
62 @Override 60 {
63 public void caretUpdate(CaretEvent event) { 61 if (selectionListener != null && sourceIndex != null) {
64 if (m_selectionListener != null && m_sourceIndex != null) { 62 Token token = sourceIndex.getReferenceToken(event.getDot());
65 Token token = m_sourceIndex.getReferenceToken(event.getDot()); 63 if (token != null) {
66 if (token != null) { 64 selectionListener.onSelect(sourceIndex.getDeobfReference(token));
67 m_selectionListener.onSelect(m_sourceIndex.getDeobfReference(token)); 65 } else {
68 } else { 66 selectionListener.onSelect(null);
69 m_selectionListener.onSelect(null);
70 }
71 } 67 }
72 } 68 }
73 }); 69 });
74 70
75 m_selectionHighlightPainter = new SelectionHighlightPainter(); 71 selectionHighlightPainter = new SelectionHighlightPainter();
76 m_sourceIndex = null;
77 m_selectionListener = null;
78 } 72 }
79 73
80 public void setSelectionListener(SelectionListener val) { 74 public void setSelectionListener(SelectionListener val) {
81 m_selectionListener = val; 75 selectionListener = val;
82 } 76 }
83 77
84 public void setCode(String code) { 78 public void setCode(String code) {
85 // sadly, the java lexer is not thread safe, so we have to serialize all these calls 79 // sadly, the java lexer is not thread safe, so we have to serialize all these calls
86 synchronized (m_lock) { 80 synchronized (lock) {
87 setText(code); 81 setText(code);
88 } 82 }
89 } 83 }
90 84
91 public SourceIndex getSourceIndex() { 85 public SourceIndex getSourceIndex() {
92 return m_sourceIndex; 86 return sourceIndex;
93 } 87 }
94 88
95 public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator) { 89 public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator) {
@@ -110,33 +104,31 @@ public class CodeReader extends JEditorPane {
110 setCode("(decompiling...)"); 104 setCode("(decompiling...)");
111 105
112 // run decompilation in a separate thread to keep ui responsive 106 // run decompilation in a separate thread to keep ui responsive
113 new Thread() { 107 new Thread(() ->
114 @Override 108 {
115 public void run() {
116 109
117 // decompile it 110 // decompile it
118 CompilationUnit sourceTree = deobfuscator.getSourceTree(classEntry.getOutermostClassName()); 111 CompilationUnit sourceTree = deobfuscator.getSourceTree(classEntry.getOutermostClassName());
119 String source = deobfuscator.getSource(sourceTree); 112 String source = deobfuscator.getSource(sourceTree);
120 setCode(source); 113 setCode(source);
121 m_sourceIndex = deobfuscator.getSourceIndex(sourceTree, source, ignoreBadTokens); 114 sourceIndex = deobfuscator.getSourceIndex(sourceTree, source, ignoreBadTokens);
122 115
123 if (callback != null) { 116 if (callback != null) {
124 callback.run(); 117 callback.run();
125 }
126 } 118 }
127 }.start(); 119 }).start();
128 } 120 }
129 121
130 public void navigateToClassDeclaration(ClassEntry classEntry) { 122 public void navigateToClassDeclaration(ClassEntry classEntry) {
131 123
132 // navigate to the class declaration 124 // navigate to the class declaration
133 Token token = m_sourceIndex.getDeclarationToken(classEntry); 125 Token token = sourceIndex.getDeclarationToken(classEntry);
134 if (token == null) { 126 if (token == null) {
135 // couldn't find the class declaration token, might be an anonymous class 127 // couldn't find the class declaration token, might be an anonymous class
136 // look for any declaration in that class instead 128 // look for any declaration in that class instead
137 for (Entry entry : m_sourceIndex.declarations()) { 129 for (Entry entry : sourceIndex.declarations()) {
138 if (entry.getClassEntry().equals(classEntry)) { 130 if (entry.getClassEntry().equals(classEntry)) {
139 token = m_sourceIndex.getDeclarationToken(entry); 131 token = sourceIndex.getDeclarationToken(entry);
140 break; 132 break;
141 } 133 }
142 } 134 }
@@ -151,7 +143,7 @@ public class CodeReader extends JEditorPane {
151 } 143 }
152 144
153 public void navigateToToken(final Token token) { 145 public void navigateToToken(final Token token) {
154 navigateToToken(this, token, m_selectionHighlightPainter); 146 navigateToToken(this, token, selectionHighlightPainter);
155 } 147 }
156 148
157 // HACKHACK: someday we can update the main GUI to use this code reader 149 // HACKHACK: someday we can update the main GUI to use this code reader
@@ -167,34 +159,29 @@ public class CodeReader extends JEditorPane {
167 Rectangle end = editor.modelToView(token.end); 159 Rectangle end = editor.modelToView(token.end);
168 final Rectangle show = start.union(end); 160 final Rectangle show = start.union(end);
169 show.grow(start.width * 10, start.height * 6); 161 show.grow(start.width * 10, start.height * 6);
170 SwingUtilities.invokeLater(new Runnable() { 162 SwingUtilities.invokeLater(() -> editor.scrollRectToVisible(show));
171 @Override
172 public void run() {
173 editor.scrollRectToVisible(show);
174 }
175 });
176 } catch (BadLocationException ex) { 163 } catch (BadLocationException ex) {
177 throw new Error(ex); 164 throw new Error(ex);
178 } 165 }
179 166
180 // highlight the token momentarily 167 // highlight the token momentarily
181 final Timer timer = new Timer(200, new ActionListener() { 168 final Timer timer = new Timer(200, new ActionListener() {
182 private int m_counter = 0; 169 private int counter = 0;
183 private Object m_highlight = null; 170 private Object highlight = null;
184 171
185 @Override 172 @Override
186 public void actionPerformed(ActionEvent event) { 173 public void actionPerformed(ActionEvent event) {
187 if (m_counter % 2 == 0) { 174 if (counter % 2 == 0) {
188 try { 175 try {
189 m_highlight = editor.getHighlighter().addHighlight(token.start, token.end, highlightPainter); 176 highlight = editor.getHighlighter().addHighlight(token.start, token.end, highlightPainter);
190 } catch (BadLocationException ex) { 177 } catch (BadLocationException ex) {
191 // don't care 178 // don't care
192 } 179 }
193 } else if (m_highlight != null) { 180 } else if (highlight != null) {
194 editor.getHighlighter().removeHighlight(m_highlight); 181 editor.getHighlighter().removeHighlight(highlight);
195 } 182 }
196 183
197 if (m_counter++ > 6) { 184 if (counter++ > 6) {
198 Timer timer = (Timer) event.getSource(); 185 Timer timer = (Timer) event.getSource();
199 timer.stop(); 186 timer.stop();
200 } 187 }