Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 42f2964c24bb64311b32013b9e4d0b2f99f028ec (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
package org.eclipse.team.internal.ccvs.ui.wizards;

/*
 * (c) Copyright IBM Corp. 2000, 2002.
 * All Rights Reserved.
 */

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.team.internal.ccvs.ui.wizards.KSubstWizard.KSubstChangeElement;
import org.eclipse.ui.model.WorkbenchViewerSorter;

public class KSubstWizardSummaryPage extends CVSWizardPage {
	private CheckboxTableViewer tableViewer = null;
	private KSubstOption[] ksubstOptions;
	private String[] ksubstOptionsDisplayText;
	private int filterType;
	
	private Button showUnaffectedFilesButton;
	private boolean showUnaffectedFiles;

	public KSubstWizardSummaryPage(String pageName, String title, ImageDescriptor image, boolean showUnaffectedFiles) {
		super(pageName, title, image);
		this.showUnaffectedFiles = showUnaffectedFiles;

		// sort the options by display text
		ksubstOptions = KSubstOption.getAllKSubstOptions();
		ksubstOptionsDisplayText = new String[ksubstOptions.length];
		Arrays.sort(ksubstOptions, new Comparator() {
			public int compare(Object a, Object b) {
				String aKey = getModeDisplayText((KSubstOption) a);
				String bKey = getModeDisplayText((KSubstOption) b);
				return aKey.compareTo(bKey);
			}
		});
		for (int i = 0; i < ksubstOptions.length; i++) {
			ksubstOptionsDisplayText[i] = getModeDisplayText(ksubstOptions[i]);
		}
	}
	
	public void createControl(Composite parent) {
		Composite top = new Composite(parent, SWT.NONE);
		top.setLayout(new GridLayout());
		setControl(top);
		createWrappingLabel(top, Policy.bind("KSubstWizardSummaryPage.contents"), 0, LABEL_WIDTH_HINT);		 //$NON-NLS-1$

		createSeparator(top, 0);

		showUnaffectedFilesButton = new Button(top, SWT.CHECK);
		showUnaffectedFilesButton.setText(Policy.bind("KSubstWizardSummaryPage.showUnaffectedFiles")); //$NON-NLS-1$
		showUnaffectedFilesButton.setSelection(showUnaffectedFiles);
		showUnaffectedFilesButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				BusyIndicator.showWhile(getContainer().getShell().getDisplay(), new Runnable() {
					public void run() {
						showUnaffectedFiles = showUnaffectedFilesButton.getSelection();
						refresh(false);
					}
				});
			}
		});

		tableViewer = createFileTableViewer(top,
			Policy.bind("KSubstWizardSummaryPage.summaryViewer.title"), //$NON-NLS-1$
			Policy.bind("KSubstWizardSummaryPage.summaryViewer.fileHeader"), //$NON-NLS-1$
			Policy.bind("KSubstWizardSummaryPage.summaryViewer.ksubstHeader"), //$NON-NLS-1$
			LIST_HEIGHT_HINT);
	}
	
	/**
	 * Creates a TableViewer whose input is a Map from IFile to KSubstOption.
	 * 
	 * @param parent the parent of the viewer
	 * @param title the text for the title label
	 * @param heightHint the nominal height of the list
	 * @return the created list viewer
	 */
	public CheckboxTableViewer createFileTableViewer(Composite parent, String title,
		String fileHeader, String ksubstHeader, int heightHint) {
		createLabel(parent, title);
		// create a table
		Table table = new Table(parent, SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		GridData data = new GridData(GridData.FILL_BOTH);
		data.heightHint = heightHint;
		table.setLayoutData(data);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		// add the columns
		TableColumn column = new TableColumn(table, SWT.LEFT);
		column.setText(fileHeader);
		column = new TableColumn(table, SWT.LEFT);
		column.setText(ksubstHeader);
		TableLayout tableLayout = new TableLayout();
		table.setLayout(tableLayout);
		tableLayout.addColumnData(new ColumnWeightData(1, true));
		tableLayout.addColumnData(new ColumnWeightData(1, true));

		// create a viewer for the table
		final CheckboxTableViewer tableViewer = new CheckboxTableViewer(table);
		tableViewer.setContentProvider(new IStructuredContentProvider() {
			public Object[] getElements(Object inputElement) {	
				return (Object[]) inputElement;
			}
			public void dispose() {
			}
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			}
		});
		
		// show file name and keyword substitution mode
		tableViewer.setLabelProvider(new ITableLabelProvider() {
			public Image getColumnImage(Object element, int columnIndex) {
				return null;
			}
			public String getColumnText(Object element, int columnIndex) {
				KSubstChangeElement change = (KSubstChangeElement) element;
				if (columnIndex == 0) {
					return change.getFile().getFullPath().toString();
				} else if (columnIndex == 1) {
					return getModeDisplayText(change.getKSubst());
				}
				return null;
			}
			public void addListener(ILabelProviderListener listener) {
			}
			public void dispose() {
			}
			public boolean isLabelProperty(Object element, String property) {
				return false;
			}
			public void removeListener(ILabelProviderListener listener) {
			}
		});
		
		// sort by file name
		tableViewer.setSorter(new WorkbenchViewerSorter() {
			public int compare(Viewer viewer, Object e1, Object e2) {
				IFile file1 = ((KSubstChangeElement) e1).getFile();
				IFile file2 = ((KSubstChangeElement) e2).getFile();
				return super.compare(viewer, file1, file2);
			}
		});
		
		// filter
		tableViewer.addFilter(new ViewerFilter() {
			public boolean select(Viewer viewer, Object parentElement, Object element) {
				KSubstChangeElement change = (KSubstChangeElement) element;
				return ( showUnaffectedFiles || change.isNewKSubstMode()) && change.matchesFilter(filterType);
			}
		});
		
		// add a check state listener
		tableViewer.addCheckStateListener(new ICheckStateListener() {
			public void checkStateChanged(CheckStateChangedEvent event) {
				KSubstChangeElement change = (KSubstChangeElement) event.getElement();
				if (tableViewer.getGrayed(change)) {
					// if it's grayed then give it the appearance of being disabled
					updateCheckStatus(change);
				} else {
					// otherwise record the change
					change.setExcluded(! event.getChecked());
				}
			}
		});
		
		// add a cell editor in the Keyword Substitution Mode column
		TableEditor tableEditor = new TableEditor(table);
		CellEditor cellEditor = new ComboBoxCellEditor(table, ksubstOptionsDisplayText);
		tableViewer.setCellEditors(new CellEditor[] { null, cellEditor });
		tableViewer.setColumnProperties(new String[] { "file", "mode" }); //$NON-NLS-1$ //$NON-NLS-2$
		tableViewer.setCellModifier(new ICellModifier() {
			public Object getValue(Object element, String property) {
				KSubstChangeElement change = (KSubstChangeElement) element;
				KSubstOption option = change.getKSubst();
				for (int i = 0; i < ksubstOptions.length; ++i) {
					if (ksubstOptions[i].equals(option)) return new Integer(i);
				}
				// XXX need to handle this better
				return null;
			}
			public boolean canModify(Object element, String property) {
				return true;
			}
			public void modify(Object element, String property, Object value) {
				// XXX The runtime type of 'element' seems to be a TableItem instead of the
				//     actual element data as with the other methods.  As a workaround, use
				//     the table's selection mechanism instead.
				IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
				element = selection.getFirstElement();
				int index = ((Integer) value).intValue();
				// selection will be -1 if some arbitrary text was entered since the combo box is not read only
				if (index != -1) {
					KSubstChangeElement change = (KSubstChangeElement) element;
					KSubstOption newOption = ksubstOptions[index];
					if (! newOption.equals(change.getKSubst())) {
						// the option has been changed, include it by default now if it wasn't before
						// since the user has shown interest in it
						change.setKSubst(newOption);
						change.setExcluded(false);
						tableViewer.refresh(change, true /*updateLabels*/);
						updateCheckStatus(change);
					}
				}
			}
		});
		return tableViewer;
	}
	
	public void setChangeList(List changes, int filterType) {
		this.filterType = filterType;
		tableViewer.setInput(changes.toArray());
		refresh(true);
	}
	
	private void refresh(boolean updateLabels) {
		tableViewer.refresh(updateLabels);
		Object[] elements = (Object[]) tableViewer.getInput();
		for (int i = 0; i < elements.length; i++) {
			KSubstChangeElement change = (KSubstChangeElement) elements[i];
			updateCheckStatus(change);
		}
	}
	
	private void updateCheckStatus(KSubstChangeElement change) {
		if (change.isNewKSubstMode()) {
			// if the mode differs, the checkbox indicates the inclusion/exclusion status
			tableViewer.setGrayed(change, false);
			tableViewer.setChecked(change, ! change.isExcluded());
		} else {
			// otherwise, the checkbox is meaningless except to indicate that the file will not be changed
			tableViewer.setGrayed(change, true);
			tableViewer.setChecked(change, false);
		}
	}
	
	private String getModeDisplayText(KSubstOption option) {
		return option.getLongDisplayText();
	}
}

Back to the top