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  package com.atlassian.theplugin.idea;
17  
18  import com.atlassian.theplugin.cfg.CfgUtil;
19  import com.atlassian.theplugin.commons.cfg.CfgManager;
20  import com.atlassian.theplugin.commons.cfg.ConfigurationListener;
21  import com.atlassian.theplugin.commons.cfg.ProjectConfiguration;
22  import com.atlassian.theplugin.commons.cfg.ProjectConfigurationFactory;
23  import com.atlassian.theplugin.commons.cfg.ProjectId;
24  import com.atlassian.theplugin.commons.cfg.ServerCfgFactoryException;
25  import com.atlassian.theplugin.commons.cfg.xstream.JDomProjectConfigurationFactory;
26  import com.atlassian.theplugin.idea.config.ProjectConfigurationPanel;
27  import com.intellij.openapi.components.ProjectComponent;
28  import com.intellij.openapi.components.SettingsSavingComponent;
29  import com.intellij.openapi.options.Configurable;
30  import com.intellij.openapi.options.ConfigurationException;
31  import com.intellij.openapi.project.Project;
32  import com.intellij.openapi.ui.Messages;
33  import com.intellij.openapi.util.IconLoader;
34  import com.intellij.openapi.vfs.VirtualFile;
35  import org.jdom.Document;
36  import org.jdom.Element;
37  import org.jdom.input.SAXBuilder;
38  import org.jdom.output.Format;
39  import org.jdom.output.XMLOutputter;
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.io.File;
47  import java.io.FileWriter;
48  import java.io.IOException;
49  
50  public class ProjectConfigurationComponent implements ProjectComponent, SettingsSavingComponent, Configurable,
51  		ConfigurationListener {
52  
53  	private final Project project;
54  	private final CfgManager cfgManager;
55  	private static final String CFG_LOAD_ERROR_MSG = "Error while loading Atlassian Plugin configuration.";
56  	private static final Icon PLUGIN_SETTINGS_ICON = IconLoader.getIcon("/icons/ico_plugin.png");
57  	private ProjectConfigurationPanel projectConfigurationPanel;
58  
59  	public ProjectConfigurationComponent(final Project project, CfgManager cfgManager) {
60  		this.project = project;
61  		this.cfgManager = cfgManager;
62  	}
63  
64  
65  	public static void handleServerCfgFactoryException(Project theProject, final Exception e) {
66  		Messages.showWarningDialog(theProject, CFG_LOAD_ERROR_MSG + "\n" + e.getMessage()
67  				+ "\nEmpty configuration will be used.", CFG_LOAD_ERROR_MSG);
68  	}
69  
70  	public void projectOpened() {
71  		load();
72  		cfgManager.addProjectConfigurationListener(getProjectId(), this);
73  	}
74  
75  
76  	public void projectClosed() {
77  		cfgManager.removeProjectConfigurationListener(getProjectId(), this);
78  		cfgManager.removeProject(getProjectId());
79  	}
80  
81  	@NonNls
82  	@NotNull
83  	public String getComponentName() {
84  		return ProjectConfigurationComponent.class.getSimpleName();
85  	}
86  
87  	public void initComponent() {
88  	}
89  
90  	public void disposeComponent() {
91  	}
92  
93  
94  	private void load() {
95  		final Document root;
96  		final SAXBuilder builder = new SAXBuilder(false);
97  		try {
98  			final String path = getCfgFilePath();
99  			if (new File(path).exists() == false) {
100 				setDefaultProjectConfiguration();
101 				return;
102 			}
103 			root = builder.build(path);
104 		} catch (Exception e) {
105 			handleServerCfgFactoryException(project, e);
106 			setDefaultProjectConfiguration();
107 			return;
108 		}
109 
110 		Document privateRoot = null; // null means that there is no private cfg available
111 		try {
112 			final String privateCfgFile = getPrivateCfgFilePath();
113 			if (new File(privateCfgFile).exists()) {
114 				privateRoot = builder.build(privateCfgFile);
115 			}
116 		} catch (Exception e) {
117 			handleServerCfgFactoryException(project, e);
118 		}
119 
120 		ProjectConfigurationFactory cfgFactory = new JDomProjectConfigurationFactory(root.getRootElement(),
121 				privateRoot != null ? privateRoot.getRootElement() : null);
122 		ProjectConfiguration projectConfiguration;
123 		try {
124 			projectConfiguration = cfgFactory.load();
125 		} catch (ServerCfgFactoryException e) {
126 			handleServerCfgFactoryException(project, e);
127 			setDefaultProjectConfiguration();
128 			return;
129 		}
130 		cfgManager.updateProjectConfiguration(CfgUtil.getProjectId(project), projectConfiguration);
131 
132 	}
133 
134 	private ProjectConfiguration setDefaultProjectConfiguration() {
135 		final ProjectConfiguration configuration = ProjectConfiguration.emptyConfiguration();
136 		cfgManager.updateProjectConfiguration(CfgUtil.getProjectId(project),
137 				configuration);
138 		return configuration;
139 	}
140 
141 	private String getCfgFilePath() {
142 		final VirtualFile baseDir = project.getBaseDir();
143 		if (baseDir == null) {
144 			return null;
145 		}
146 		return baseDir.getPath() + File.separator + "atlassian-ide-plugin.xml";
147 	}
148 
149 	private String getPrivateCfgFilePath() {
150 		final VirtualFile baseDir = project.getBaseDir();
151 		if (baseDir == null) {
152 			return null;
153 		}
154 		return baseDir.getPath() + File.separator + "atlassian-ide-plugin.private.xml";
155 	}
156 
157 
158 	private ProjectId getProjectId() {
159 		return CfgUtil.getProjectId(project);
160 	}
161 
162 	public void save() {
163 		final Element element = new Element("atlassian-ide-plugin");
164 		final Element privateElement = new Element("atlassian-ide-plugin-private");
165 		JDomProjectConfigurationFactory cfgFactory = new JDomProjectConfigurationFactory(element, privateElement);
166 		final ProjectConfiguration configuration = cfgManager.getProjectConfiguration(getProjectId());
167 		if (configuration != null) {
168 			cfgFactory.save(configuration);
169 		}
170 		final String publicCfgFile = getCfgFilePath();
171 		final String privateCfgFile = getPrivateCfgFilePath();
172 		writeXmlFile(element, publicCfgFile);
173 		writeXmlFile(privateElement, privateCfgFile);
174 	}
175 
176 
177 	private void writeXmlFile(final Element element, final String filepath) {
178 		if (filepath == null) {
179 			return; // handlig for instance default dummy project
180 		}
181 		try {
182 			final FileWriter writer = new FileWriter(filepath);
183 			new XMLOutputter(Format.getPrettyFormat()).output(element, writer);
184 			writer.close();
185 		} catch (IOException e) {
186 			Messages.showWarningDialog(project, "Cannot save project configuration settings to [" + filepath + ":\n"
187 					+ e.getMessage(), "Error");
188 		}
189 	}
190 
191 	@Nls
192 	public String getDisplayName() {
193 		return "Atlassian Plugin\nProject Settings";
194 	}
195 
196 	@Nullable
197 	public Icon getIcon() {
198 		return PLUGIN_SETTINGS_ICON;
199 	}
200 
201 	@Nullable
202 	@NonNls
203 	public String getHelpTopic() {
204 		return null;
205 	}
206 
207 	public JComponent createComponent() {
208 		ProjectConfiguration configuration = cfgManager.getProjectConfiguration(getProjectId());
209 		if (configuration == null) {
210 			// may happen for Default Template project
211 			configuration = setDefaultProjectConfiguration();
212 		}
213 		projectConfigurationPanel = new ProjectConfigurationPanel(project, configuration.getClone());
214 		return projectConfigurationPanel;
215 	}
216 
217 	public boolean isModified() {
218 		projectConfigurationPanel.saveData(false);
219 		return !cfgManager.getProjectConfiguration(getProjectId()).equals(projectConfigurationPanel.getProjectConfiguration());
220 	}
221 
222 	public void apply() throws ConfigurationException {
223 		if (projectConfigurationPanel == null) {
224 			return;
225 		}
226 		projectConfigurationPanel.saveData(true);
227 		cfgManager.updateProjectConfiguration(getProjectId(), projectConfigurationPanel.getProjectConfiguration());
228 		projectConfigurationPanel.setData(cfgManager.getProjectConfiguration(getProjectId()).getClone());
229 	}
230 
231 	public void reset() {
232 	}
233 
234 	public void disposeUIResources() {
235 		projectConfigurationPanel = null;
236 	}
237 
238 	public void configurationUpdated(final ProjectConfiguration aProjectConfiguration) {
239 		save();
240 		IdeaHelper.getAppComponent().rescheduleStatusCheckers(true);
241 	}
242 
243 	public void projectUnregistered() {
244 	}
245 }