View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      // htmlView panel shows HTML content
34      private final JEditorPane htmlView = new JEditorPane();
35  
36      // title bar of the tooltip
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  		// html view is the main view of the tooltip
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          // title bar of the tooltip
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  		// add scroll facility to the html area
67  		scrollPane = new JScrollPane(htmlView,
68  				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
69  		scrollPane.setWheelScrollingEnabled(true);
70  
71  		// put components on the main window
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  		// hide tooltip when focus lost
78  		this.addWindowFocusListener(new WindowAdapter() {
79  			public void windowLostFocus(WindowEvent e) {
80  				setVisible(false);
81  			}
82  		});
83  
84  		// pas key events to main window
85  //		this.addKeyListener(new KeyAdapter() {
86  //			public void keyTyped(KeyEvent e) {
87  //
88  //				e.setSource(PluginStatusBarToolTip.this.getOwner());
89  //				java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
90  //				PluginStatusBarToolTip.this.getOwner().dispatchEvent(e);
91  //			}
92  //		});
93  	}
94  
95  	/**
96       * Shows tooltip in the place where mouse pointer is located.
97       *
98       * @param xMouse current horizontal position of the mouse pointer (x of right-bottom corner of the tooltip)
99       * @param yMouse current vertical position of the mouse pointer (y of right-bottom corner of the tooltip)
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 //	 * Shows tooltip in the place where was visible before
124 //	 */
125 //	public void showTooltip() {
126 //		this.setVisible(true);
127 //        this.requestFocus();
128 //		this.toFront();
129 //	}
130 //
131 //	/**
132 //	 * Hides tooltip
133 //	 */
134 //	public void hideTooltip() {
135 //		this.setVisible(false);
136 //	}
137 
138 	public void setHtmlContent(String html) {
139         htmlView.setText(html);
140 		//scrollPane.getVerticalScrollBar().setValue(0);
141 		// move scroll bar to the top
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 		// resize label
151 		private final JLabel resizeLabel = new JLabel(" ");
152 		// title label
153         private final GradientLabel titleLabel = new GradientLabel();
154         // close icon container (top-right corner)
155         private final JLabel closeIconLabel = new JLabel();
156         // reference to the main tooltip window
157         private PluginStatusBarToolTip tooltipWindow;
158 
159         /**
160          * Creates titlebar in the tooltip with title and close button (icon).
161          *
162          * @param window reference to the main tooltip window to hide when close button clicked
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             // add resize label, title and close icon to title bar
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             // hide tooltip when clicked on close icon
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             // move tooltip when draging title label
196             ToolTipMoveHandler moveHandler = window.getNewMoveHandler();
197             titleLabel.addMouseMotionListener(moveHandler);
198             titleLabel.addMouseListener(moveHandler);
199 
200 			// resize tooltip
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); //255,255,255);
302         private static final Color TITLE_BAR_COLOR2 = new Color(240, 240, 240); //255,255,100);
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 }