Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0034ef15204644430ed333133696df9c0d0f547d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
 * Copyright (c) 2013 Ericsson AB and others.
 * 
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Description:
 * 
 * This class implements the "Add ..." a new Gerrit 
 * project locations.
 * 
 * Contributors:
 *   Jacques Bouthillier - Created for Mylyn Review R4E-Gerrit project
 *   
 ******************************************************************************/
package org.eclipse.mylyn.reviews.r4e_gerrit.ui.internal.commands;

import java.util.Map;
import java.util.Set;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.mylyn.internal.gerrit.core.GerritConnector;
import org.eclipse.mylyn.internal.gerrit.core.GerritQuery;
import org.eclipse.mylyn.internal.tasks.ui.wizards.EditRepositoryWizard;
import org.eclipse.mylyn.reviews.r4e_gerrit.R4EGerritPlugin;
import org.eclipse.mylyn.reviews.r4e_gerrit.ui.internal.utils.R4EGerritServerUtility;
import org.eclipse.mylyn.reviews.r4e_gerrit.ui.internal.utils.R4EUIConstants;
import org.eclipse.mylyn.reviews.r4egerrit.ui.views.R4EGerritTableView;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.wizards.TaskRepositoryWizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Widget;


/**
 * @author Jacques Bouthillier
 * @version $Revision: 1.0 $
 */
public class AddGerritSiteHandler extends AbstractHandler {

	// ------------------------------------------------------------------------
	// Constants
	// ------------------------------------------------------------------------

	/**
	 * Field COMMAND_MESSAGE. (value is ""Add a Gerrit location ..."")
	 */
	private static final String COMMAND_MESSAGE = "Add a Gerrit location ...";

	// ------------------------------------------------------------------------
	// Variables
	// ------------------------------------------------------------------------

	private R4EGerritServerUtility fServerUtil = null;
	
	private Map<TaskRepository, String> fMapRepoServer = null;

	
	// ------------------------------------------------------------------------
	// Methods
	// ------------------------------------------------------------------------

	/**
	 * Method execute.
	 * 
	 * @param aEvent
	 *            ExecutionEvent
	 * @return Object
	 * @see org.eclipse.core.commands.IHandler#execute(ExecutionEvent)
	 */
	public Object execute(final ExecutionEvent aEvent) {

	    R4EGerritPlugin.Ftracer.traceInfo("Create the Add button to search the Gerrit location " ); //$NON-NLS-1$
		String menuItemText = "";
		fServerUtil = R4EGerritServerUtility.getInstance();
		Object obj = aEvent.getTrigger();
		Map<String, String> param = aEvent.getParameters();
		R4EGerritTableView reviewTableView = R4EGerritTableView
				.getActiveView();
		
		if (obj instanceof Event) {
			Event ev = (Event) obj;
			Widget objWidget = ev.widget;
			if (objWidget instanceof MenuItem) {
				MenuItem menuItem = (MenuItem) objWidget;
				menuItemText = menuItem.getText();
				R4EGerritPlugin.Ftracer.traceInfo("MenuItem: " + menuItemText + "\t value: " +
						param.get(menuItemText) + " VS saved: "  + fServerUtil.getLastSavedGerritServer());
				fMapRepoServer = fServerUtil.getGerritMapping();
				String stURL = fServerUtil.getMenuSelectionURL(menuItemText);
				R4EGerritPlugin.Ftracer.traceInfo("URL for the menuItemText: " + stURL);
				if (!fMapRepoServer.isEmpty()) {
					Set<TaskRepository> mapSet = fMapRepoServer.keySet();
					R4EGerritPlugin.Ftracer.traceInfo("-------------------");
					for (TaskRepository key: mapSet) {
					    R4EGerritPlugin.Ftracer.traceInfo("Map Key name: " + key.getRepositoryLabel() + "\t URL: " + fMapRepoServer.get(key));
					}
				}
				// Open the review table first;
				reviewTableView.openView(); 
				
				//Verify if we selected the "Add.." button or a pre=defined Gerrit
				if (stURL != null) {
					if (stURL.equals(fServerUtil.getLastSavedGerritServer())) {
					    R4EGerritPlugin.Ftracer.traceInfo("LAST SAVED server is the SAME ");
						fServerUtil.getReviewListFromServer ();
						
						//Initiate the request for the list of reviews with a default query
						reviewTableView.processCommands(GerritQuery.MY_WATCHED_CHANGES);

						return Status.OK_STATUS; //For now , do not process the dialogue
					} else {
						//Store the new Gerrit server into a file
						fServerUtil.saveLastGerritServer(stURL);
						fServerUtil.getReviewListFromServer ();
						
						//Initiate the request for the list of reviews with a default query
						reviewTableView.processCommands(GerritQuery.MY_WATCHED_CHANGES);

						return Status.OK_STATUS; //For now , do not process the dialogue
					}
				}
			}
		}

		//Open the Dialogue to enter a new Gerrit URL
		Object dialogObj = openDialogue ();
//		//Set the table view with the last TaskRepo and the default query
//		reviewTableView.processCommands(GerritQuery.MY_WATCHED_CHANGES);
		
		return dialogObj;
	}

