PageRenderTime 75ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/sylfra/idea/plugins/revu/utils/RevuUtils.java

http://idea-revu.googlecode.com/
Java | 410 lines | 347 code | 55 blank | 8 comment | 54 complexity | a346aeaaa233b76b1ee2efc44159c88d MD5 | raw file
  1. package org.sylfra.idea.plugins.revu.utils;
  2. import com.intellij.ide.DataManager;
  3. import com.intellij.openapi.actionSystem.AnActionEvent;
  4. import com.intellij.openapi.actionSystem.DataKeys;
  5. import com.intellij.openapi.application.ApplicationManager;
  6. import com.intellij.openapi.components.ServiceManager;
  7. import com.intellij.openapi.editor.Document;
  8. import com.intellij.openapi.editor.Editor;
  9. import com.intellij.openapi.editor.EditorFactory;
  10. import com.intellij.openapi.fileEditor.FileDocumentManager;
  11. import com.intellij.openapi.options.ShowSettingsUtil;
  12. import com.intellij.openapi.project.Project;
  13. import com.intellij.openapi.util.io.FileUtil;
  14. import com.intellij.openapi.vfs.VirtualFile;
  15. import com.intellij.psi.PsiDocumentManager;
  16. import com.intellij.psi.PsiFile;
  17. import com.intellij.psi.PsiManager;
  18. import com.intellij.util.ui.UIUtil;
  19. import org.apache.commons.codec.digest.DigestUtils;
  20. import org.jetbrains.annotations.NotNull;
  21. import org.jetbrains.annotations.Nullable;
  22. import org.sylfra.idea.plugins.revu.RevuBundle;
  23. import org.sylfra.idea.plugins.revu.RevuIconProvider;
  24. import org.sylfra.idea.plugins.revu.RevuPlugin;
  25. import org.sylfra.idea.plugins.revu.business.ReviewManager;
  26. import org.sylfra.idea.plugins.revu.model.*;
  27. import org.sylfra.idea.plugins.revu.settings.app.RevuAppSettings;
  28. import org.sylfra.idea.plugins.revu.settings.app.RevuAppSettingsComponent;
  29. import org.sylfra.idea.plugins.revu.settings.project.RevuProjectSettings;
  30. import org.sylfra.idea.plugins.revu.settings.project.RevuProjectSettingsComponent;
  31. import org.sylfra.idea.plugins.revu.settings.project.workspace.RevuWorkspaceSettings;
  32. import org.sylfra.idea.plugins.revu.settings.project.workspace.RevuWorkspaceSettingsComponent;
  33. import org.sylfra.idea.plugins.revu.ui.forms.settings.RevuAppSettingsForm;
  34. import org.sylfra.idea.plugins.revu.ui.forms.settings.RevuProjectSettingsForm;
  35. import javax.swing.*;
  36. import javax.swing.text.JTextComponent;
  37. import java.awt.*;
  38. import java.awt.event.ActionEvent;
  39. import java.awt.event.InputEvent;
  40. import java.awt.event.KeyEvent;
  41. import java.util.ArrayList;
  42. import java.util.Collection;
  43. import java.util.Date;
  44. import java.util.List;
  45. /**
  46. * @author <a href="mailto:syllant@gmail.com">Sylvain FRANCOIS</a>
  47. * @version $Id: RevuUtils.java 7 2008-11-15 09:20:32Z syllant $
  48. */
  49. public class RevuUtils
  50. {
  51. @Nullable
  52. public static Project getProject()
  53. {
  54. return DataKeys.PROJECT.getData(DataManager.getInstance().getDataContext());
  55. }
  56. @Nullable
  57. public static PsiFile getPsiFile(@NotNull Project project, @NotNull Issue issue)
  58. {
  59. return (issue.getFile() == null) ? null : PsiManager.getInstance(project).findFile(issue.getFile());
  60. }
  61. @Nullable
  62. public static Document getDocument(@NotNull Project project, @NotNull Issue issue)
  63. {
  64. PsiFile psiFile = getPsiFile(project, issue);
  65. return (psiFile == null) ? null : PsiDocumentManager.getInstance(project).getDocument(psiFile);
  66. }
  67. @NotNull
  68. public static List<Editor> getEditors(@NotNull Issue issue)
  69. {
  70. List<Editor> result = new ArrayList<Editor>();
  71. if (issue.getFile() != null)
  72. {
  73. Editor[] editors = EditorFactory.getInstance().getAllEditors();
  74. for (Editor editor : editors)
  75. {
  76. VirtualFile vFile = FileDocumentManager.getInstance().getFile(editor.getDocument());
  77. if (issue.getFile().equals(vFile))
  78. {
  79. result.add(editor);
  80. }
  81. }
  82. }
  83. return result;
  84. }
  85. /**
  86. * Retrieve virtual file from data context, by checking also current editor (in diff dialog)
  87. */
  88. @Nullable
  89. public static VirtualFile getVirtualFile(AnActionEvent e)
  90. {
  91. VirtualFile result = e.getData(DataKeys.VIRTUAL_FILE);
  92. if (result == null)
  93. {
  94. Editor editor = e.getData(DataKeys.EDITOR);
  95. if (editor != null)
  96. {
  97. result = FileDocumentManager.getInstance().getFile(editor.getDocument());
  98. }
  99. }
  100. return result;
  101. }
  102. @NotNull
  103. public static String z(@Nullable String s1, @Nullable String s2)
  104. {
  105. if ((s1 == null) || ("".equals(s1)))
  106. {
  107. return "";
  108. }
  109. return DigestUtils.md5Hex(s1 + RevuPlugin.PLUGIN_NAME + ((s2 == null) ? "" : s2));
  110. }
  111. @Nullable
  112. public static String getCurrentUserLogin()
  113. {
  114. return ServiceManager.getService(RevuAppSettingsComponent.class).getState().getLogin();
  115. }
  116. @Nullable
  117. public static User getCurrentUser()
  118. {
  119. RevuAppSettings appSettings = ServiceManager.getService(RevuAppSettingsComponent.class).getState();
  120. if (appSettings.getLogin() == null)
  121. {
  122. return null;
  123. }
  124. User user = new User();
  125. user.setLogin(appSettings.getLogin());
  126. user.setPassword(appSettings.getPassword());
  127. user.setDisplayName(appSettings.getLogin());
  128. return user;
  129. }
  130. @Nullable
  131. public static User getCurrentUser(@Nullable Review review)
  132. {
  133. User user;
  134. if (review == null)
  135. {
  136. user = null;
  137. }
  138. else
  139. {
  140. String login = RevuUtils.getCurrentUserLogin();
  141. user = (login == null) ? null : review.getDataReferential().getUser(login, true);
  142. }
  143. return user;
  144. }
  145. public static boolean hasRole(@NotNull Review review, @NotNull User.Role role)
  146. {
  147. String login = RevuUtils.getCurrentUserLogin();
  148. if (login == null)
  149. {
  150. return false;
  151. }
  152. User user = review.getDataReferential().getUser(login, true);
  153. return (user != null) && user.hasRole(role);
  154. }
  155. @NotNull
  156. public static User getNonNullUser(@Nullable User user)
  157. {
  158. return (user == null) ? User.UNKNOWN : user;
  159. }
  160. @NotNull
  161. public static User getNonNullUser(@NotNull DataReferential dataReferential, @Nullable String login)
  162. {
  163. if (login == null)
  164. {
  165. return User.UNKNOWN;
  166. }
  167. if (User.DEFAULT.getLogin().equals(login))
  168. {
  169. return User.DEFAULT;
  170. }
  171. return getNonNullUser(dataReferential.getUser(login, true));
  172. }
  173. public static void configureTextAreaAsStandardField(@NotNull final JTextArea... textAreas)
  174. {
  175. for (JTextArea textArea : textAreas)
  176. {
  177. AbstractAction nextTabAction = new AbstractAction("NextTab")
  178. {
  179. public void actionPerformed(ActionEvent evt)
  180. {
  181. ((JTextArea) evt.getSource()).transferFocus();
  182. }
  183. };
  184. AbstractAction previousTabAction = new AbstractAction("PreviousTab")
  185. {
  186. public void actionPerformed(ActionEvent evt)
  187. {
  188. ((JTextArea) evt.getSource()).transferFocusBackward();
  189. }
  190. };
  191. textArea.getActionMap().put(nextTabAction.getValue(Action.NAME), nextTabAction);
  192. textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0, false),
  193. nextTabAction.getValue(Action.NAME));
  194. textArea.getActionMap().put(previousTabAction.getValue(Action.NAME), previousTabAction);
  195. textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK, false),
  196. previousTabAction.getValue(Action.NAME));
  197. }
  198. }
  199. public static void setWriteAccess(boolean canWrite, @NotNull JComponent... components)
  200. {
  201. for (JComponent component : components)
  202. {
  203. if (component instanceof JTextComponent)
  204. {
  205. ((JTextComponent) component).setEditable(canWrite);
  206. }
  207. else
  208. {
  209. UIUtil.setEnabled(component, canWrite, true);
  210. }
  211. }
  212. }
  213. @NotNull
  214. public static String buildFileNameFromReviewName(@NotNull String name)
  215. {
  216. return FileUtil.sanitizeFileName(name) + ".xml";
  217. }
  218. @NotNull
  219. public static String buildIssueStatusLabel(@NotNull IssueStatus status)
  220. {
  221. return RevuBundle.message("general.issueStatus." + status.toString().toLowerCase() + ".text");
  222. }
  223. @NotNull
  224. public static String buildReviewStatusLabel(@NotNull ReviewStatus status, boolean lowerCase)
  225. {
  226. String label = RevuBundle.message("general.reviewStatus." + status.toString().toLowerCase() + ".text");
  227. return lowerCase ? label.toLowerCase() : label;
  228. }
  229. @NotNull
  230. public static History buildHistory(@NotNull Review review)
  231. {
  232. String login = getCurrentUserLogin();
  233. User user = (login == null) ? User.UNKNOWN : review.getDataReferential().getUser(login, true);
  234. Date now = new Date();
  235. History history = new History();
  236. history.setCreatedBy(user);
  237. history.setCreatedOn(now);
  238. history.setLastUpdatedBy(user);
  239. history.setLastUpdatedOn(now);
  240. return history;
  241. }
  242. public static boolean isActive(@NotNull Review review)
  243. {
  244. return ((review.getStatus() == ReviewStatus.FIXING) || (review.getStatus() == ReviewStatus.REVIEWING));
  245. }
  246. public static boolean isActiveForCurrentUser(@NotNull Review review)
  247. {
  248. return (((review.getStatus() == ReviewStatus.FIXING) || (review.getStatus() == ReviewStatus.REVIEWING))
  249. && (review.getDataReferential().getUser(getCurrentUserLogin(), true) != null));
  250. }
  251. @NotNull
  252. public static String getHex(@NotNull Color color)
  253. {
  254. return "#" + Integer.toHexString((color.getRGB() & 0xffffff) | 0x1000000).substring(1);
  255. }
  256. public static Color getIssueStatusColor(@NotNull IssueStatus status)
  257. {
  258. // @TODO manage Colors in cache (already done in IssueTable)
  259. RevuAppSettings appSettings = ApplicationManager.getApplication().getComponent(RevuAppSettingsComponent.class)
  260. .getState();
  261. return Color.decode(appSettings.getIssueStatusColors().get(status));
  262. }
  263. @Nullable
  264. public static Review getReviewingReview(@NotNull Project project)
  265. {
  266. String reviewName = getWorkspaceSettings(project).getReviewingReviewName();
  267. if (reviewName == null)
  268. {
  269. return null;
  270. }
  271. Review review = project.getComponent(ReviewManager.class).getReviewByName(reviewName);
  272. User user = RevuUtils.getCurrentUser(review);
  273. return ((user != null) && (user.hasRole(User.Role.REVIEWER))) ? review : null;
  274. }
  275. public static boolean hasRoleForReviewingReview(@NotNull Project project, @NotNull User.Role role)
  276. {
  277. Review review = RevuUtils.getReviewingReview(project);
  278. if (review != null)
  279. {
  280. User user = RevuUtils.getCurrentUser(review);
  281. if ((user != null) && (user.hasRole(role)))
  282. {
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. @NotNull
  289. public static Collection<Review> getActiveReviewsForCurrentUser(@NotNull Project project)
  290. {
  291. return project.getComponent(ReviewManager.class).getReviews(RevuUtils.getCurrentUserLogin(), true);
  292. }
  293. @NotNull
  294. public static RevuAppSettings getAppSettings()
  295. {
  296. return ApplicationManager.getApplication().getComponent(RevuAppSettingsComponent.class).getState();
  297. }
  298. @NotNull
  299. public static RevuWorkspaceSettings getWorkspaceSettings(@NotNull Project project)
  300. {
  301. return project.getComponent(RevuWorkspaceSettingsComponent.class).getState();
  302. }
  303. @NotNull
  304. public static RevuProjectSettings getProjectSettings(@NotNull Project project)
  305. {
  306. return project.getComponent(RevuProjectSettingsComponent.class).getState();
  307. }
  308. public static void editProjectSettings(@NotNull Project project, @Nullable final Review review)
  309. {
  310. final RevuProjectSettingsForm form = project.getComponent(RevuProjectSettingsForm.class);
  311. ShowSettingsUtil.getInstance().editConfigurable(project, form, new Runnable()
  312. {
  313. public void run()
  314. {
  315. if (review != null)
  316. {
  317. form.selectItem(review);
  318. }
  319. }
  320. });
  321. }
  322. public static void editAppSettings(@Nullable Project project)
  323. {
  324. ShowSettingsUtil.getInstance().editConfigurable(project,
  325. ApplicationManager.getApplication().getComponent(RevuAppSettingsForm.class));
  326. }
  327. @NotNull
  328. public static Icon findIcon(@NotNull Collection<Issue> issues, boolean fullySynchronized)
  329. {
  330. boolean allResolved = true;
  331. for (Issue issue : issues)
  332. {
  333. if ((!issue.getStatus().equals(IssueStatus.RESOLVED)) && (!issue.getStatus().equals(IssueStatus.CLOSED)))
  334. {
  335. allResolved = false;
  336. }
  337. }
  338. return RevuIconProvider.getIcon(fullySynchronized
  339. ? (allResolved ? RevuIconProvider.IconRef.GUTTER_ISSUES_RESOLVED
  340. : RevuIconProvider.IconRef.GUTTER_ISSUES)
  341. : (allResolved ? RevuIconProvider.IconRef.GUTTER_ISSUES_DESYNCHRONIZED
  342. : RevuIconProvider.IconRef.GUTTER_ISSUES_DESYNCHRONIZED_RESOLVED));
  343. }
  344. @NotNull
  345. public static Icon findIcon(@NotNull Issue issue, boolean fullySynchronized)
  346. {
  347. IssueStatus status = issue.getStatus();
  348. boolean resolved = (status.equals(IssueStatus.RESOLVED) || status.equals(IssueStatus.CLOSED));
  349. return RevuIconProvider.getIcon(fullySynchronized
  350. ? (resolved ? RevuIconProvider.IconRef.GUTTER_ISSUE_RESOLVED
  351. : RevuIconProvider.IconRef.GUTTER_ISSUE)
  352. : (resolved ? RevuIconProvider.IconRef.GUTTER_ISSUE_DESYNCHRONIZED
  353. : RevuIconProvider.IconRef.GUTTER_ISSUE_DESYNCHRONIZED_RESOLVED));
  354. }
  355. }