Hello Friends,
This is one of my tutorials for people working with RCP, SWT, JFace to export grid data. It is very obvious to display data in a grid/tabular form. Most of the times, user wants to export the data which is being displayed so how to export grid data from the grid to an output file. This is not that tricky to achieve. Lets see how to export grid data from a TreeViewer grid to the console. You can replace the console output to be dumped in any output file as per your convenience and instead of TreeViewer you can use any grid type of JFace/SWT.
Lets first create a SWT/JFace project and add a new Shell to it.
Below is the Tree Viewer demo class consisting of the Shell that displays the data in tree viewer and other logic to print the data to console. Since we are working on TreeViewer, I thought it would be better if we dump that data as well in Tree form, so I have used an additional logic for maintaining the tabs/spaces to be displayed. Rest you can see the code for details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
package com.code2java.swt; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; public class TreeViewerDemo extends Shell { /** * Launch the application. * @param args */ public static void main(String args[]) { Display display = new Display (); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); new TreeViewerDemo(shell); shell.open (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } /** * Create the shell. * @param display */ public TreeViewerDemo(Shell shell) { setText("Tree Viewer Example"); setSize(450, 300); shell.setLayout(new GridLayout()); Button exportButton = new Button(shell, SWT.NONE); exportButton.setBounds(10, 207, 75, 25); exportButton.setText("Export Data"); TreeViewer viewer = new TreeViewer(shell, SWT.BORDER); Tree tree = viewer.getTree(); tree.setBounds(0, 0, 404, 201); viewer.setLabelProvider(new LabelProvider()); viewer.setContentProvider(new TreeContentProvider()); viewer.setInput(createModel()); viewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH)); exportButton.addSelectionListener(new SelectionListener() { int tabCount = 0; @Override public void widgetSelected(SelectionEvent arg0) { TreeItem[] items = viewer.getTree().getItems(); lookupChild(items); } /** * Recursive Functions to get and write the children * @param items */ private void lookupChild(TreeItem[] items) { for(TreeItem item : items) { if(item.getData() instanceof ViewModel) { ViewModel modelData = (ViewModel) item.getData(); System.out.println(getTabs(tabCount) + modelData.toString()); { if(modelData.child.size() > 0) { // Check if items are expanded or not - Optional if (item.getExpanded()) { tabCount++; lookupChild(item.getItems()); } } } } } if(tabCount>0) { tabCount--; } } /** * Helps get the tab count so that proper tree structure can be shown * @param counter * @return */ private String getTabs(int counter) { String str = ""; while(counter!=0) { str += "\t"; counter--; } return str; } @Override public void widgetDefaultSelected(SelectionEvent arg0) { // TODO Auto-generated method stub } }); } /** * Created a dummy model for Tree Viewer that will be displayed as parent and children in Tree Viewer * @return */ private Object createModel() { ViewModel root = new ViewModel(0, null); ViewModel tmp, tmp1, tmp2; for( int i = 0; i < 2; i++ ) { tmp = new ViewModel(i, root); root.child.add(tmp); for( int j = 1; j < 5; j++ ) { tmp1 = new ViewModel(j, tmp); tmp.child.add(tmp1); for(int x = 1; x < 5; x++) { tmp2 = new ViewModel(x, tmp1); tmp1.child.add(tmp2); if(x%2 == 0) { tmp2.child.add(new ViewModel(x, tmp2)); } } } } return root; } @Override protected void checkSubclass() { // Disable the check that prevents subclassing of SWT components } } |
We will create a ViewModel to hold the data of tree. Below is the one. I have not used more elements in the Model as it is just a demonstration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.code2java.swt; import java.util.ArrayList; public class ViewModel { public ViewModel parent; public ArrayList<ViewModel> child = new ArrayList<ViewModel>(); public int counter; public ViewModel(int counter, ViewModel parent) { this.parent = parent; this.counter = counter; } @Override public String toString() { return "element"+counter; } } |
There should be a content provider for any Tree Viewer, so we have added our own implementation of ITreeContentProvider. It will display the content Tree Viewer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.code2java.swt; import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.Viewer; public class TreeContentProvider implements ITreeContentProvider { public Object[] getElements(Object inputElement) { return ((ViewModel)inputElement).child.toArray(); } public void dispose() { } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } public Object[] getChildren(Object parentElement) { return getElements(parentElement); } public Object getParent(Object element) { if( element == null) { return null; } return ((ViewModel)element).parent; } public boolean hasChildren(Object element) { return ((ViewModel)element).child.size() > 0; } } |
Now we will run our application to get the below UI shell with our data in Tree Viewer –
Output
You can see an Export button on top of the window, click it to print the current structure on console –
Hope this helps you.
Thanks !