Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53260515c63b075675929329d75bbca7a43faf73 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*******************************************************************************
 * Copyright (c) 2007, 2018 IBM Corporation 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.team.internal.ccvs.ui;

import java.util.*;
import java.util.List;

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.internal.ccvs.core.CVSRepositoryLocationMatcher;
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;

public class ConfigureRepositoryLocationsTable implements ICellModifier,
		IStructuredContentProvider, ITableLabelProvider {

	private static final class AlternativeRepositoryComparator extends
			ViewerComparator {

		public AlternativeRepositoryComparator() {
		}

		private int getCategory(Object element) {
			if (element instanceof RepositoryLocationItem) {
				return 0;
			}
			return 2;
		}

		@Override
		public int compare(Viewer viewer, Object e1, Object e2) {
			final int compare = getCategory(e1) - getCategory(e2);
			if (compare != 0)
				return compare;
			return super.compare(viewer, ((Item) e1).location,
					((Item) e2).location);
		}
	}

	public abstract static class Item implements Comparable {
		public final ICVSRepositoryLocation location;
		public List alternativeList;
		public int selected;

		public Item(ICVSRepositoryLocation name, List alternative) {
			this.location = name;
			this.alternativeList = alternative;
			this.selected = 0;
		}

		@Override
		public int compareTo(Object o) {
			return location.getLocation(false).compareTo(
					((Item) o).location.getLocation(false));
		}
	}

	public static class RepositoryLocationItem extends Item {
		public RepositoryLocationItem(
				ICVSRepositoryLocation projectSetRepositoryLocation,
				List suggestedRepositoryLocations) {
			super(projectSetRepositoryLocation, suggestedRepositoryLocations);
		}
	}

	protected static final String ITEM = "item"; //$NON-NLS-1$
	protected static final String PROPERTY_ALTERNATIVE_LIST = "alternativeList"; //$NON-NLS-1$

	private TableViewer fTableViewer;

	private CellEditor[] cellEditors;

	private TextCellEditor dummyAlternativeRepositoryEditor;

	private Table table;

	/**
	 * List of <code>AlternativeRepositoryTable.RepositoryLocationItem</code>
	 * used as an input to the table.
	 */
	private List fAlternatives;

	private boolean fShowOnlyCompatibeLocations = true;

	public ConfigureRepositoryLocationsTable(Map alternativesMap) {
		fAlternatives = new ArrayList();
		for (Iterator iterator = alternativesMap.entrySet().iterator(); iterator
				.hasNext();) {
			Map.Entry entry = (Map.Entry) iterator.next();
			fAlternatives
					.add(new ConfigureRepositoryLocationsTable.RepositoryLocationItem(
							(ICVSRepositoryLocation) entry.getKey(),
							(List) entry.getValue()));
		}
	}

	public Composite createControl(final Composite composite) {
		/**
		 * Create a table.
		 */
		table = new Table(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI
				| SWT.FULL_SELECTION);
		// table.setLayoutData(SWTUtils.createHVFillGridData());
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.addListener(SWT.MeasureItem, event -> {
			// int clientWidth = table.getClientArea().width;
			event.height = event.gc.getFontMetrics().getHeight() + 5;
			// event.width = clientWidth * 2;
		});

		/**
		 * The 'Project set repository location' column
		 */
		final TableColumn projectSetRepositoryColumn = new TableColumn(table,
				SWT.NONE, 0);
		projectSetRepositoryColumn
				.setText(CVSUIMessages.ConfigureRepositoryLocationsWizard_column0);

		/**
		 * The 'Repository location' column
		 */
		final TableColumn alternativeRepositoryColums = new TableColumn(table,
				SWT.NONE, 1);
		alternativeRepositoryColums
				.setText(CVSUIMessages.ConfigureRepositoryLocationsWizard_column1);

		composite.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				Rectangle area = composite.getClientArea();
				Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
				ScrollBar vBar = table.getVerticalBar();
				int width = area.width - table.computeTrim(0, 0, 0, 0).width
						- vBar.getSize().x;
				if (size.y > area.height + table.getHeaderHeight()) {
					// Subtract the scrollbar width from the total column width
					// if a vertical scrollbar will be required
					Point vBarSize = vBar.getSize();
					width -= vBarSize.x;
				}
				Point oldSize = table.getSize();
				if (oldSize.x > area.width) {
					// table is getting smaller so make the columns
					// smaller first and then resize the table to
					// match the client area width
					projectSetRepositoryColumn.setWidth(width / 2);
					alternativeRepositoryColums.setWidth(width
							- projectSetRepositoryColumn.getWidth());
					table.setSize(area.width, area.height);
				} else {
					// table is getting bigger so make the table
					// bigger first and then make the columns wider
					// to match the client area width
					table.setSize(area.width, area.height);
					projectSetRepositoryColumn.setWidth(width / 2);
					alternativeRepositoryColums.setWidth(width
							- projectSetRepositoryColumn.getWidth());
				}
			}
		});

		/**
		 * Create a viewer for the table.
		 */
		fTableViewer = new TableViewer(table);
		fTableViewer.setContentProvider(this);
		fTableViewer.setLabelProvider(this);
		fTableViewer.setComparator(new AlternativeRepositoryComparator());
		
		/**
		 * Add a cell editor in the 'Repository location' column
		 */
		TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(
				fTableViewer, new FocusCellOwnerDrawHighlighter(fTableViewer));
		ColumnViewerEditorActivationStrategy editorActivationStrategy = new ColumnViewerEditorActivationStrategy(
				fTableViewer) {
			@Override
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.F2);
			}
		};
		TableViewerEditor.create(fTableViewer, focusCellManager,
				editorActivationStrategy, ColumnViewerEditor.DEFAULT
						| ColumnViewerEditor.KEYBOARD_ACTIVATION);

		cellEditors = new CellEditor[2];
		cellEditors[0] = null;
		// to enable cell editing, create a dummy cell editor
		cellEditors[1] = dummyAlternativeRepositoryEditor = new TextCellEditor(
				table, SWT.READ_ONLY);

		fTableViewer.setCellEditors(cellEditors);
		fTableViewer.setColumnProperties(new String[] { ITEM,
				PROPERTY_ALTERNATIVE_LIST });
		fTableViewer.setCellModifier(this);
		fTableViewer.setInput(fAlternatives);
		
		// set initial selection
		for (int i = 0; i < fTableViewer.getTable().getItemCount(); i++) {
			Object element = fTableViewer.getElementAt(i);
			RepositoryLocationItem locationItem = (RepositoryLocationItem) element;
			// select second entry only when it's compatible and the first is
			// unknown (from project set file)
			if (locationItem.alternativeList.size() > 1
					&& !KnownRepositories
							.getInstance()
							.isKnownRepository(
									((ICVSRepositoryLocation) locationItem.alternativeList
											.get(0)).getLocation(false))
					&& CVSRepositoryLocationMatcher
							.isCompatible(
									locationItem.location,
									(ICVSRepositoryLocation) locationItem.alternativeList
											.get(1), false)) {
				locationItem.selected = 1;
			}
		}
		fTableViewer.refresh();
		return table;
	}

	@Override
	public Object getValue(Object element, String property) {

		final Item item = (Item) element;

		if (PROPERTY_ALTERNATIVE_LIST.equals(property)) {
			return Integer.valueOf(item.selected);
		}
		return null;
	}

	@Override
	public boolean canModify(Object element, String property) {
		// set the correct cell editor for this element
		cellEditors[1] = getCellEditor(element);
		// only allow modification for editable elements
		return PROPERTY_ALTERNATIVE_LIST.equals(property);
	}

	private CellEditor getCellEditor(Object element) {

		if (element instanceof RepositoryLocationItem) {
			// create combo-box list of alternative repositories
			RepositoryLocationItem item = (RepositoryLocationItem) element;
			
			return new ComboBoxCellEditor(table, getFilteredAlternativeRepositoriesForDisplay(item),
					SWT.READ_ONLY);
		}
		return dummyAlternativeRepositoryEditor;
	}

	@Override
	public void modify(Object element, String property, Object value) {

		final IStructuredSelection selection = fTableViewer.getStructuredSelection();
		final Item item = (Item) selection.getFirstElement();
		if (item == null)
			return;

		final int comboIndex = ((Integer) value).intValue();

		if (PROPERTY_ALTERNATIVE_LIST.equals(property)) {
			item.selected = comboIndex;
		}
		fTableViewer.refresh(item);
	}

	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		return null;
	}

	@Override
	public String getColumnText(Object element, int columnIndex) {
		final Item item = (Item) element;

		switch (columnIndex) {
		case 0:
			return item.location.getLocation(false);
		case 1: 
			return getFilteredAlternativeRepositoriesForDisplay(item)[item.selected];
		default:
			return null;
		}
	}

	@Override
	public void addListener(ILabelProviderListener listener) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {
	}

	@Override
	public Object[] getElements(Object inputElement) {
		return ((Collection) inputElement).toArray();
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	}

	public IStructuredSelection getSelection() {
		return fTableViewer.getStructuredSelection();
	}

	public TableViewer getViewer() {
		return fTableViewer;
	}

	/**
	 * @return a selected (in a combo box) alternative repository location for
	 *         the first selected element
	 */
	public CVSRepositoryLocation getSelectedAlternativeRepository() {
		RepositoryLocationItem firstElement = (RepositoryLocationItem) getSelection()
				.getFirstElement();
		return (CVSRepositoryLocation) getFilteredAlternativeRepositories(firstElement)
				.get(firstElement.selected);
	}

	/**
	 * Add newly created repository location to all RepositoryLocationItems. The
	 * location is added at the end of a list. For selected and compatible rows
	 * it will be automatically selected.
	 * 
	 * @param location
	 *            Location to add.
	 */
	void addAlternativeRepository(ICVSRepositoryLocation location) {
		for (int i = 0; i < fTableViewer.getTable().getItemCount(); i++) {
			Object element = fTableViewer.getElementAt(i);
			RepositoryLocationItem rli = (RepositoryLocationItem) element;
			// TODO: at this moment a newly created repository location
			// is added at the end of every locationItem, we could
			// consider sorting the list again
			rli.alternativeList.add(location);
			if (getSelection().toList().contains(element)
					&& CVSRepositoryLocationMatcher.isCompatible(location,
							rli.location, false)) {
				// at the end
				rli.selected = getFilteredAlternativeRepositories(rli).size() - 1;
			}
		}
		fTableViewer.refresh(true);
	}

	/**
	 * @return A map with repository location from the project set as a key and
	 *         selected repository location from a combo box as value.
	 */
	public Map getSelected() {
		Map map = new HashMap();
		for (Iterator iterator = fAlternatives.iterator(); iterator.hasNext();) {
			RepositoryLocationItem rli = (RepositoryLocationItem) iterator
					.next();
			map.put(rli.location, getFilteredAlternativeRepositories(rli).get(rli.selected));
		}
		return map;
	}

	/**
	 * Change the fShowOnlyCompatibeLocations flag. If set to <code>true</code>
	 * only compatible repository locations are shown, current selection will be
	 * updated when a non-compatible entry is selected. If set to
	 * <code>false</code> all repository locations are shown, current
	 * selection will be updated if necessary.
	 * 
	 * @param show
	 *            The flag
	 */
	public void setShowOnlyCompatibleLocations(boolean show) {
		fShowOnlyCompatibeLocations = show;
		for (int i = 0; i < fTableViewer.getTable().getItemCount(); i++) {
			Object element = fTableViewer.getElementAt(i);
			RepositoryLocationItem rli = (RepositoryLocationItem) element;
			updateSelection(rli); 
		}
		fTableViewer.refresh(true);
	}
	
	private List getFilteredAlternativeRepositories(Item item) {
		return getFilteredAlternativeRepositories(item, fShowOnlyCompatibeLocations);
	}
	
	private List getFilteredAlternativeRepositories(Item item, boolean showOnlyCompatible) {
		List alternativeList = item.alternativeList;
		if (!showOnlyCompatible) {
			return alternativeList;
		} else {
			List alternativeFiltered = new ArrayList();
			for (int i = 0; i < alternativeList.size(); i++) {
				CVSRepositoryLocation repo = (CVSRepositoryLocation) alternativeList.get(i);
				// If "Show only compatible..." option is on add only compatible
				// locations or the location itself
				if (!CVSRepositoryLocationMatcher.isCompatible(item.location,
						repo, true)){
					continue; // skip this repo location
				}
				alternativeFiltered.add(repo);
			}
			return alternativeFiltered;
		}
	}
	
	private void updateSelection(Item item) {
		if (fShowOnlyCompatibeLocations) {
			int shift = 0;
			for (int j = 0; j <= item.selected; j++) {
				ICVSRepositoryLocation rl = (ICVSRepositoryLocation) item.alternativeList
						.get(j);
				if (!CVSRepositoryLocationMatcher.isCompatible(item.location,
						rl, true)) {
					shift++;
				}
			}
			item.selected -= shift;

			// the selected location is neither compatible nor equal to the
			// one from the project set
			ICVSRepositoryLocation selected = (ICVSRepositoryLocation) getFilteredAlternativeRepositories(
					item).get(item.selected);
			if (!CVSRepositoryLocationMatcher.isCompatible(item.location,
					selected, true)) {
				item.selected = 0; // default
				// find compatible
				for (int j = 0; j < item.alternativeList.size(); j++) {
					ICVSRepositoryLocation l = (ICVSRepositoryLocation) item.alternativeList
							.get(j);
					if (CVSRepositoryLocationMatcher.isCompatible(l,
							item.location, true)) {
						item.selected = j;
						break;
					}
				}
			}
		} else {
			// show all
			int shift = 0;
			// index of (item.)selected object from the full list
			for (int j = 0; j <= getFilteredAlternativeRepositories(item)
					.indexOf(
							getFilteredAlternativeRepositories(item, true).get(
									item.selected)); j++) {
				ICVSRepositoryLocation rl = (ICVSRepositoryLocation) item.alternativeList
						.get(j);
				if (!CVSRepositoryLocationMatcher.isCompatible(item.location, rl,
						true)) {
					shift++;
				}
			}
			item.selected += shift;
		}
	}
	
	private String[] getFilteredAlternativeRepositoriesForDisplay(Item item) {
		List filteredAlternativeList = getFilteredAlternativeRepositories(item);
		List repositoriesForDisplay = new ArrayList();
		for (int i = 0; i < filteredAlternativeList.size(); i++) {
			CVSRepositoryLocation rl = (CVSRepositoryLocation) filteredAlternativeList
					.get(i);
			repositoriesForDisplay.add(rl.getLocation());
		}
		return (String[]) repositoriesForDisplay.toArray(new String[0]);
	}

}

Back to the top