DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
CSV Example In SWT
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class CsvReader {
private Display display;
private Shell shell;
private HashMap<String, Image> hashImages;
private Label label;
private Table table;
private Text source;
private final String initialCSV = "Col #1;Col #2;Col #3\nA;a;0\nB;b;1\nC;c;2\n";
private Color GRAY;
private Color WHITE;
private CTabFolder tab;
public CsvReader() {
display = new Display();
shell = new Shell(display);
init();
createGUI();
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
}
private void init() {
hashImages = new HashMap<String, Image>();
//TODO remove absolute path
hashImages.put("open", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\fileopen.png"));
hashImages.put("close", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\fileclose.png"));
hashImages.put("save", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\filesave.png"));
hashImages.put("saveas", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\filesaveas.png"));
hashImages.put("exit", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\exit.png"));
hashImages.put("cut", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\editcut.png"));
hashImages.put("copy", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\editcopy.png"));
hashImages.put("paste", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\editpaste.png"));
hashImages.put("undo", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\undo.png"));
hashImages.put("redo", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\redo.png"));
hashImages.put("help", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\help.png"));
hashImages.put("about", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\about_kde.png"));
hashImages.put("tab_table", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\1day.png"));
hashImages.put("tab_source", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\14_pencil.png"));
hashImages.put("reload", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\reload.png"));
GRAY = new Color(display, 220, 220, 220);
WHITE = new Color(display, 255, 255, 255);
}
private void createGUI() {
shell.setLayout(new GridLayout(1, false));
shell.setText("CSV Reader");
shell.setSize(500, 300);
shell.setImage(hashImages.get("about"));
ToolBar bar = new ToolBar(shell, SWT.NONE);
bar.setLayoutData(new GridData());
ToolItem citem = new ToolItem(bar, SWT.NONE);
// citem.setText("Reload");
citem.setToolTipText("Reload table");
citem.setImage(hashImages.get("reload"));
citem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
reloadData();
label.setText("Table reloaded.");
}
});
tab = new CTabFolder(shell, SWT.BORDER);
tab.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
CTabItem item = new CTabItem(tab, SWT.NONE);
table = new Table(tab, SWT.SINGLE | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
table.setHeaderVisible(true);
table.setLinesVisible(true);
item.setText("CSV Data");
item.setImage(hashImages.get("tab_table"));
item.setControl(table);
item = new CTabItem(tab, SWT.NONE);
source = new Text(tab, SWT.V_SCROLL | SWT.H_SCROLL);
source.setText(initialCSV);
item.setText("CSV Source");
item.setImage(hashImages.get("tab_source"));
item.setControl(source);
label = new Label(shell, SWT.NONE);
label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
label.setText("Ready");
}
private void reloadData() {
System.err.println("reloading");
BufferedReader br = new BufferedReader(new StringReader(source.getText()));
String line = null;
String[] linedata;
try {
// read first line with headers
line = br.readLine();
linedata = line.split(";");
// System.err.println("got headers '" + line + "' (" + linedata.length + ")");
TableColumn col;
for (int i = 0; i < linedata.length; i++) {
col = new TableColumn(table, SWT.LEFT, i);
col.setText(linedata[i]);
col.setWidth(100);
}
TableItem row;
int count = 0;
while((line = br.readLine()) != null) {
linedata = line.split(";");
// System.err.println("adding '" + line + "' (" + linedata.length + ")");
row = new TableItem(table, SWT.NONE);
row.setText(linedata);
row.setBackground((count++ % 2 == 0) ? GRAY : WHITE);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new CsvReader();
}
}





