PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/java/org/sylfra/idea/plugins/revu/ui/forms/settings/RevuProjectSettingsForm.java

http://idea-revu.googlecode.com/
Java | 338 lines | 254 code | 43 blank | 41 comment | 17 complexity | bed352371bb6100806ab32e79f84f112 MD5 | raw file
  1. package org.sylfra.idea.plugins.revu.ui.forms.settings;
  2. import com.intellij.openapi.actionSystem.ActionGroup;
  3. import com.intellij.openapi.actionSystem.ActionManager;
  4. import com.intellij.openapi.actionSystem.DataKey;
  5. import com.intellij.openapi.application.ApplicationManager;
  6. import com.intellij.openapi.components.ProjectComponent;
  7. import com.intellij.openapi.diagnostic.Logger;
  8. import com.intellij.openapi.options.ConfigurationException;
  9. import com.intellij.openapi.project.Project;
  10. import com.intellij.openapi.ui.Messages;
  11. import org.jetbrains.annotations.Nls;
  12. import org.jetbrains.annotations.NonNls;
  13. import org.jetbrains.annotations.NotNull;
  14. import org.jetbrains.annotations.Nullable;
  15. import org.sylfra.idea.plugins.revu.RevuBundle;
  16. import org.sylfra.idea.plugins.revu.RevuDataKeys;
  17. import org.sylfra.idea.plugins.revu.RevuIconProvider;
  18. import org.sylfra.idea.plugins.revu.RevuPlugin;
  19. import org.sylfra.idea.plugins.revu.business.ReviewManager;
  20. import org.sylfra.idea.plugins.revu.model.Review;
  21. import org.sylfra.idea.plugins.revu.settings.IRevuSettingsListener;
  22. import org.sylfra.idea.plugins.revu.settings.app.RevuAppSettings;
  23. import org.sylfra.idea.plugins.revu.settings.app.RevuAppSettingsComponent;
  24. import org.sylfra.idea.plugins.revu.settings.project.RevuProjectSettings;
  25. import org.sylfra.idea.plugins.revu.settings.project.RevuProjectSettingsComponent;
  26. import org.sylfra.idea.plugins.revu.settings.project.workspace.RevuWorkspaceSettings;
  27. import org.sylfra.idea.plugins.revu.settings.project.workspace.RevuWorkspaceSettingsComponent;
  28. import org.sylfra.idea.plugins.revu.ui.forms.AbstractListUpdatableForm;
  29. import org.sylfra.idea.plugins.revu.ui.forms.review.ReviewForm;
  30. import org.sylfra.idea.plugins.revu.utils.RevuUtils;
  31. import org.sylfra.idea.plugins.revu.utils.RevuVfsUtils;
  32. import javax.swing.*;
  33. import java.awt.event.MouseAdapter;
  34. import java.awt.event.MouseEvent;
  35. import java.util.AbstractList;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import java.util.Map;
  39. /**
  40. * Used to interface settings inside Settings panel
  41. *
  42. * @author <a href="mailto:syllant@gmail.com">Sylvain FRANCOIS</a>
  43. * @version $Id: RevuProjectSettingsForm.java 53 2010-04-30 19:58:04Z syllant $
  44. */
  45. public class RevuProjectSettingsForm extends AbstractListUpdatableForm<Review, ReviewForm> implements ProjectComponent
  46. {
  47. private static final Logger LOGGER = Logger.getInstance(RevuProjectSettingsForm.class.getName());
  48. private final IRevuSettingsListener<RevuAppSettings> appSettingsListener;
  49. public RevuProjectSettingsForm(@NotNull Project project)
  50. {
  51. super(project);
  52. RevuAppSettingsComponent appSettingsComponent =
  53. ApplicationManager.getApplication().getComponent(RevuAppSettingsComponent.class);
  54. appSettingsListener = new IRevuSettingsListener<RevuAppSettings>()
  55. {
  56. public void settingsChanged(RevuAppSettings oldSettings, RevuAppSettings newSettings)
  57. {
  58. updateUIDependingOnAppSettings(newSettings);
  59. }
  60. };
  61. appSettingsComponent.addListener(appSettingsListener);
  62. }
  63. @Override
  64. protected void setupUI()
  65. {
  66. super.setupUI();
  67. // Later this label might display distinct message depending on app settings, but for now, it is used for
  68. // only one intention, so build it here once to optimize it !
  69. lbMessageWholePane.setText(RevuBundle.message("general.form.noLogin.text"));
  70. lbMessageWholePane.setIcon(Messages.getInformationIcon());
  71. lbMessageWholePane.setIconTextGap(20);
  72. lbMessageWholePane.addMouseListener(new MouseAdapter()
  73. {
  74. @Override
  75. public void mouseClicked(MouseEvent e)
  76. {
  77. // Open App settings in a new dialog. Could also open settings using current dialog...
  78. RevuUtils.editAppSettings(project);
  79. }
  80. });
  81. RevuAppSettingsComponent appSettingsComponent =
  82. ApplicationManager.getApplication().getComponent(RevuAppSettingsComponent.class);
  83. updateUIDependingOnAppSettings(appSettingsComponent.getState());
  84. }
  85. private void updateUIDependingOnAppSettings(RevuAppSettings settings)
  86. {
  87. showWholeMessage((settings.getLogin() == null) || (settings.getLogin().trim().length() == 0));
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. @NonNls
  93. @NotNull
  94. public String getComponentName()
  95. {
  96. return RevuPlugin.PLUGIN_NAME + "." + getClass().getSimpleName();
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public void initComponent()
  102. {
  103. }
  104. /**
  105. * {@inheritDoc}
  106. */
  107. public void disposeComponent()
  108. {
  109. ApplicationManager.getApplication().getComponent(RevuAppSettingsComponent.class)
  110. .removeListener(appSettingsListener);
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. @Nls
  116. public String getDisplayName()
  117. {
  118. return RevuPlugin.PLUGIN_NAME;
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. @Nullable
  124. public Icon getIcon()
  125. {
  126. return RevuIconProvider.getIcon(RevuIconProvider.IconRef.REVU_LARGE);
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. @Nullable
  132. @NonNls
  133. public String getHelpTopic()
  134. {
  135. return null;
  136. }
  137. /**
  138. * {@inheritDoc}
  139. */
  140. public JComponent createComponent()
  141. {
  142. return contentPane;
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public void disposeUIResources()
  148. {
  149. dispose();
  150. }
  151. public void projectOpened()
  152. {
  153. }
  154. public void projectClosed()
  155. {
  156. }
  157. @Override
  158. public void reset()
  159. {
  160. super.reset();
  161. // Change extended reviews so the point to clones
  162. for (Review review : originalItemsMap.keySet())
  163. {
  164. Review extendedReview = review.getExtendedReview();
  165. if (extendedReview != null)
  166. {
  167. review.setExtendedReview(retrieveCloneItem(extendedReview));
  168. }
  169. }
  170. }
  171. /**
  172. * {@inheritDoc}
  173. */
  174. @Override
  175. protected void apply(Map<Review, Review> items) throws ConfigurationException
  176. {
  177. ReviewManager reviewManager = project.getComponent(ReviewManager.class);
  178. List<Review> originalReviews = getOriginalItems();
  179. // Manage existing reviews
  180. List<String> projectReviewFiles = new ArrayList<String>();
  181. List<String> workspaceReviewFiles = new ArrayList<String>();
  182. for (Map.Entry<Review, Review> entry : items.entrySet())
  183. {
  184. Review editedReview = entry.getKey();
  185. Review originalReview = entry.getValue();
  186. if (editedReview.isEmbedded())
  187. {
  188. continue;
  189. }
  190. String reviewFilePath = RevuVfsUtils.buildRelativePath(project, editedReview.getFile());
  191. if (editedReview.isShared())
  192. {
  193. projectReviewFiles.add(reviewFilePath);
  194. }
  195. else
  196. {
  197. workspaceReviewFiles.add(reviewFilePath);
  198. }
  199. // No change
  200. if ((originalReview != null) && (originalReview.equals(editedReview)))
  201. {
  202. continue;
  203. }
  204. // Change original review to avoid handling an obsolete instance
  205. if (originalReview == null)
  206. {
  207. originalReview = editedReview;
  208. }
  209. else
  210. {
  211. originalReview.copyFromTemplate(editedReview);
  212. }
  213. try
  214. {
  215. reviewManager.save(originalReview);
  216. }
  217. catch (Exception e)
  218. {
  219. LOGGER.warn(e);
  220. final String details = ((e.getLocalizedMessage() == null) ? e.toString() : e.getLocalizedMessage());
  221. throw new ConfigurationException(
  222. RevuBundle.message("projectSettings.error.save.title.text", originalReview.getName(), details),
  223. RevuBundle.message("general.plugin.title"));
  224. }
  225. }
  226. // Delete obsolete reviews
  227. for (Review review : originalReviews)
  228. {
  229. if (!items.containsValue(review))
  230. {
  231. reviewManager.removeReview(review);
  232. }
  233. }
  234. RevuProjectSettingsComponent projectSettingsComponent =
  235. project.getComponent(RevuProjectSettingsComponent.class);
  236. RevuProjectSettings projectSettings = retrieveProjectSettings();
  237. projectSettings.setReviewFiles(projectReviewFiles);
  238. projectSettingsComponent.loadState(projectSettings);
  239. RevuWorkspaceSettingsComponent workspaceSettingsComponent =
  240. project.getComponent(RevuWorkspaceSettingsComponent.class);
  241. RevuWorkspaceSettings workspaceSettings = retrieveWorkspaceSettings();
  242. workspaceSettings.setReviewFiles(workspaceReviewFiles);
  243. workspaceSettingsComponent.loadState(workspaceSettings);
  244. }
  245. private RevuProjectSettings retrieveProjectSettings()
  246. {
  247. return RevuUtils.getProjectSettings(project);
  248. }
  249. private RevuWorkspaceSettings retrieveWorkspaceSettings()
  250. {
  251. return RevuUtils.getWorkspaceSettings(project);
  252. }
  253. @Override
  254. protected void customizeListCellRendererComponent(JLabel renderer, JList list, Review entity, int index,
  255. boolean selected, boolean cellHasFocus)
  256. {
  257. renderer.setIcon(RevuIconProvider.getIcon(entity.isShared()
  258. ? RevuIconProvider.IconRef.REVIEW_SHARED : RevuIconProvider.IconRef.REVIEW_LOCAL));
  259. }
  260. protected ReviewForm createMainForm()
  261. {
  262. List<Review> editedReviews = new AbstractList<Review>()
  263. {
  264. @Override
  265. public Review get(int index)
  266. {
  267. return (Review) list.getModel().getElementAt(index);
  268. }
  269. @Override
  270. public int size()
  271. {
  272. return list.getModel().getSize();
  273. }
  274. };
  275. return new ReviewForm(project, editedReviews);
  276. }
  277. protected ActionGroup createActionGroup()
  278. {
  279. return (ActionGroup) ActionManager.getInstance().getAction("revu.settings.project.reviews");
  280. }
  281. protected List<Review> getOriginalItems()
  282. {
  283. return new ArrayList<Review>(project.getComponent(ReviewManager.class).getReviews());
  284. }
  285. @Override
  286. protected DataKey createListSelectedEntityDataKey()
  287. {
  288. return RevuDataKeys.REVIEW;
  289. }
  290. @Override
  291. protected DataKey createListAllEntitiesDataKeys()
  292. {
  293. return RevuDataKeys.REVIEW_LIST;
  294. }
  295. }