summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cuchaz/enigma/analysis/SourceIndex.java2
-rw-r--r--src/cuchaz/enigma/gui/BoxHighlightPainter.java23
-rw-r--r--src/cuchaz/enigma/gui/Gui.java61
-rw-r--r--src/cuchaz/enigma/gui/SelectionHighlightPainter.java35
4 files changed, 109 insertions, 12 deletions
diff --git a/src/cuchaz/enigma/analysis/SourceIndex.java b/src/cuchaz/enigma/analysis/SourceIndex.java
index 960ec36..1a5a80d 100644
--- a/src/cuchaz/enigma/analysis/SourceIndex.java
+++ b/src/cuchaz/enigma/analysis/SourceIndex.java
@@ -117,7 +117,7 @@ public class SourceIndex
117 public Token getReferenceToken( int pos ) 117 public Token getReferenceToken( int pos )
118 { 118 {
119 Token token = m_tokenToReference.floorKey( new Token( pos, pos ) ); 119 Token token = m_tokenToReference.floorKey( new Token( pos, pos ) );
120 if( token.contains( pos ) ) 120 if( token != null && token.contains( pos ) )
121 { 121 {
122 return token; 122 return token;
123 } 123 }
diff --git a/src/cuchaz/enigma/gui/BoxHighlightPainter.java b/src/cuchaz/enigma/gui/BoxHighlightPainter.java
index b9474ff..2c11834 100644
--- a/src/cuchaz/enigma/gui/BoxHighlightPainter.java
+++ b/src/cuchaz/enigma/gui/BoxHighlightPainter.java
@@ -33,10 +33,23 @@ public abstract class BoxHighlightPainter implements Highlighter.HighlightPainte
33 @Override 33 @Override
34 public void paint( Graphics g, int start, int end, Shape shape, JTextComponent text ) 34 public void paint( Graphics g, int start, int end, Shape shape, JTextComponent text )
35 { 35 {
36 Rectangle bounds = getBounds( text, start, end );
37
38 // fill the area
39 g.setColor( m_fillColor );
40 g.fillRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 );
41
42 // draw a box around the area
43 g.setColor( m_borderColor );
44 g.drawRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 );
45 }
46
47 protected static Rectangle getBounds( JTextComponent text, int start, int end )
48 {
36 try 49 try
37 { 50 {
38 // determine the bounds of the text 51 // determine the bounds of the text
39 Rectangle bounds = text.getUI().modelToView( text, start ).union( text.getUI().modelToView( text, end ) ); 52 Rectangle bounds = text.modelToView( start ).union( text.modelToView( end ) );
40 53
41 // adjust the box so it looks nice 54 // adjust the box so it looks nice
42 bounds.x -= 2; 55 bounds.x -= 2;
@@ -44,13 +57,7 @@ public abstract class BoxHighlightPainter implements Highlighter.HighlightPainte
44 bounds.y += 1; 57 bounds.y += 1;
45 bounds.height -= 2; 58 bounds.height -= 2;
46 59
47 // fill the area 60 return bounds;
48 g.setColor( m_fillColor );
49 g.fillRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 );
50
51 // draw a box around the area
52 g.setColor( m_borderColor );
53 g.drawRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 );
54 } 61 }
55 catch( BadLocationException ex ) 62 catch( BadLocationException ex )
56 { 63 {
diff --git a/src/cuchaz/enigma/gui/Gui.java b/src/cuchaz/enigma/gui/Gui.java
index cd0fac7..914359b 100644
--- a/src/cuchaz/enigma/gui/Gui.java
+++ b/src/cuchaz/enigma/gui/Gui.java
@@ -17,6 +17,7 @@ import java.awt.Dimension;
17import java.awt.FlowLayout; 17import java.awt.FlowLayout;
18import java.awt.Font; 18import java.awt.Font;
19import java.awt.GridLayout; 19import java.awt.GridLayout;
20import java.awt.Rectangle;
20import java.awt.event.ActionEvent; 21import java.awt.event.ActionEvent;
21import java.awt.event.ActionListener; 22import java.awt.event.ActionListener;
22import java.awt.event.InputEvent; 23import java.awt.event.InputEvent;
@@ -53,6 +54,7 @@ import javax.swing.JTextField;
53import javax.swing.JTree; 54import javax.swing.JTree;
54import javax.swing.KeyStroke; 55import javax.swing.KeyStroke;
55import javax.swing.ListSelectionModel; 56import javax.swing.ListSelectionModel;
57import javax.swing.Timer;
56import javax.swing.WindowConstants; 58import javax.swing.WindowConstants;
57import javax.swing.event.CaretEvent; 59import javax.swing.event.CaretEvent;
58import javax.swing.event.CaretListener; 60import javax.swing.event.CaretListener;
@@ -142,8 +144,9 @@ public class Gui
142 private JList<String> m_deobfClasses; 144 private JList<String> m_deobfClasses;
143 private JEditorPane m_editor; 145 private JEditorPane m_editor;
144 private JPanel m_infoPanel; 146 private JPanel m_infoPanel;
145 private BoxHighlightPainter m_obfuscatedHighlightPainter; 147 private ObfuscatedHighlightPainter m_obfuscatedHighlightPainter;
146 private BoxHighlightPainter m_deobfuscatedHighlightPainter; 148 private DeobfuscatedHighlightPainter m_deobfuscatedHighlightPainter;
149 private SelectionHighlightPainter m_selectionHighlightPainter;
147 private JTree m_inheritanceTree; 150 private JTree m_inheritanceTree;
148 private JTree m_callsTree; 151 private JTree m_callsTree;
149 private JList<Token> m_tokens; 152 private JList<Token> m_tokens;
@@ -250,6 +253,7 @@ public class Gui
250 DefaultSyntaxKit.initKit(); 253 DefaultSyntaxKit.initKit();
251 m_obfuscatedHighlightPainter = new ObfuscatedHighlightPainter(); 254 m_obfuscatedHighlightPainter = new ObfuscatedHighlightPainter();
252 m_deobfuscatedHighlightPainter = new DeobfuscatedHighlightPainter(); 255 m_deobfuscatedHighlightPainter = new DeobfuscatedHighlightPainter();
256 m_selectionHighlightPainter = new SelectionHighlightPainter();
253 m_editor = new JEditorPane(); 257 m_editor = new JEditorPane();
254 m_editor.setEditable( false ); 258 m_editor.setEditable( false );
255 m_editor.setCaret( new BrowserCaret() ); 259 m_editor.setCaret( new BrowserCaret() );
@@ -756,14 +760,64 @@ public class Gui
756 m_editor.setText( source ); 760 m_editor.setText( source );
757 } 761 }
758 762
759 public void showToken( Token token ) 763 public void showToken( final Token token )
760 { 764 {
761 if( token == null ) 765 if( token == null )
762 { 766 {
763 throw new IllegalArgumentException( "Token cannot be null!" ); 767 throw new IllegalArgumentException( "Token cannot be null!" );
764 } 768 }
769
770 // set the caret position to the token
765 m_editor.setCaretPosition( token.start ); 771 m_editor.setCaretPosition( token.start );
766 m_editor.grabFocus(); 772 m_editor.grabFocus();
773
774 try
775 {
776 // make sure the token is visible in the scroll window
777 Rectangle start = m_editor.modelToView( token.start );
778 Rectangle end = m_editor.modelToView( token.end );
779 Rectangle show = start.union( end );
780 show.grow( 0, start.height*6 );
781 m_editor.scrollRectToVisible( show );
782 }
783 catch( BadLocationException ex )
784 {
785 throw new Error( ex );
786 }
787
788 // highlight the token momentarily
789 final Timer timer = new Timer( 200, new ActionListener( )
790 {
791 private int m_counter = 0;
792 private Object m_highlight = null;
793
794 @Override
795 public void actionPerformed( ActionEvent event )
796 {
797 if( m_counter % 2 == 0 )
798 {
799 try
800 {
801 m_highlight = m_editor.getHighlighter().addHighlight( token.start, token.end, m_selectionHighlightPainter );
802 }
803 catch( BadLocationException ex )
804 {
805 // don't care
806 }
807 }
808 else if( m_highlight != null )
809 {
810 m_editor.getHighlighter().removeHighlight( m_highlight );
811 }
812
813 if( m_counter++ > 6 )
814 {
815 Timer timer = (Timer)event.getSource();
816 timer.stop();
817 }
818 }
819 } );
820 timer.start();
767 } 821 }
768 822
769 public void showTokens( Collection<Token> tokens ) 823 public void showTokens( Collection<Token> tokens )
@@ -790,6 +844,7 @@ public class Gui
790 // remove any old highlighters 844 // remove any old highlighters
791 m_editor.getHighlighter().removeAllHighlights(); 845 m_editor.getHighlighter().removeAllHighlights();
792 846
847
793 // color things based on the index 848 // color things based on the index
794 if( obfuscatedTokens != null ) 849 if( obfuscatedTokens != null )
795 { 850 {
diff --git a/src/cuchaz/enigma/gui/SelectionHighlightPainter.java b/src/cuchaz/enigma/gui/SelectionHighlightPainter.java
new file mode 100644
index 0000000..35f9451
--- /dev/null
+++ b/src/cuchaz/enigma/gui/SelectionHighlightPainter.java
@@ -0,0 +1,35 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.gui;
12
13import java.awt.BasicStroke;
14import java.awt.Color;
15import java.awt.Graphics;
16import java.awt.Graphics2D;
17import java.awt.Rectangle;
18import java.awt.Shape;
19
20import javax.swing.text.Highlighter;
21import javax.swing.text.JTextComponent;
22
23public class SelectionHighlightPainter implements Highlighter.HighlightPainter
24{
25 @Override
26 public void paint( Graphics g, int start, int end, Shape shape, JTextComponent text )
27 {
28 // draw a thick border
29 Graphics2D g2d = (Graphics2D)g;
30 Rectangle bounds = BoxHighlightPainter.getBounds( text, start, end );
31 g2d.setColor( Color.black );
32 g2d.setStroke( new BasicStroke( 2.0f ) );
33 g2d.drawRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 );
34 }
35}