View Javadoc

1   package com.atlassian.theplugin.idea.action.reviews;
2   
3   import com.atlassian.theplugin.commons.util.LoggerImpl;
4   import com.atlassian.theplugin.idea.IdeaHelper;
5   import com.atlassian.theplugin.idea.crucible.CrucibleReviewGroupBy;
6   import com.atlassian.theplugin.idea.crucible.ReviewListToolWindowPanel;
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  /**
19   * User: jgorycki
20   * Date: Dec 5, 2008
21   * Time: 2:26:25 PM
22   */
23  public class GroupByAction extends AbstractCrucibleToolbarAction implements CustomComponentAction {
24  	private static final String COMBOBOX_KEY = GroupByAction.class.getName() + ".combo";
25  
26  	@Override
27  	public void actionPerformed(AnActionEvent e) {
28  	}
29  
30  	public JComponent createCustomComponent(Presentation presentation) {
31  		final JComboBox combo = new JComboBox(createModel());
32  		ComboWithLabel cwl = new ComboWithLabel(combo, "Group By");
33  
34  		Project project = IdeaHelper.getCurrentProject(DataManager.getInstance().getDataContext());
35  		if (project != null) {
36  			ReviewListToolWindowPanel panel = IdeaHelper.getReviewListToolWindowPanel(project);
37  			updateSelection(panel, combo);
38  		}
39  
40  		presentation.putClientProperty(COMBOBOX_KEY, combo);
41  		combo.addActionListener(new ActionListener() {
42  			public void actionPerformed(ActionEvent e) {
43  				final Project currentProject = IdeaHelper.getCurrentProject(DataManager.getInstance().getDataContext(combo));
44  				if (currentProject != null) {
45  					ReviewListToolWindowPanel panel = IdeaHelper.getReviewListToolWindowPanel(currentProject);
46  					if (panel != null) {
47  						panel.setGroupBy((CrucibleReviewGroupBy) combo.getSelectedItem());
48  					} else {
49  						LoggerImpl.getInstance().error(GroupByAction.class.getName() + ": cannot find "
50  								+ ReviewListToolWindowPanel.class);
51  					}
52  				} else {
53  					LoggerImpl.getInstance().error(GroupByAction.class.getName() + ": cannot determine current project");
54  				}
55  
56  			}
57  		});
58  		return cwl;
59  	}
60  
61  
62  	private ComboBoxModel createModel() {
63  		return new DefaultComboBoxModel(CrucibleReviewGroupBy.values());
64  	}
65  
66  	@Override
67  	protected void onUpdateFinished(AnActionEvent e, boolean enabled) {
68  		Object myProperty = e.getPresentation().getClientProperty(COMBOBOX_KEY);
69  		if (myProperty instanceof JComboBox) {
70  			final JComboBox jComboBox = (JComboBox) myProperty;
71  			jComboBox.setEnabled(enabled);
72  			ReviewListToolWindowPanel panel = IdeaHelper.getReviewListToolWindowPanel(e);
73  			updateSelection(panel, jComboBox);
74  		}
75  	}
76  
77  	private void updateSelection(ReviewListToolWindowPanel panel, JComboBox combo) {
78  		if (panel != null && !panel.getGroupBy().equals(combo.getSelectedItem())) {
79  			combo.setSelectedItem(panel.getGroupBy());
80  		}
81  	}
82  }