PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-intellij-connector/src/main/java/com/atlassian/theplugin/idea/bamboo/build/CommitDetailsPanel.java

https://bitbucket.org/chadum/connector-idea-2.4.2-crucible
Java | 305 lines | 249 code | 50 blank | 6 comment | 33 complexity | 0c4d5dc54ca3b57bba8ff403d13ac5d5 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause, Apache-2.0
  1. package com.atlassian.theplugin.idea.bamboo.build;
  2. import com.atlassian.theplugin.commons.bamboo.BambooChangeSet;
  3. import com.atlassian.theplugin.idea.Constants;
  4. import com.atlassian.connector.intellij.bamboo.BambooBuildAdapter;
  5. import com.atlassian.theplugin.idea.crucible.tree.AtlassianTreeWithToolbar;
  6. import com.atlassian.theplugin.idea.crucible.tree.ModelProvider;
  7. import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeModel;
  8. import com.atlassian.theplugin.idea.ui.tree.file.BambooFileNode;
  9. import com.atlassian.theplugin.idea.ui.tree.file.FileTreeModelBuilder;
  10. import com.atlassian.theplugin.idea.ui.tree.paneltree.SelectableLabel;
  11. import com.atlassian.theplugin.idea.ui.tree.paneltree.TreeUISetup;
  12. import com.intellij.openapi.actionSystem.ActionGroup;
  13. import com.intellij.openapi.actionSystem.ActionManager;
  14. import com.intellij.openapi.actionSystem.DataProvider;
  15. import com.intellij.openapi.project.Project;
  16. import com.intellij.openapi.ui.Splitter;
  17. import com.intellij.psi.PsiFile;
  18. import org.jetbrains.annotations.NonNls;
  19. import org.jetbrains.annotations.Nullable;
  20. import javax.swing.*;
  21. import javax.swing.event.ListSelectionEvent;
  22. import javax.swing.event.ListSelectionListener;
  23. import javax.swing.tree.TreePath;
  24. import java.awt.*;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.MouseAdapter;
  28. import java.awt.event.MouseEvent;
  29. import java.text.DateFormat;
  30. import java.util.List;
  31. /**
  32. * User: jgorycki
  33. * Date: Jan 7, 2009
  34. * Time: 12:36:28 PM
  35. */
  36. public class CommitDetailsPanel extends JPanel implements DataProvider, ActionListener {
  37. private static final float SPLIT_RATIO = 0.6f;
  38. protected static final int ROW_HEIGHT = 16;
  39. private AtlassianTreeWithToolbar fileTree = new AtlassianTreeWithToolbar(TOOLBAR_NAME, (TreeUISetup) null, null);
  40. private final Project project;
  41. private final BambooBuildAdapter build;
  42. private static final String TOOLBAR_NAME = "ThePlugin.Bamboo.CommitListToolBar";
  43. public CommitDetailsPanel(Project project, final BambooBuildAdapter build) {
  44. setLayout(new GridBagLayout());
  45. this.project = project;
  46. this.build = build;
  47. }
  48. public void actionPerformed(ActionEvent e) {
  49. // ignore
  50. }
  51. public void showError(final Exception e) {
  52. SwingUtilities.invokeLater(new Runnable() {
  53. public void run() {
  54. add(new JLabel("Failed to retrieve changed files: " + e.getMessage()));
  55. }
  56. });
  57. }
  58. private class ChangeSetListModel extends DefaultListModel {
  59. public ChangeSetListModel(List<BambooChangeSet> commits) {
  60. int i = 0;
  61. for (BambooChangeSet cs : commits) {
  62. add(i++, cs);
  63. }
  64. }
  65. }
  66. private static final class ChangeSetRendererPanel extends JPanel {
  67. private SelectableLabel comment = new SelectableLabel(true, true, "NOTHING YET", ROW_HEIGHT, false, false);
  68. private SelectableLabel author = new SelectableLabel(true, true, "NOTHING HERE ALSO", ROW_HEIGHT, true, false);
  69. private SelectableLabel date = new SelectableLabel(true, true, "NEITHER HERE", ROW_HEIGHT, false, false);
  70. private ChangeSetRendererPanel() {
  71. setLayout(new GridBagLayout());
  72. GridBagConstraints gbc = new GridBagConstraints();
  73. gbc.weightx = 1.0;
  74. gbc.gridx = 0;
  75. gbc.gridy = 0;
  76. gbc.fill = GridBagConstraints.HORIZONTAL;
  77. add(comment, gbc);
  78. gbc.gridx++;
  79. gbc.weightx = 0.0;
  80. gbc.anchor = GridBagConstraints.LINE_END;
  81. gbc.fill = GridBagConstraints.NONE;
  82. author.setHorizontalAlignment(SwingConstants.RIGHT);
  83. add(author, gbc);
  84. gbc.gridx++;
  85. add(date, gbc);
  86. }
  87. void setChangeSet(BambooChangeSet cs) {
  88. comment.setText(" " + cs.getComment());
  89. author.setText(cs.getAuthor());
  90. DateFormat dfo = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
  91. String commitDate = dfo.format(cs.getCommitDate());
  92. date.setText(", " + commitDate + " ");
  93. }
  94. void setSelected(boolean selected) {
  95. comment.setSelected(selected);
  96. author.setSelected(selected);
  97. date.setSelected(selected);
  98. }
  99. }
  100. private static final ChangeSetRendererPanel CHANGEST_RENDERER_PANEL = new ChangeSetRendererPanel();
  101. public void fillContent(List<BambooChangeSet> commits) {
  102. if (commits == null || commits.size() == 0) {
  103. add(new JLabel("No changes in " + build.getPlanKey() + "-" + build.getBuildNumberAsString()));
  104. return;
  105. }
  106. Splitter split = new Splitter(false, SPLIT_RATIO);
  107. split.setShowDividerControls(true);
  108. JPanel listPanel = new JPanel();
  109. listPanel.setLayout(new BorderLayout());
  110. final JList changesList = new JList() {
  111. @Override
  112. public boolean getScrollableTracksViewportWidth() {
  113. return true;
  114. }
  115. };
  116. changesList.setModel(new ChangeSetListModel(commits));
  117. changesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  118. changesList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
  119. public void valueChanged(ListSelectionEvent e) {
  120. if (e.getValueIsAdjusting()) {
  121. return;
  122. }
  123. fillFileTree((BambooChangeSet) changesList.getSelectedValue());
  124. }
  125. });
  126. changesList.setCellRenderer(new ListCellRenderer() {
  127. public Component getListCellRendererComponent(JList list, Object value, int index,
  128. boolean isSelected, boolean cellHasFocus) {
  129. CHANGEST_RENDERER_PANEL.setChangeSet((BambooChangeSet) value);
  130. CHANGEST_RENDERER_PANEL.setSelected(isSelected);
  131. CHANGEST_RENDERER_PANEL.validate();
  132. return CHANGEST_RENDERER_PANEL;
  133. }
  134. });
  135. final JScrollPane scroll = new JScrollPane(changesList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
  136. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  137. listPanel.add(scroll, BorderLayout.CENTER);
  138. split.setFirstComponent(listPanel);
  139. JPanel fileTreePanel = new JPanel();
  140. fileTreePanel.setLayout(new BorderLayout());
  141. fileTreePanel.add(new JLabel("Changed Files"), BorderLayout.NORTH);
  142. fileTree.setRootVisible(false);
  143. fileTree.getTreeComponent().addMouseListener(new NavigateToCodeHandler(build.getPlanKey()));
  144. fileTreePanel.add(fileTree, BorderLayout.CENTER);
  145. split.setSecondComponent(fileTreePanel);
  146. split.setShowDividerControls(false);
  147. setLayout(new BorderLayout());
  148. add(split, BorderLayout.CENTER);
  149. if (commits.size() == 1) {
  150. changesList.getSelectionModel().setSelectionInterval(0, 0);
  151. }
  152. validate();
  153. }
  154. @Nullable
  155. public Object getData(@NonNls final String dataId) {
  156. if (dataId.equals(Constants.FILE_TREE)) {
  157. return fileTree;
  158. } else if (dataId.equals(Constants.BUILD_CHANGES_WINDOW)) {
  159. return this;
  160. }
  161. return null;
  162. }
  163. private void fillFileTree(final BambooChangeSet changeSet) {
  164. if (changeSet == null) {
  165. fileTree.setModelProvider(ModelProvider.EMPTY_MODEL_PROVIDER);
  166. } else {
  167. fileTree.setModelProvider(new ModelProvider() {
  168. private AtlassianTreeModel diredModel =
  169. FileTreeModelBuilder.buildTreeModelFromBambooChangeSet(project, changeSet);
  170. private AtlassianTreeModel flatModel =
  171. FileTreeModelBuilder.buildFlatTreeModelFromBambooChangeSet(project, changeSet);
  172. @Override
  173. public AtlassianTreeModel getModel(final AtlassianTreeWithToolbar.State state) {
  174. switch (state) {
  175. case DIRED:
  176. return diredModel;
  177. case FLAT:
  178. return flatModel;
  179. default:
  180. throw new IllegalStateException("Unknown model requested");
  181. }
  182. }
  183. });
  184. }
  185. fileTree.setRootVisible(false);
  186. fileTree.expandAll();
  187. }
  188. private final class NavigateToCodeHandler extends MouseAdapter {
  189. private String place;
  190. private NavigateToCodeHandler(String place) {
  191. this.place = place;
  192. }
  193. @Nullable
  194. private BambooFileNode getBambooFileNode(MouseEvent e) {
  195. final JTree theTree = (JTree) e.getComponent();
  196. TreePath path = theTree.getPathForLocation(e.getX(), e.getY());
  197. if (path == null) {
  198. return null;
  199. }
  200. Object o = path.getLastPathComponent();
  201. if (o instanceof BambooFileNode) {
  202. return (BambooFileNode) o;
  203. }
  204. return null;
  205. }
  206. @Override
  207. public void mouseClicked(MouseEvent e) {
  208. if (e.getClickCount() != 2) {
  209. return;
  210. }
  211. final JTree theTree = (JTree) e.getComponent();
  212. TreePath path = theTree.getPathForLocation(e.getX(), e.getY());
  213. if (path == null) {
  214. return;
  215. }
  216. Object o = path.getLastPathComponent();
  217. if (o instanceof BambooFileNode) {
  218. BambooFileNode bfn = (BambooFileNode) o;
  219. PsiFile psiFile = bfn.getPsiFile();
  220. if (psiFile != null && psiFile.canNavigateToSource()) {
  221. psiFile.navigate(true);
  222. }
  223. }
  224. }
  225. @Override
  226. public void mousePressed(MouseEvent e) {
  227. processPopup(e);
  228. }
  229. @Override
  230. public void mouseReleased(MouseEvent e) {
  231. processPopup(e);
  232. }
  233. public void processPopup(MouseEvent e) {
  234. if (!e.isPopupTrigger()) {
  235. return;
  236. }
  237. final JTree theTree = (JTree) e.getComponent();
  238. TreePath path = theTree.getPathForLocation(e.getX(), e.getY());
  239. if (path == null) {
  240. return;
  241. }
  242. theTree.setSelectionPath(path);
  243. final BambooFileNode bfn = getBambooFileNode(e);
  244. if (bfn == null) {
  245. return;
  246. }
  247. ActionManager aManager = ActionManager.getInstance();
  248. ActionGroup menu = (ActionGroup) aManager.getAction(TOOLBAR_NAME);
  249. if (menu == null) {
  250. return;
  251. }
  252. aManager.createActionPopupMenu(place, menu).getComponent().show(fileTree, e.getX(), e.getY());
  253. }
  254. }
  255. }