KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > KSubstWizardSummaryPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.wizards;
12
13
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.viewers.CellEditor;
22 import org.eclipse.jface.viewers.CheckStateChangedEvent;
23 import org.eclipse.jface.viewers.CheckboxTableViewer;
24 import org.eclipse.jface.viewers.ColumnWeightData;
25 import org.eclipse.jface.viewers.ComboBoxCellEditor;
26 import org.eclipse.jface.viewers.ICellModifier;
27 import org.eclipse.jface.viewers.ICheckStateListener;
28 import org.eclipse.jface.viewers.ILabelProviderListener;
29 import org.eclipse.jface.viewers.IStructuredContentProvider;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.ITableLabelProvider;
32 import org.eclipse.jface.viewers.TableLayout;
33 import org.eclipse.jface.viewers.Viewer;
34 import org.eclipse.jface.viewers.ViewerFilter;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.custom.BusyIndicator;
37 import org.eclipse.swt.custom.TableEditor;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Event;
44 import org.eclipse.swt.widgets.Listener;
45 import org.eclipse.swt.widgets.Table;
46 import org.eclipse.swt.widgets.TableColumn;
47 import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption;
48 import org.eclipse.team.internal.ccvs.ui.IHelpContextIds;
49 import org.eclipse.team.internal.ccvs.ui.Policy;
50 import org.eclipse.team.internal.ccvs.ui.wizards.KSubstWizard.KSubstChangeElement;
51 import org.eclipse.ui.help.WorkbenchHelp;
52 import org.eclipse.ui.model.WorkbenchViewerSorter;
53
54 public class KSubstWizardSummaryPage extends CVSWizardPage {
55     private CheckboxTableViewer tableViewer = null;
56     private KSubstOption[] ksubstOptions;
57     private String JavaDoc[] ksubstOptionsDisplayText;
58     private int filterType;
59     
60     private Button showUnaffectedFilesButton;
61     private boolean showUnaffectedFiles;
62
63     public KSubstWizardSummaryPage(String JavaDoc pageName, String JavaDoc title, ImageDescriptor image, boolean showUnaffectedFiles) {
64         super(pageName, title, image);
65         this.showUnaffectedFiles = showUnaffectedFiles;
66
67         // sort the options by display text
68
ksubstOptions = KSubstOption.getAllKSubstOptions();
69         ksubstOptionsDisplayText = new String JavaDoc[ksubstOptions.length];
70         Arrays.sort(ksubstOptions, new Comparator JavaDoc() {
71             public int compare(Object JavaDoc a, Object JavaDoc b) {
72                 String JavaDoc aKey = getModeDisplayText((KSubstOption) a);
73                 String JavaDoc bKey = getModeDisplayText((KSubstOption) b);
74                 return aKey.compareTo(bKey);
75             }
76         });
77         for (int i = 0; i < ksubstOptions.length; i++) {
78             ksubstOptionsDisplayText[i] = getModeDisplayText(ksubstOptions[i]);
79         }
80     }
81     
82     public void createControl(Composite parent) {
83         Composite top = new Composite(parent, SWT.NONE);
84         top.setLayout(new GridLayout());
85         setControl(top);
86         createWrappingLabel(top, Policy.bind("KSubstWizardSummaryPage.contents"), 0); //$NON-NLS-1$
87

88         // set F1 help
89
WorkbenchHelp.setHelp(top, IHelpContextIds.KEYWORD_SUBSTITUTION_SUMMARY_PAGE);
90         
91         createSeparator(top, 0);
92
93         showUnaffectedFilesButton = new Button(top, SWT.CHECK);
94         showUnaffectedFilesButton.setText(Policy.bind("KSubstWizardSummaryPage.showUnaffectedFiles")); //$NON-NLS-1$
95
showUnaffectedFilesButton.setSelection(showUnaffectedFiles);
96         showUnaffectedFilesButton.addListener(SWT.Selection, new Listener() {
97             public void handleEvent(Event e) {
98                 BusyIndicator.showWhile(getContainer().getShell().getDisplay(), new Runnable JavaDoc() {
99                     public void run() {
100                         showUnaffectedFiles = showUnaffectedFilesButton.getSelection();
101                         refresh(false);
102                     }
103                 });
104             }
105         });
106
107         tableViewer = createFileTableViewer(top,
108             Policy.bind("KSubstWizardSummaryPage.summaryViewer.title"), //$NON-NLS-1$
109
Policy.bind("KSubstWizardSummaryPage.summaryViewer.fileHeader"), //$NON-NLS-1$
110
Policy.bind("KSubstWizardSummaryPage.summaryViewer.ksubstHeader"), //$NON-NLS-1$
111
LIST_HEIGHT_HINT);
112         Dialog.applyDialogFont(parent);
113     }
114     
115     /**
116      * Creates a TableViewer whose input is a Map from IFile to KSubstOption.
117      *
118      * @param parent the parent of the viewer
119      * @param title the text for the title label
120      * @param heightHint the nominal height of the list
121      * @return the created list viewer
122      */

123     public CheckboxTableViewer createFileTableViewer(Composite parent, String JavaDoc title,
124         String JavaDoc fileHeader, String JavaDoc ksubstHeader, int heightHint) {
125         createLabel(parent, title);
126         // create a table
127
Table table = new Table(parent, SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
128         GridData data = new GridData(GridData.FILL_BOTH);
129         data.heightHint = heightHint;
130         table.setLayoutData(data);
131         table.setLinesVisible(true);
132         table.setHeaderVisible(true);
133         
134         // add the columns
135
TableColumn column = new TableColumn(table, SWT.LEFT);
136         column.setText(fileHeader);
137         column = new TableColumn(table, SWT.LEFT);
138         column.setText(ksubstHeader);
139         TableLayout tableLayout = new TableLayout();
140         table.setLayout(tableLayout);
141         tableLayout.addColumnData(new ColumnWeightData(1, true));
142         tableLayout.addColumnData(new ColumnWeightData(1, true));
143
144         // create a viewer for the table
145
final CheckboxTableViewer tableViewer = new CheckboxTableViewer(table);
146         tableViewer.setContentProvider(new IStructuredContentProvider() {
147             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
148                 return (Object JavaDoc[]) inputElement;
149             }
150             public void dispose() {
151             }
152             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
153             }
154         });
155         
156         // show file name and keyword substitution mode
157
tableViewer.setLabelProvider(new ITableLabelProvider() {
158             public Image getColumnImage(Object JavaDoc element, int columnIndex) {
159                 return null;
160             }
161             public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
162                 KSubstChangeElement change = (KSubstChangeElement) element;
163                 if (columnIndex == 0) {
164                     return change.getFile().getFullPath().toString();
165                 } else if (columnIndex == 1) {
166                     return getModeDisplayText(change.getKSubst());
167                 }
168                 return null;
169             }
170             public void addListener(ILabelProviderListener listener) {
171             }
172             public void dispose() {
173             }
174             public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
175                 return false;
176             }
177             public void removeListener(ILabelProviderListener listener) {
178             }
179         });
180         
181         // sort by file name
182
tableViewer.setSorter(new WorkbenchViewerSorter() {
183             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
184                 IFile file1 = ((KSubstChangeElement) e1).getFile();
185                 IFile file2 = ((KSubstChangeElement) e2).getFile();
186                 return super.compare(viewer, file1, file2);
187             }
188         });
189         
190         // filter
191
tableViewer.addFilter(new ViewerFilter() {
192             public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
193                 KSubstChangeElement change = (KSubstChangeElement) element;
194                 return ( showUnaffectedFiles || change.isNewKSubstMode()) && change.matchesFilter(filterType);
195             }
196         });
197         
198         // add a check state listener
199
tableViewer.addCheckStateListener(new ICheckStateListener() {
200             public void checkStateChanged(CheckStateChangedEvent event) {
201                 KSubstChangeElement change = (KSubstChangeElement) event.getElement();
202                 if (tableViewer.getGrayed(change)) {
203                     // if it's grayed then give it the appearance of being disabled
204
updateCheckStatus(change);
205                 } else {
206                     // otherwise record the change
207
change.setExcluded(! event.getChecked());
208                 }
209             }
210         });
211         
212         // add a cell editor in the Keyword Substitution Mode column
213
TableEditor tableEditor = new TableEditor(table);
214         CellEditor cellEditor = new ComboBoxCellEditor(table, ksubstOptionsDisplayText);
215         tableViewer.setCellEditors(new CellEditor[] { null, cellEditor });
216         tableViewer.setColumnProperties(new String JavaDoc[] { "file", "mode" }); //$NON-NLS-1$ //$NON-NLS-2$
217
tableViewer.setCellModifier(new ICellModifier() {
218             public Object JavaDoc getValue(Object JavaDoc element, String JavaDoc property) {
219                 KSubstChangeElement change = (KSubstChangeElement) element;
220                 KSubstOption option = change.getKSubst();
221                 for (int i = 0; i < ksubstOptions.length; ++i) {
222                     if (ksubstOptions[i].equals(option)) return new Integer JavaDoc(i);
223                 }
224                 // XXX need to handle this better
225
return null;
226             }
227             public boolean canModify(Object JavaDoc element, String JavaDoc property) {
228                 return true;
229             }
230             public void modify(Object JavaDoc element, String JavaDoc property, Object JavaDoc value) {
231                 // XXX The runtime type of 'element' seems to be a TableItem instead of the
232
// actual element data as with the other methods. As a workaround, use
233
// the table's selection mechanism instead.
234
IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
235                 element = selection.getFirstElement();
236                 int index = ((Integer JavaDoc) value).intValue();
237                 // selection will be -1 if some arbitrary text was entered since the combo box is not read only
238
if (index != -1) {
239                     KSubstChangeElement change = (KSubstChangeElement) element;
240                     KSubstOption newOption = ksubstOptions[index];
241                     if (! newOption.equals(change.getKSubst())) {
242                         // the option has been changed, include it by default now if it wasn't before
243
// since the user has shown interest in it
244
change.setKSubst(newOption);
245                         change.setExcluded(false);
246                         tableViewer.refresh(change, true /*updateLabels*/);
247                         updateCheckStatus(change);
248                     }
249                 }
250             }
251         });
252         return tableViewer;
253     }
254     
255     public void setChangeList(List JavaDoc changes, int filterType) {
256         this.filterType = filterType;
257         tableViewer.setInput(changes.toArray());
258         refresh(true);
259     }
260     
261     private void refresh(boolean updateLabels) {
262         tableViewer.refresh(updateLabels);
263         Object JavaDoc[] elements = (Object JavaDoc[]) tableViewer.getInput();
264         for (int i = 0; i < elements.length; i++) {
265             KSubstChangeElement change = (KSubstChangeElement) elements[i];
266             updateCheckStatus(change);
267         }
268     }
269     
270     private void updateCheckStatus(KSubstChangeElement change) {
271         if (change.isNewKSubstMode()) {
272             // if the mode differs, the checkbox indicates the inclusion/exclusion status
273
tableViewer.setGrayed(change, false);
274             tableViewer.setChecked(change, ! change.isExcluded());
275         } else {
276             // otherwise, the checkbox is meaningless except to indicate that the file will not be changed
277
tableViewer.setGrayed(change, true);
278             tableViewer.setChecked(change, false);
279         }
280     }
281     
282     private String JavaDoc getModeDisplayText(KSubstOption option) {
283         return option.getLongDisplayText();
284     }
285     public void setVisible(boolean visible) {
286         super.setVisible(visible);
287         if (visible) {
288             showUnaffectedFilesButton.setFocus();
289         }
290     }
291 }
292
Popular Tags