I have developed an application i.e. eclipse plugin
1.to read and xml and display it in the table grid.
2.user can edit the elements of a table.
3. after editing the table when u press the save button an updated xml is generated.
Now, the problem is after displaying an xml in the table grid , i need to add new row in between the two rows of a table and able to edit the fields.
can anybody help in doing that.
the code that i am using is:
- Code: Select all
class EditDialog extends Composite {
public static final String ID = "sample.views.View";
private final static String[] COLUMN_HEADINGS = { "FieldName", "FieldType","Class", "Parent", "DefaultValue" };
protected static final Point Point = null;
public static TableViewer viewer;
private TableViewer tableViewer;
public static int backgroundMode;
private Text fName;
private Text fType;
private Text parent1;
private Text class1;
private Text defaultvalue;
private String name;
ValueObject selEmpObj = null;
public EditDialog(Shell parent, ValueObject selEmpObj) {
super(parent, backgroundMode);
this.selEmpObj = selEmpObj;
name = selEmpObj.getInstanceName();
}
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
public String getColumnText(Object obj1, int index) {
System.out.println("in the viewlabelprovider getColumnText() of edit dialog");
String returnValue = null;
switch (index) {
case 0:
returnValue = ((ValueObject) obj1).getInstanceName();
break;
case 1:
returnValue = ((ValueObject) obj1).getType();
break;
case 2:
returnValue = ((ValueObject) obj1).getClass1();
break;
case 3:
returnValue = ((ValueObject) obj1).getParent1();
break;
case 4:
returnValue = ((ValueObject) obj1).getDefaultvalue();
break;
default:
break;
}
return returnValue;
}
public Image getColumnImage(Object arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
}
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
// do nothing
}
public void dispose() {
// do nothing
}
@SuppressWarnings("unchecked")
public Object[] getElements(Object parent) {
System.out.println("in the view content provider in edit dialog");
Object[] returnObjectArray = ((List) parent).toArray();
return returnObjectArray;
}
}
public void modify(Object element, String property, Object value) {
List list = Arrays.asList(COLUMN_HEADINGS);
int columnIndex = list.indexOf(property);
TableItem tableItem = (TableItem) element;
ValueObject row1 = (ValueObject) tableItem.getData();
switch (columnIndex) {
case 0:
String key = (String) value;
if (key.length() > 0) {
row1.setInstanceName(key);
}
break;
case 1:
String v = (String) value;
if (v.length() > 0) {
row1.setType(v);
}
break;
case 2:
String c = (String) value;
if (c.length() > 0) {
row1.setClass1(c);
}
break;
case 3:
String p = (String) value;
if (p.length() > 0) {
row1.setParent1(p);
}
break;
case 4:
String d = (String) value;
if (d.length() > 0) {
row1.setDefaultvalue(d);
}
break;
}
tableViewer.update(row1, null);
}
//For displaying the values of an xml in a table grid format.
public void createPartControl(final Composite shell) {
try
{
shell.setSize(360, 400);
shell.setLayout(new FillLayout());
final int NUM = 5;
final String[] options = { "int", "float", "double", "String", "Collection", "Object" };
final String[] options1 = { "root", "com.symphony.Employee", "com.symphony.Address" };
final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableLayout tableLayout = new TableLayout();
table.setLayout(tableLayout);
tableLayout.addColumnData(new ColumnWeightData(15, 60, true));
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(COLUMN_HEADINGS[0]);
column.setAlignment(SWT.LEFT);
tableLayout.addColumnData(new ColumnWeightData(20, 120, true));
column = new TableColumn(table, SWT.NONE);
column.setText(COLUMN_HEADINGS[1]);
column.setAlignment(SWT.LEFT);
tableLayout.addColumnData(new ColumnWeightData(25, 180, true));
column = new TableColumn(table, SWT.NONE);
column.setText(COLUMN_HEADINGS[2]);
column.setAlignment(SWT.LEFT);
tableLayout.addColumnData(new ColumnWeightData(30, 240, true));
column = new TableColumn(table, SWT.NONE);
column.setText(COLUMN_HEADINGS[3]);
column.setAlignment(SWT.LEFT);
tableLayout.addColumnData(new ColumnWeightData(35, 300, true));
column = new TableColumn(table, SWT.NONE);
column.setText(COLUMN_HEADINGS[4]);
column.setAlignment(SWT.LEFT);
final TableViewer tableViewer = new TableViewer(table);
tableViewer.setColumnProperties(COLUMN_HEADINGS);
//tableViewer.setContentProvider(new ViewContentProvider());
//tableViewer.setLabelProvider(new ViewLabelProvider());
CellEditor[] editors = new CellEditor[8];
editors[0] = new TextCellEditor(table);
editors[1] = new TextCellEditor(table);
editors[2] = new TextCellEditor(table);
editors[3] = new TextCellEditor(table);
editors[4] = new TextCellEditor(table);
tableViewer.setCellEditors(editors);
//for combo box
TableEditor[] colorEditors = new TableEditor[NUM];
Button[] colorButtons = new Button[NUM];
for (int i = 0; i < NUM; i++) {
final TableItem item = new TableItem(table, SWT.NONE);
colorEditors[i] = new TableEditor(table);
colorButtons[i] = new Button(table, SWT.PUSH);
colorEditors[i].setEditor(colorButtons[i], item, 2);
}
for (int j = 0; j < NUM; j++) {
final TableItem item1 = new TableItem(table, SWT.NONE);
colorEditors[j] = new TableEditor(table);
colorButtons[j] = new Button(table, SWT.PUSH);
colorEditors[j].setEditor(colorButtons[j], item1, 3);
}
final TableEditor editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
final CCombo combo = new CCombo(table, SWT.READ_ONLY);
for (int i = 0, n = options.length; i < n; i++) {
combo.add(options[i]);
System.out.println("the options length-->"+options.length);
}
final TableItem item = table.getItem(0);
combo.select(combo.indexOf(item.getText(0)));
editor.minimumWidth = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
table.getColumn(2).setWidth(editor.minimumWidth);
combo.setFocus();
editor.setEditor(combo, item, 2);
//for class name
final CCombo combo1 = new CCombo(table, SWT.READ_ONLY);
for (int j = 0, n = options1.length; j < n; j++) {
combo1.add(options1[j]);
System.out.println("the options1 length --->"+options1.length);
}
final TableItem item1 = table.getItem(0);
combo1.select(combo1.indexOf(item.getText(0)));
editor.minimumWidth = combo1.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
table.getColumn(3).setWidth(editor.minimumWidth);
combo1.setFocus();
editor.setEditor(combo1, item1, 3);
table.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent event) {
Control old = editor.getEditor();
if (old != null)
old.dispose();
Point pt = new Point(event.x, event.y);
final TableItem item = table.getItem(pt);
final TableItem item1 = table.getItem(pt);
if (item != null || item1 != null) {
int column = -1;
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
column = i;
break;
}
}
for (int j = 0, n = table.getColumnCount(); j < n; j++) {
Rectangle rect = item1.getBounds(j);
if (rect.contains(pt)) {
column = j;
break;
}
}
if (column == 1) {
final CCombo combo = new CCombo(table, SWT.READ_ONLY);
final CCombo combo1 = new CCombo(table, SWT.READ_ONLY);
for (int i = 0, n = options.length; i < n; i++) {
combo.add(options[i]);
}
for (int j = 0, n = options1.length; j < n; j++) {
combo1.add(options1[j]);
}
combo.select(combo.indexOf(item.getText(column)));
combo1.select(combo1.indexOf(item1.getText(column)));
editor.minimumWidth = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
table.getColumn(2).setWidth(editor.minimumWidth);
editor.minimumWidth = combo1.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
table.getColumn(3).setWidth(editor.minimumWidth);
combo.setFocus();
editor.setEditor(combo, item, 2);
combo1.setFocus();
editor.setEditor(combo1, item1, 3);
} else if (column == 2) {
final Text text = new Text(table, SWT.NONE);
text.setForeground(item.getForeground());
text.setText(item.getText(column));
text.setForeground(item.getForeground());
text.selectAll();
text.setFocus();
editor.minimumWidth = text.getBounds().width;
editor.setEditor(text, item, column);
} else if (column == 3) {
final Text text1 = new Text(table, SWT.NONE);
text1.setForeground(item1.getForeground());
text1.setText(item1.getText(column));
text1.setForeground(item1.getForeground());
text1.selectAll();
text1.setFocus();
editor.minimumWidth = text1.getBounds().width;
editor.setEditor(text1, item1, column);
}
}
}
});
// add the new record
Button addButton = new Button(shell, SWT.PUSH);
addButton.setText("Add");
addButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("in the add functionality");
ValueObject row = new ValueObject("", "", "", "", "");
for (int i = 0; i < 5; i++) {
tableViewer.insert(row,table.getSelectionCount());
tableViewer.add(row);
table.setTopIndex(table.getItemCount());
table.select(table.getItemCount() - 1);
tableViewer.editElement(row, i);
}
}
});
// remove the record
Button removeButton = new Button(shell, SWT.PUSH);
removeButton.setText("Remove");
removeButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
ISelection selection = tableViewer.getSelection();
if (selection instanceof IStructuredSelection) {
Iterator iterator = ((IStructuredSelection) selection).iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
tableViewer.remove(obj);
}
}
}
});
Button saveButton = new Button(shell, SWT.PUSH);
saveButton.setText("Save");
removeButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
ArrayList<ValueObject> list = ReadXml.getObjectList();
for (int i = 0; i < list.size(); i++) {
ValueObject object = list.get(i);
if (object.getInstanceName().equals(name)) {
list.get(i).setType(fType.getText());
list.get(i).setParent1(parent1.getText());
list.get(i).setClass1(class1.getText());
list.get(i).setDefaultvalue(defaultvalue.getText());
}
System.out.println("in the save of the edit dialog");
}
WriteXml.visitSerializableFields(list);
SampleView.viewer.refresh();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
Button cancelButton = new Button(shell, SWT.PUSH);
cancelButton.setText("Cancel");
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
ISelection selection1 = tableViewer.getSelection();
if (selection1 instanceof IStructuredSelection) {
Iterator iterator = ((IStructuredSelection) selection1).iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
tableViewer.remove(obj);
}
}
}
});
((Shell) shell).open();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}catch (Throwable e) {
e.printStackTrace();
}
}
public void refresh() {
viewer.refresh();
}
@Override
public boolean setFocus() {
return false;
// TODO Auto-generated method stub
}
}