From 32b7ea70ff20d3584f8021e598141c20c2200398 Mon Sep 17 00:00:00 2001 From: jeff Date: Fri, 22 Aug 2014 01:25:52 -0400 Subject: added show token effects --- src/cuchaz/enigma/analysis/SourceIndex.java | 2 +- src/cuchaz/enigma/gui/BoxHighlightPainter.java | 23 +++++--- src/cuchaz/enigma/gui/Gui.java | 61 ++++++++++++++++++++-- .../enigma/gui/SelectionHighlightPainter.java | 35 +++++++++++++ 4 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 src/cuchaz/enigma/gui/SelectionHighlightPainter.java (limited to 'src') diff --git a/src/cuchaz/enigma/analysis/SourceIndex.java b/src/cuchaz/enigma/analysis/SourceIndex.java index 960ec36f..1a5a80d6 100644 --- a/src/cuchaz/enigma/analysis/SourceIndex.java +++ b/src/cuchaz/enigma/analysis/SourceIndex.java @@ -117,7 +117,7 @@ public class SourceIndex public Token getReferenceToken( int pos ) { Token token = m_tokenToReference.floorKey( new Token( pos, pos ) ); - if( token.contains( pos ) ) + if( token != null && token.contains( pos ) ) { return token; } diff --git a/src/cuchaz/enigma/gui/BoxHighlightPainter.java b/src/cuchaz/enigma/gui/BoxHighlightPainter.java index b9474ff8..2c118340 100644 --- a/src/cuchaz/enigma/gui/BoxHighlightPainter.java +++ b/src/cuchaz/enigma/gui/BoxHighlightPainter.java @@ -32,11 +32,24 @@ public abstract class BoxHighlightPainter implements Highlighter.HighlightPainte @Override public void paint( Graphics g, int start, int end, Shape shape, JTextComponent text ) + { + Rectangle bounds = getBounds( text, start, end ); + + // fill the area + g.setColor( m_fillColor ); + g.fillRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 ); + + // draw a box around the area + g.setColor( m_borderColor ); + g.drawRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 ); + } + + protected static Rectangle getBounds( JTextComponent text, int start, int end ) { try { // determine the bounds of the text - Rectangle bounds = text.getUI().modelToView( text, start ).union( text.getUI().modelToView( text, end ) ); + Rectangle bounds = text.modelToView( start ).union( text.modelToView( end ) ); // adjust the box so it looks nice bounds.x -= 2; @@ -44,13 +57,7 @@ public abstract class BoxHighlightPainter implements Highlighter.HighlightPainte bounds.y += 1; bounds.height -= 2; - // fill the area - g.setColor( m_fillColor ); - g.fillRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 ); - - // draw a box around the area - g.setColor( m_borderColor ); - g.drawRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 ); + return bounds; } catch( BadLocationException ex ) { diff --git a/src/cuchaz/enigma/gui/Gui.java b/src/cuchaz/enigma/gui/Gui.java index cd0fac76..914359b4 100644 --- a/src/cuchaz/enigma/gui/Gui.java +++ b/src/cuchaz/enigma/gui/Gui.java @@ -17,6 +17,7 @@ import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; +import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; @@ -53,6 +54,7 @@ import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.KeyStroke; import javax.swing.ListSelectionModel; +import javax.swing.Timer; import javax.swing.WindowConstants; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; @@ -142,8 +144,9 @@ public class Gui private JList m_deobfClasses; private JEditorPane m_editor; private JPanel m_infoPanel; - private BoxHighlightPainter m_obfuscatedHighlightPainter; - private BoxHighlightPainter m_deobfuscatedHighlightPainter; + private ObfuscatedHighlightPainter m_obfuscatedHighlightPainter; + private DeobfuscatedHighlightPainter m_deobfuscatedHighlightPainter; + private SelectionHighlightPainter m_selectionHighlightPainter; private JTree m_inheritanceTree; private JTree m_callsTree; private JList m_tokens; @@ -250,6 +253,7 @@ public class Gui DefaultSyntaxKit.initKit(); m_obfuscatedHighlightPainter = new ObfuscatedHighlightPainter(); m_deobfuscatedHighlightPainter = new DeobfuscatedHighlightPainter(); + m_selectionHighlightPainter = new SelectionHighlightPainter(); m_editor = new JEditorPane(); m_editor.setEditable( false ); m_editor.setCaret( new BrowserCaret() ); @@ -756,14 +760,64 @@ public class Gui m_editor.setText( source ); } - public void showToken( Token token ) + public void showToken( final Token token ) { if( token == null ) { throw new IllegalArgumentException( "Token cannot be null!" ); } + + // set the caret position to the token m_editor.setCaretPosition( token.start ); m_editor.grabFocus(); + + try + { + // make sure the token is visible in the scroll window + Rectangle start = m_editor.modelToView( token.start ); + Rectangle end = m_editor.modelToView( token.end ); + Rectangle show = start.union( end ); + show.grow( 0, start.height*6 ); + m_editor.scrollRectToVisible( show ); + } + catch( BadLocationException ex ) + { + throw new Error( ex ); + } + + // highlight the token momentarily + final Timer timer = new Timer( 200, new ActionListener( ) + { + private int m_counter = 0; + private Object m_highlight = null; + + @Override + public void actionPerformed( ActionEvent event ) + { + if( m_counter % 2 == 0 ) + { + try + { + m_highlight = m_editor.getHighlighter().addHighlight( token.start, token.end, m_selectionHighlightPainter ); + } + catch( BadLocationException ex ) + { + // don't care + } + } + else if( m_highlight != null ) + { + m_editor.getHighlighter().removeHighlight( m_highlight ); + } + + if( m_counter++ > 6 ) + { + Timer timer = (Timer)event.getSource(); + timer.stop(); + } + } + } ); + timer.start(); } public void showTokens( Collection tokens ) @@ -790,6 +844,7 @@ public class Gui // remove any old highlighters m_editor.getHighlighter().removeAllHighlights(); + // color things based on the index if( obfuscatedTokens != null ) { diff --git a/src/cuchaz/enigma/gui/SelectionHighlightPainter.java b/src/cuchaz/enigma/gui/SelectionHighlightPainter.java new file mode 100644 index 00000000..35f94518 --- /dev/null +++ b/src/cuchaz/enigma/gui/SelectionHighlightPainter.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2014 Jeff Martin. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + * + * Contributors: + * Jeff Martin - initial API and implementation + ******************************************************************************/ +package cuchaz.enigma.gui; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.Shape; + +import javax.swing.text.Highlighter; +import javax.swing.text.JTextComponent; + +public class SelectionHighlightPainter implements Highlighter.HighlightPainter +{ + @Override + public void paint( Graphics g, int start, int end, Shape shape, JTextComponent text ) + { + // draw a thick border + Graphics2D g2d = (Graphics2D)g; + Rectangle bounds = BoxHighlightPainter.getBounds( text, start, end ); + g2d.setColor( Color.black ); + g2d.setStroke( new BasicStroke( 2.0f ) ); + g2d.drawRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 ); + } +} -- cgit v1.2.3