diff options
Diffstat (limited to 'src/cuchaz/enigma/gui/GuiTricks.java')
| -rw-r--r-- | src/cuchaz/enigma/gui/GuiTricks.java | 58 |
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 @@ | |||
| 11 | package cuchaz.enigma.gui; | 11 | package cuchaz.enigma.gui; |
| 12 | 12 | ||
| 13 | import java.awt.Font; | 13 | import java.awt.Font; |
| 14 | import java.awt.Rectangle; | ||
| 15 | import java.awt.event.ActionEvent; | ||
| 16 | import java.awt.event.ActionListener; | ||
| 14 | import java.awt.event.MouseEvent; | 17 | import java.awt.event.MouseEvent; |
| 15 | 18 | ||
| 16 | import javax.swing.JComponent; | 19 | import javax.swing.JComponent; |
| 20 | import javax.swing.JEditorPane; | ||
| 17 | import javax.swing.JLabel; | 21 | import javax.swing.JLabel; |
| 22 | import javax.swing.SwingUtilities; | ||
| 23 | import javax.swing.Timer; | ||
| 18 | import javax.swing.ToolTipManager; | 24 | import javax.swing.ToolTipManager; |
| 25 | import javax.swing.text.BadLocationException; | ||
| 26 | import javax.swing.text.Highlighter.HighlightPainter; | ||
| 27 | |||
| 28 | import cuchaz.enigma.analysis.Token; | ||
| 19 | 29 | ||
| 20 | public class GuiTricks { | 30 | public 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 | } |