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.ICVSRepositoryLocation; |
25 |
import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; |
26 |
import org.eclipse.team.internal.ui.SWTUtils; |
27 |
|
28 |
|
29 |
public class AlternativeRepositoryTable implements ICellModifier, 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 String location; |
55 |
public List alternativeList; |
56 |
public int selected; |
57 |
|
58 |
public Item(String 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.compareTo(((Item) o).location); |
71 |
} |
72 |
} |
73 |
|
74 |
public static class RepositoryLocationItem extends Item { |
75 |
public RepositoryLocationItem(CVSRepositoryLocation repo, |
76 |
List repositoryLocation) { |
77 |
super(repo.getLocation(), repositoryLocation); |
78 |
} |
79 |
} |
80 |
|
81 |
protected static final String ITEM = "item"; //$NON-NLS-1$ |
82 |
protected static final String PROPERTY_ALTERNATIVE_LIST = "alternativeList"; //$NON-NLS-1$ |
83 |
|
84 |
private final TableViewer fTableViewer; |
85 |
private final List fItems; |
86 |
|
87 |
private CellEditor[] cellEditors; |
88 |
|
89 |
private TextCellEditor dummyAlternativeRepositoryEditor; |
90 |
|
91 |
private final Table table; |
92 |
|
93 |
public AlternativeRepositoryTable(final Composite composite, List items) { |
94 |
|
95 |
fItems = items; |
96 |
|
97 |
/** |
98 |
* Create a table. |
99 |
*/ |
100 |
table = new Table(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION); |
101 |
table.setLayoutData(SWTUtils.createHVFillGridData()); |
102 |
table.setLinesVisible(true); |
103 |
table.setHeaderVisible(true); |
104 |
table.addListener(SWT.MeasureItem, new Listener() { |
105 |
public void handleEvent(Event event) { |
106 |
// int clientWidth = table.getClientArea().width; |
107 |
event.height = event.gc.getFontMetrics().getHeight() + 5; |
108 |
// event.width = clientWidth * 2; |
109 |
} |
110 |
}); |
111 |
|
112 |
/** |
113 |
* The 'Project Set repository location' column |
114 |
*/ |
115 |
final TableColumn projectSetRepositoryColumn = new TableColumn(table, SWT.NONE, 0); |
116 |
projectSetRepositoryColumn.setText(CVSUIMessages.AlternativeRepositoryWizard_column0); |
117 |
|
118 |
/** |
119 |
* The 'Alternative repository locations' column |
120 |
*/ |
121 |
final TableColumn alternativeRepositoryColums = new TableColumn(table, SWT.NONE, 1); |
122 |
alternativeRepositoryColums.setText(CVSUIMessages.AlternativeRepositoryWizard_column1); |
123 |
|
124 |
composite.addControlListener(new ControlAdapter() { |
125 |
public void controlResized(ControlEvent e) { |
126 |
Rectangle area = composite.getClientArea(); |
127 |
Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT); |
128 |
ScrollBar vBar = table.getVerticalBar(); |
129 |
int width = area.width - table.computeTrim(0,0,0,0).width - vBar.getSize().x; |
130 |
if (size.y > area.height + table.getHeaderHeight()) { |
131 |
// Subtract the scrollbar width from the total column width |
132 |
// if a vertical scrollbar will be required |
133 |
Point vBarSize = vBar.getSize(); |
134 |
width -= vBarSize.x; |
135 |
} |
136 |
Point oldSize = table.getSize(); |
137 |
if (oldSize.x > area.width) { |
138 |
// table is getting smaller so make the columns |
139 |
// smaller first and then resize the table to |
140 |
// match the client area width |
141 |
projectSetRepositoryColumn.setWidth(width/2); |
142 |
alternativeRepositoryColums.setWidth(width - projectSetRepositoryColumn.getWidth()); |
143 |
table.setSize(area.width, area.height); |
144 |
} else { |
145 |
// table is getting bigger so make the table |
146 |
// bigger first and then make the columns wider |
147 |
// to match the client area width |
148 |
table.setSize(area.width, area.height); |
149 |
projectSetRepositoryColumn.setWidth(width/2); |
150 |
alternativeRepositoryColums.setWidth(width - projectSetRepositoryColumn.getWidth()); |
151 |
} |
152 |
} |
153 |
}); |
154 |
|
155 |
/** |
156 |
* Create a viewer for the table. |
157 |
*/ |
158 |
fTableViewer = new TableViewer(table); |
159 |
fTableViewer.setContentProvider(this); |
160 |
fTableViewer.setLabelProvider(this); |
161 |
fTableViewer.setComparator(new AlternativeRepositoryComparator()); |
162 |
|
163 |
/** |
164 |
* Add a cell editor in the 'Alternative repository locations' column |
165 |
*/ |
166 |
new TableEditor(table); |
167 |
|
168 |
cellEditors = new CellEditor[2]; |
169 |
cellEditors[0] = null; |
170 |
// to enable cell editing, create a dummy cell editor |
171 |
cellEditors[1] = dummyAlternativeRepositoryEditor = new TextCellEditor(table, SWT.READ_ONLY); |
172 |
|
173 |
fTableViewer.setCellEditors(cellEditors); |
174 |
fTableViewer.setColumnProperties(new String [] { ITEM, PROPERTY_ALTERNATIVE_LIST }); |
175 |
fTableViewer.setCellModifier(this); |
176 |
fTableViewer.setInput(fItems); |
177 |
} |
178 |
|
179 |
|
180 |
public Object getValue(Object element, String property) { |
181 |
|
182 |
final Item item = (Item)element; |
183 |
|
184 |
if (PROPERTY_ALTERNATIVE_LIST.equals(property)) { |
185 |
return new Integer(item.selected); |
186 |
} |
187 |
return null; |
188 |
} |
189 |
|
190 |
public boolean canModify(Object element, String property) { |
191 |
// set the correct cell editor for this element |
192 |
cellEditors[1] = getCellEditor(element); |
193 |
// only allow modification for editable elements |
194 |
return PROPERTY_ALTERNATIVE_LIST.equals(property); |
195 |
} |
196 |
|
197 |
private CellEditor getCellEditor(Object element) { |
198 |
|
199 |
if (element instanceof RepositoryLocationItem) { |
200 |
|
201 |
// create combo-box list of alternative repositories |
202 |
List alternativeList = ((RepositoryLocationItem) element).alternativeList; |
203 |
String[] alternativeNames = new String[alternativeList.size()]; |
204 |
int i = 0; |
205 |
for (Iterator iterator = alternativeList.iterator(); iterator.hasNext();) { |
206 |
CVSRepositoryLocation repo = (CVSRepositoryLocation) iterator.next(); |
207 |
alternativeNames[i++] = repo.getLocation(); |
208 |
} |
209 |
return new ComboBoxCellEditor(table, alternativeNames, |
210 |
SWT.READ_ONLY); |
211 |
} |
212 |
return dummyAlternativeRepositoryEditor; |
213 |
} |
214 |
|
215 |
|
216 |
public void modify(Object element, String property, Object value) { |
217 |
|
218 |
final IStructuredSelection selection = (IStructuredSelection)fTableViewer.getSelection(); |
219 |
final Item item = (Item)selection.getFirstElement(); |
220 |
if (item == null) |
221 |
return; |
222 |
|
223 |
final int comboIndex = ((Integer)value).intValue(); |
224 |
|
225 |
if (PROPERTY_ALTERNATIVE_LIST.equals(property)) { |
226 |
item.selected = comboIndex; |
227 |
} |
228 |
fTableViewer.refresh(item); |
229 |
} |
230 |
|
231 |
public Image getColumnImage(Object element, int columnIndex) { |
232 |
return null; |
233 |
} |
234 |
|
235 |
public String getColumnText(Object element, int columnIndex) { |
236 |
|
237 |
final Item item = (Item) element; |
238 |
|
239 |
if (columnIndex == 0) { |
240 |
return item.location; |
241 |
} |
242 |
|
243 |
if (columnIndex == 1) { |
244 |
return ((CVSRepositoryLocation) item.alternativeList |
245 |
.get(item.selected)).getLocation(); |
246 |
} |
247 |
return null; |
248 |
} |
249 |
|
250 |
public void addListener(ILabelProviderListener listener) {} |
251 |
|
252 |
public void dispose() {} |
253 |
|
254 |
public boolean isLabelProperty(Object element, String property) { |
255 |
return false; |
256 |
} |
257 |
|
258 |
public void removeListener(ILabelProviderListener listener) {} |
259 |
|
260 |
public Object[] getElements(Object inputElement) { |
261 |
return ((Collection)inputElement).toArray(); |
262 |
} |
263 |
|
264 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} |
265 |
|
266 |
public IStructuredSelection getSelection() { |
267 |
return (IStructuredSelection)fTableViewer.getSelection(); |
268 |
} |
269 |
|
270 |
public TableViewer getViewer() { |
271 |
return fTableViewer; |
272 |
} |
273 |
|
274 |
public CVSRepositoryLocation getSelectedAlternativeRepository() { |
275 |
RepositoryLocationItem firstElement = (RepositoryLocationItem) getSelection() |
276 |
.getFirstElement(); |
277 |
return (CVSRepositoryLocation) firstElement.alternativeList |
278 |
.get(firstElement.selected); |
279 |
} |
280 |
|
281 |
public void addAlternativeRepositoryToSelection( |
282 |
ICVSRepositoryLocation location) { |
283 |
RepositoryLocationItem firstElement = (RepositoryLocationItem) getSelection() |
284 |
.getFirstElement(); |
285 |
// put new alternative repository at first position |
286 |
firstElement.alternativeList.add(0, location); |
287 |
firstElement.selected = 0; |
288 |
fTableViewer.refresh(firstElement); |
289 |
} |
290 |
} |