1 package com.atlassian.theplugin.idea.action.issues; 2 3 import com.atlassian.theplugin.commons.util.LoggerImpl; 4 import com.atlassian.theplugin.idea.IdeaHelper; 5 import com.atlassian.theplugin.idea.jira.IssueListToolWindowPanel; 6 import com.atlassian.theplugin.idea.jira.JiraIssueGroupBy; 7 import com.atlassian.theplugin.idea.ui.ComboWithLabel; 8 import com.intellij.ide.DataManager; 9 import com.intellij.openapi.actionSystem.AnActionEvent; 10 import com.intellij.openapi.actionSystem.Presentation; 11 import com.intellij.openapi.actionSystem.ex.CustomComponentAction; 12 import com.intellij.openapi.project.Project; 13 14 import javax.swing.*; 15 import java.awt.event.ActionEvent; 16 import java.awt.event.ActionListener; 17 18 public class GroupByAction extends JIRAAbstractAction implements CustomComponentAction { 19 private static final String COMBOBOX_KEY = GroupByAction.class.getName() + ".combo"; 20 21 22 @Override 23 public void actionPerformed(AnActionEvent e) { 24 } 25 26 public JComponent createCustomComponent(Presentation presentation) { 27 final JComboBox combo = new JComboBox(createModel()); 28 ComboWithLabel cwl = new ComboWithLabel(combo, "Group By"); 29 30 Project project = IdeaHelper.getCurrentProject(DataManager.getInstance().getDataContext()); 31 if (project != null) { 32 IssueListToolWindowPanel panel = IdeaHelper.getIssueListToolWindowPanel(project); 33 updateSelection(panel, combo); 34 } 35 36 presentation.putClientProperty(COMBOBOX_KEY, combo); 37 combo.addActionListener(new ActionListener() { 38 public void actionPerformed(ActionEvent e) { 39 final Project currentProject = IdeaHelper.getCurrentProject(DataManager.getInstance().getDataContext(combo)); 40 if (currentProject != null) { 41 IssueListToolWindowPanel panel = IdeaHelper.getIssueListToolWindowPanel(currentProject); 42 if (panel != null) { 43 panel.setGroupBy((JiraIssueGroupBy) combo.getSelectedItem()); 44 } else { 45 LoggerImpl.getInstance().error(GroupByAction.class.getName() + ": cannot find " 46 + IssueListToolWindowPanel.class); 47 } 48 } else { 49 System.out.println("current project is null"); 50 LoggerImpl.getInstance().error(GroupByAction.class.getName() + ": cannot determine current project"); 51 } 52 53 } 54 }); 55 return cwl; 56 } 57 58 59 private ComboBoxModel createModel() { 60 return new DefaultComboBoxModel(JiraIssueGroupBy.values()); 61 } 62 63 @Override 64 public void onUpdate(AnActionEvent event) { 65 } 66 67 @Override 68 public void onUpdate(AnActionEvent event, boolean enabled) { 69 Object myProperty = event.getPresentation().getClientProperty(COMBOBOX_KEY); 70 IssueListToolWindowPanel panel = IdeaHelper.getIssueListToolWindowPanel(event); 71 if (myProperty instanceof JComboBox) { 72 final JComboBox jComboBox = (JComboBox) myProperty; 73 updateSelection(panel, jComboBox); 74 if (ModelFreezeUpdater.getState(event)) { 75 boolean e = panel != null && (panel.getSelectedServer() != null || panel.isRecentlyOpenFilterSelected()); 76 jComboBox.setEnabled(e); 77 } 78 } 79 } 80 81 private void updateSelection(IssueListToolWindowPanel panel, JComboBox combo) { 82 if (panel != null && !panel.getGroupBy().equals(combo.getSelectedItem())) { 83 combo.setSelectedItem(panel.getGroupBy()); 84 } 85 } 86 }