View Javadoc

1   package com.atlassian.theplugin.idea.crucible;
2   
3   import com.atlassian.theplugin.commons.configuration.CrucibleConfigurationBean;
4   import com.atlassian.theplugin.commons.configuration.PluginConfigurationBean;
5   import com.atlassian.theplugin.commons.crucible.CrucibleServerFacadeImpl;
6   import com.atlassian.theplugin.configuration.CrucibleWorkspaceConfiguration;
7   import com.atlassian.theplugin.configuration.WorkspaceConfigurationBean;
8   import com.atlassian.theplugin.idea.GridBagLayoutConstraints;
9   import com.atlassian.theplugin.idea.config.ProjectCfgManagerImpl;
10  import com.intellij.openapi.vcs.CheckinProjectPanel;
11  import com.intellij.openapi.vcs.VcsException;
12  import com.intellij.openapi.vcs.changes.CommitExecutor;
13  import com.intellij.openapi.vcs.checkin.CheckinHandler;
14  import com.intellij.openapi.vcs.checkin.CheckinHandlerFactory;
15  import com.intellij.openapi.vcs.ui.RefreshableOnComponent;
16  import org.jetbrains.annotations.NotNull;
17  import org.jetbrains.annotations.Nullable;
18  
19  import javax.swing.*;
20  import java.awt.*;
21  import java.awt.event.ActionEvent;
22  import java.awt.event.ActionListener;
23  import java.util.List;
24  
25  public class PostCommitReviewCheckinHandlerFactory extends CheckinHandlerFactory {
26  	private CrucibleWorkspaceConfiguration config;
27  	private final ProjectCfgManagerImpl projectCfgManager;
28  	private CrucibleConfigurationBean cruciblePluginConfig;
29  
30  	public PostCommitReviewCheckinHandlerFactory(@NotNull final WorkspaceConfigurationBean projectConfiguration,
31  			@NotNull ProjectCfgManagerImpl cfgManager,
32  			@NotNull PluginConfigurationBean pluginCfg) {
33  		this.projectCfgManager = cfgManager;
34  		config = projectConfiguration.getCrucibleConfiguration();
35  		cruciblePluginConfig = pluginCfg.getCrucibleConfigurationData();
36  	}
37  
38  	@NotNull
39  	public CheckinHandler createHandler(CheckinProjectPanel checkinProjectPanel) {
40  		return new Handler(checkinProjectPanel);
41  	}
42  
43  	private class Handler extends CheckinHandler {
44  		private CheckinProjectPanel checkinProjectPanel;
45  		private JCheckBox cbCreateReview = new JCheckBox("Create Crucible review");
46  
47  		private RefreshableOnComponent afterCheckinConfig = new AfterCheckinConfiguration();
48  		private CrucibleCreatePostCommitReviewDelayedForm form;
49  
50  		public Handler(CheckinProjectPanel checkinProjectPanel) {
51  			this.checkinProjectPanel = checkinProjectPanel;
52  			cbCreateReview.addActionListener(new ActionListener() {
53  				public void actionPerformed(ActionEvent actionEvent) {
54  					config.setCreateReviewOnCommit(cbCreateReview.isSelected());
55  				}
56  			});
57  		}
58  
59  		@Override
60  		public RefreshableOnComponent getBeforeCheckinConfigurationPanel() {
61  			return super.getBeforeCheckinConfigurationPanel();
62  		}
63  
64  		@Override
65  		public RefreshableOnComponent getAfterCheckinConfigurationPanel() {
66  			return afterCheckinConfig;
67  		}
68  
69  		@Override
70  		public ReturnResult beforeCheckin(@Nullable CommitExecutor commitExecutor) {
71  			if (cbCreateReview.isSelected()) {
72  				form = new CrucibleCreatePostCommitReviewDelayedForm(
73  						checkinProjectPanel.getProject(), CrucibleServerFacadeImpl.getInstance(),
74  						projectCfgManager, cruciblePluginConfig, checkinProjectPanel.getCommitMessage(),
75  						checkinProjectPanel.getVirtualFiles());
76  				form.show();
77  			}
78  			return ReturnResult.COMMIT;
79  		}
80  
81  		@Override
82  		public ReturnResult beforeCheckin() {
83  			return beforeCheckin(null);
84  		}
85  
86  		@Override
87  		public void checkinSuccessful() {
88  			if (form != null) {
89  				form.startReviewCreation();
90  			}
91  		}
92  
93  		@Override
94  		public void checkinFailed(List<VcsException> vcsExceptions) {
95  		}
96  
97  		private class AfterCheckinConfiguration implements RefreshableOnComponent {
98  
99  			public JComponent getComponent() {
100 				JPanel p = new JPanel(new GridBagLayout());
101 				GridBagConstraints gbc = new GridBagConstraints();
102 				gbc.gridx = 0;
103 				gbc.gridy = 0;
104 				gbc.weightx = 0.0;
105 				gbc.weighty = 0.0;
106 				gbc.fill = GridBagConstraints.NONE;
107 				p.add(cbCreateReview, gbc);
108 				gbc.gridx++;
109 				gbc.weightx = 1.0;
110 				gbc.fill = GridBagLayoutConstraints.HORIZONTAL;
111 				p.add(new JPanel(), gbc);
112 				return p;
113 			}
114 
115 			public void refresh() {
116 			}
117 
118 			public void saveState() {
119 				config.setCreateReviewOnCommit(cbCreateReview.isSelected());
120 			}
121 
122 			public void restoreState() {
123 				cbCreateReview.setSelected(config.isCreateReviewOnCommit());
124 			}
125 		}
126 	}
127 }