aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormpaturzo2014-03-18 07:17:22 (EDT)
committermpaturzo2014-03-18 07:17:22 (EDT)
commit9663efd8fa1556c5c8cee59aad7ac252f95636e2 (patch)
tree92820a2fc8dcee4e83c83138691eca4f7090c692
parent2532af8dbf2d84dcbbed3f58c5c48ef3e7b1cd19 (diff)
downloadorg.eclipse.nebula-9663efd8fa1556c5c8cee59aad7ac252f95636e2.zip
org.eclipse.nebula-9663efd8fa1556c5c8cee59aad7ac252f95636e2.tar.gz
org.eclipse.nebula-9663efd8fa1556c5c8cee59aad7ac252f95636e2.tar.bz2
Bug 321837 - Export Grid Datarefs/changes/26/23526/1
I have implemented a utils class called GridUtils. In this class there is a method called gridToXml. I have added some functions to already realized Grid snippets. For Grid Table there is a full support for export, all the items are exported. For Grid Tree, unfortunately only its visible items are exported. Change-Id: I6328f827bcc7188cadf697665c077ffb3cce2150 Signed-off-by: mpaturzo <caosmpz@yahoo.it>
-rw-r--r--examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposePerformance.java228
-rw-r--r--examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposeTreeTest.java215
-rw-r--r--widgets/grid/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/GridUtils.java165
3 files changed, 502 insertions, 106 deletions
diff --git a/examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposePerformance.java b/examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposePerformance.java
index aa4c9ad..3eefa0a 100644
--- a/examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposePerformance.java
+++ b/examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposePerformance.java
@@ -11,6 +11,14 @@ package org.eclipse.nebula.snippets.grid.viewer;
* defaults properties change
*******************************************************************************/
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.viewers.CellEditor;
@@ -29,6 +37,7 @@ import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
import org.eclipse.nebula.jface.gridviewer.GridViewerEditor;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
+import org.eclipse.nebula.widgets.grid.GridUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
@@ -40,6 +49,7 @@ import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
/**
@@ -56,12 +66,14 @@ import org.eclipse.swt.widgets.Shell;
* MemoryAnalyzer
*
*/
-public class GridViewerSnippetDisposePerformance {
+public class GridViewerSnippetDisposePerformance
+{
private static final int NUM_COLUMNS = 10;
private static final int NUM_MODELS = 10;
- private class MyContentProvider implements IStructuredContentProvider {
+ private class MyContentProvider implements IStructuredContentProvider
+ {
/*
* (non-Javadoc)
@@ -70,7 +82,8 @@ public class GridViewerSnippetDisposePerformance {
* java.lang.Object)
*/
- public Object[] getElements(Object inputElement) {
+ public Object[] getElements(Object inputElement)
+ {
return (MyModel[]) inputElement;
}
@@ -79,7 +92,8 @@ public class GridViewerSnippetDisposePerformance {
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
- public void dispose() {
+ public void dispose()
+ {
}
@@ -90,7 +104,8 @@ public class GridViewerSnippetDisposePerformance {
* .jface.viewers.Viewer, java.lang.Object, java.lang.Object)
*/
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
+ {
}
@@ -98,15 +113,18 @@ public class GridViewerSnippetDisposePerformance {
public static boolean flag = true;
- public class MyModel {
+ public class MyModel
+ {
public int counter;
- public MyModel(int counter) {
+ public MyModel(int counter)
+ {
this.counter = counter;
}
@Override
- public String toString() {
+ public String toString()
+ {
return "Item " + this.counter;
}
}
@@ -114,32 +132,41 @@ public class GridViewerSnippetDisposePerformance {
final FontRegistry registry = new FontRegistry();
public class MyLabelProvider extends LabelProvider implements ITableLabelProvider, ITableFontProvider,
- ITableColorProvider {
+ ITableColorProvider
+ {
- public Image getColumnImage(Object element, int columnIndex) {
+ public Image getColumnImage(Object element, int columnIndex)
+ {
return null;
}
- public String getColumnText(Object element, int columnIndex) {
+ public String getColumnText(Object element, int columnIndex)
+ {
return "Column " + columnIndex + " => " + element.toString();
}
- public Font getFont(Object element, int columnIndex) {
- if (((MyModel) element).counter % 2 == 0) {
+ public Font getFont(Object element, int columnIndex)
+ {
+ if (((MyModel) element).counter % 2 == 0)
+ {
return registry.getBold(Display.getCurrent().getSystemFont().getFontData()[0].getName());
}
return null;
}
- public Color getBackground(Object element, int columnIndex) {
- if (((MyModel) element).counter % 2 == 0) {
+ public Color getBackground(Object element, int columnIndex)
+ {
+ if (((MyModel) element).counter % 2 == 0)
+ {
return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
}
return null;
}
- public Color getForeground(Object element, int columnIndex) {
- if (((MyModel) element).counter % 2 == 1) {
+ public Color getForeground(Object element, int columnIndex)
+ {
+ if (((MyModel) element).counter % 2 == 1)
+ {
return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
}
return null;
@@ -147,26 +174,31 @@ public class GridViewerSnippetDisposePerformance {
}
- public GridViewerSnippetDisposePerformance(final Shell shell, boolean createButtons) {
+ public GridViewerSnippetDisposePerformance(final Shell shell, boolean createButtons)
+ {
final GridTableViewer v = new GridTableViewer(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
v.setLabelProvider(new MyLabelProvider());
v.setContentProvider(new MyContentProvider());
v.getGrid().setCellSelectionEnabled(true);
v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getGrid()), new TextCellEditor(v.getGrid()) });
- v.setCellModifier(new ICellModifier() {
+ v.setCellModifier(new ICellModifier()
+ {
- public boolean canModify(Object element, String property) {
+ public boolean canModify(Object element, String property)
+ {
return true;
}
- public Object getValue(Object element, String property) {
+ public Object getValue(Object element, String property)
+ {
if (element == null)
return "Element is null";
return "Column " + property + " => " + element.toString();
}
- public void modify(Object element, String property, Object value) {
+ public void modify(Object element, String property, Object value)
+ {
}
@@ -174,10 +206,12 @@ public class GridViewerSnippetDisposePerformance {
v.setColumnProperties(new String[] { "1", "2" });
- ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
+ ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v)
+ {
@Override
- protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
+ protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event)
+ {
return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR);
@@ -188,24 +222,30 @@ public class GridViewerSnippetDisposePerformance {
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_VERTICAL
| ColumnViewerEditor.KEYBOARD_ACTIVATION);
- for (int i = 0; i < NUM_COLUMNS; i++) {
+ for (int i = 0; i < NUM_COLUMNS; i++)
+ {
createColumn(v, "Column " + i);
}
- if (createButtons) {
+ if (createButtons)
+ {
Composite buttons = new Composite(shell, SWT.NONE);
buttons.setLayout(new RowLayout());
final Button remove3 = new Button(buttons, SWT.NONE);
remove3.setText("remove row with index 3");
final String restore = "restore grid";
- remove3.addMouseListener(new MouseAdapter() {
+ remove3.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
// button.dispose();
- try {
+ try
+ {
v.getGrid().remove(3);
}
- catch (java.lang.IllegalArgumentException ie) {
+ catch (java.lang.IllegalArgumentException ie)
+ {
new MessageDialog(shell, "IndexOutOfBound error", null, "Restore the grid with button \""
+ restore + "\"", MessageDialog.WARNING, new String[] { "Ok" }, 0).open();
}
@@ -216,9 +256,11 @@ public class GridViewerSnippetDisposePerformance {
final Button add3 = new Button(buttons, SWT.NONE);
add3.setText("add row with index 3");
- add3.addMouseListener(new MouseAdapter() {
+ add3.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
GridItem gridItem = new GridItem(v.getGrid(), SWT.NONE, 3);
gridItem.setText(0, "Added in 3");
shell.layout();
@@ -226,25 +268,31 @@ public class GridViewerSnippetDisposePerformance {
});
final Button addColumn1 = new Button(buttons, SWT.NONE);
addColumn1.setText("add column in 1");
- addColumn1.addMouseListener(new MouseAdapter() {
+ addColumn1.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
new GridColumn(v.getGrid(), SWT.NONE, 1);
shell.layout();
}
});
final Button removeColumn = new Button(buttons, SWT.NONE);
removeColumn.setText("remove first column");
- removeColumn.addMouseListener(new MouseAdapter() {
+ removeColumn.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
// button.dispose();
- try {
+ try
+ {
v.getGrid().getColumn(0).dispose();
v.getGrid().redraw();
v.getGrid().layout();
}
- catch (java.lang.IllegalArgumentException ie) {
+ catch (java.lang.IllegalArgumentException ie)
+ {
new MessageDialog(shell, "IndexOutOfBound error", null, "Restore the grid with button \""
+ restore + "\"", MessageDialog.WARNING, new String[] { "Ok" }, 0).open();
}
@@ -254,14 +302,18 @@ public class GridViewerSnippetDisposePerformance {
});
final Button addColumn = new Button(buttons, SWT.NONE);
addColumn.setText("add column");
- addColumn.addMouseListener(new MouseAdapter() {
+ addColumn.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
// button.dispose();
- try {
+ try
+ {
createColumn(v, "Added");
}
- catch (java.lang.IllegalArgumentException ie) {
+ catch (java.lang.IllegalArgumentException ie)
+ {
new MessageDialog(shell, "IndexOutOfBound error", null, "Restore the grid with button \""
+ restore + "\"", MessageDialog.WARNING, new String[] { "Ok" }, 0).open();
}
@@ -271,9 +323,11 @@ public class GridViewerSnippetDisposePerformance {
});
final Button changeFont = new Button(buttons, SWT.NONE);
changeFont.setText("Change Default");
- changeFont.addMouseListener(new MouseAdapter() {
+ changeFont.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
v.getGrid().setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
v.getGrid().setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
v.getGrid().setFont(
@@ -281,16 +335,73 @@ public class GridViewerSnippetDisposePerformance {
shell.layout();
}
});
+ Button exportGrid = new Button(buttons, SWT.NONE);
+ exportGrid.setText("Export Grid");
+ exportGrid.addMouseListener(new MouseAdapter()
+ {
+ @Override
+ public void mouseDown(MouseEvent e)
+ {
+ try
+ {
+ if (v.getGrid().isDisposed())
+ return;
+ FileDialog fileDialog = new FileDialog(shell);
+ fileDialog.setFilterExtensions(new String[] { "*.xml" });
+ String open = fileDialog.open();
+ if (open != null)
+ {
+ open = open.endsWith(".xml") ? open : open + ".xml";
+ FileOutputStream outputStream = new FileOutputStream(new File(open));
+ try
+ {
+ GridUtils.gridToXml(v.getGrid(), outputStream);
+ new MessageDialog(shell, "Success", null, "Exported in " + open,
+ MessageDialog.INFORMATION, new String[] { "Great!" }, 1).open();
+ }
+ catch (ParserConfigurationException e1)
+ {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ catch (TransformerException e1)
+ {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ finally
+ {
+ try
+ {
+ outputStream.close();
+ }
+ catch (IOException e1)
+ {
+ e1.printStackTrace();
+ }
+ }
+ }
+ }
+ catch (FileNotFoundException e1)
+ {
+ e1.printStackTrace();
+ }
+ }
+ });
final Button restoreButton = new Button(buttons, SWT.NONE);
restoreButton.setText(restore);
- restoreButton.addMouseListener(new MouseAdapter() {
+ restoreButton.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
GridColumn[] columns = v.getGrid().getColumns();
- for (GridColumn gridColumn : columns) {
+ for (GridColumn gridColumn : columns)
+ {
gridColumn.dispose();
}
- for (int i = 0; i < NUM_COLUMNS; i++) {
+ for (int i = 0; i < NUM_COLUMNS; i++)
+ {
createColumn(v, "Column " + i);
}
v.setInput(createModel());
@@ -302,9 +413,11 @@ public class GridViewerSnippetDisposePerformance {
});
Button gcButton = new Button(buttons, SWT.NONE);
gcButton.setText("gc");
- gcButton.addMouseListener(new MouseAdapter() {
+ gcButton.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
System.gc();
}
});
@@ -316,16 +429,19 @@ public class GridViewerSnippetDisposePerformance {
v.getGrid().setHeaderVisible(true);
}
- private void createColumn(final GridTableViewer v, String name) {
+ private void createColumn(final GridTableViewer v, String name)
+ {
GridColumn column = new GridColumn(v.getGrid(), SWT.NONE);
column.setWidth(200);
column.setText(name);
}
- private MyModel[] createModel() {
+ private MyModel[] createModel()
+ {
MyModel[] elements = new MyModel[NUM_MODELS];
- for (int i = 0; i < NUM_MODELS; i++) {
+ for (int i = 0; i < NUM_MODELS; i++)
+ {
elements[i] = new MyModel(i);
}
@@ -335,7 +451,8 @@ public class GridViewerSnippetDisposePerformance {
/**
* @param args
*/
- public static void main(String[] args) {
+ public static void main(String[] args)
+ {
Display display = new Display();
Shell shell = new Shell(display);
@@ -343,7 +460,8 @@ public class GridViewerSnippetDisposePerformance {
new GridViewerSnippetDisposePerformance(shell, true);
shell.open();
- while (!shell.isDisposed()) {
+ while (!shell.isDisposed())
+ {
if (!display.readAndDispatch())
display.sleep();
}
diff --git a/examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposeTreeTest.java b/examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposeTreeTest.java
index 6f3e97c..b16991c 100644
--- a/examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposeTreeTest.java
+++ b/examples/org.eclipse.nebula.snippets/src/org/eclipse/nebula/snippets/grid/viewer/GridViewerSnippetDisposeTreeTest.java
@@ -12,8 +12,15 @@
package org.eclipse.nebula.snippets.grid.viewer;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
import java.util.ArrayList;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.viewers.ITableColorProvider;
@@ -26,6 +33,7 @@ import org.eclipse.nebula.jface.gridviewer.GridTreeViewer;
import org.eclipse.nebula.jface.gridviewer.GridViewerColumn;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
+import org.eclipse.nebula.widgets.grid.GridUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
@@ -37,6 +45,7 @@ import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
/**
@@ -46,15 +55,18 @@ import org.eclipse.swt.widgets.Shell;
* @author Mirko Paturzo <mirko.paturzo@exeura.eu>
*
*/
-public class GridViewerSnippetDisposeTreeTest {
- private class MyContentProvider implements ITreeContentProvider {
+public class GridViewerSnippetDisposeTreeTest
+{
+ private class MyContentProvider implements ITreeContentProvider
+ {
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
*/
- public Object[] getElements(Object inputElement) {
+ public Object[] getElements(Object inputElement)
+ {
return ((MyModel) inputElement).child.toArray();
}
@@ -63,7 +75,8 @@ public class GridViewerSnippetDisposeTreeTest {
*
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
- public void dispose() {
+ public void dispose()
+ {
}
@@ -73,7 +86,8 @@ public class GridViewerSnippetDisposeTreeTest {
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
* java.lang.Object, java.lang.Object)
*/
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
+ {
}
@@ -82,7 +96,8 @@ public class GridViewerSnippetDisposeTreeTest {
*
* @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
*/
- public Object[] getChildren(Object parentElement) {
+ public Object[] getChildren(Object parentElement)
+ {
return getElements(parentElement);
}
@@ -91,8 +106,10 @@ public class GridViewerSnippetDisposeTreeTest {
*
* @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
*/
- public Object getParent(Object element) {
- if (element == null) {
+ public Object getParent(Object element)
+ {
+ if (element == null)
+ {
return null;
}
@@ -104,28 +121,33 @@ public class GridViewerSnippetDisposeTreeTest {
*
* @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
*/
- public boolean hasChildren(Object element) {
+ public boolean hasChildren(Object element)
+ {
return ((MyModel) element).child.size() > 0;
}
}
- public class MyModel {
+ public class MyModel
+ {
public MyModel parent;
public ArrayList child = new ArrayList();
public int counter;
- public MyModel(int counter, MyModel parent) {
+ public MyModel(int counter, MyModel parent)
+ {
this.parent = parent;
this.counter = counter;
}
@Override
- public String toString() {
+ public String toString()
+ {
String rv = "Item ";
- if (parent != null) {
+ if (parent != null)
+ {
rv = parent.toString() + ".";
}
@@ -138,32 +160,41 @@ public class GridViewerSnippetDisposeTreeTest {
final FontRegistry registry = new FontRegistry();
public class MyLabelProvider extends LabelProvider implements ITableLabelProvider, ITableFontProvider,
- ITableColorProvider {
+ ITableColorProvider
+ {
- public Image getColumnImage(Object element, int columnIndex) {
+ public Image getColumnImage(Object element, int columnIndex)
+ {
return null;
}
- public String getColumnText(Object element, int columnIndex) {
+ public String getColumnText(Object element, int columnIndex)
+ {
return "Column " + columnIndex + " => " + element.toString();
}
- public Font getFont(Object element, int columnIndex) {
- if (((MyModel) element).counter % 2 == 0) {
+ public Font getFont(Object element, int columnIndex)
+ {
+ if (((MyModel) element).counter % 2 == 0)
+ {
return registry.getBold(Display.getCurrent().getSystemFont().getFontData()[0].getName());
}
return null;
}
- public Color getBackground(Object element, int columnIndex) {
- if (((MyModel) element).counter % 2 == 0) {
+ public Color getBackground(Object element, int columnIndex)
+ {
+ if (((MyModel) element).counter % 2 == 0)
+ {
return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
}
return null;
}
- public Color getForeground(Object element, int columnIndex) {
- if (((MyModel) element).counter % 2 == 1) {
+ public Color getForeground(Object element, int columnIndex)
+ {
+ if (((MyModel) element).counter % 2 == 1)
+ {
return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
}
return null;
@@ -171,7 +202,8 @@ public class GridViewerSnippetDisposeTreeTest {
}
- public GridViewerSnippetDisposeTreeTest(final Shell shell) {
+ public GridViewerSnippetDisposeTreeTest(final Shell shell)
+ {
final GridTreeViewer v = new GridTreeViewer(shell);
Composite buttons = new Composite(shell, SWT.NONE);
@@ -179,14 +211,18 @@ public class GridViewerSnippetDisposeTreeTest {
final Button remove3 = new Button(buttons, SWT.NONE);
remove3.setText("remove row with index 3");
final String restore = "restore grid";
- remove3.addMouseListener(new MouseAdapter() {
+ remove3.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
// button.dispose();
- try {
+ try
+ {
v.getGrid().remove(3);
}
- catch (java.lang.IllegalArgumentException ie) {
+ catch (java.lang.IllegalArgumentException ie)
+ {
new MessageDialog(shell, "IndexOutOfBound error", null, "Restore the grid with button \"" + restore
+ "\"", MessageDialog.WARNING, new String[] { "Ok" }, 0).open();
}
@@ -196,9 +232,11 @@ public class GridViewerSnippetDisposeTreeTest {
});
final Button add3 = new Button(buttons, SWT.NONE);
add3.setText("add row with index 3");
- add3.addMouseListener(new MouseAdapter() {
+ add3.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
GridItem gridItem = new GridItem(v.getGrid(), SWT.NONE, 3);
gridItem.setText(0, "Added in 3");
shell.layout();
@@ -206,25 +244,31 @@ public class GridViewerSnippetDisposeTreeTest {
});
final Button addColumn1 = new Button(buttons, SWT.NONE);
addColumn1.setText("add column in 1");
- addColumn1.addMouseListener(new MouseAdapter() {
+ addColumn1.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
new GridColumn(v.getGrid(), SWT.NONE, 1);
shell.layout();
}
});
final Button removeColumn = new Button(buttons, SWT.NONE);
removeColumn.setText("remove first column");
- removeColumn.addMouseListener(new MouseAdapter() {
+ removeColumn.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
// button.dispose();
- try {
+ try
+ {
v.getGrid().getColumn(0).dispose();
v.getGrid().redraw();
v.getGrid().layout();
}
- catch (java.lang.IllegalArgumentException ie) {
+ catch (java.lang.IllegalArgumentException ie)
+ {
new MessageDialog(shell, "IndexOutOfBound error", null, "Restore the grid with button \"" + restore
+ "\"", MessageDialog.WARNING, new String[] { "Ok" }, 0).open();
}
@@ -234,14 +278,18 @@ public class GridViewerSnippetDisposeTreeTest {
});
final Button addColumn = new Button(buttons, SWT.NONE);
addColumn.setText("add column");
- addColumn.addMouseListener(new MouseAdapter() {
+ addColumn.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
// button.dispose();
- try {
+ try
+ {
createColumn(v, "Added");
}
- catch (java.lang.IllegalArgumentException ie) {
+ catch (java.lang.IllegalArgumentException ie)
+ {
new MessageDialog(shell, "IndexOutOfBound error", null, "Restore the grid with button \"" + restore
+ "\"", MessageDialog.WARNING, new String[] { "Ok" }, 0).open();
}
@@ -249,11 +297,66 @@ public class GridViewerSnippetDisposeTreeTest {
shell.layout();
}
});
+ Button exportGrid = new Button(buttons, SWT.NONE);
+ exportGrid.setText("Export Grid");
+ exportGrid.addMouseListener(new MouseAdapter()
+ {
+ @Override
+ public void mouseDown(MouseEvent e)
+ {
+ try
+ {
+ if (v.getGrid().isDisposed())
+ return;
+ FileDialog fileDialog = new FileDialog(shell);
+ fileDialog.setFilterExtensions(new String[] { "*.xml" });
+ String open = fileDialog.open();
+ if (open != null)
+ {
+ open = open.endsWith(".xml") ? open : open + ".xml";
+ FileOutputStream outputStream = new FileOutputStream(new File(open));
+ try
+ {
+ GridUtils.gridToXml(v.getGrid(), outputStream);
+ new MessageDialog(shell, "Success", null, "Exported in " + open, MessageDialog.INFORMATION,
+ new String[] { "Great!" }, 1).open();
+ }
+ catch (ParserConfigurationException e1)
+ {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ catch (TransformerException e1)
+ {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ finally
+ {
+ try
+ {
+ outputStream.close();
+ }
+ catch (IOException e1)
+ {
+ e1.printStackTrace();
+ }
+ }
+ }
+ }
+ catch (FileNotFoundException e1)
+ {
+ e1.printStackTrace();
+ }
+ }
+ });
final Button changeFont = new Button(buttons, SWT.NONE);
changeFont.setText("Change Default");
- changeFont.addMouseListener(new MouseAdapter() {
+ changeFont.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
v.getGrid().setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
v.getGrid().setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
v.getGrid()
@@ -263,11 +366,14 @@ public class GridViewerSnippetDisposeTreeTest {
});
final Button restoreButton = new Button(buttons, SWT.NONE);
restoreButton.setText(restore);
- restoreButton.addMouseListener(new MouseAdapter() {
+ restoreButton.addMouseListener(new MouseAdapter()
+ {
@Override
- public void mouseDown(MouseEvent e) {
+ public void mouseDown(MouseEvent e)
+ {
GridColumn[] columns = v.getGrid().getColumns();
- for (GridColumn gridColumn : columns) {
+ for (GridColumn gridColumn : columns)
+ {
gridColumn.dispose();
}
revertGrid(v);
@@ -277,7 +383,8 @@ public class GridViewerSnippetDisposeTreeTest {
revertGrid(v);
}
- private void revertGrid(final GridTreeViewer v) {
+ private void revertGrid(final GridTreeViewer v)
+ {
GridViewerColumn column = new GridViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setText("Column 1");
@@ -292,16 +399,19 @@ public class GridViewerSnippetDisposeTreeTest {
v.setInput(createModel());
}
- private MyModel createModel() {
+ private MyModel createModel()
+ {
MyModel root = new MyModel(0, null);
root.counter = 0;
MyModel tmp;
- for (int i = 1; i < 10; i++) {
+ for (int i = 1; i < 10; i++)
+ {
tmp = new MyModel(i, root);
root.child.add(tmp);
- for (int j = 1; j < i; j++) {
+ for (int j = 1; j < i; j++)
+ {
tmp.child.add(new MyModel(j, tmp));
}
}
@@ -309,14 +419,16 @@ public class GridViewerSnippetDisposeTreeTest {
return root;
}
- public static void main(String[] args) {
+ public static void main(String[] args)
+ {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new GridViewerSnippetDisposeTreeTest(shell);
shell.open();
- while (!shell.isDisposed()) {
+ while (!shell.isDisposed())
+ {
if (!display.readAndDispatch())
display.sleep();
}
@@ -324,7 +436,8 @@ public class GridViewerSnippetDisposeTreeTest {
display.dispose();
}
- private static void createColumn(final GridTreeViewer v, String name) {
+ private static void createColumn(final GridTreeViewer v, String name)
+ {
GridColumn column = new GridColumn(v.getGrid(), SWT.NONE);
column.setWidth(200);
column.setText(name);
diff --git a/widgets/grid/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/GridUtils.java b/widgets/grid/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/GridUtils.java
new file mode 100644
index 0000000..ef835ef
--- /dev/null
+++ b/widgets/grid/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/GridUtils.java
@@ -0,0 +1,165 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Mirko Paturzo (Exeura srl).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Mirko Paturzo - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.nebula.widgets.grid;
+
+import java.io.OutputStream;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Some tools for Grid.
+ *
+ * @author Mirko Paturzo <mirko.paturzo@exeura.eu>
+ */
+public class GridUtils
+{
+
+ private static final String INDET_PROPERTY = "{http://xml.apache.org/xslt}indent-amount";
+ private static final String INDENT_VALUE = "2";
+ private static final String INDENT_ACCEPTED_VALUE = "yes";
+
+ /**
+ * Tags in xml
+ */
+ private static final String GRID_TAG = "grid";
+ private static final String HEADER_TAG = "header";
+ private static final String COLUMN_TAG = "column";
+ private static final String CHILDREN_TAG = "children";
+ private static final String ROWS_TAG = "rows";
+ private static final String ROW_TAG = "row";
+ private static final String ID_TAG = "id";
+
+ /**
+ * This method export a grid into a outputstream using xml.
+ * SWT Main thread is required for the export.
+ * Full supports for Grid Table.
+ * Grid Tree only visible items was exported.
+ *
+ * @param grid the grid who will be export to xml.
+ * @param outputStream used for the export.
+ * @throws ParserConfigurationException
+ * @throws TransformerException
+ */
+ public static void gridToXml(Grid grid, OutputStream outputStream) throws ParserConfigurationException,
+ TransformerException
+ {
+
+ DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
+
+ final Document doc = docBuilder.newDocument();
+
+ Element rootElement = doc.createElement(GRID_TAG);
+ doc.appendChild(rootElement);
+
+ GridColumn[] columnsArray = grid.getColumns();
+
+ Element header = doc.createElement(HEADER_TAG);
+ rootElement.appendChild(header);
+
+ for (int column = 0; column < columnsArray.length; column++)
+ {
+
+ Element columnElement = doc.createElement(COLUMN_TAG);
+ columnElement.appendChild(doc.createTextNode(columnsArray[column].getText()));
+ header.appendChild(columnElement);
+ Attr columnNumber = doc.createAttribute(ID_TAG);
+ columnNumber.setValue(Integer.toString(column));
+ columnElement.setAttributeNode(columnNumber);
+
+ }
+
+ GridItem[] itemsList = grid.getItems();
+
+ DataVisualizer dataVisualizer = grid.getDataVisualizer();
+
+ Element rowsElement = doc.createElement(ROWS_TAG);
+ rootElement.appendChild(rowsElement);
+ writeChildren(doc, rowsElement, columnsArray, itemsList, dataVisualizer, 0);
+
+ // write the content into xml file
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ Transformer transformer = transformerFactory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, INDENT_ACCEPTED_VALUE);
+ transformer.setOutputProperty(INDET_PROPERTY, INDENT_VALUE);
+ DOMSource source = new DOMSource(doc);
+ StreamResult result = new StreamResult(outputStream);
+
+ // Output to console for testing
+ // StreamResult result = new StreamResult(System.out);
+ transformer.transform(source, result);
+
+ }
+
+ private static void writeChildren(Document doc, Element rootElement, GridColumn[] columnsList,
+ GridItem[] itemsArray, DataVisualizer dataVisualizer, int level)
+ {
+ for (int row = 0; row < itemsArray.length; row++)
+ {
+ GridItem gridItem = itemsArray[row];
+
+ if (gridItem.isVisible() && gridItem.getLevel() == level)
+ {
+ Element rowElement = writeGridItemInformation(doc, rootElement, columnsList, dataVisualizer, gridItem);
+
+ GridItem[] items = gridItem.getItems();
+ if (items.length > 0)
+ {
+ Element childrenElement = doc.createElement(CHILDREN_TAG);
+ rowElement.appendChild(childrenElement);
+ writeChildren(doc, childrenElement, columnsList, items,
+ gridItem.getGridItemChildrenDataVisualizer(), level + 1);
+ }
+ }
+ }
+ }
+
+ private static Element writeGridItemInformation(Document doc, Element rootElement, GridColumn[] columnsList,
+ DataVisualizer dataVisualizer, GridItem item)
+ {
+ Element rowElement = doc.createElement(ROW_TAG);
+ rootElement.appendChild(rowElement);
+ // Attr rowNumber = doc.createAttribute("id");
+ // rowNumber.setValue(Integer.toString(item.getRowIndex()));
+ // rowElement.setAttributeNode(rowNumber);
+
+ for (int column = 0; column < columnsList.length; column++)
+ {
+
+ String text = dataVisualizer.getText(item, column);
+
+ if (text != null)
+ {
+ Element columnElement = doc.createElement(COLUMN_TAG);
+ columnElement.appendChild(doc.createTextNode(text));
+ rowElement.appendChild(columnElement);
+ Attr columnNumber = doc.createAttribute(ID_TAG);
+ columnNumber.setValue(Integer.toString(column));
+ columnElement.setAttributeNode(columnNumber);
+ }
+
+ }
+
+ return rowElement;
+ }
+}