private void sort(int column) { if (table.getItemCount() <= 1) return; TableItem[] items = table.getItems(); String[][] data = new String[items.length][table.getColumnCount()]; for (int i = 0; i < items.length; i++) { for (int j = 0; j < table.getColumnCount(); j++) { data[i][j] = items[i].getText(j); } } Arrays.sort(data, new RowComparator(column)); if (lastSortColumn != column) { table.setSortColumn(table.getColumn(column)); table.setSortDirection(SWT.DOWN); for (int i = 0; i < data.length; i++) { items[i].setText(data[i]); } lastSortColumn = column; } else { // reverse order if the current column is selected again table.setSortDirection(SWT.UP); int j = data.length - 1; for (int i = 0; i < data.length; i++) { items[i].setText(data[j--]); } lastSortColumn = -1; } }
private void openFile(String fileName, Table table) { String outStr; try { CSVReader reader = new CSVReader(new FileReader(fileName), ',', '"'); String[] strLineArr; // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(fileName); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; table.clearAll(); table.removeAll(); table.redraw(); int length = 0; boolean first = true; // Read File Line By Line while ((strLineArr = reader.readNext()) != null) { // while ((strLine = br.readLine()) != null) { // Print the content on the console // outStr = strLine; TableItem item = null; if (first) { length = strLineArr.length; } else { item = new TableItem(table, SWT.NONE); } int thisLen = strLineArr.length; if (thisLen <= length) { // String[] line = new String[length]; for (int i = 0; i < thisLen; i++) { // line[i] = st.nextToken(); // item.setText (i, st.nextToken()); if (first) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText(strLineArr[i]); } else { // System.out.println("-- "+i+" -- " + strLineArr[i]); item.setText(i, strLineArr[i]); } } } first = false; } // System.out.println("Finisehd file"); for (int i = 0; i < length; i++) { table.getColumn(i).pack(); } // System.out.println("finished packing"); // Close the input stream in.close(); } catch (Exception e) { // Catch exception if any System.err.println("Error: " + e.getMessage()); e.printStackTrace(); } }