1 | |
package org.jbehave.core.context; |
2 | |
|
3 | |
import static java.text.MessageFormat.format; |
4 | |
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; |
5 | |
|
6 | |
import java.awt.BorderLayout; |
7 | |
import java.awt.Point; |
8 | |
import java.awt.event.MouseEvent; |
9 | |
import java.util.concurrent.TimeUnit; |
10 | |
|
11 | |
import javax.swing.JFrame; |
12 | |
import javax.swing.JLabel; |
13 | |
import javax.swing.JPanel; |
14 | |
import javax.swing.border.EmptyBorder; |
15 | |
import javax.swing.event.MouseInputAdapter; |
16 | |
|
17 | |
public class JFrameContextView implements ContextView { |
18 | |
|
19 | |
protected JFrame frame; |
20 | |
protected JLabel label; |
21 | |
protected int width; |
22 | |
protected int height; |
23 | |
protected int x; |
24 | |
protected int y; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public JFrameContextView() { |
30 | 0 | sized(380, 85); |
31 | 0 | located(0, 0); |
32 | 0 | } |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public JFrameContextView sized(final int width, final int height) { |
42 | 0 | this.width = width; |
43 | 0 | this.height = height; |
44 | 0 | return this; |
45 | |
} |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public JFrameContextView located(final int x, final int y) { |
55 | 0 | this.x = x; |
56 | 0 | this.y = y; |
57 | 0 | return this; |
58 | |
} |
59 | |
|
60 | |
public synchronized void show(String story, String scenario, String step) { |
61 | 0 | if (frame == null) { |
62 | 0 | initialize(); |
63 | |
} |
64 | 0 | label.setText(formatText(story, scenario, step)); |
65 | |
try { |
66 | 0 | TimeUnit.MILLISECONDS.sleep(pauseInMillis()); |
67 | 0 | } catch (InterruptedException e) { |
68 | |
|
69 | 0 | } |
70 | 0 | } |
71 | |
|
72 | |
protected String formatText(String story, String scenario, String step) { |
73 | 0 | return format(labelTemplate(), (story != null ? escapeHtml(story) : ""), (scenario != null ? escapeHtml(scenario) : ""), escapeHtml(step)); |
74 | |
} |
75 | |
|
76 | |
protected String labelTemplate() { |
77 | 0 | return "<html><h3>{0}</h3><h4>{1}</h4><p>{2}</p></html>"; |
78 | |
} |
79 | |
|
80 | |
protected long pauseInMillis() { |
81 | 0 | return 250; |
82 | |
} |
83 | |
|
84 | |
public synchronized void close() { |
85 | 0 | if (frame != null) { |
86 | 0 | frame.setVisible(false); |
87 | 0 | frame.dispose(); |
88 | 0 | frame = null; |
89 | 0 | label = null; |
90 | |
} |
91 | 0 | } |
92 | |
|
93 | |
protected void initialize() { |
94 | 0 | frame = new JFrame(); |
95 | 0 | label = new JLabel(); |
96 | 0 | frame.setAlwaysOnTop(true); |
97 | 0 | frame.setSize(width, height); |
98 | 0 | frame.setLocation(x, y); |
99 | 0 | frame.setUndecorated(true); |
100 | 0 | JPanel panel = new JPanel(); |
101 | 0 | frame.setContentPane(panel); |
102 | 0 | panel.setLayout(new BorderLayout()); |
103 | 0 | label.setBorder(new EmptyBorder(3, 3, 3, 3)); |
104 | 0 | panel.add(label, BorderLayout.CENTER); |
105 | |
|
106 | 0 | MouseInputAdapter mia = new MouseInputAdapter() { |
107 | |
private Point mousePressedScreenCoords; |
108 | |
private Point mousePressedCompCoords; |
109 | |
|
110 | |
public void mouseReleased(MouseEvent e) { |
111 | 0 | mousePressedScreenCoords = null; |
112 | 0 | mousePressedCompCoords = null; |
113 | 0 | } |
114 | |
|
115 | |
public void mousePressed(MouseEvent e) { |
116 | 0 | mousePressedScreenCoords = e.getLocationOnScreen(); |
117 | 0 | mousePressedCompCoords = e.getPoint(); |
118 | 0 | } |
119 | |
|
120 | |
public void mouseDragged(MouseEvent e) { |
121 | 0 | Point currCoords = e.getLocationOnScreen(); |
122 | 0 | x = mousePressedScreenCoords.x |
123 | |
+ (currCoords.x - mousePressedScreenCoords.x) |
124 | |
- mousePressedCompCoords.x; |
125 | 0 | y = mousePressedScreenCoords.y |
126 | |
+ (currCoords.y - mousePressedScreenCoords.y) |
127 | |
- mousePressedCompCoords.y; |
128 | 0 | frame.setLocation(x, y); |
129 | 0 | } |
130 | |
}; |
131 | |
|
132 | 0 | frame.addMouseListener(mia); |
133 | 0 | frame.addMouseMotionListener(mia); |
134 | |
|
135 | 0 | frame.setVisible(true); |
136 | 0 | } |
137 | |
|
138 | |
} |