/* File: Demo1.java Contains: Sample application that demonstrates the use of the Argent Coffee Table. Copyright: 1996-99 Argent Software */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.StringTokenizer; import java.util.Date; import java.io.*; import java.net.*; import Argent.Grid.*; public class Demo1 extends Applet { private GridPanel grid = null; class MyAdapter extends GridAdapter { public void gridCellsClicked(GridEvent e) { System.out.println(e.toString()); } public void gridDoubleClicked(GridEvent e) { System.out.println(e.toString()); } public void gridCellsReleased(GridEvent e) { System.out.println(e.toString()); } public void gridStartEdit(GridEvent e) { System.out.println(e.toString()); } public void gridCommitEdit(GridEvent e) { System.out.println(e.toString()); } public void gridCancelEdit(GridEvent e) { System.out.println(e.toString()); } public void gridResizeRow(GridEvent e) { System.out.println(e.toString()); } public void gridResizeCol(GridEvent e) { System.out.println(e.toString()); } public void gridSortColumn(GridEvent e) { System.out.println(e.toString()); } public void gridSelChanged(GridEvent e) { System.out.println(e.toString()); } } public void init() { // set up a BorderLayout for the panel, the grid will be inserted in the center // to take up all the space setLayout(new BorderLayout()); // instantiate a new Grid Panel with 54 rows and 6 columns and other defaults like cell // selection, row and column headers, row and column lines enabled and with scrollbars. grid = new GridPanel(54, 6); // set up attributes for all the cells in the grid, to have the Times fornt and left justification // with string truncation with ellipses grid.setGridAttributes(new GridAttributes(grid, new Font("SansSerif", Font.PLAIN, 12), null, null, GridPanel.JUST_LEFT | GridPanel.TRUNC_STRING, 0), true); // ... and for the grid headers grid.setHeaderAttributes(new GridAttributes(grid, new Font("Dialog", Font.PLAIN, 12), null, null, 0, 0), true); // add it into the center of the panel add("Center", grid); // make the inner adapter "MyAdapter" a listener for this Grid's events grid.addGridListener(new MyAdapter()); // enable row headers with a width of 30 pixels and draw row numbers in row headers grid.setRowHeaderWidth(30); grid.setRowNumbers(true); // enable row selection and multiple cell selection grid.setRowSelection(true); grid.setMultipleSelection(true); // enable the threeD border grid.setThreeDBorder(true); // set up the column widths for all six columns grid.setColWidths("80,80,60,140,200,120"); // set up attributes for column 1, setting to to be a choice list with the following items String[] items = { "Cinema", "Computing", "Entertainment", "News", "Search", "Sport", "Television"}; grid.setColAttributes(1, new GridAttributes(grid, null, null, null, 0, GridPanel.CHOICE, items), true); // set up attributes for column 2, editable text cell type grid.setColAttributes(2, new GridAttributes(grid, null, null, null, 0, GridPanel.EDIT_TEXT), true); // set up attributes for column 4, overriding the text color and cell type, inherit rest from grid attributes grid.setColAttributes(4, new GridAttributes(grid, null, Color.blue, null, 0, GridPanel.URL), true); // set up attributes for column 5, editable cell type, justification style to be left justification // with word wrapping, inherit rest from grid attributes grid.setColAttributes(5, new GridAttributes(grid, null, null, null, GridPanel.JUST_LEFT | GridPanel.WORD_WRAP, GridPanel.EDIT_TEXT), true); // set up attributes for column 6, overriding the text color and cell type, inherit rest from grid attributes grid.setColAttributes(6, new GridAttributes(grid, null, Color.blue, null, 0, GridPanel.MAIL), true); // enable sorting for all columns and set sort column color to a very light gray grid.setSortEnable(true); grid.setSortAttributes(true); grid.setSortColumnColor(new Color(0xEFEFEF)); String dataFile = getParameter("DATAFILE"); if (dataFile != null) { // read the urls.txt file into the grid readData(dataFile, grid, grid.getNumRows(), grid.getNumCols()); } grid.autoResizeRows(false); // resize all row height to fit text, where WORD_WRAP is enabled grid.setAutoResizeRows(true); // set up to automatically resize row heights from now on Label message = new Label(""); message.setFont(new Font("TimesRoman", Font.PLAIN, 10)); message.setBackground(this.getBackground()); message.setText(" Argent Coffee Table version " + grid.getVersionString() + " \u00A9 1996-1998 Argent Software"); add("South", message); doLayout(); } void readData(String dataFile, GridPanel panel, int numRows, int numCols) { if (dataFile != null) { try { InputStream aStream = this.getClass().getResourceAsStream(dataFile); BufferedReader in = new BufferedReader(new InputStreamReader(aStream)); String line = in.readLine(); // read the column headers from the first line StringTokenizer st = new StringTokenizer(line, "\t"); for (int col = 1; col <= numCols; col++) { if (st.hasMoreTokens()) panel.setColHeaderText(col, st.nextToken(), false); } // ...and the data from the remaining lines int row = 1; while ( row <= numRows ) { line = in.readLine(); if (line == null) break; st = new StringTokenizer(line, "\t"); for (int col = 1; col <= numCols; col++) { if (st.hasMoreTokens()) panel.setCellText( col, row, st.nextToken(), false ); } row++; } in.close(); } catch (Exception ex) { System.out.println("Error: " + ex); } } } //{{DECLARE_CONTROLS //}} }