Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2005 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
|
12 |
package org.eclipse.team.internal.ccvs.ui; |
13 |
|
14 |
import java.util.*; |
15 |
import java.util.List; |
16 |
|
17 |
import org.eclipse.jface.viewers.*; |
18 |
import org.eclipse.swt.SWT; |
19 |
import org.eclipse.swt.custom.TableEditor; |
20 |
import org.eclipse.swt.events.ControlAdapter; |
21 |
import org.eclipse.swt.events.ControlEvent; |
22 |
import org.eclipse.swt.graphics.*; |
23 |
import org.eclipse.swt.widgets.*; |
24 |
import org.eclipse.team.internal.ccvs.core.CVSProjectSetCapability; |
25 |
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation; |
26 |
import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; |
27 |
|
28 |
public class ConfigureRepositoryLocationsTable implements ICellModifier, |
29 |
IStructuredContentProvider, ITableLabelProvider { |
30 |
|
31 |
private static final class AlternativeRepositoryComparator extends |
32 |
ViewerComparator { |
33 |
|
34 |
public AlternativeRepositoryComparator() { |
35 |
} |
36 |
|
37 |
private int getCategory(Object element) { |
38 |
if (element instanceof RepositoryLocationItem) { |
39 |
return 0; |
40 |
} |
41 |
return 2; |
42 |
} |
43 |
|
44 |
public int compare(Viewer viewer, Object e1, Object e2) { |
45 |
final int compare = getCategory(e1) - getCategory(e2); |
46 |
if (compare != 0) |
47 |
return compare; |
48 |
return super.compare(viewer, ((Item) e1).location, |
49 |
((Item) e2).location); |
50 |
} |
51 |
} |
52 |
|
53 |
public abstract static class Item implements Comparable { |
54 |
public final ICVSRepositoryLocation location; |
55 |
public List alternativeList; |
56 |
public int selected; |
57 |
|
58 |
public Item(ICVSRepositoryLocation name, List alternative) { |
59 |
this.location = name; |
60 |
this.alternativeList = alternative; |
61 |
this.selected = 0; |
62 |
} |
63 |
|
64 |
/* |
65 |
* (non-Javadoc) |
66 |
* |
67 |
* @see java.lang.Comparable#compareTo(java.lang.Object) |
68 |
*/ |
69 |
public int compareTo(Object o) { |
70 |
return location.getLocation(false).compareTo( |
71 |
((Item) o).location.getLocation(false)); |
72 |
} |
73 |
} |
74 |
|
75 |
public static class RepositoryLocationItem extends Item { |
76 |
public RepositoryLocationItem( |
77 |
ICVSRepositoryLocation projectSetRepositoryLocation, |
78 |
List suggestedRepositoryLocations) { |
79 |
super(projectSetRepositoryLocation, suggestedRepositoryLocations); |
80 |
} |
81 |
} |
82 |
|
83 |
protected static final String ITEM = "item"; //$NON-NLS-1$ |
84 |
protected static final String PROPERTY_ALTERNATIVE_LIST = "alternativeList"; //$NON-NLS-1$ |
85 |
|
86 |
private TableViewer fTableViewer; |
87 |
|
88 |
private CellEditor[] cellEditors; |
89 |
|
90 |
private TextCellEditor dummyAlternativeRepositoryEditor; |
91 |
|
92 |
private Table table; |
93 |
|
94 |
/** |
95 |
* List of <code>AlternativeRepositoryTable.RepositoryLocationItem</code> |
96 |
* used as an input to the table. |
97 |
*/ |
98 |
private List fAlternatives; |
99 |
|
100 |
/** |
101 |
* Indicates whether a connection method should be displayed in the first |
102 |
* column - project set information. |
103 |
*/ |
104 |
private boolean fShowConnectionMethod; |
105 |
|
106 |
private boolean fNoDuplicateRepositoryLocationFound; |
107 |
|
108 |
public ConfigureRepositoryLocationsTable(Map alternativesMap) { |
109 |
fAlternatives = new ArrayList(); |
110 |
Set checkSet = new HashSet(); |
111 |
for (Iterator iterator = alternativesMap.entrySet().iterator(); iterator |
112 |
.hasNext();) { |
113 |
Map.Entry entry = (Map.Entry) iterator.next(); |
114 |
fAlternatives |
115 |
.add(new ConfigureRepositoryLocationsTable.RepositoryLocationItem( |
116 |
(ICVSRepositoryLocation) entry.getKey(), |
117 |
(List) entry.getValue())); |
118 |
fNoDuplicateRepositoryLocationFound = checkSet |
119 |
.add(excludeConnectionMethod((ICVSRepositoryLocation) entry |
120 |
.getKey())); |
121 |
} |
122 |
fShowConnectionMethod = !fNoDuplicateRepositoryLocationFound; |
123 |
// we won't need it anymore |
124 |
checkSet = null; |
125 |
} |
126 |
|
127 |
public Composite createControl(final Composite composite) { |
128 |
/** |
129 |
* Create a table. |
130 |
*/ |
131 |
table = new Table(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI |
132 |
| SWT.FULL_SELECTION); |
133 |
// table.setLayoutData(SWTUtils.createHVFillGridData()); |
134 |
table.setLinesVisible(true); |
135 |
table.setHeaderVisible(true); |
136 |
table.addListener(SWT.MeasureItem, new Listener() { |
137 |
public void handleEvent(Event event) { |
138 |
// int clientWidth = table.getClientArea().width; |
139 |
event.height = event.gc.getFontMetrics().getHeight() + 5; |
140 |
// event.width = clientWidth * 2; |
141 |
} |
142 |
}); |
143 |
|
144 |
/** |
145 |
* The 'Project Set repository location' column |
146 |
*/ |
147 |
final TableColumn projectSetRepositoryColumn = new TableColumn(table, |
148 |
SWT.NONE, 0); |
149 |
projectSetRepositoryColumn |
150 |
.setText(CVSUIMessages.ConfigureRepositoryLocationsWizard_column0); |
151 |
|
152 |
/** |
153 |
* The 'Alternative repository locations' column |
154 |
*/ |
155 |
final TableColumn alternativeRepositoryColums = new TableColumn(table, |
156 |
SWT.NONE, 1); |
157 |
alternativeRepositoryColums |
158 |
.setText(CVSUIMessages.ConfigureRepositoryLocationsWizard_column1); |
159 |
|
160 |
composite.addControlListener(new ControlAdapter() { |
161 |
public void controlResized(ControlEvent e) { |
162 |
Rectangle area = composite.getClientArea(); |
163 |
Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT); |
164 |
ScrollBar vBar = table.getVerticalBar(); |
165 |
int width = area.width - table.computeTrim(0, 0, 0, 0).width |
166 |
- vBar.getSize().x; |
167 |
if (size.y > area.height + table.getHeaderHeight()) { |
168 |
// Subtract the scrollbar width from the total column width |
169 |
// if a vertical scrollbar will be required |
170 |
Point vBarSize = vBar.getSize(); |
171 |
width -= vBarSize.x; |
172 |
} |
173 |
Point oldSize = table.getSize(); |
174 |
if (oldSize.x > area.width) { |
175 |
// table is getting smaller so make the columns |
176 |
// smaller first and then resize the table to |
177 |
// match the client area width |
178 |
projectSetRepositoryColumn.setWidth(width / 2); |
179 |
alternativeRepositoryColums.setWidth(width |
180 |
- projectSetRepositoryColumn.getWidth()); |
181 |
table.setSize(area.width, area.height); |
182 |
} else { |
183 |
// table is getting bigger so make the table |
184 |
// bigger first and then make the columns wider |
185 |
// to match the client area width |
186 |
table.setSize(area.width, area.height); |
187 |
projectSetRepositoryColumn.setWidth(width / 2); |
188 |
alternativeRepositoryColums.setWidth(width |
189 |
- projectSetRepositoryColumn.getWidth()); |
190 |
} |
191 |
} |
192 |
}); |
193 |
|
194 |
/** |
195 |
* Create a viewer for the table. |
196 |
*/ |
197 |
fTableViewer = new TableViewer(table); |
198 |
fTableViewer.setContentProvider(this); |
199 |
fTableViewer.setLabelProvider(this); |
200 |
fTableViewer.setComparator(new AlternativeRepositoryComparator()); |
201 |
|
202 |
/** |
203 |
* Add a cell editor in the 'Alternative repository locations' column |
204 |
*/ |
205 |
new TableEditor(table); |
206 |
|
207 |
cellEditors = new CellEditor[2]; |
208 |
cellEditors[0] = null; |
209 |
// to enable cell editing, create a dummy cell editor |
210 |
cellEditors[1] = dummyAlternativeRepositoryEditor = new TextCellEditor( |
211 |
table, SWT.READ_ONLY); |
212 |
|
213 |
fTableViewer.setCellEditors(cellEditors); |
214 |
fTableViewer.setColumnProperties(new String[] { ITEM, |
215 |
PROPERTY_ALTERNATIVE_LIST }); |
216 |
fTableViewer.setCellModifier(this); |
217 |
fTableViewer.setInput(fAlternatives); |
218 |
|
219 |
return table; |
220 |
} |
221 |
|
222 |
public Object getValue(Object element, String property) { |
223 |
|
224 |
final Item item = (Item) element; |
225 |
|
226 |
if (PROPERTY_ALTERNATIVE_LIST.equals(property)) { |
227 |
return new Integer(item.selected); |
228 |
} |
229 |
return null; |
230 |
} |
231 |
|
232 |
public boolean canModify(Object element, String property) { |
233 |
// set the correct cell editor for this element |
234 |
cellEditors[1] = getCellEditor(element); |
235 |
// only allow modification for editable elements |
236 |
return PROPERTY_ALTERNATIVE_LIST.equals(property); |
237 |
} |
238 |
|
239 |
private CellEditor getCellEditor(Object element) { |
240 |
|
241 |
if (element instanceof RepositoryLocationItem) { |
242 |
|
243 |
// create combo-box list of alternative repositories |
244 |
List alternativeList = ((RepositoryLocationItem) element).alternativeList; |
245 |
String[] alternativeNames = new String[alternativeList.size()]; |
246 |
int i = 0; |
247 |
for (Iterator iterator = alternativeList.iterator(); iterator |
248 |
.hasNext();) { |
249 |
CVSRepositoryLocation repo = (CVSRepositoryLocation) iterator |
250 |
.next(); |
251 |
alternativeNames[i++] = repo.getLocation(); |
252 |
} |
253 |
return new ComboBoxCellEditor(table, alternativeNames, |
254 |
SWT.READ_ONLY); |
255 |
} |
256 |
return dummyAlternativeRepositoryEditor; |
257 |
} |
258 |
|
259 |
public void modify(Object element, String property, Object value) { |
260 |
|
261 |
final IStructuredSelection selection = (IStructuredSelection) fTableViewer |
262 |
.getSelection(); |
263 |
final Item item = (Item) selection.getFirstElement(); |
264 |
if (item == null) |
265 |
return; |
266 |
|
267 |
final int comboIndex = ((Integer) value).intValue(); |
268 |
|
269 |
if (PROPERTY_ALTERNATIVE_LIST.equals(property)) { |
270 |
item.selected = comboIndex; |
271 |
} |
272 |
fTableViewer.refresh(item); |
273 |
} |
274 |
|
275 |
public Image getColumnImage(Object element, int columnIndex) { |
276 |
return null; |
277 |
} |
278 |
|
279 |
public String getColumnText(Object element, int columnIndex) { |
280 |
final Item item = (Item) element; |
281 |
|
282 |
switch (columnIndex) { |
283 |
case 0: |
284 |
return fShowConnectionMethod ? item.location.getLocation(false) |
285 |
: excludeConnectionMethod(item.location); |
286 |
case 1: |
287 |
return ((CVSRepositoryLocation) item.alternativeList |
288 |
.get(item.selected)).getLocation(); |
289 |
default: |
290 |
return null; |
291 |
} |
292 |
} |
293 |
|
294 |
public void addListener(ILabelProviderListener listener) { |
295 |
} |
296 |
|
297 |
public void dispose() { |
298 |
} |
299 |
|
300 |
public boolean isLabelProperty(Object element, String property) { |
301 |
return false; |
302 |
} |
303 |
|
304 |
public void removeListener(ILabelProviderListener listener) { |
305 |
} |
306 |
|
307 |
public Object[] getElements(Object inputElement) { |
308 |
return ((Collection) inputElement).toArray(); |
309 |
} |
310 |
|
311 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
312 |
} |
313 |
|
314 |
public IStructuredSelection getSelection() { |
315 |
return (IStructuredSelection) fTableViewer.getSelection(); |
316 |
} |
317 |
|
318 |
public TableViewer getViewer() { |
319 |
return fTableViewer; |
320 |
} |
321 |
|
322 |
/** |
323 |
* @return a selected (in a combo box) alternative repository location for |
324 |
* the first selected element |
325 |
*/ |
326 |
public CVSRepositoryLocation getSelectedAlternativeRepository() { |
327 |
RepositoryLocationItem firstElement = (RepositoryLocationItem) getSelection() |
328 |
.getFirstElement(); |
329 |
return (CVSRepositoryLocation) firstElement.alternativeList |
330 |
.get(firstElement.selected); |
331 |
} |
332 |
|
333 |
public void addAlternativeRepositoryToSelection( |
334 |
ICVSRepositoryLocation location) { |
335 |
// add newly created repository location to all selected elements |
336 |
for (Iterator iterator = getSelection().iterator(); iterator.hasNext();) { |
337 |
RepositoryLocationItem selectedItem = (RepositoryLocationItem) iterator |
338 |
.next(); |
339 |
selectedItem.alternativeList.add(0, location); |
340 |
selectedItem.selected = 0; |
341 |
// fTableViewer.refresh(selectedItem); |
342 |
} |
343 |
|
344 |
// add newly created repository location to not-selected elements |
345 |
// new location must be compatible with the one from the project set |
346 |
for (int i = 0; i < fTableViewer.getTable().getItemCount(); i++) { |
347 |
Object element = fTableViewer.getElementAt(i); |
348 |
if (!getSelection().toList().contains(element)) { |
349 |
RepositoryLocationItem locationItem = (RepositoryLocationItem) element; |
350 |
if (CVSProjectSetCapability.isCompatible(location, |
351 |
locationItem.location)) { |
352 |
locationItem.alternativeList.add(location); |
353 |
} |
354 |
} |
355 |
} |
356 |
|
357 |
// update labels because of the first loop - first item changed |
358 |
fTableViewer.refresh(true); |
359 |
} |
360 |
|
361 |
/** |
362 |
* @return A map with repository location from the project set as a key and |
363 |
* selected repository location from a combo box as value. |
364 |
*/ |
365 |
public Map getSelected() { |
366 |
Map map = new HashMap(); |
367 |
for (Iterator iterator = fAlternatives.iterator(); iterator.hasNext();) { |
368 |
ConfigureRepositoryLocationsTable.RepositoryLocationItem rli = (ConfigureRepositoryLocationsTable.RepositoryLocationItem) iterator |
369 |
.next(); |
370 |
map.put(rli.location, rli.alternativeList.get(rli.selected)); |
371 |
} |
372 |
return map; |
373 |
} |
374 |
|
375 |
public void setShowConnectionMethod(boolean show) { |
376 |
fShowConnectionMethod = show; |
377 |
fTableViewer.refresh(true); |
378 |
} |
379 |
|
380 |
private String excludeConnectionMethod(ICVSRepositoryLocation location) { |
381 |
String user = location.getUsername(); |
382 |
String host = location.getHost(); |
383 |
int port = location.getPort(); |
384 |
String root = location.getRootDirectory(); |
385 |
|
386 |
return (user != null && !user.equals("") ? (user + CVSRepositoryLocation.HOST_SEPARATOR) //$NON-NLS-1$ |
387 |
: "") + //$NON-NLS-1$ |
388 |
host |
389 |
+ CVSRepositoryLocation.COLON |
390 |
+ ((port == CVSRepositoryLocation.USE_DEFAULT_PORT) ? "" : (new Integer(port).toString())) + //$NON-NLS-1$ |
391 |
root; |
392 |
} |
393 |
|
394 |
public boolean noDuplicateRepositoryLocationFound() { |
395 |
return fNoDuplicateRepositoryLocationFound; |
396 |
} |
397 |
} |