summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/gui/GuiTricks.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/gui/GuiTricks.java')
-rw-r--r--src/cuchaz/enigma/gui/GuiTricks.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/gui/GuiTricks.java b/src/cuchaz/enigma/gui/GuiTricks.java
index df9e221..5a3a01d 100644
--- a/src/cuchaz/enigma/gui/GuiTricks.java
+++ b/src/cuchaz/enigma/gui/GuiTricks.java
@@ -11,11 +11,21 @@
11package cuchaz.enigma.gui; 11package cuchaz.enigma.gui;
12 12
13import java.awt.Font; 13import java.awt.Font;
14import java.awt.Rectangle;
15import java.awt.event.ActionEvent;
16import java.awt.event.ActionListener;
14import java.awt.event.MouseEvent; 17import java.awt.event.MouseEvent;
15 18
16import javax.swing.JComponent; 19import javax.swing.JComponent;
20import javax.swing.JEditorPane;
17import javax.swing.JLabel; 21import javax.swing.JLabel;
22import javax.swing.SwingUtilities;
23import javax.swing.Timer;
18import javax.swing.ToolTipManager; 24import javax.swing.ToolTipManager;
25import javax.swing.text.BadLocationException;
26import javax.swing.text.Highlighter.HighlightPainter;
27
28import cuchaz.enigma.analysis.Token;
19 29
20public class GuiTricks { 30public class GuiTricks {
21 31
@@ -33,4 +43,52 @@ public class GuiTricks {
33 manager.mouseMoved(new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 0, 0, 0, false)); 43 manager.mouseMoved(new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 0, 0, 0, false));
34 manager.setInitialDelay(oldDelay); 44 manager.setInitialDelay(oldDelay);
35 } 45 }
46
47 public static void navigateToToken(final JEditorPane editor, final Token token, final HighlightPainter highlightPainter) {
48
49 // set the caret position to the token
50 editor.setCaretPosition(token.start);
51 editor.grabFocus();
52
53 try {
54 // make sure the token is visible in the scroll window
55 Rectangle start = editor.modelToView(token.start);
56 Rectangle end = editor.modelToView(token.end);
57 final Rectangle show = start.union(end);
58 show.grow(start.width * 10, start.height * 6);
59 SwingUtilities.invokeLater(new Runnable() {
60 @Override
61 public void run() {
62 editor.scrollRectToVisible(show);
63 }
64 });
65 } catch (BadLocationException ex) {
66 throw new Error(ex);
67 }
68
69 // highlight the token momentarily
70 final Timer timer = new Timer(200, new ActionListener() {
71 private int m_counter = 0;
72 private Object m_highlight = null;
73
74 @Override
75 public void actionPerformed(ActionEvent event) {
76 if (m_counter % 2 == 0) {
77 try {
78 m_highlight = editor.getHighlighter().addHighlight(token.start, token.end, highlightPainter);
79 } catch (BadLocationException ex) {
80 // don't care
81 }
82 } else if (m_highlight != null) {
83 editor.getHighlighter().removeHighlight(m_highlight);
84 }
85
86 if (m_counter++ > 6) {
87 Timer timer = (Timer)event.getSource();
88 timer.stop();
89 }
90 }
91 });
92 timer.start();
93 }
36} 94}