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.SchedulableChecker;
20  import com.atlassian.theplugin.commons.cfg.CfgManager;
21  import com.atlassian.theplugin.commons.cfg.ConfigurationListener;
22  import com.atlassian.theplugin.commons.cfg.ProjectConfiguration;
23  import com.atlassian.theplugin.commons.configuration.ConfigurationFactory;
24  import com.atlassian.theplugin.commons.configuration.PluginConfigurationBean;
25  import com.atlassian.theplugin.commons.util.LoggerImpl;
26  import com.atlassian.theplugin.idea.autoupdate.NewVersionChecker;
27  import com.atlassian.theplugin.idea.config.ConfigPanel;
28  import com.atlassian.theplugin.util.HttpConfigurableIdeaImpl;
29  import com.atlassian.theplugin.util.PicoUtil;
30  import com.atlassian.theplugin.util.PluginSSLProtocolSocketFactory;
31  import com.intellij.openapi.components.ApplicationComponent;
32  import com.intellij.openapi.components.PersistentStateComponent;
33  import com.intellij.openapi.components.State;
34  import com.intellij.openapi.components.Storage;
35  import com.intellij.openapi.extensions.AreaPicoContainer;
36  import com.intellij.openapi.extensions.Extensions;
37  import com.intellij.openapi.options.Configurable;
38  import com.intellij.openapi.options.ConfigurationException;
39  import com.intellij.openapi.util.IconLoader;
40  import org.jetbrains.annotations.Nls;
41  import org.jetbrains.annotations.NonNls;
42  import org.jetbrains.annotations.NotNull;
43  import org.jetbrains.annotations.Nullable;
44  
45  import javax.swing.*;
46  import java.util.Collection;
47  import java.util.HashSet;
48  import java.util.Iterator;
49  import java.util.Timer;
50  import java.util.TimerTask;
51  
52  @State(name = "atlassian-ide-plugin",
53  		storages = { @Storage(id = "atlassian-ide-plugin-id", file = "$APP_CONFIG$/atlassian-ide-plugin.app.xml") })
54  public class ThePluginApplicationComponent
55  		implements ApplicationComponent, Configurable, PersistentStateComponent<PluginConfigurationBean>,
56  		ConfigurationListener {
57  
58  	static {
59  		AreaPicoContainer apc = Extensions.getRootArea().getPicoContainer();
60  		PicoUtil.populateGlobalPicoContainer(apc);
61  	}
62  
63  	private static final Icon PLUGIN_SETTINGS_ICON = IconLoader.getIcon("/icons/ico_plugin.png");
64  
65  
66  	private ConfigPanel configPanel;
67  	private final PluginConfigurationBean configuration;
68  	private final CfgManager cfgManager;
69  
70  	private final Timer timer = new Timer("atlassian-idea-plugin background status checkers");
71  	private static final int TIMER_START_DELAY = 20000;
72  
73  	private final Collection<TimerTask> scheduledComponents = new HashSet<TimerTask>();
74  
75  	public Collection<SchedulableChecker> getSchedulableCheckers() {
76  		return schedulableCheckers;
77  	}
78  
79  	private final Collection<SchedulableChecker> schedulableCheckers = new HashSet<SchedulableChecker>();
80  
81  
82  	public ThePluginApplicationComponent(PluginConfigurationBean configuration, CfgManager cfgManager) {
83  		this.configuration = configuration;
84  		this.cfgManager = cfgManager;
85  		this.configuration.transientSetHttpConfigurable(HttpConfigurableIdeaImpl.getInstance());
86  
87  		this.schedulableCheckers.add(NewVersionChecker.getInstance(configuration));
88  
89  		ConfigurationFactory.setConfiguration(configuration);
90  		PluginSSLProtocolSocketFactory.initializeSocketFactory();
91  	}
92  
93  	@Nls
94  	public String getDisplayName() {
95  		return "Atlassian Plugin\nGlobal Settings";
96  	}
97  
98  	@Nullable
99  	public Icon getIcon() {
100 		return PLUGIN_SETTINGS_ICON;
101 	}
102 
103 	@Nullable
104 	@NonNls
105 	public String getHelpTopic() {
106 		return null;
107 	}
108 
109 
110 	@NotNull
111 	@NonNls
112 	public String getComponentName() {
113 		return "ThePluginApplicationComponent";
114 	}
115 
116 	public void initComponent() {
117 		new IdeaLoggerImpl(com.intellij.openapi.diagnostic.Logger.getInstance(LoggerImpl.LOGGER_CATEGORY));
118 	}
119 
120 	public void disposeComponent() {
121 		timer.cancel();
122 	}
123 
124 	public JComponent createComponent() {
125 		if (configPanel == null) {
126 			configPanel = new ConfigPanel(configuration, cfgManager);
127 		}
128 		return configPanel;
129 	}
130 
131 	public boolean isModified() {
132 		return configPanel.isModified();
133 	}
134 
135 	private void disableTimers() {
136 		Iterator<TimerTask> i = scheduledComponents.iterator();
137 		while (i.hasNext()) {
138 			TimerTask timerTask = i.next();
139 			i.remove();
140 			timerTask.cancel();
141 		}
142 
143 		timer.purge();
144 	}
145 
146 	/**
147 	 * Reschedule the BambooStatusChecker with immediate execution trigger.
148 	 *
149 	 * @param rightNow set to false if the first execution should be delayed by {@link #TIMER_START_DELAY}.
150 	 */
151 	public void rescheduleStatusCheckers(boolean rightNow) {
152 
153 		disableTimers();
154 		long delay = rightNow ? 0 : TIMER_START_DELAY;
155 
156 		for (SchedulableChecker checker : schedulableCheckers) {
157 			if (checker.canSchedule()) {
158 				final TimerTask newTask = checker.newTimerTask();
159 				scheduledComponents.add(newTask);
160 				timer.schedule(newTask, delay, checker.getInterval());
161 			} else {
162 				checker.resetListenersState();
163 			}
164 		}
165 	}
166 
167 
168 
169 	public void apply() throws ConfigurationException {
170 		if (configPanel != null) {
171 			// Get data from configPanel to component
172 			configPanel.saveData();
173 			//configPanel.setData();
174 
175 //			for (Project project : ProjectManager.getInstance().getOpenProjects()) {
176 //				ThePluginProjectComponent projectComponent = project.getComponent(ThePluginProjectComponent.class);
177 //				// show icons if necessary
178 //				projectComponent.getStatusBarBambooIcon().showOrHideIcon();
179 //				projectComponent.getStatusBarCrucibleIcon().showOrHideIcon();
180 //
181 //				projectComponent.getToolWindow().showHidePanels();
182 //			}
183 			rescheduleStatusCheckers(true);
184 		}
185 
186 	}
187 
188 	public void reset() {
189 		if (configPanel != null) {
190 			// Reset configPanel data from component
191 			configPanel.setData();
192 		}
193 	}
194 
195 	public void disposeUIResources() {
196 		configPanel = null;
197 	}
198 
199 	public PluginConfigurationBean getState() {
200 		return configuration;
201 	}
202 
203 	public void loadState(PluginConfigurationBean state) {
204 		configuration.setConfiguration(state);
205 		configuration.transientSetHttpConfigurable(HttpConfigurableIdeaImpl.getInstance());
206 	}
207 
208 	public void configurationUpdated(final ProjectConfiguration aProjectConfiguration) {
209 		rescheduleStatusCheckers(true);
210 	}
211 
212 	public void projectUnregistered() {
213 	}
214 }