summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/utils/Utils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/utils/Utils.java')
-rw-r--r--src/main/java/cuchaz/enigma/utils/Utils.java134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/utils/Utils.java b/src/main/java/cuchaz/enigma/utils/Utils.java
new file mode 100644
index 0000000..e391b5a
--- /dev/null
+++ b/src/main/java/cuchaz/enigma/utils/Utils.java
@@ -0,0 +1,134 @@
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 ******************************************************************************/
11package cuchaz.enigma.utils;
12
13import com.google.common.io.CharStreams;
14
15import java.awt.Desktop;
16import java.awt.Font;
17import java.awt.Rectangle;
18import java.awt.event.ActionEvent;
19import java.awt.event.ActionListener;
20import java.awt.event.MouseEvent;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.InputStreamReader;
24import java.net.URI;
25import java.net.URISyntaxException;
26import java.util.Arrays;
27
28import javax.swing.*;
29import javax.swing.text.BadLocationException;
30import javax.swing.text.Highlighter;
31
32import cuchaz.enigma.analysis.Token;
33
34public class Utils {
35
36 public static int combineHashesOrdered(Object... objs) {
37 return combineHashesOrdered(Arrays.asList(objs));
38 }
39
40 public static int combineHashesOrdered(Iterable<Object> objs) {
41 final int prime = 67;
42 int result = 1;
43 for (Object obj : objs) {
44 result *= prime;
45 if (obj != null) {
46 result += obj.hashCode();
47 }
48 }
49 return result;
50 }
51
52 public static String readStreamToString(InputStream in) throws IOException {
53 return CharStreams.toString(new InputStreamReader(in, "UTF-8"));
54 }
55
56 public static String readResourceToString(String path) throws IOException {
57 InputStream in = Utils.class.getResourceAsStream(path);
58 if (in == null) {
59 throw new IllegalArgumentException("Resource not found! " + path);
60 }
61 return readStreamToString(in);
62 }
63
64 public static void openUrl(String url) {
65 if (Desktop.isDesktopSupported()) {
66 Desktop desktop = Desktop.getDesktop();
67 try {
68 desktop.browse(new URI(url));
69 } catch (IOException ex) {
70 throw new Error(ex);
71 } catch (URISyntaxException ex) {
72 throw new IllegalArgumentException(ex);
73 }
74 }
75 }
76
77 public static JLabel unboldLabel(JLabel label) {
78 Font font = label.getFont();
79 label.setFont(font.deriveFont(font.getStyle() & ~Font.BOLD));
80 return label;
81 }
82
83 public static void showToolTipNow(JComponent component) {
84 // HACKHACK: trick the tooltip manager into showing the tooltip right now
85 ToolTipManager manager = ToolTipManager.sharedInstance();
86 int oldDelay = manager.getInitialDelay();
87 manager.setInitialDelay(0);
88 manager.mouseMoved(new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 0, 0, 0, false));
89 manager.setInitialDelay(oldDelay);
90 }
91
92 public static void navigateToToken(final JEditorPane editor, final Token token, final Highlighter.HighlightPainter highlightPainter) {
93
94 // set the caret position to the token
95 editor.setCaretPosition(token.start);
96 editor.grabFocus();
97
98 try {
99 // make sure the token is visible in the scroll window
100 Rectangle start = editor.modelToView(token.start);
101 Rectangle end = editor.modelToView(token.end);
102 final Rectangle show = start.union(end);
103 show.grow(start.width * 10, start.height * 6);
104 SwingUtilities.invokeLater(() -> editor.scrollRectToVisible(show));
105 } catch (BadLocationException ex) {
106 throw new Error(ex);
107 }
108
109 // highlight the token momentarily
110 final Timer timer = new Timer(200, new ActionListener() {
111 private int m_counter = 0;
112 private Object m_highlight = null;
113
114 @Override
115 public void actionPerformed(ActionEvent event) {
116 if (m_counter % 2 == 0) {
117 try {
118 m_highlight = editor.getHighlighter().addHighlight(token.start, token.end, highlightPainter);
119 } catch (BadLocationException ex) {
120 // don't care
121 }
122 } else if (m_highlight != null) {
123 editor.getHighlighter().removeHighlight(m_highlight);
124 }
125
126 if (m_counter++ > 6) {
127 Timer timer = (Timer) event.getSource();
128 timer.stop();
129 }
130 }
131 });
132 timer.start();
133 }
134}