	/**
	 * Initiate a JOB to open the Gerrit definition dialogue
	 * @return Object
	 */
	private Object openDialogue () {
		final Job job = new Job(COMMAND_MESSAGE) {

			public String familyName = R4EUIConstants.R4E_UI_JOB_FAMILY;

			@Override
			public boolean belongsTo(Object aFamily) {
				return familyName.equals(aFamily);
			}

			@Override
			public IStatus run(final IProgressMonitor aMonitor) {
				aMonitor.beginTask(COMMAND_MESSAGE, IProgressMonitor.UNKNOWN);
						
				TaskRepository taskRepository = getTaskRepository(fServerUtil.getLastSavedGerritServer()); 
				
				R4EGerritPlugin.Ftracer.traceInfo("repository:   " + taskRepository.getUrl()); //$NON-NLS-1$
				
				final EditRepositoryWizard wizard = new EditRepositoryWizard(taskRepository);
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
							WizardDialog dialog = new TaskRepositoryWizardDialog(wizard.getShell(), wizard);
							dialog.create();
							dialog.setBlockOnOpen(true);
							dialog.open();
					}
				});
				
				//When the wizard is closed
				taskRepository = wizard.getRepository();//Possibility the taskRepository has changed
				if (taskRepository.getUrl().isEmpty() || 
						taskRepository.getUrl().endsWith(R4EUIConstants.DEFAULT_REPOSITORY)) {
					//User selected the Cancel button
				    R4EGerritPlugin.Ftracer.traceInfo("AFTER: repository: CANCEL "  ); //$NON-NLS-1$
				} else {
				    R4EGerritPlugin.Ftracer.traceInfo("AFTER: repository: :  FINISH " ); //$NON-NLS-1$		
					fServerUtil.saveLastGerritServer(taskRepository.getUrl());
					//Test if we already have the Gerrit server in our internal map
					TaskRepository taskRepositoryTmp = fServerUtil.getTaskRepo (taskRepository.getUrl());
					if (taskRepositoryTmp == null) {
						//Need to re-map our internal Gerrit Repo
						fServerUtil.getGerritMapping();
					}
					/*****************************************************/
					/*                                                   */
					/*    Now, we need to get the Gerrit repo data       */
					/*    and populate the list of Reviews               */
					/*                                                   */
					/*                                                   */
					/*****************************************************/
					fServerUtil.getReviewListFromServer ();
					R4EGerritTableView reviewTableView = R4EGerritTableView
							.getActiveView();

					//Set the table view with the last TaskRepo and the default query
					reviewTableView.processCommands(GerritQuery.MY_WATCHED_CHANGES);

				}
				
				R4EGerritPlugin.Ftracer.traceInfo("AFTER: repository: :  " + taskRepository.getUrl() + 
						"\n\t repo: " + taskRepository.getRepositoryUrl() ); //$NON-NLS-1$
				

				aMonitor.done();
				return Status.OK_STATUS;
			}
		};
		job.setUser(true);
		job.schedule();
		return null;	
	}
	
	
	/**
	 * Look at the current Gerrit repository and return a default value 
	 * 
	 * @param String default URL
	 * @return TaskRepository
	 */
	private TaskRepository getTaskRepository (String aUrl) {
		TaskRepository taskRepo = null;
		//Search for the current Gerrit connector
		taskRepo =  R4EGerritServerUtility.getInstance().getTaskRepo(aUrl);
		
		if (taskRepo == null) {
			if (aUrl != null) {
				taskRepo = new TaskRepository (GerritConnector.CONNECTOR_KIND, aUrl);
			} else {
				//Create a default Task repo
				taskRepo = new TaskRepository (GerritConnector.CONNECTOR_KIND, R4EUIConstants.DEFAULT_REPOSITORY);				
			}
			
		} else {
		    R4EGerritPlugin.Ftracer.traceInfo("Repo already in list:  " + taskRepo.getRepositoryLabel()); 
			
		}
		return taskRepo;
	}
	
}

Back to the top