|
Lines 1-22
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006 Tom Schindl and others. |
2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
7 |
* |
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation |
9 |
* rmcamara@us.ibm.com - initial API and implementation |
| 10 |
* IBM Corporation |
10 |
*******************************************************************************/ |
| 11 |
*******************************************************************************/ |
11 |
|
| 12 |
package org.eclipse.swt.nebula.nebface.viewers; |
12 |
package org.eclipse.swt.nebula.nebface.viewers; |
| 13 |
|
13 |
|
| 14 |
import org.eclipse.jface.viewers.AbstractViewerEditor; |
14 |
import org.eclipse.jface.viewers.AbstractTableViewer; |
| 15 |
import org.eclipse.jface.viewers.BaseTableViewer; |
15 |
import org.eclipse.jface.viewers.ColumnViewerEditor; |
| 16 |
import org.eclipse.jface.viewers.StructuredSelection; |
16 |
import org.eclipse.jface.viewers.StructuredSelection; |
|
|
17 |
import org.eclipse.jface.viewers.ViewerCell; |
| 17 |
import org.eclipse.jface.viewers.ViewerRow; |
18 |
import org.eclipse.jface.viewers.ViewerRow; |
| 18 |
import org.eclipse.jface.viewers.CellEditor.LayoutData; |
19 |
import org.eclipse.jface.viewers.CellEditor.LayoutData; |
| 19 |
import org.eclipse.swt.SWT; |
20 |
import org.eclipse.swt.SWT; |
|
|
21 |
import org.eclipse.swt.graphics.Image; |
| 20 |
import org.eclipse.swt.graphics.Point; |
22 |
import org.eclipse.swt.graphics.Point; |
| 21 |
import org.eclipse.swt.nebula.widgets.grid.Grid; |
23 |
import org.eclipse.swt.nebula.widgets.grid.Grid; |
| 22 |
import org.eclipse.swt.nebula.widgets.grid.GridEditor; |
24 |
import org.eclipse.swt.nebula.widgets.grid.GridEditor; |
|
Lines 24-210
Link Here
|
| 24 |
import org.eclipse.swt.widgets.Composite; |
26 |
import org.eclipse.swt.widgets.Composite; |
| 25 |
import org.eclipse.swt.widgets.Control; |
27 |
import org.eclipse.swt.widgets.Control; |
| 26 |
import org.eclipse.swt.widgets.Item; |
28 |
import org.eclipse.swt.widgets.Item; |
|
|
29 |
import org.eclipse.swt.widgets.TableItem; |
| 27 |
import org.eclipse.swt.widgets.Widget; |
30 |
import org.eclipse.swt.widgets.Widget; |
| 28 |
|
31 |
|
| 29 |
public class GridViewer extends BaseTableViewer { |
32 |
/** |
| 30 |
private static final int DEFAULT_STYLE = SWT.MULTI | SWT.H_SCROLL |
33 |
* A concrete viewer based on an Grid control. |
| 31 |
| SWT.V_SCROLL | SWT.BORDER; |
34 |
* <p> |
| 32 |
|
35 |
* This class is not intended to be subclassed outside the viewer framework. It |
| 33 |
private Grid grid; |
36 |
* is designed to be instantiated with a pre-existing Grid control and |
| 34 |
|
37 |
* configured with a domain-specific content provider, label provider, element |
| 35 |
private GridEditor gridEditor; |
38 |
* filter (optional), and element sorter (optional). |
| 36 |
|
39 |
* <p> |
| 37 |
public GridViewer(Composite parent) { |
40 |
* Content providers for grid viewers must not implement the |
| 38 |
this(parent, DEFAULT_STYLE); |
41 |
* {@code ITreeContentProvider} interface. |
| 39 |
} |
42 |
* <p> |
|
|
43 |
* |
| 44 |
* @author rmcamara@us.ibm.com |
| 45 |
* @since |
| 46 |
* @3.3 |
| 47 |
*/ |
| 48 |
public class GridViewer extends AbstractTableViewer |
| 49 |
{ |
| 50 |
/** This viewer's grid control. */ |
| 51 |
private Grid grid; |
| 52 |
|
| 53 |
/** Editor support for tables. */ |
| 54 |
private GridEditor gridEditor; |
| 55 |
|
| 56 |
/** |
| 57 |
* Creates a grid viewer on a newly-created grid control under the given |
| 58 |
* parent. The grid control is created using the SWT style bits |
| 59 |
* <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The |
| 60 |
* viewer has no input, no content provider, a default label provider, no |
| 61 |
* sorter, and no filters. |
| 62 |
* |
| 63 |
* @param parent the parent control |
| 64 |
*/ |
| 65 |
public GridViewer(Composite parent) |
| 66 |
{ |
| 67 |
this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); |
| 68 |
} |
| 40 |
|
69 |
|
| 41 |
public GridViewer(Composite parent, int style) { |
70 |
/** |
| 42 |
this(new Grid(parent, style)); |
71 |
* Creates a grid viewer on a newly-created grid control under the given |
| 43 |
} |
72 |
* parent. The grid control is created using the given SWT style bits. The |
|
|
73 |
* viewer has no input, no content provider, a default label provider, no |
| 74 |
* sorter, and no filters. |
| 75 |
* |
| 76 |
* @param parent the parent control |
| 77 |
* @param style the SWT style bits used to create the grid. |
| 78 |
*/ |
| 79 |
public GridViewer(Composite parent, int style) |
| 80 |
{ |
| 81 |
this(new Grid(parent, style)); |
| 82 |
} |
| 44 |
|
83 |
|
| 45 |
public GridViewer(Grid gridControl) { |
84 |
/** |
| 46 |
super(); |
85 |
* Creates a grid viewer on the given grid control. The viewer has no |
| 47 |
grid = gridControl; |
86 |
* input, no content provider, a default label provider, no sorter, and no |
| 48 |
gridEditor = new GridEditor(gridControl); |
87 |
* filters. |
|
|
88 |
* |
| 89 |
* @param grid the grid control |
| 90 |
*/ |
| 91 |
public GridViewer(Grid grid) |
| 92 |
{ |
| 93 |
this.grid = grid; |
| 94 |
gridEditor = new GridEditor(grid); |
| 49 |
hookControl(grid); |
95 |
hookControl(grid); |
| 50 |
} |
96 |
} |
| 51 |
|
97 |
|
| 52 |
public Grid getGrid() { |
98 |
/** |
| 53 |
return grid; |
99 |
* Returns the underlying Grid Control. |
| 54 |
} |
100 |
* |
| 55 |
|
101 |
* @return grid control. |
| 56 |
protected ViewerRow createNewRowPart(int style, int rowIndex) { |
102 |
*/ |
| 57 |
GridItem item; |
103 |
public Grid getGrid() |
| 58 |
|
104 |
{ |
| 59 |
if (rowIndex >= 0) { |
105 |
return grid; |
| 60 |
item = new GridItem(grid, style, rowIndex); |
106 |
} |
| 61 |
} else { |
|
|
| 62 |
item = new GridItem(grid, style); |
| 63 |
} |
| 64 |
|
| 65 |
return getViewerRowFromItem(item); |
| 66 |
} |
| 67 |
|
107 |
|
| 68 |
protected ViewerRow getViewerRowFromItem(Widget item) { |
108 |
/** {@inheritDoc} */ |
| 69 |
ViewerRow part = (ViewerRow) item.getData(ViewerRow.ROWPART_KEY); |
109 |
protected ViewerRow internalCreateNewRowPart(int style, int rowIndex) |
|
|
110 |
{ |
| 111 |
GridItem item; |
| 70 |
|
112 |
|
| 71 |
if (part == null) { |
113 |
if (rowIndex >= 0) |
| 72 |
part = new GridViewerRow(((GridItem) item)); |
114 |
{ |
|
|
115 |
item = new GridItem(grid, style, rowIndex); |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
item = new GridItem(grid, style); |
| 73 |
} |
120 |
} |
| 74 |
|
121 |
|
| 75 |
return part; |
122 |
return getViewerRowFromItem(item); |
| 76 |
} |
123 |
} |
| 77 |
|
124 |
|
| 78 |
protected AbstractViewerEditor createViewerEditor() { |
125 |
/** {@inheritDoc} */ |
| 79 |
return new AbstractViewerEditor(this) { |
126 |
protected ColumnViewerEditor createViewerEditor() |
| 80 |
|
127 |
{ |
| 81 |
protected StructuredSelection createSelection(Object element) { |
128 |
return new ColumnViewerEditor(this) |
|
|
129 |
{ |
| 130 |
protected StructuredSelection createSelection(Object element) |
| 131 |
{ |
| 82 |
return new StructuredSelection(element); |
132 |
return new StructuredSelection(element); |
| 83 |
} |
133 |
} |
| 84 |
|
134 |
|
| 85 |
protected Item[] getSelection() { |
135 |
protected void setEditor(Control w, Item item, int fColumnNumber) |
| 86 |
return getGrid().getSelection(); |
136 |
{ |
| 87 |
} |
|
|
| 88 |
|
| 89 |
protected void setEditor(Control w, Item item, int fColumnNumber) { |
| 90 |
gridEditor.setEditor(w, (GridItem) item, fColumnNumber); |
137 |
gridEditor.setEditor(w, (GridItem) item, fColumnNumber); |
| 91 |
} |
138 |
} |
| 92 |
|
139 |
|
| 93 |
protected void setLayoutData(LayoutData layoutData) { |
140 |
protected void setLayoutData(LayoutData layoutData) |
|
|
141 |
{ |
| 94 |
gridEditor.grabHorizontal = layoutData.grabHorizontal; |
142 |
gridEditor.grabHorizontal = layoutData.grabHorizontal; |
| 95 |
gridEditor.horizontalAlignment = layoutData.horizontalAlignment; |
143 |
gridEditor.horizontalAlignment = layoutData.horizontalAlignment; |
| 96 |
gridEditor.minimumWidth = layoutData.minimumWidth; |
144 |
gridEditor.minimumWidth = layoutData.minimumWidth; |
| 97 |
} |
145 |
} |
| 98 |
|
|
|
| 99 |
protected void showSelection() { |
| 100 |
getGrid().showSelection(); |
| 101 |
} |
| 102 |
|
| 103 |
}; |
146 |
}; |
| 104 |
} |
147 |
} |
| 105 |
|
|
|
| 106 |
protected void internalClear(int index) { |
| 107 |
// TODO NEEDS IMP |
| 108 |
} |
| 109 |
|
148 |
|
| 110 |
protected void internalClearAll() { |
149 |
/** {@inheritDoc} */ |
| 111 |
// TODO NEEDS IMP |
150 |
protected void doClear(int index) |
| 112 |
} |
151 |
{ |
|
|
152 |
// TODO Fix when grid supports virtual |
| 153 |
} |
| 113 |
|
154 |
|
| 114 |
protected void internalDeselectAll() { |
155 |
/** {@inheritDoc} */ |
| 115 |
grid.deselectAll(); |
156 |
protected void doClearAll() |
| 116 |
} |
157 |
{ |
|
|
158 |
//TODO Fix when grid supports virtual |
| 159 |
} |
| 160 |
|
| 161 |
/** {@inheritDoc} */ |
| 162 |
protected void doSetItemCount(int count) |
| 163 |
{ |
| 164 |
//TODO Once grid supports virtual |
| 165 |
} |
| 117 |
|
166 |
|
| 118 |
protected Widget internalGetColumn(int index) { |
167 |
/** {@inheritDoc} */ |
| 119 |
return grid.getColumn(index); |
168 |
protected void doDeselectAll() |
| 120 |
} |
169 |
{ |
|
|
170 |
grid.deselectAll(); |
| 171 |
} |
| 121 |
|
172 |
|
| 122 |
protected int internalGetColumnCount() { |
173 |
/** {@inheritDoc} */ |
| 123 |
return grid.getColumnCount(); |
174 |
protected Widget doGetColumn(int index) |
| 124 |
} |
175 |
{ |
|
|
176 |
return grid.getColumn(index); |
| 177 |
} |
| 125 |
|
178 |
|
| 126 |
protected Widget[] internalGetColumns() { |
179 |
/** {@inheritDoc} */ |
| 127 |
return grid.getColumns(); |
180 |
protected int doGetColumnCount() |
| 128 |
} |
181 |
{ |
|
|
182 |
return grid.getColumnCount(); |
| 183 |
} |
| 129 |
|
184 |
|
| 130 |
protected Item internalGetItem(int index) { |
185 |
/** {@inheritDoc} */ |
| 131 |
return grid.getItem(index); |
186 |
protected Item doGetItem(int index) |
| 132 |
} |
187 |
{ |
|
|
188 |
return grid.getItem(index); |
| 189 |
} |
| 133 |
|
190 |
|
| 134 |
protected Item internalGetItem(Point point) { |
191 |
/** {@inheritDoc} */ |
| 135 |
return grid.getItem(point); |
192 |
protected int doGetItemCount() |
| 136 |
} |
193 |
{ |
|
|
194 |
return grid.getItemCount(); |
| 195 |
} |
| 137 |
|
196 |
|
| 138 |
protected int internalGetItemCount() { |
197 |
/** {@inheritDoc} */ |
| 139 |
return grid.getItemCount(); |
198 |
protected Item[] doGetItems() |
| 140 |
} |
199 |
{ |
|
|
200 |
return grid.getItems(); |
| 201 |
} |
| 141 |
|
202 |
|
| 142 |
protected Item[] internalGetItems() { |
203 |
/** {@inheritDoc} */ |
| 143 |
return grid.getItems(); |
204 |
protected Item[] doGetSelection() |
| 144 |
} |
205 |
{ |
|
|
206 |
return grid.getSelection(); |
| 207 |
} |
| 145 |
|
208 |
|
| 146 |
protected Widget[] internalGetSelection() { |
209 |
/** {@inheritDoc} */ |
| 147 |
return grid.getSelection(); |
210 |
protected int[] doGetSelectionIndices() |
| 148 |
} |
211 |
{ |
|
|
212 |
return grid.getSelectionIndices(); |
| 213 |
} |
| 149 |
|
214 |
|
| 150 |
protected int[] internalGetSelectionIndices() { |
215 |
/** {@inheritDoc} */ |
| 151 |
return grid.getSelectionIndices(); |
216 |
protected int doIndexOf(Item item) |
| 152 |
} |
217 |
{ |
|
|
218 |
return grid.indexOf((GridItem) item); |
| 219 |
} |
| 153 |
|
220 |
|
| 154 |
protected int internalIndexOf(Item item) { |
221 |
/** {@inheritDoc} */ |
| 155 |
return grid.indexOf((GridItem) item); |
222 |
protected void doRemove(int[] indices) |
| 156 |
} |
223 |
{ |
|
|
224 |
grid.remove(indices); |
| 225 |
} |
| 157 |
|
226 |
|
| 158 |
protected void internalRemove(int start, int end) { |
227 |
/** {@inheritDoc} */ |
| 159 |
grid.remove(start, end); |
228 |
protected void doRemove(int start, int end) |
| 160 |
} |
229 |
{ |
|
|
230 |
grid.remove(start, end); |
| 231 |
} |
| 161 |
|
232 |
|
| 162 |
protected void internalRemove(int[] indices) { |
233 |
/** {@inheritDoc} */ |
| 163 |
grid.remove(indices); |
234 |
protected void doRemoveAll() |
| 164 |
} |
235 |
{ |
|
|
236 |
grid.removeAll(); |
| 237 |
} |
| 165 |
|
238 |
|
| 166 |
protected void internalRemoveAll() { |
239 |
/** {@inheritDoc} */ |
| 167 |
grid.removeAll(); |
240 |
protected void doSetSelection(Item[] items) |
| 168 |
} |
241 |
{ |
|
|
242 |
GridItem[] items2 = new GridItem[items.length]; |
| 243 |
for (int i = 0; i < items.length; i++) |
| 244 |
{ |
| 245 |
items2[i] = (GridItem) items[i]; |
| 246 |
} |
| 247 |
grid.setSelection(items2); |
| 248 |
} |
| 169 |
|
249 |
|
| 170 |
protected void internalSetItemCount(int count) { |
250 |
/** {@inheritDoc} */ |
| 171 |
// TODO NEEDS IMP |
251 |
protected void doSetSelection(int[] indices) |
| 172 |
} |
252 |
{ |
|
|
253 |
grid.setSelection(indices); |
| 254 |
} |
| 173 |
|
255 |
|
| 174 |
protected void internalSetSelection(Item[] items) { |
256 |
/** {@inheritDoc} */ |
| 175 |
if (items != null) { |
257 |
protected void doShowItem(Item item) |
| 176 |
grid.setSelection(new GridItem[0]); |
258 |
{ |
| 177 |
} else { |
259 |
grid.showItem((GridItem)item); |
| 178 |
GridItem[] tmp = new GridItem[items.length]; |
260 |
} |
| 179 |
System.arraycopy(items, 0, tmp, 0, items.length); |
|
|
| 180 |
grid.setSelection(tmp); |
| 181 |
} |
| 182 |
|
261 |
|
| 183 |
} |
262 |
/** {@inheritDoc} */ |
|
|
263 |
protected void doShowSelection() |
| 264 |
{ |
| 265 |
grid.showSelection(); |
| 266 |
} |
| 184 |
|
267 |
|
| 185 |
protected void internalSetSelection(int[] indices) { |
268 |
/** {@inheritDoc} */ |
| 186 |
grid.setSelection(indices); |
269 |
protected Item getItemAt(Point point) |
| 187 |
} |
270 |
{ |
|
|
271 |
return grid.getItem(point); |
| 272 |
} |
| 188 |
|
273 |
|
| 189 |
protected void internalShowItem(Item item) { |
274 |
/** {@inheritDoc} */ |
| 190 |
grid.showItem((GridItem) item); |
275 |
public Control getControl() |
| 191 |
} |
276 |
{ |
|
|
277 |
return grid; |
| 278 |
} |
| 279 |
|
| 280 |
/** {@inheritDoc} */ |
| 281 |
protected ViewerRow getViewerRowFromItem(Widget item) |
| 282 |
{ |
| 283 |
ViewerRow part = (ViewerRow) item.getData(ViewerRow.ROWPART_KEY); |
| 192 |
|
284 |
|
| 193 |
protected void internalShowSelection() { |
285 |
if (part == null) |
| 194 |
grid.showSelection(); |
286 |
{ |
| 195 |
} |
287 |
part = new GridViewerRow(((GridItem) item)); |
|
|
288 |
} |
| 196 |
|
289 |
|
| 197 |
protected void internalSetControl(Control table) { |
290 |
return part; |
| 198 |
grid = (Grid) table; |
291 |
} |
| 199 |
} |
|
|
| 200 |
|
292 |
|
| 201 |
public Control getControl() { |
293 |
|
| 202 |
return grid; |
294 |
|
|
|
295 |
protected void doResetItem(Item item) { |
| 296 |
GridItem gridItem = (GridItem) item; |
| 297 |
int columnCount = Math.max(1, grid.getColumnCount()); |
| 298 |
for (int i = 0; i < columnCount; i++) { |
| 299 |
gridItem.setText(i, ""); //$NON-NLS-1$ |
| 300 |
gridItem.setImage(null); |
| 301 |
} |
| 203 |
} |
302 |
} |
| 204 |
|
303 |
|
| 205 |
protected Item getItemAt(Point point) |
304 |
ViewerCell getFocusCell() { |
| 206 |
{ |
305 |
if( getGrid().getCellSelectionEnabled() ) { |
| 207 |
return grid.getItem(point); |
306 |
Point p = getGrid().getFocusCell(); |
| 208 |
} |
307 |
|
|
|
308 |
if( p.x >= 0 && p.y >= 0 ) { |
| 309 |
GridItem item = getGrid().getItem(p.y); |
| 310 |
if( item != null ) { |
| 311 |
ViewerRow row = (ViewerRow) item.getData(ViewerRow.ROWPART_KEY); |
| 312 |
return row.getCell(p.x); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 209 |
|
316 |
|
|
|
317 |
return null; |
| 318 |
} |
| 210 |
} |
319 |
} |