|
Added
Link Here
|
| 1 |
package org.eclipse.nebula.snippets.gridviewer; |
| 2 |
|
| 3 |
import java.util.regex.Matcher; |
| 4 |
import java.util.regex.Pattern; |
| 5 |
|
| 6 |
import org.eclipse.jface.viewers.ArrayContentProvider; |
| 7 |
import org.eclipse.jface.viewers.CellEditor; |
| 8 |
import org.eclipse.jface.viewers.ColumnLabelProvider; |
| 9 |
import org.eclipse.jface.viewers.ColumnViewerEditor; |
| 10 |
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent; |
| 11 |
import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener; |
| 12 |
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy; |
| 13 |
import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent; |
| 14 |
import org.eclipse.jface.viewers.ICellModifier; |
| 15 |
import org.eclipse.jface.viewers.TextCellEditor; |
| 16 |
import org.eclipse.nebula.jface.gridviewer.GridTableViewer; |
| 17 |
import org.eclipse.nebula.jface.gridviewer.GridViewerColumn; |
| 18 |
import org.eclipse.nebula.jface.gridviewer.GridViewerEditor; |
| 19 |
import org.eclipse.nebula.widgets.grid.Grid; |
| 20 |
import org.eclipse.nebula.widgets.grid.GridItem; |
| 21 |
import org.eclipse.swt.SWT; |
| 22 |
import org.eclipse.swt.layout.FillLayout; |
| 23 |
import org.eclipse.swt.widgets.Display; |
| 24 |
import org.eclipse.swt.widgets.Shell; |
| 25 |
|
| 26 |
|
| 27 |
public class Test |
| 28 |
{ |
| 29 |
private Pattern m_IsValidPattern = null; |
| 30 |
|
| 31 |
public class MyModel |
| 32 |
{ |
| 33 |
private String sCounter = ""; |
| 34 |
|
| 35 |
public MyModel(){ |
| 36 |
} |
| 37 |
|
| 38 |
public void setCounter(String sCounter) |
| 39 |
{ |
| 40 |
this.sCounter = sCounter; |
| 41 |
} |
| 42 |
|
| 43 |
public String toString() |
| 44 |
{ |
| 45 |
return sCounter; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public class MyTextCellEditor extends TextCellEditor |
| 50 |
{ |
| 51 |
/** |
| 52 |
* search pattern is Numeric character |
| 53 |
*/ |
| 54 |
private Pattern m_IsValidPattern = null; |
| 55 |
|
| 56 |
public MyTextCellEditor(Grid parent) { |
| 57 |
super(parent); |
| 58 |
m_IsValidPattern = Pattern.compile("[\\w+-\\.]"); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void activate(ColumnViewerEditorActivationEvent activationEvent) { |
| 63 |
|
| 64 |
// set the activation character |
| 65 |
String s1 = String.valueOf( activationEvent.character ); |
| 66 |
Matcher matcher = m_IsValidPattern.matcher( s1 ); |
| 67 |
if (matcher.matches()) { |
| 68 |
doSetValue(s1); |
| 69 |
} |
| 70 |
super.activate(activationEvent); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
public Test(Shell shell) |
| 76 |
{ |
| 77 |
/* allow numbers and characters */ |
| 78 |
m_IsValidPattern = Pattern.compile("[\\w+-\\.]"); |
| 79 |
|
| 80 |
final GridTableViewer v = new GridTableViewer(shell, SWT.BORDER |
| 81 |
| SWT.H_SCROLL | SWT.V_SCROLL); |
| 82 |
|
| 83 |
|
| 84 |
v.setCellEditors(new CellEditor[] { |
| 85 |
new MyTextCellEditor(v.getGrid()), |
| 86 |
new MyTextCellEditor(v.getGrid()) |
| 87 |
}); |
| 88 |
|
| 89 |
v.setCellModifier(new ICellModifier() { |
| 90 |
|
| 91 |
public boolean canModify(Object element, String property) { |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
public Object getValue(Object element, String property) { |
| 96 |
return element.toString(); |
| 97 |
} |
| 98 |
|
| 99 |
public void modify(Object element, String property, Object value) { |
| 100 |
if (element instanceof GridItem) { |
| 101 |
GridItem item = (GridItem)element; |
| 102 |
MyModel model = (MyModel)(item.getData()); |
| 103 |
model.setCounter(value.toString()); |
| 104 |
v.update(model, null); |
| 105 |
} |
| 106 |
} |
| 107 |
}); |
| 108 |
|
| 109 |
|
| 110 |
ColumnViewerEditorActivationListener listener = new ColumnViewerEditorActivationListener() { |
| 111 |
|
| 112 |
public void afterEditorActivated( |
| 113 |
ColumnViewerEditorActivationEvent event) |
| 114 |
{ |
| 115 |
} |
| 116 |
|
| 117 |
public void afterEditorDeactivated( |
| 118 |
ColumnViewerEditorDeactivationEvent event) |
| 119 |
{ |
| 120 |
} |
| 121 |
|
| 122 |
public void beforeEditorActivated( |
| 123 |
ColumnViewerEditorActivationEvent event) |
| 124 |
{ |
| 125 |
} |
| 126 |
|
| 127 |
public void beforeEditorDeactivated( |
| 128 |
ColumnViewerEditorDeactivationEvent event) |
| 129 |
{ |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
v.setContentProvider(new ArrayContentProvider()); |
| 134 |
v.setColumnProperties(new String[] { "1", "2" }); |
| 135 |
v.getGrid().setCellSelectionEnabled(true); |
| 136 |
v.getGrid().setLinesVisible(true); |
| 137 |
v.getGrid().setHeaderVisible(true); |
| 138 |
|
| 139 |
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy( |
| 140 |
v) { |
| 141 |
protected boolean isEditorActivationEvent( |
| 142 |
ColumnViewerEditorActivationEvent event) |
| 143 |
{ |
| 144 |
boolean result; |
| 145 |
|
| 146 |
String s1 = String.valueOf( event.character ); |
| 147 |
Matcher matcher = m_IsValidPattern.matcher( s1 ); |
| 148 |
boolean bIsCharKey = matcher.matches(); |
| 149 |
|
| 150 |
boolean bEnableKey = |
| 151 |
(event.keyCode == SWT.CR) || |
| 152 |
(event.keyCode == SWT.KEYPAD_CR) || |
| 153 |
(event.keyCode == SWT.F2) || |
| 154 |
bIsCharKey; |
| 155 |
|
| 156 |
result = event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL |
| 157 |
|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION |
| 158 |
|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && bEnableKey) |
| 159 |
|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC; |
| 160 |
|
| 161 |
return result; |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
GridViewerEditor.create(v, actSupport, |
| 166 |
ColumnViewerEditor.TABBING_HORIZONTAL |
| 167 |
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR |
| 168 |
| ColumnViewerEditor.TABBING_VERTICAL |
| 169 |
| ColumnViewerEditor.KEYBOARD_ACTIVATION); |
| 170 |
|
| 171 |
v.getColumnViewerEditor().addEditorActivationListener(listener); |
| 172 |
|
| 173 |
GridViewerColumn column = new GridViewerColumn(v, SWT.NONE); |
| 174 |
column.getColumn().setWidth(200); |
| 175 |
column.getColumn().setMoveable(true); |
| 176 |
column.getColumn().setText("Column 1"); |
| 177 |
column.setLabelProvider(new ColumnLabelProvider()); |
| 178 |
|
| 179 |
column = new GridViewerColumn(v, SWT.NONE); |
| 180 |
column.getColumn().setWidth(200); |
| 181 |
column.getColumn().setMoveable(true); |
| 182 |
column.getColumn().setText("Column 2"); |
| 183 |
column.setLabelProvider(new ColumnLabelProvider()); |
| 184 |
|
| 185 |
MyModel[] model = createModel(); |
| 186 |
|
| 187 |
v.setInput(model); |
| 188 |
} |
| 189 |
|
| 190 |
private MyModel[] createModel() { |
| 191 |
|
| 192 |
MyModel[] elements = new MyModel[10]; |
| 193 |
|
| 194 |
for (int i = 0; i < 10; i++) |
| 195 |
{ |
| 196 |
elements[i] = new MyModel(); |
| 197 |
if (i<3){ |
| 198 |
Double h = Double.valueOf(i); |
| 199 |
elements[i].setCounter( h.toString() ); |
| 200 |
} |
| 201 |
} |
| 202 |
return elements; |
| 203 |
} |
| 204 |
|
| 205 |
public static void main(String[] args) |
| 206 |
{ |
| 207 |
Display display = new Display(); |
| 208 |
|
| 209 |
Shell shell = new Shell(display); |
| 210 |
shell.setLayout(new FillLayout()); |
| 211 |
new Test(shell); |
| 212 |
shell.open(); |
| 213 |
|
| 214 |
while (!shell.isDisposed()) |
| 215 |
{ |
| 216 |
if (!display.readAndDispatch()) |
| 217 |
display.sleep(); |
| 218 |
} |
| 219 |
|
| 220 |
display.dispose(); |
| 221 |
|
| 222 |
} |
| 223 |
} |