summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/gui/CodeReader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/CodeReader.java')
-rw-r--r--src/main/java/cuchaz/enigma/gui/CodeReader.java73
1 files changed, 0 insertions, 73 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/CodeReader.java b/src/main/java/cuchaz/enigma/gui/CodeReader.java
deleted file mode 100644
index e119640..0000000
--- a/src/main/java/cuchaz/enigma/gui/CodeReader.java
+++ /dev/null
@@ -1,73 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11
12package cuchaz.enigma.gui;
13
14import cuchaz.enigma.analysis.Token;
15
16import javax.swing.*;
17import javax.swing.text.BadLocationException;
18import javax.swing.text.Document;
19import javax.swing.text.Highlighter.HighlightPainter;
20import java.awt.*;
21import java.awt.event.ActionEvent;
22import java.awt.event.ActionListener;
23
24public class CodeReader extends JEditorPane {
25 private static final long serialVersionUID = 3673180950485748810L;
26
27 // HACKHACK: someday we can update the main GUI to use this code reader
28 public static void navigateToToken(final JEditorPane editor, final Token token, final HighlightPainter highlightPainter) {
29
30 // set the caret position to the token
31 Document document = editor.getDocument();
32 int clampedPosition = Math.min(Math.max(token.start, 0), document.getLength());
33
34 editor.setCaretPosition(clampedPosition);
35 editor.grabFocus();
36
37 try {
38 // make sure the token is visible in the scroll window
39 Rectangle start = editor.modelToView(token.start);
40 Rectangle end = editor.modelToView(token.end);
41 final Rectangle show = start.union(end);
42 show.grow(start.width * 10, start.height * 6);
43 SwingUtilities.invokeLater(() -> editor.scrollRectToVisible(show));
44 } catch (BadLocationException ex) {
45 throw new Error(ex);
46 }
47
48 // highlight the token momentarily
49 final Timer timer = new Timer(200, new ActionListener() {
50 private int counter = 0;
51 private Object highlight = null;
52
53 @Override
54 public void actionPerformed(ActionEvent event) {
55 if (counter % 2 == 0) {
56 try {
57 highlight = editor.getHighlighter().addHighlight(token.start, token.end, highlightPainter);
58 } catch (BadLocationException ex) {
59 // don't care
60 }
61 } else if (highlight != null) {
62 editor.getHighlighter().removeHighlight(highlight);
63 }
64
65 if (counter++ > 6) {
66 Timer timer = (Timer) event.getSource();
67 timer.stop();
68 }
69 }
70 });
71 timer.start();
72 }
73}