diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/CodeReader.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/gui/CodeReader.java | 66 |
1 files changed, 23 insertions, 43 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/CodeReader.java b/src/main/java/cuchaz/enigma/gui/CodeReader.java index 93f9a75..a476fa5 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; | |||
| 19 | import javax.swing.JEditorPane; | 19 | import javax.swing.JEditorPane; |
| 20 | import javax.swing.SwingUtilities; | 20 | import javax.swing.SwingUtilities; |
| 21 | import javax.swing.Timer; | 21 | import javax.swing.Timer; |
| 22 | import javax.swing.event.CaretEvent; | ||
| 23 | import javax.swing.event.CaretListener; | ||
| 24 | import javax.swing.text.BadLocationException; | 22 | import javax.swing.text.BadLocationException; |
| 25 | import javax.swing.text.Highlighter.HighlightPainter; | 23 | import javax.swing.text.Highlighter.HighlightPainter; |
| 26 | 24 | ||
| @@ -37,15 +35,15 @@ public class CodeReader extends JEditorPane { | |||
| 37 | 35 | ||
| 38 | private static final long serialVersionUID = 3673180950485748810L; | 36 | private static final long serialVersionUID = 3673180950485748810L; |
| 39 | 37 | ||
| 40 | private static final Object m_lock = new Object(); | 38 | private static final Object lock = new Object(); |
| 41 | 39 | ||
| 42 | public interface SelectionListener { | 40 | public interface SelectionListener { |
| 43 | void onSelect(EntryReference<Entry, Entry> reference); | 41 | void onSelect(EntryReference<Entry, Entry> reference); |
| 44 | } | 42 | } |
| 45 | 43 | ||
| 46 | private SelectionHighlightPainter m_selectionHighlightPainter; | 44 | private SelectionHighlightPainter selectionHighlightPainter; |
| 47 | private SourceIndex m_sourceIndex; | 45 | private SourceIndex sourceIndex; |
| 48 | private SelectionListener m_selectionListener; | 46 | private SelectionListener selectionListener; |
| 49 | 47 | ||
| 50 | public CodeReader() { | 48 | public CodeReader() { |
| 51 | 49 | ||
| @@ -57,42 +55,35 @@ public class CodeReader extends JEditorPane { | |||
| 57 | kit.toggleComponent(this, "de.sciss.syntaxpane.components.TokenMarker"); | 55 | kit.toggleComponent(this, "de.sciss.syntaxpane.components.TokenMarker"); |
| 58 | 56 | ||
| 59 | // hook events | 57 | // hook events |
| 60 | addCaretListener(new CaretListener() { | 58 | addCaretListener(event -> { |
| 61 | @Override | 59 | if (this.selectionListener != null && this.sourceIndex != null) { |
| 62 | public void caretUpdate(CaretEvent event) { | 60 | Token token = this.sourceIndex.getReferenceToken(event.getDot()); |
| 63 | if (m_selectionListener != null && m_sourceIndex != null) { | 61 | if (token != null) { |
| 64 | Token token = m_sourceIndex.getReferenceToken(event.getDot()); | 62 | this.selectionListener.onSelect(this.sourceIndex.getDeobfReference(token)); |
| 65 | if (token != null) { | 63 | } else { |
| 66 | m_selectionListener.onSelect(m_sourceIndex.getDeobfReference(token)); | 64 | this.selectionListener.onSelect(null); |
| 67 | } else { | ||
| 68 | m_selectionListener.onSelect(null); | ||
| 69 | } | ||
| 70 | } | 65 | } |
| 71 | } | 66 | } |
| 72 | }); | 67 | }); |
| 73 | 68 | ||
| 74 | m_selectionHighlightPainter = new SelectionHighlightPainter(); | 69 | this.selectionHighlightPainter = new SelectionHighlightPainter(); |
| 75 | m_sourceIndex = null; | 70 | this.sourceIndex = null; |
| 76 | m_selectionListener = null; | 71 | this.selectionListener = null; |
| 77 | } | 72 | } |
| 78 | 73 | ||
| 79 | public void setSelectionListener(SelectionListener val) { | 74 | public void setSelectionListener(SelectionListener val) { |
| 80 | m_selectionListener = val; | 75 | this.selectionListener = val; |
| 81 | } | 76 | } |
| 82 | 77 | ||
| 83 | public void setCode(String code) { | 78 | public void setCode(String code) { |
| 84 | // 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 |
| 85 | synchronized (m_lock) { | 80 | synchronized (lock) { |
| 86 | setText(code); | 81 | setText(code); |
| 87 | } | 82 | } |
| 88 | } | 83 | } |
| 89 | 84 | ||
| 90 | public SourceIndex getSourceIndex() { | 85 | public SourceIndex getSourceIndex() { |
| 91 | return m_sourceIndex; | 86 | return this.sourceIndex; |
| 92 | } | ||
| 93 | |||
| 94 | public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator) { | ||
| 95 | decompileClass(classEntry, deobfuscator, null); | ||
| 96 | } | 87 | } |
| 97 | 88 | ||
| 98 | public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator, Runnable callback) { | 89 | public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator, Runnable callback) { |
| @@ -117,7 +108,7 @@ public class CodeReader extends JEditorPane { | |||
| 117 | CompilationUnit sourceTree = deobfuscator.getSourceTree(classEntry.getOutermostClassName()); | 108 | CompilationUnit sourceTree = deobfuscator.getSourceTree(classEntry.getOutermostClassName()); |
| 118 | String source = deobfuscator.getSource(sourceTree); | 109 | String source = deobfuscator.getSource(sourceTree); |
| 119 | setCode(source); | 110 | setCode(source); |
| 120 | m_sourceIndex = deobfuscator.getSourceIndex(sourceTree, source, ignoreBadTokens); | 111 | sourceIndex = deobfuscator.getSourceIndex(sourceTree, source, ignoreBadTokens); |
| 121 | 112 | ||
| 122 | if (callback != null) { | 113 | if (callback != null) { |
| 123 | callback.run(); | 114 | callback.run(); |
| @@ -129,13 +120,13 @@ public class CodeReader extends JEditorPane { | |||
| 129 | public void navigateToClassDeclaration(ClassEntry classEntry) { | 120 | public void navigateToClassDeclaration(ClassEntry classEntry) { |
| 130 | 121 | ||
| 131 | // navigate to the class declaration | 122 | // navigate to the class declaration |
| 132 | Token token = m_sourceIndex.getDeclarationToken(classEntry); | 123 | Token token = this.sourceIndex.getDeclarationToken(classEntry); |
| 133 | if (token == null) { | 124 | if (token == null) { |
| 134 | // couldn't find the class declaration token, might be an anonymous class | 125 | // couldn't find the class declaration token, might be an anonymous class |
| 135 | // look for any declaration in that class instead | 126 | // look for any declaration in that class instead |
| 136 | for (Entry entry : m_sourceIndex.declarations()) { | 127 | for (Entry entry : this.sourceIndex.declarations()) { |
| 137 | if (entry.getClassEntry().equals(classEntry)) { | 128 | if (entry.getClassEntry().equals(classEntry)) { |
| 138 | token = m_sourceIndex.getDeclarationToken(entry); | 129 | token = this.sourceIndex.getDeclarationToken(entry); |
| 139 | break; | 130 | break; |
| 140 | } | 131 | } |
| 141 | } | 132 | } |
| @@ -150,7 +141,7 @@ public class CodeReader extends JEditorPane { | |||
| 150 | } | 141 | } |
| 151 | 142 | ||
| 152 | public void navigateToToken(final Token token) { | 143 | public void navigateToToken(final Token token) { |
| 153 | navigateToToken(this, token, m_selectionHighlightPainter); | 144 | navigateToToken(this, token, this.selectionHighlightPainter); |
| 154 | } | 145 | } |
| 155 | 146 | ||
| 156 | // HACKHACK: someday we can update the main GUI to use this code reader | 147 | // HACKHACK: someday we can update the main GUI to use this code reader |
| @@ -166,12 +157,7 @@ public class CodeReader extends JEditorPane { | |||
| 166 | Rectangle end = editor.modelToView(token.end); | 157 | Rectangle end = editor.modelToView(token.end); |
| 167 | final Rectangle show = start.union(end); | 158 | final Rectangle show = start.union(end); |
| 168 | show.grow(start.width * 10, start.height * 6); | 159 | show.grow(start.width * 10, start.height * 6); |
| 169 | SwingUtilities.invokeLater(new Runnable() { | 160 | SwingUtilities.invokeLater(() -> editor.scrollRectToVisible(show)); |
| 170 | @Override | ||
| 171 | public void run() { | ||
| 172 | editor.scrollRectToVisible(show); | ||
| 173 | } | ||
| 174 | }); | ||
| 175 | } catch (BadLocationException ex) { | 161 | } catch (BadLocationException ex) { |
| 176 | throw new Error(ex); | 162 | throw new Error(ex); |
| 177 | } | 163 | } |
| @@ -202,12 +188,6 @@ public class CodeReader extends JEditorPane { | |||
| 202 | timer.start(); | 188 | timer.start(); |
| 203 | } | 189 | } |
| 204 | 190 | ||
| 205 | public void setHighlightedTokens(Iterable<Token> tokens, HighlightPainter painter) { | ||
| 206 | for (Token token : tokens) { | ||
| 207 | setHighlightedToken(token, painter); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | public void setHighlightedToken(Token token, HighlightPainter painter) { | 191 | public void setHighlightedToken(Token token, HighlightPainter painter) { |
| 212 | try { | 192 | try { |
| 213 | getHighlighter().addHighlight(token.start, token.end, painter); | 193 | getHighlighter().addHighlight(token.start, token.end, painter); |