View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.atlassian.theplugin.idea.crucible;
17  
18  import com.atlassian.theplugin.commons.UiTaskAdapter;
19  import com.atlassian.theplugin.commons.UiTaskExecutor;
20  import com.atlassian.theplugin.commons.crucible.CrucibleServerFacade;
21  import com.atlassian.theplugin.commons.crucible.api.model.Review;
22  import com.atlassian.theplugin.commons.exception.ServerPasswordNotProvidedException;
23  import com.atlassian.theplugin.commons.remoteapi.RemoteApiException;
24  import com.atlassian.theplugin.commons.remoteapi.ServerData;
25  import com.atlassian.theplugin.idea.IdeaVersionFacade;
26  import com.atlassian.theplugin.idea.config.ProjectCfgManagerImpl;
27  import com.intellij.openapi.actionSystem.*;
28  import com.intellij.openapi.project.Project;
29  import com.intellij.openapi.ui.DialogWrapper;
30  import com.intellij.openapi.vcs.AbstractVcs;
31  import com.intellij.openapi.vcs.CachingCommittedChangesProvider;
32  import com.intellij.openapi.vcs.ProjectLevelVcsManager;
33  import com.intellij.openapi.vcs.RepositoryLocation;
34  import com.intellij.openapi.vcs.changes.ChangeList;
35  import com.intellij.openapi.vcs.changes.committed.CommittedChangesTreeBrowser;
36  import com.intellij.openapi.vcs.versionBrowser.ChangeBrowserSettings;
37  import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList;
38  import com.intellij.openapi.vfs.VirtualFile;
39  import com.intellij.vcsUtil.VcsUtil;
40  import org.jetbrains.annotations.NonNls;
41  import org.jetbrains.annotations.NotNull;
42  
43  import java.awt.*;
44  import java.util.Collections;
45  import java.util.List;
46  
47  public class CrucibleCreatePostCommitReviewForm extends AbstractCrucibleCreatePostCommitReviewForm implements DataProvider {
48  	public static final String COMMITTED_CHANGES_BROWSER = "theplugin.crucible.committedchangesbrowser";
49  	public static final DataKey<CrucibleCreatePostCommitReviewForm> COMMITTED_CHANGES_BROWSER_KEY = DataKey
50  			.create(COMMITTED_CHANGES_BROWSER);
51  
52  	private CommittedChangesTreeBrowser commitedChangesBrowser;
53  	private final UiTaskExecutor taskExecutor;
54  	private int revisionsNumber = 30;
55  
56  	public CrucibleCreatePostCommitReviewForm(final Project project, final CrucibleServerFacade crucibleServerFacade,
57  			@NotNull final ProjectCfgManagerImpl projectCfgManager, @NotNull final UiTaskExecutor taskExecutor) {
58  		super(project, crucibleServerFacade, "", projectCfgManager);
59  		this.taskExecutor = taskExecutor;
60  
61  		commitedChangesBrowser = new CommittedChangesTreeBrowser(project, Collections.<CommittedChangeList>emptyList());
62  		ActionManager manager = ActionManager.getInstance();
63  		ActionGroup group = (ActionGroup) manager.getAction("ThePlugin.CommittedChangesToolbar");
64  		ActionToolbar toolbar = manager.createActionToolbar("PostCommitReview", group, true);
65  		commitedChangesBrowser.addToolBar(toolbar.getComponent());
66  		setCustomComponent(commitedChangesBrowser);
67  
68  		this.taskExecutor.execute(new ChangesRefreshTask("Fetching recent commits", getContentPane()));
69  
70  		setTitle("Create Review");
71  		pack();
72  	}
73  
74  	public void updateChanges() {
75  		CrucibleRevisionsNumber dialog = new CrucibleRevisionsNumber(revisionsNumber);
76  		dialog.show();
77  		if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
78  			revisionsNumber = dialog.getValue();
79  			this.taskExecutor.execute(new ChangesRefreshTask("Fetching recent commits", getContentPane()));
80  		}
81  	}
82  
83  	@Override
84  	protected Review createReview(final ServerData server, final ReviewProvider reviewProvider)
85  			throws RemoteApiException, ServerPasswordNotProvidedException {
86  		final MyDataSink dataSink = new MyDataSink();
87  		//noinspection deprecation
88  		commitedChangesBrowser.calcData(DataKeys.CHANGE_LISTS, dataSink);
89  		final ChangeList[] changes = dataSink.getChanges();
90  		return createReviewImpl(server, reviewProvider, changes);
91  	}
92  
93  	public Object getData(@NonNls final String dataId) {
94  		if (dataId.equals(COMMITTED_CHANGES_BROWSER)) {
95  			return this;
96  		}
97  		return null;
98  	}
99  
100 	private class MyDataSink implements DataSink {
101 		public ChangeList[] getChanges() {
102 			return changes;
103 		}
104 
105 		private ChangeList[] changes;
106 
107 		public <T> void put(final DataKey<T> key, final T data) {
108 			changes = (ChangeList[]) data;
109 		}
110 	}
111 
112 	private class ChangesRefreshTask extends UiTaskAdapter {
113 		List<CommittedChangeList> list;
114 
115 		public ChangesRefreshTask(final String actionMame, final Component component) {
116 			super(actionMame, component);
117 		}
118 
119 		public void run() throws Exception {
120 			commitedChangesBrowser.setEmptyText("Fetching recent commits...");
121 			IdeaVersionFacade.getInstance()
122 					.setCommitedChangesList(commitedChangesBrowser, Collections.<CommittedChangeList>emptyList(), false);
123 			final VirtualFile baseDir = project.getBaseDir();
124 			if (baseDir == null) {
125 				throw new RuntimeException("Cannot determine base directory of the project");
126 			}
127 			ProjectLevelVcsManager mgr = ProjectLevelVcsManager.getInstance(project);
128 			AbstractVcs abstractVcs = mgr.getVcsFor(baseDir);
129 			if (abstractVcs != null) {
130 				@SuppressWarnings("unchecked")
131 				final CachingCommittedChangesProvider<CommittedChangeList, ChangeBrowserSettings> committedChangesProvider
132 						= abstractVcs.getCachingCommittedChangesProvider();
133 				if (committedChangesProvider == null) {
134 					throw new RuntimeException("Cannot determine VCS support for the project");
135 				}
136 				ChangeBrowserSettings changeBrowserSettings = new ChangeBrowserSettings();
137 				RepositoryLocation repositoryLocation = committedChangesProvider
138 						.getLocationFor(VcsUtil.getFilePath(baseDir.getPath()));
139 				list = committedChangesProvider.getCommittedChanges(changeBrowserSettings, repositoryLocation, revisionsNumber);
140 			}
141 		}
142 
143 		@Override
144 		public void onSuccess() {
145 			commitedChangesBrowser.setEmptyText("No recent commits");
146 			IdeaVersionFacade.getInstance().setCommitedChangesList(commitedChangesBrowser, list, false);
147 		}
148 	}
149 }