diff options
Diffstat (limited to 'src/cuchaz/enigma/gui/CodeReader.java')
| -rw-r--r-- | src/cuchaz/enigma/gui/CodeReader.java | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/src/cuchaz/enigma/gui/CodeReader.java b/src/cuchaz/enigma/gui/CodeReader.java index 05feb59..aa7e2db 100644 --- a/src/cuchaz/enigma/gui/CodeReader.java +++ b/src/cuchaz/enigma/gui/CodeReader.java | |||
| @@ -7,12 +7,15 @@ import java.awt.event.ActionListener; | |||
| 7 | import javax.swing.JEditorPane; | 7 | import javax.swing.JEditorPane; |
| 8 | import javax.swing.SwingUtilities; | 8 | import javax.swing.SwingUtilities; |
| 9 | import javax.swing.Timer; | 9 | import javax.swing.Timer; |
| 10 | import javax.swing.event.CaretEvent; | ||
| 11 | import javax.swing.event.CaretListener; | ||
| 10 | import javax.swing.text.BadLocationException; | 12 | import javax.swing.text.BadLocationException; |
| 11 | import javax.swing.text.Highlighter.HighlightPainter; | 13 | import javax.swing.text.Highlighter.HighlightPainter; |
| 12 | 14 | ||
| 13 | import com.strobel.decompiler.languages.java.ast.CompilationUnit; | 15 | import com.strobel.decompiler.languages.java.ast.CompilationUnit; |
| 14 | 16 | ||
| 15 | import cuchaz.enigma.Deobfuscator; | 17 | import cuchaz.enigma.Deobfuscator; |
| 18 | import cuchaz.enigma.analysis.EntryReference; | ||
| 16 | import cuchaz.enigma.analysis.SourceIndex; | 19 | import cuchaz.enigma.analysis.SourceIndex; |
| 17 | import cuchaz.enigma.analysis.Token; | 20 | import cuchaz.enigma.analysis.Token; |
| 18 | import cuchaz.enigma.mapping.ClassEntry; | 21 | import cuchaz.enigma.mapping.ClassEntry; |
| @@ -26,8 +29,13 @@ public class CodeReader extends JEditorPane { | |||
| 26 | 29 | ||
| 27 | private static final Object m_lock = new Object(); | 30 | private static final Object m_lock = new Object(); |
| 28 | 31 | ||
| 29 | private SelectionHighlightPainter m_highlightPainter; | 32 | public static interface SelectionListener { |
| 33 | void onSelect(EntryReference<Entry,Entry> reference); | ||
| 34 | } | ||
| 35 | |||
| 36 | private SelectionHighlightPainter m_selectionHighlightPainter; | ||
| 30 | private SourceIndex m_sourceIndex; | 37 | private SourceIndex m_sourceIndex; |
| 38 | private SelectionListener m_selectionListener; | ||
| 31 | 39 | ||
| 32 | public CodeReader() { | 40 | public CodeReader() { |
| 33 | 41 | ||
| @@ -38,8 +46,28 @@ public class CodeReader extends JEditorPane { | |||
| 38 | DefaultSyntaxKit kit = (DefaultSyntaxKit)getEditorKit(); | 46 | DefaultSyntaxKit kit = (DefaultSyntaxKit)getEditorKit(); |
| 39 | kit.toggleComponent(this, "de.sciss.syntaxpane.components.TokenMarker"); | 47 | kit.toggleComponent(this, "de.sciss.syntaxpane.components.TokenMarker"); |
| 40 | 48 | ||
| 41 | m_highlightPainter = new SelectionHighlightPainter(); | 49 | // hook events |
| 50 | addCaretListener(new CaretListener() { | ||
| 51 | @Override | ||
| 52 | public void caretUpdate(CaretEvent event) { | ||
| 53 | if (m_selectionListener != null && m_sourceIndex != null) { | ||
| 54 | Token token = m_sourceIndex.getReferenceToken(event.getDot()); | ||
| 55 | if (token != null) { | ||
| 56 | m_selectionListener.onSelect(m_sourceIndex.getDeobfReference(token)); | ||
| 57 | } else { | ||
| 58 | m_selectionListener.onSelect(null); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | }); | ||
| 63 | |||
| 64 | m_selectionHighlightPainter = new SelectionHighlightPainter(); | ||
| 42 | m_sourceIndex = null; | 65 | m_sourceIndex = null; |
| 66 | m_selectionListener = null; | ||
| 67 | } | ||
| 68 | |||
| 69 | public void setSelectionListener(SelectionListener val) { | ||
| 70 | m_selectionListener = val; | ||
| 43 | } | 71 | } |
| 44 | 72 | ||
| 45 | public void setCode(String code) { | 73 | public void setCode(String code) { |
| @@ -49,6 +77,10 @@ public class CodeReader extends JEditorPane { | |||
| 49 | } | 77 | } |
| 50 | } | 78 | } |
| 51 | 79 | ||
| 80 | public SourceIndex getSourceIndex() { | ||
| 81 | return m_sourceIndex; | ||
| 82 | } | ||
| 83 | |||
| 52 | public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator) { | 84 | public void decompileClass(ClassEntry classEntry, Deobfuscator deobfuscator) { |
| 53 | decompileClass(classEntry, deobfuscator, null); | 85 | decompileClass(classEntry, deobfuscator, null); |
| 54 | } | 86 | } |
| @@ -110,7 +142,7 @@ public class CodeReader extends JEditorPane { | |||
| 110 | } | 142 | } |
| 111 | 143 | ||
| 112 | public void navigateToToken(final Token token) { | 144 | public void navigateToToken(final Token token) { |
| 113 | navigateToToken(this, token, m_highlightPainter); | 145 | navigateToToken(this, token, m_selectionHighlightPainter); |
| 114 | } | 146 | } |
| 115 | 147 | ||
| 116 | // HACKHACK: someday we can update the main GUI to use this code reader | 148 | // HACKHACK: someday we can update the main GUI to use this code reader |
| @@ -161,4 +193,22 @@ public class CodeReader extends JEditorPane { | |||
| 161 | }); | 193 | }); |
| 162 | timer.start(); | 194 | timer.start(); |
| 163 | } | 195 | } |
| 196 | |||
| 197 | public void setHighlightedTokens(Iterable<Token> tokens, HighlightPainter painter) { | ||
| 198 | for (Token token : tokens) { | ||
| 199 | setHighlightedToken(token, painter); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | public void setHighlightedToken(Token token, HighlightPainter painter) { | ||
| 204 | try { | ||
| 205 | getHighlighter().addHighlight(token.start, token.end, painter); | ||
| 206 | } catch (BadLocationException ex) { | ||
| 207 | throw new IllegalArgumentException(ex); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | public void clearHighlights() { | ||
| 212 | getHighlighter().removeAllHighlights(); | ||
| 213 | } | ||
| 164 | } | 214 | } |