/* File: Demo2.java Contains: Sample application that demonstrates the use of the Argent Coffee Table. Copyright: 1996-99 Argent Software */ import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; import java.util.*; import Argent.Grid.*; import MyDate; import MyRank; public class Demo2 extends Applet { // Our grid. GridPanel fGrid; // Rows and Columns; int fNumCols = 11; int fNumRows = 42; /** * Initialize. */ public void init() { // Let's use a BorderLayout to help us with the user input areas. setLayout(new BorderLayout()); // Instantiate a new GridPanel with the specified rows and columns // and other defaults like cell selection, row and column headers, // row and column lines enabled and with scrollbars. fGrid = new GridPanel(fNumRows, fNumCols); // Make room for the images. fGrid.setRowHeight(32); // Show the row headers. fGrid.setRowHeaderWidth(40); fGrid.setRowNumbers(true); // Let's use Times, plain, 9-point. fGrid.setGridFont(new Font("TimesRoman", Font.PLAIN, 9)); // Set the background color to light yellow. fGrid.setGridBackground(new Color(255, 255, 230)); // Set the grid lines to red. fGrid.setLineColor(Color.gray); fGrid.setColAttributes(2, new GridAttributes(fGrid, null, null, null, 0, GridPanel.IMAGE), true); // CONFIGURE SELECTIONS AND HIGHLIGHTING ----------------------- // Enable selection by rows. fGrid.setRowSelection(true); // Choose the selection highlighting colors. fGrid.setHighlightColor(Color.blue); // CONFIGURE SORTING ------------------------------------------ // Enable sorting for all columns fGrid.setSortEnable(true); // Disable sorting for the image column. fGrid.setSortEnable(2, false); // Set sort column color to a very light gray. fGrid.setSortColumnColor(new Color(0xEFEFEF)); // USE CUSTOM SORTING BY OVERRIDING GRIDDATA ------------------- fGrid.setGridData(new MyGridData(fNumRows, fNumCols), false); // Add the grid into the applet panel add("Center", fGrid); // Read in the data. this.readData("presidents.txt"); Label message = new Label(""); message.setFont(new Font("TimesRoman", Font.PLAIN, 10)); message.setText("Argent Coffee Table version " + fGrid.getVersionString() + " \u00A9 1996-1999 Argent Software"); add("South", message); doLayout(); } /** * Read data from text file. */ void readData(String dataFile) { if (dataFile != null) { try { // Open the file. InputStream aStream = this.getClass().getResourceAsStream(dataFile); BufferedReader in = new BufferedReader(new InputStreamReader(aStream)); // The first line are the column widths String line = in.readLine(); StringTokenizer st = new StringTokenizer(line, "\t"); for (int col = 1; col <= fNumCols; col++) { if (st.hasMoreTokens()) { // Set widths of each column. int width = Integer.parseInt(st.nextToken()); fGrid.setColWidth(col, width, true, true); } } // The second line are the column headers line = in.readLine(); st = new StringTokenizer(line, "\t"); for (int col = 1; col <= fNumCols; col++) { if (st.hasMoreTokens()) { // Set the labels for each column. fGrid.setColHeaderText(col, st.nextToken(), false); } } // Create a MediaTracker for the images. MediaTracker tracker = new MediaTracker(this); GridAttributes redAttr = new GridAttributes(fGrid, null, Color.red, null, 0); GridAttributes blueAttr = new GridAttributes(fGrid, null, Color.blue, null, 0); // The real data follows, read and parse one line at a time. int row = 1; while ( row <= fNumRows ) { line = in.readLine(); if (line == null) break; st = new StringTokenizer(line, "\t"); for (int col = 1; col <= fNumCols; col++) { if (st.hasMoreTokens()) { String str = st.nextToken(); if (col == 1) { // Column 1 are the rankings. fGrid.setCellData(col, row, MyRank.parseMyRank(str), false); } else if (col == 2) { // Column 2 are the images. InputStream imageStream = this.getClass().getResourceAsStream("presidents/"+str); byte imageBytes[]=new byte[imageStream.available()]; imageStream.read(imageBytes); Image im = Toolkit.getDefaultToolkit().createImage(imageBytes); fGrid.setCellData(col, row, im, false); // Let's track it so we can wait for all to load. tracker.addImage(im, row); } else if (col == 5) { if (str.equals("Republican")) fGrid.setCellAttributes(col, row, redAttr, false); else if (str.equals("Democratic")) fGrid.setCellAttributes(col, row, blueAttr, false); fGrid.setCellText(col, row, str, false); } else if (col == 8) { // Column 8 are the birthdates. fGrid.setCellData(col, row, MyDate.parseMyDate(str), false); } else { // Other columns can be treated as text. fGrid.setCellText(col, row, str, false); } } } row++; } // Wait for all the images to load. try { tracker.waitForAll(); } catch (Exception ex) { } } catch (Exception ex) { System.out.println("Error: " + ex); } } } } /** * Here's a simple date object. */ public class MyDate implements Comparable { // The date storage. private int fDate; private int fMonth; private int fYear; // Constructor public MyDate() { this(7, 11, 1965); } // Another Constructor. public MyDate(int date, int month, int year) { fDate = date; fMonth = month; fYear = year; } // compareTo: negative if ab. public int compareTo(Object anotherObject) { MyDate bDate = (MyDate)anotherObject; if (this.fYear != bDate.fYear) { return (this.fYear - bDate.fYear); } else if (this.fMonth != bDate.fMonth) { return (this.fMonth - bDate.fMonth); } else { return (this.fDate - bDate.fDate); } } // Month to string conversion. public String monthString() { switch (fMonth) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } return ""; } // Parser. public static MyDate parseMyDate(String str) { StringTokenizer st = new StringTokenizer(str, "/"); int month = Integer.parseInt(st.nextToken()); int date = Integer.parseInt(st.nextToken()); int year = Integer.parseInt(st.nextToken()); return new MyDate(date, month, year); } // String representation. public String toString() { return monthString() + " " + fDate + ", " + fYear; } } /** * Here's a sortable numeric ranking object. */ public class MyRank implements Comparable { int fRank; // Constructor public MyRank(int rank) { fRank = rank; } // Mutator/accessor public int getRank() { return(fRank); } public void setRank(int rank) { fRank = rank; } // Parser. public static MyRank parseMyRank(String str) { return new MyRank(Integer.parseInt(str)); } // compareTo: negative if ab. public int compareTo(Object anotherObject) { int a = fRank; int b = ((MyRank)anotherObject).getRank(); return (a-b); } // toString: returns the string representation. public String toString() { switch (fRank) { case 11: return (fRank + "th"); case 12: return (fRank + "th"); case 13: return (fRank + "th"); } switch (fRank%10) { case 1: return (fRank + "st"); case 2: return (fRank + "nd"); case 3: return (fRank + "rd"); } return (fRank + "th"); } } /** * Here's another way to modify the sorting behaviour. */ public class MyGridData extends GridData { // Constructor. public MyGridData(int numRows, int numCols) { super(numRows, numCols); } // Custom sorting. public int compare(Object aData, Object bData, int col) { // Column 1 contains the rankings, let's sort in *reverse* order. if (col == 1) { int a = ((MyRank)aData).getRank(); int b = ((MyRank)bData).getRank(); return (b-a); // Reverse ordering. } else { // Call the default behaviour for all other columns. return super.compare(aData, bData, col); } } }