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.autoupdate;
18  
19  import com.atlassian.theplugin.commons.configuration.PluginConfiguration;
20  import com.atlassian.theplugin.commons.exception.ThePluginException;
21  import com.atlassian.theplugin.commons.cfg.CfgManager;
22  import com.atlassian.theplugin.idea.StatusBarPluginIcon;
23  import com.atlassian.theplugin.util.InfoServer;
24  import com.atlassian.theplugin.util.PluginUtil;
25  import com.intellij.openapi.project.Project;
26  import com.intellij.openapi.util.IconLoader;
27  
28  import javax.swing.*;
29  import java.awt.event.MouseAdapter;
30  import java.awt.event.MouseEvent;
31  import java.util.Timer;
32  import java.util.TimerTask;
33  
34  public class PluginUpdateIcon extends StatusBarPluginIcon {
35  
36  	private static final Icon ICON_BLINK_ON = IconLoader.getIcon("/icons/status_plugin.png");
37  	private static final Icon ICON_BLINK_OFF = IconLoader.getIcon("/icons/icn_update_16-empty.png");
38  	private transient InfoServer.VersionInfo version;
39  	private transient Timer timer;
40  	private boolean blinkOn = false;
41  	private static final int ICON_BLINK_TIME = 1000;
42  	private transient UpdateActionHandler handler = null;
43  
44  	public PluginUpdateIcon(final Project project, final PluginConfiguration pluginConfiguration, final CfgManager cfgManager) {
45  		super(project, cfgManager);
46  		handler = new NewVersionConfirmHandler(this, project, pluginConfiguration.getGeneralConfigurationData());
47  
48  		addMouseListener(new MouseAdapter() {
49  			@Override
50  			public void mouseClicked(MouseEvent e) {
51  				stopBlinking();
52  				try {
53  					handler.doAction(version, true);
54  				} catch (ThePluginException e1) {
55  					PluginUtil.getLogger().info("Error retrieving new version: " + e1.getMessage(), e1);
56  				}
57  			}
58  		});
59  	}
60  
61  	public void stopBlinking() {
62  		if (timer != null) {
63  			this.timer.cancel();
64  			this.timer = null;
65  		}
66  		hideIcon();
67  	}
68  
69  	protected void startBlinking() {
70  		if (this.timer == null) {
71  			this.timer = new Timer();
72  			timer.scheduleAtFixedRate(new TimerTask() {
73  				public void run() {
74  					blinkIcon();
75  				}
76  			}, 0, ICON_BLINK_TIME);
77  		}
78  	}
79  
80  	/**
81  	 * changes status
82  	 */
83  	public void triggerUpdateAvailableAction(InfoServer.VersionInfo newVersion) {
84  		this.version = newVersion;
85  		startBlinking();
86  		this.setToolTipText("New version (" + newVersion.getVersion() + ") of the "
87  				+ PluginUtil.getInstance().getName() + " available");
88  
89  	}
90  
91  	public void blinkIcon() {
92  		if (!isIconShown()) {
93  			showIcon();
94  		}
95  		if (blinkOn) {
96  			blinkOn();
97  			blinkOn = false;
98  		} else {
99  			blinkOff();
100 			blinkOn = true;
101 		}
102 	}
103 
104 	private void blinkOff() {
105 		this.setIcon(ICON_BLINK_OFF);
106 	}
107 
108 	private void blinkOn() {
109 		this.setIcon(ICON_BLINK_ON);
110 	}
111 }