1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.theplugin.idea;
18
19 import com.atlassian.theplugin.commons.exception.ThePluginException;
20 import thirdparty.javaworld.ClasspathHTMLEditorKit;
21 import com.intellij.openapi.util.IconLoader;
22
23 import javax.swing.*;
24 import javax.swing.event.MouseInputAdapter;
25 import java.awt.*;
26 import java.awt.event.MouseAdapter;
27 import java.awt.event.MouseEvent;
28 import java.awt.event.WindowAdapter;
29 import java.awt.event.WindowEvent;
30
31 public class PluginStatusBarToolTip extends Window {
32
33
34 private final JEditorPane htmlView = new JEditorPane();
35
36
37 private final ToolTipTitleBar titleBar;
38
39 private static final int INITIAL_SIZE_X = 400;
40 private static final int INITIAL_SIZE_Y = 200;
41 private static final int SIZE_TITLE_BAR_Y = 260;
42 private static final Color BACKGROUND_COLOR = new Color(253, 254, 226);
43 private JScrollPane scrollPane;
44
45 public PluginStatusBarToolTip(JFrame frame) {
46
47 super(frame);
48
49
50 htmlView.setEditable(false);
51 htmlView.setContentType("text/html");
52 htmlView.setBackground(BACKGROUND_COLOR);
53 htmlView.addHyperlinkListener(new GenericHyperlinkListener());
54 htmlView.setEditorKit(new ClasspathHTMLEditorKit());
55
56 try {
57 titleBar = new ToolTipTitleBar(this);
58 } catch (ThePluginException e) {
59 throw new RuntimeException(e);
60 }
61 titleBar.setTitle(" " + "Bamboo builds status");
62 titleBar.setSize(INITIAL_SIZE_X, SIZE_TITLE_BAR_Y);
63 titleBar.setMaximumSize(new Dimension(INITIAL_SIZE_X, SIZE_TITLE_BAR_Y));
64 titleBar.setMinimumSize(new Dimension(INITIAL_SIZE_X, SIZE_TITLE_BAR_Y));
65
66
67 scrollPane = new JScrollPane(htmlView,
68 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
69 scrollPane.setWheelScrollingEnabled(true);
70
71
72 this.setSize(INITIAL_SIZE_X, INITIAL_SIZE_Y);
73 this.setLayout(new BorderLayout());
74 this.add(titleBar, BorderLayout.NORTH);
75 this.add(scrollPane, BorderLayout.CENTER);
76
77
78 this.addWindowFocusListener(new WindowAdapter() {
79 public void windowLostFocus(WindowEvent e) {
80 setVisible(false);
81 }
82 });
83
84
85
86
87
88
89
90
91
92
93 }
94
95
96
97
98
99
100
101 public void showToltip(int xMouse, int yMouse) {
102
103
104 int startX = xMouse - (int) this.getSize().getWidth();
105 int startY = yMouse - (int) this.getSize().getHeight();
106
107 if (startX < 0) {
108 startX = xMouse;
109 }
110
111 if (startY < 0) {
112 startY = yMouse;
113 }
114
115 this.setLocation(startX, startY);
116
117 this.setVisible(true);
118 this.requestFocus();
119 this.toFront();
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public void setHtmlContent(String html) {
139 htmlView.setText(html);
140
141
142 htmlView.setCaretPosition(0);
143 }
144
145 private static class ToolTipTitleBar extends JPanel {
146
147 private Icon closeButtonIcon = IconLoader.getIcon("/icons/close_icon_30x15.png");
148 private Icon closeButtonIconHover = IconLoader.getIcon("/icons/close_icon_30x15_hover.png");
149
150
151 private final JLabel resizeLabel = new JLabel(" ");
152
153 private final GradientLabel titleLabel = new GradientLabel();
154
155 private final JLabel closeIconLabel = new JLabel();
156
157 private PluginStatusBarToolTip tooltipWindow;
158
159
160
161
162
163
164 ToolTipTitleBar(PluginStatusBarToolTip window) throws ThePluginException {
165
166 if (window == null) {
167 throw new ThePluginException("Null tooltip window reference passed to tooltip window title bar");
168 }
169
170 tooltipWindow = window;
171
172 closeIconLabel.setIcon(closeButtonIcon);
173
174
175 this.setLayout(new BorderLayout());
176 this.add(resizeLabel, BorderLayout.WEST);
177 this.add(titleLabel, BorderLayout.CENTER);
178 this.add(closeIconLabel, BorderLayout.EAST);
179
180
181 closeIconLabel.addMouseListener(new MouseAdapter() {
182 public void mouseClicked(MouseEvent e) {
183 tooltipWindow.setVisible(false);
184 }
185
186 public void mouseEntered(MouseEvent e) {
187 closeIconLabel.setIcon(closeButtonIconHover);
188 }
189
190 public void mouseExited(MouseEvent e) {
191 closeIconLabel.setIcon(closeButtonIcon);
192 }
193 });
194
195
196 ToolTipMoveHandler moveHandler = window.getNewMoveHandler();
197 titleLabel.addMouseMotionListener(moveHandler);
198 titleLabel.addMouseListener(moveHandler);
199
200
201 ToolTipResizeHandler resizeHandler = window.getNewResizeHandler();
202 resizeLabel.addMouseListener(resizeHandler);
203 resizeLabel.addMouseMotionListener(resizeHandler);
204
205 }
206
207 public void setTitle(String title) {
208 titleLabel.setText(title);
209 }
210
211 }
212
213 private ToolTipResizeHandler getNewResizeHandler() {
214 return new ToolTipResizeHandler();
215 }
216
217 private class ToolTipResizeHandler extends MouseInputAdapter {
218 private static final int MINIMUM_X_SIZE = 200;
219 private static final int MINIMUM_Y_SIZE = 100;
220
221 private int mousePressAbsoluteX;
222 private int mousePressAbsoluteY;
223 private int initialWindowXSize;
224 private int initialWindowYSize;
225 private int initialWindowXPosition;
226 private int initialWindowYPosition;
227
228 public void mouseEntered(MouseEvent e) {
229 Cursor c = new Cursor(Cursor.NW_RESIZE_CURSOR);
230 ((JComponent) e.getSource()).setCursor(c);
231 }
232
233 public void mouseDragged(MouseEvent e) {
234
235 int changedX = mousePressAbsoluteX - (int) (MouseInfo.getPointerInfo().getLocation().getX());
236 int changedY = mousePressAbsoluteY - (int) (MouseInfo.getPointerInfo().getLocation().getY());
237
238 int newXSize = initialWindowXSize + changedX;
239 int newYSize = initialWindowYSize + changedY;
240
241 if (newXSize > MINIMUM_X_SIZE) {
242 int currentYPosition = (int) PluginStatusBarToolTip.this.getLocation().getY();
243 int currentYSize = (int) PluginStatusBarToolTip.this.getSize().getHeight();
244
245 PluginStatusBarToolTip.this.setSize(
246 initialWindowXSize + changedX,
247 currentYSize);
248
249 PluginStatusBarToolTip.this.setLocation(
250 initialWindowXPosition - changedX,
251 currentYPosition);
252 }
253 if (newYSize > MINIMUM_Y_SIZE) {
254 int currentXPosition = (int) PluginStatusBarToolTip.this.getLocation().getX();
255 int currentXSize = (int) PluginStatusBarToolTip.this.getSize().getWidth();
256
257 PluginStatusBarToolTip.this.setSize(
258 currentXSize,
259 initialWindowYSize + changedY);
260
261 PluginStatusBarToolTip.this.setLocation(
262 currentXPosition,
263 initialWindowYPosition - changedY);
264 }
265 }
266
267 public void mousePressed(MouseEvent e) {
268 mousePressAbsoluteX = (int) (MouseInfo.getPointerInfo().getLocation().getX());
269 mousePressAbsoluteY = (int) (MouseInfo.getPointerInfo().getLocation().getY());
270
271 initialWindowXSize = (int) PluginStatusBarToolTip.this.getSize().getWidth();
272 initialWindowYSize = (int) PluginStatusBarToolTip.this.getSize().getHeight();
273
274 initialWindowXPosition = (int) PluginStatusBarToolTip.this.getLocation().getX();
275 initialWindowYPosition = (int) PluginStatusBarToolTip.this.getLocation().getY();
276 }
277 }
278
279 private ToolTipMoveHandler getNewMoveHandler() {
280 return new ToolTipMoveHandler();
281 }
282
283 private class ToolTipMoveHandler extends MouseInputAdapter {
284 private int mousePressRelativeX = 0;
285 private int mousePressRelativeY = 0;
286
287 public void mouseDragged(MouseEvent e) {
288 PluginStatusBarToolTip.this.setLocation(
289 (int) (MouseInfo.getPointerInfo().getLocation().getX() - mousePressRelativeX),
290 (int) (MouseInfo.getPointerInfo().getLocation().getY() - mousePressRelativeY));
291 }
292
293 public void mousePressed(MouseEvent e) {
294 mousePressRelativeX = e.getX();
295 mousePressRelativeY = e.getY();
296 }
297 }
298
299 private static class GradientLabel extends JLabel {
300
301 private static final Color TITLE_BAR_COLOR1 = new Color(218, 236, 254);
302 private static final Color TITLE_BAR_COLOR2 = new Color(240, 240, 240);
303
304 protected void paintComponent(Graphics g) {
305 final Graphics2D g2 = (Graphics2D) g;
306 int w = getWidth();
307 int h = getHeight();
308 GradientPaint gradient = new GradientPaint(0, 0, TITLE_BAR_COLOR1, w, 0, TITLE_BAR_COLOR2, false);
309 g2.setPaint(gradient);
310 g2.fillRect(0, 0, w, h);
311
312 super.paintComponent(g);
313 }
314 }
315 }