diff options
Diffstat (limited to 'src/cuchaz/enigma/gui/Gui.java')
| -rw-r--r-- | src/cuchaz/enigma/gui/Gui.java | 228 |
1 files changed, 206 insertions, 22 deletions
diff --git a/src/cuchaz/enigma/gui/Gui.java b/src/cuchaz/enigma/gui/Gui.java index e0d53d8..d1a3cb2 100644 --- a/src/cuchaz/enigma/gui/Gui.java +++ b/src/cuchaz/enigma/gui/Gui.java | |||
| @@ -12,11 +12,20 @@ package cuchaz.enigma.gui; | |||
| 12 | 12 | ||
| 13 | import java.awt.BorderLayout; | 13 | import java.awt.BorderLayout; |
| 14 | import java.awt.Container; | 14 | import java.awt.Container; |
| 15 | import java.awt.Dimension; | ||
| 16 | import java.awt.FlowLayout; | ||
| 17 | import java.awt.Font; | ||
| 18 | import java.awt.GridLayout; | ||
| 19 | import java.awt.event.ActionEvent; | ||
| 20 | import java.awt.event.ActionListener; | ||
| 15 | import java.awt.event.MouseAdapter; | 21 | import java.awt.event.MouseAdapter; |
| 16 | import java.awt.event.MouseEvent; | 22 | import java.awt.event.MouseEvent; |
| 17 | import java.util.List; | 23 | import java.util.List; |
| 18 | import java.util.Vector; | 24 | import java.util.Vector; |
| 19 | 25 | ||
| 26 | import javax.swing.BorderFactory; | ||
| 27 | import javax.swing.BoxLayout; | ||
| 28 | import javax.swing.JButton; | ||
| 20 | import javax.swing.JEditorPane; | 29 | import javax.swing.JEditorPane; |
| 21 | import javax.swing.JFrame; | 30 | import javax.swing.JFrame; |
| 22 | import javax.swing.JLabel; | 31 | import javax.swing.JLabel; |
| @@ -24,14 +33,21 @@ import javax.swing.JList; | |||
| 24 | import javax.swing.JPanel; | 33 | import javax.swing.JPanel; |
| 25 | import javax.swing.JScrollPane; | 34 | import javax.swing.JScrollPane; |
| 26 | import javax.swing.JSplitPane; | 35 | import javax.swing.JSplitPane; |
| 36 | import javax.swing.JTextField; | ||
| 27 | import javax.swing.ListSelectionModel; | 37 | import javax.swing.ListSelectionModel; |
| 28 | import javax.swing.WindowConstants; | 38 | import javax.swing.WindowConstants; |
| 39 | import javax.swing.event.CaretListener; | ||
| 29 | import javax.swing.text.BadLocationException; | 40 | import javax.swing.text.BadLocationException; |
| 30 | 41 | ||
| 31 | import jsyntaxpane.DefaultSyntaxKit; | 42 | import jsyntaxpane.DefaultSyntaxKit; |
| 32 | import jsyntaxpane.Token; | 43 | import jsyntaxpane.Token; |
| 33 | import cuchaz.enigma.ClassFile; | 44 | import cuchaz.enigma.ClassFile; |
| 34 | import cuchaz.enigma.analysis.SourceIndex; | 45 | import cuchaz.enigma.analysis.SourceIndex; |
| 46 | import cuchaz.enigma.mapping.ArgumentEntry; | ||
| 47 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 48 | import cuchaz.enigma.mapping.Entry; | ||
| 49 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 50 | import cuchaz.enigma.mapping.MethodEntry; | ||
| 35 | 51 | ||
| 36 | public class Gui | 52 | public class Gui |
| 37 | { | 53 | { |
| @@ -42,11 +58,18 @@ public class Gui | |||
| 42 | private JList<ClassFile> m_obfClasses; | 58 | private JList<ClassFile> m_obfClasses; |
| 43 | private JList<ClassFile> m_deobfClasses; | 59 | private JList<ClassFile> m_deobfClasses; |
| 44 | private JEditorPane m_editor; | 60 | private JEditorPane m_editor; |
| 61 | private JPanel m_actionPanel; | ||
| 62 | private JPanel m_renamePanel; | ||
| 63 | private JLabel m_typeLabel; | ||
| 64 | private JTextField m_nameField; | ||
| 65 | private JButton m_renameButton; | ||
| 45 | 66 | ||
| 46 | // handlers | 67 | // listeners |
| 47 | private ClassSelectionHandler m_classSelectionHandler; | 68 | private ClassSelectionListener m_classSelectionListener; |
| 69 | private RenameListener m_renameListener; | ||
| 48 | 70 | ||
| 49 | private BoxHighlightPainter m_highlightPainter; | 71 | private BoxHighlightPainter m_highlightPainter; |
| 72 | private Entry m_selectedEntry; | ||
| 50 | 73 | ||
| 51 | public Gui( ) | 74 | public Gui( ) |
| 52 | { | 75 | { |
| @@ -66,12 +89,12 @@ public class Gui | |||
| 66 | { | 89 | { |
| 67 | if( event.getClickCount() == 2 ) | 90 | if( event.getClickCount() == 2 ) |
| 68 | { | 91 | { |
| 69 | if( m_classSelectionHandler != null ) | 92 | if( m_classSelectionListener != null ) |
| 70 | { | 93 | { |
| 71 | ClassFile selected = m_obfClasses.getSelectedValue(); | 94 | ClassFile selected = m_obfClasses.getSelectedValue(); |
| 72 | if( selected != null ) | 95 | if( selected != null ) |
| 73 | { | 96 | { |
| 74 | m_classSelectionHandler.classSelected( selected ); | 97 | m_classSelectionListener.classSelected( selected ); |
| 75 | } | 98 | } |
| 76 | } | 99 | } |
| 77 | } | 100 | } |
| @@ -93,6 +116,34 @@ public class Gui | |||
| 93 | deobfPanel.add( new JLabel( "De-obfuscated Classes" ), BorderLayout.NORTH ); | 116 | deobfPanel.add( new JLabel( "De-obfuscated Classes" ), BorderLayout.NORTH ); |
| 94 | deobfPanel.add( deobfScroller, BorderLayout.CENTER ); | 117 | deobfPanel.add( deobfScroller, BorderLayout.CENTER ); |
| 95 | 118 | ||
| 119 | // init action panel | ||
| 120 | m_actionPanel = new JPanel(); | ||
| 121 | m_actionPanel.setLayout( new BoxLayout( m_actionPanel, BoxLayout.Y_AXIS ) ); | ||
| 122 | m_actionPanel.setPreferredSize( new Dimension( 0, 120 ) ); | ||
| 123 | m_actionPanel.setBorder( BorderFactory.createTitledBorder( "Identifier Info" ) ); | ||
| 124 | m_nameField = new JTextField( 26 ); | ||
| 125 | m_renameButton = new JButton( "Rename" ); | ||
| 126 | m_renameButton.addActionListener( new ActionListener( ) | ||
| 127 | { | ||
| 128 | @Override | ||
| 129 | public void actionPerformed( ActionEvent event ) | ||
| 130 | { | ||
| 131 | if( m_renameListener != null && m_selectedEntry != null ) | ||
| 132 | { | ||
| 133 | m_renameListener.rename( m_selectedEntry, m_nameField.getText() ); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } ); | ||
| 137 | m_renamePanel = new JPanel(); | ||
| 138 | m_renamePanel.setLayout( new FlowLayout( FlowLayout.LEFT, 6, 0 ) ); | ||
| 139 | m_typeLabel = new JLabel( "LongName:", JLabel.RIGHT ); | ||
| 140 | // NOTE: this looks ridiculous, but it fixes the label size to the size of current text | ||
| 141 | m_typeLabel.setPreferredSize( m_typeLabel.getPreferredSize() ); | ||
| 142 | m_renamePanel.add( m_typeLabel ); | ||
| 143 | m_renamePanel.add( m_nameField ); | ||
| 144 | m_renamePanel.add( m_renameButton ); | ||
| 145 | clearEntry(); | ||
| 146 | |||
| 96 | // init editor | 147 | // init editor |
| 97 | DefaultSyntaxKit.initKit(); | 148 | DefaultSyntaxKit.initKit(); |
| 98 | m_editor = new JEditorPane(); | 149 | m_editor = new JEditorPane(); |
| @@ -102,17 +153,23 @@ public class Gui | |||
| 102 | 153 | ||
| 103 | // layout controls | 154 | // layout controls |
| 104 | JSplitPane splitLeft = new JSplitPane( JSplitPane.VERTICAL_SPLIT, true, obfPanel, deobfPanel ); | 155 | JSplitPane splitLeft = new JSplitPane( JSplitPane.VERTICAL_SPLIT, true, obfPanel, deobfPanel ); |
| 105 | JSplitPane splitMain = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, true, splitLeft, sourceScroller ); | 156 | JPanel rightPanel = new JPanel(); |
| 157 | rightPanel.setLayout( new BorderLayout() ); | ||
| 158 | rightPanel.add( m_actionPanel, BorderLayout.NORTH ); | ||
| 159 | rightPanel.add( sourceScroller, BorderLayout.CENTER ); | ||
| 160 | JSplitPane splitMain = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, true, splitLeft, rightPanel ); | ||
| 106 | pane.add( splitMain, BorderLayout.CENTER ); | 161 | pane.add( splitMain, BorderLayout.CENTER ); |
| 107 | 162 | ||
| 108 | // show the frame | 163 | // show the frame |
| 109 | pane.doLayout(); | 164 | pane.doLayout(); |
| 110 | m_frame.setSize( 800, 600 ); | 165 | m_frame.setSize( 800, 600 ); |
| 166 | m_frame.setMinimumSize( new Dimension( 640, 480 ) ); | ||
| 111 | m_frame.setVisible( true ); | 167 | m_frame.setVisible( true ); |
| 112 | m_frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); | 168 | m_frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); |
| 113 | 169 | ||
| 114 | // init handlers | 170 | // init listeners |
| 115 | m_classSelectionHandler = null; | 171 | m_classSelectionListener = null; |
| 172 | m_renameListener = null; | ||
| 116 | 173 | ||
| 117 | m_highlightPainter = new BoxHighlightPainter(); | 174 | m_highlightPainter = new BoxHighlightPainter(); |
| 118 | } | 175 | } |
| @@ -135,29 +192,156 @@ public class Gui | |||
| 135 | public void setSource( String source, SourceIndex index ) | 192 | public void setSource( String source, SourceIndex index ) |
| 136 | { | 193 | { |
| 137 | m_editor.setText( source ); | 194 | m_editor.setText( source ); |
| 138 | 195 | } | |
| 196 | |||
| 197 | public void highlightTokens( Iterable<Token> tokens ) | ||
| 198 | { | ||
| 139 | // remove any old highlighters | 199 | // remove any old highlighters |
| 140 | m_editor.getHighlighter().removeAllHighlights();; | 200 | m_editor.getHighlighter().removeAllHighlights(); |
| 141 | 201 | ||
| 142 | if( index != null ) | 202 | if( tokens == null ) |
| 143 | { | 203 | { |
| 144 | // color things based on the index | 204 | return; |
| 145 | for( Token token : index.tokens() ) | 205 | } |
| 206 | |||
| 207 | // color things based on the index | ||
| 208 | for( Token token : tokens ) | ||
| 209 | { | ||
| 210 | try | ||
| 146 | { | 211 | { |
| 147 | try | 212 | m_editor.getHighlighter().addHighlight( token.start, token.end(), m_highlightPainter ); |
| 148 | { | 213 | } |
| 149 | m_editor.getHighlighter().addHighlight( token.start, token.end(), m_highlightPainter ); | 214 | catch( BadLocationException ex ) |
| 150 | } | 215 | { |
| 151 | catch( BadLocationException ex ) | 216 | throw new IllegalArgumentException( ex ); |
| 152 | { | ||
| 153 | throw new Error( ex ); | ||
| 154 | } | ||
| 155 | } | 217 | } |
| 156 | } | 218 | } |
| 219 | |||
| 220 | redraw(); | ||
| 221 | } | ||
| 222 | |||
| 223 | public void setClassSelectionListener( ClassSelectionListener val ) | ||
| 224 | { | ||
| 225 | m_classSelectionListener = val; | ||
| 226 | } | ||
| 227 | |||
| 228 | public void setRenameListener( RenameListener val ) | ||
| 229 | { | ||
| 230 | m_renameListener = val; | ||
| 231 | } | ||
| 232 | |||
| 233 | public void setCaretListener( CaretListener listener ) | ||
| 234 | { | ||
| 235 | // remove any old listeners | ||
| 236 | for( CaretListener oldListener : m_editor.getCaretListeners() ) | ||
| 237 | { | ||
| 238 | m_editor.removeCaretListener( oldListener ); | ||
| 239 | } | ||
| 240 | |||
| 241 | m_editor.addCaretListener( listener ); | ||
| 242 | } | ||
| 243 | |||
| 244 | public void clearEntry( ) | ||
| 245 | { | ||
| 246 | m_actionPanel.removeAll(); | ||
| 247 | JLabel label = new JLabel( "No identifier selected" ); | ||
| 248 | unboldLabel( label ); | ||
| 249 | label.setHorizontalAlignment( JLabel.CENTER ); | ||
| 250 | m_actionPanel.add( label ); | ||
| 251 | |||
| 252 | redraw(); | ||
| 253 | } | ||
| 254 | |||
| 255 | public void showEntry( Entry entry ) | ||
| 256 | { | ||
| 257 | if( entry == null ) | ||
| 258 | { | ||
| 259 | clearEntry(); | ||
| 260 | return; | ||
| 261 | } | ||
| 262 | |||
| 263 | // layout the action panel | ||
| 264 | m_actionPanel.removeAll(); | ||
| 265 | m_actionPanel.add( m_renamePanel ); | ||
| 266 | m_nameField.setText( entry.getName() ); | ||
| 267 | |||
| 268 | // layout the dynamic section | ||
| 269 | JPanel dynamicPanel = new JPanel(); | ||
| 270 | dynamicPanel.setLayout( new GridLayout( 3, 1, 0, 0 ) ); | ||
| 271 | m_actionPanel.add( dynamicPanel ); | ||
| 272 | if( entry instanceof ClassEntry ) | ||
| 273 | { | ||
| 274 | showEntry( (ClassEntry)entry, dynamicPanel ); | ||
| 275 | } | ||
| 276 | else if( entry instanceof FieldEntry ) | ||
| 277 | { | ||
| 278 | showEntry( (FieldEntry)entry, dynamicPanel ); | ||
| 279 | } | ||
| 280 | else if( entry instanceof MethodEntry ) | ||
| 281 | { | ||
| 282 | showEntry( (MethodEntry)entry, dynamicPanel ); | ||
| 283 | } | ||
| 284 | else if( entry instanceof ArgumentEntry ) | ||
| 285 | { | ||
| 286 | showEntry( (ArgumentEntry)entry, dynamicPanel ); | ||
| 287 | } | ||
| 288 | else | ||
| 289 | { | ||
| 290 | throw new Error( "Unknown entry type: " + entry.getClass().getName() ); | ||
| 291 | } | ||
| 292 | |||
| 293 | redraw(); | ||
| 294 | } | ||
| 295 | |||
| 296 | public void showEntry( ClassEntry entry, JPanel panel ) | ||
| 297 | { | ||
| 298 | m_typeLabel.setText( "Class: " ); | ||
| 299 | } | ||
| 300 | |||
| 301 | public void showEntry( FieldEntry entry, JPanel panel ) | ||
| 302 | { | ||
| 303 | m_typeLabel.setText( "Field: " ); | ||
| 304 | addNameValue( panel, "Class", entry.getClassEntry().getName() ); | ||
| 305 | } | ||
| 306 | |||
| 307 | public void showEntry( MethodEntry entry, JPanel panel ) | ||
| 308 | { | ||
| 309 | m_typeLabel.setText( "Method: " ); | ||
| 310 | addNameValue( panel, "Class", entry.getClassEntry().getName() ); | ||
| 311 | addNameValue( panel, "Signature", entry.getSignature() ); | ||
| 312 | } | ||
| 313 | |||
| 314 | public void showEntry( ArgumentEntry entry, JPanel panel ) | ||
| 315 | { | ||
| 316 | m_typeLabel.setText( "Argument: " ); | ||
| 317 | addNameValue( panel, "Class", entry.getMethodEntry().getClassEntry().getName() ); | ||
| 318 | addNameValue( panel, "Method", entry.getMethodEntry().getName() ); | ||
| 319 | addNameValue( panel, "Index", Integer.toString( entry.getIndex() ) ); | ||
| 320 | } | ||
| 321 | |||
| 322 | private void addNameValue( JPanel container, String name, String value ) | ||
| 323 | { | ||
| 324 | JPanel panel = new JPanel(); | ||
| 325 | panel.setLayout( new FlowLayout( FlowLayout.LEFT, 6, 0 ) ); | ||
| 326 | container.add( panel ); | ||
| 327 | |||
| 328 | JLabel label = new JLabel( name + ":", JLabel.RIGHT ); | ||
| 329 | label.setPreferredSize( new Dimension( 80, label.getPreferredSize().height ) ); | ||
| 330 | panel.add( label ); | ||
| 331 | |||
| 332 | panel.add( unboldLabel( new JLabel( value, JLabel.LEFT ) ) ); | ||
| 333 | } | ||
| 334 | |||
| 335 | private JLabel unboldLabel( JLabel label ) | ||
| 336 | { | ||
| 337 | Font font = label.getFont(); | ||
| 338 | label.setFont( font.deriveFont( font.getStyle() & ~Font.BOLD ) ); | ||
| 339 | return label; | ||
| 157 | } | 340 | } |
| 158 | 341 | ||
| 159 | public void setClassSelectionHandler( ClassSelectionHandler val ) | 342 | private void redraw( ) |
| 160 | { | 343 | { |
| 161 | m_classSelectionHandler = val; | 344 | m_frame.validate(); |
| 345 | m_frame.repaint(); | ||
| 162 | } | 346 | } |
| 163 | } | 347 | } |