/* * This file is part of the Echo Web Application Framework (hereinafter "Echo"). * Copyright (C) 2002 NextApp, Inc. * * Echo is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Echo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Echo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Contributed by Phillip Baird */ import java.util.Iterator; import nextapp.echo.Button; import nextapp.echo.Color; import nextapp.echo.Component; import nextapp.echo.ContainerPane; import nextapp.echo.ContentPane; import nextapp.echo.EchoInstance; import nextapp.echo.EchoConstants; import nextapp.echo.Filler; import nextapp.echo.Grid; import nextapp.echo.Font; import nextapp.echo.Insets; import nextapp.echo.Label; import nextapp.echo.Panel; import nextapp.echo.Table; import nextapp.echo.Window; import nextapp.echo.event.ActionListener; import nextapp.echo.event.ActionEvent; import nextapp.echo.table.DefaultTableColumnModel; import nextapp.echo.table.DefaultTableModel; import nextapp.echo.table.TableCellRenderer; import nextapp.echo.table.TableColumn; import nextapp.echo.table.TableColumnModel; import nextapp.echo.table.TableModel; import nextapp.echoservlet.EchoServer; public class TableServlet extends EchoServer { public EchoInstance newInstance() { return new TableDemo(); } } class TableDemo extends EchoInstance implements ActionListener { // Constants for fonts and colors. public static final Color TITLE_PANE_GRAY = new Color(0x4f, 0x4f, 0x4f); public static final Color BUTTON_PANE_BLUE = new Color(0x6f, 0x6f, 0x7f); public static final Font TITLE_FONT = new Font(Font.VERDANA, Font.ITALIC, 18); // Constants for action commands. private static final String ACTION_REPLACE_MODEL = "replaceModel"; private static final String ACTION_UPDATE_MODEL = "updateModel"; private static final String ACTION_CHANGE_BORDER_COLOR = "changeBorderColor"; private static final String ACTION_CHANGE_CELL_MARGIN = "changeCellMargin"; private static final String ACTION_CHANGE_BORDER_SIZE = "changeBorderSize"; private static final TableCellRenderer BASIC_RENDERER = new TableCellRenderer() { private Font CELL_FONT = new Font(Font.VERDANA, 0, 10); public Component getTableCellRendererComponent(Table table, Object value, int col, int row) { Panel cell = new Panel(); cell.setForeground(Color.BLACK); cell.setBackground(Color.YELLOW); if (value != null) { Label label = new Label(value.toString()); label.setFont(CELL_FONT); cell.add(label); } return cell; } }; private static final TableCellRenderer HEADER_RENDERER = new TableCellRenderer() { private Font CELL_FONT = new Font(Font.VERDANA, Font.ITALIC + Font.BOLD, 18); public Component getTableCellRendererComponent(Table table, Object value, int col, int row) { Panel cell = new Panel(); cell.setForeground(Color.BLACK); cell.setBackground(Color.LIGHTGRAY); if (value != null) { Label label = new Label(value.toString()); label.setFont(CELL_FONT); cell.add(label); } return cell; } }; private static final TableCellRenderer COLORED_RENDERER = new TableCellRenderer() { private Font CELL_FONT = new Font(Font.TIMES_ROMAN, Font.ITALIC, 12); public Component getTableCellRendererComponent(Table table, Object value, int col, int row) { Label label; if (value == null) { label = new Label(""); } else { label = new Label(value.toString()); } label.setForeground(Color.BLACK); label.setFont(CELL_FONT); if (value instanceof Number) { Number aNumber = (Number)value; int val = (int)(aNumber.intValue() * 2.5); Color color = new Color(val, 255, val); label.setBackground(color); label.setHorizontalAlignment(EchoConstants.RIGHT); } else { label.setBackground(Color.RED); label.setHorizontalAlignment(EchoConstants.LEFT); } return label; } }; private TableModel demoModel; private Table demoTable; private Table demoTable2; private Table demoTable3; // In this example, Action Commands are used to // determine which button was pressed when events // occur. Each button has its actionCommand property // set to a unique string. public void actionPerformed(ActionEvent e) { if (ACTION_REPLACE_MODEL.equals( e.getActionCommand())) { replaceModel(); } else if (ACTION_UPDATE_MODEL.equals( e.getActionCommand())) { updateModel(); } else if (ACTION_CHANGE_BORDER_COLOR.equals( e.getActionCommand())) { changeBorderColor(); } else if (ACTION_CHANGE_CELL_MARGIN.equals( e.getActionCommand())) { changeCellMargin(); } else if (ACTION_CHANGE_BORDER_SIZE.equals( e.getActionCommand())) { changeBorderSize(); } } // Changes the border color of the demoTable to a random // dark color. public void changeBorderColor() { Color color = randomColor(false); demoTable.setBorderColor(color); color = randomColor(false); demoTable2.setBorderColor(color); color = randomColor(false); demoTable3.setBorderColor(color); } // Changes the border size of the demoTable between // 0, 1, 2, 4, and 8 pixels. public void changeBorderSize() { int size = demoTable.getBorderSize(); if (size == 0) { size = 1; } else if (size < 8) { size = size * 2; } else { size = 0; } demoTable.setBorderSize(size); demoTable2.setBorderSize(size); demoTable3.setBorderSize(size); } // Changes the grid cell margin of the demoTable between // 0, 2, 6, 14, and 30 pixels. public void changeCellMargin() { int margin = demoTable.getCellMargin(); if (margin < 15) { margin = margin * 2 + 2; } else { margin = 0; } demoTable.setCellMargin(margin); demoTable2.setCellMargin(margin); demoTable3.setCellMargin(margin); } /** * Create a column model to style the various columns. */ private TableColumnModel createColumnModel() { TableColumnModel model = new DefaultTableColumnModel(); TableColumn product = new TableColumn(); product.setHeaderValue("Product"); product.setCellRenderer(BASIC_RENDERER); product.setHeaderRenderer(HEADER_RENDERER); model.addColumn(product); TableColumn price = new TableColumn(); price.setHeaderValue("Price"); price.setCellRenderer(COLORED_RENDERER); price.setHeaderRenderer(HEADER_RENDERER); price.setWidth(300); model.addColumn(price); return model; } /** * Create a TableModel with some test data. */ public void createModel() { Object[][] data = { { "Apples", new Double(Math.random() * 100) }, { "Pears", null }, { "Oranges", new Long(Math.round(Math.random() * 100)) }, { "Nashi Pears", new Float(Math.random() * 100) } }; Object[] columns = { "Product", "Price" }; demoModel = new DefaultTableModel(data, columns); } /** * Replace the model in the table with a new model. */ public void replaceModel() { createModel(); demoTable.setModel(demoModel); demoTable2.setModel(demoModel); demoTable3.setModel(demoModel); } /** * Change the numbers in the model used by the tables. */ public void updateModel() { if (demoModel == null) { replaceModel(); return; } demoModel.setValueAt(new Double(Math.random() * 100), 1, 0); demoModel.setValueAt(new Long(Math.round(Math.random() * 100)), 1, 2); demoModel.setValueAt(new Float(Math.random() * 100), 1, 3); } // This method is used to create the demonstration tables that are // found in the main pane. public Table createDemoTable() { Table table = new Table(demoModel); // The cell margin is the whitespace that will // surround the contents of every cell. table.setCellMargin(4); // The table's border color is set to a random value. table.setBorderColor(randomColor(false)); table.setBorderSize(1); return table; } // The EchoInstance's initialization method. // This method will setup the contents of the window. public Window init() { // Create a panel that will house the title text. Panel titlePanel = new Panel(); // Set the alignment of the title panel to center // the title. titlePanel.setHorizontalAlignment( EchoConstants.CENTER); // Add the title text to the title pane. titlePanel.add(new Label("Table Demonstration")); // Create the title title pane. ContentPane titlePane = new ContentPane(); // The titlePane will be 40 pixels high. titlePane.setHeight(40); // The setScrollBarPolicy() method allows you to // control whether the pane will have scroll bars. // In this case, we do not ever want the title // pane to have scroll bars, even if they would be // required to see all its content. titlePane.setScrollBarPolicy( ContentPane.SCROLL_BAR_NEVER); titlePane.setForeground(Color.WHITE); titlePane.setBackground(TITLE_PANE_GRAY); titlePane.setFont(TITLE_FONT); // Add the title panel to the title pane. titlePane.add(titlePanel); // Create a new, empty grid to hold the buttons. Grid buttonGrid = new Grid(); // Set the cell margin to 5. buttonGrid.setCellMargin(5); Button button; button = new Button("Replace current TableModel"); button.setActionCommand(ACTION_REPLACE_MODEL); button.addActionListener(this); buttonGrid.add(0, 0, button); button = new Button("Update cells in current TableModel"); button.setActionCommand(ACTION_UPDATE_MODEL); button.addActionListener(this); buttonGrid.add(0, 1, button); button = new Button("Change Border Size"); button.setActionCommand(ACTION_CHANGE_BORDER_SIZE); button.addActionListener(this); buttonGrid.add(0, 2, button); button = new Button("Change Border Color"); button.setActionCommand(ACTION_CHANGE_BORDER_COLOR); button.addActionListener(this); buttonGrid.add(0, 3, button); button = new Button("Change Cell Margin"); button.setActionCommand(ACTION_CHANGE_CELL_MARGIN); button.addActionListener(this); buttonGrid.add(0, 4, button); ContentPane buttonPane = new ContentPane(); // The buttonPane will be exactly 100 pixels wide. buttonPane.setWidth(100); // The buttonPane will have no scroll bars. buttonPane.setScrollBarPolicy( ContentPane.SCROLL_BAR_NEVER); // Set the insets (margins) for the button pane. // In this case we want all margins except the top // margin to be zero. The top margin will be set // to thirty pixels in height. buttonPane.setInsets(new Insets(0, 30, 0, 0)); buttonPane.setForeground(Color.WHITE); buttonPane.setBackground(BUTTON_PANE_BLUE); buttonPane.add(buttonGrid); // Create the main pane. ContentPane mainPane = new ContentPane(); // Create a "demo table" and add it to the // main pane. createModel(); demoTable = createDemoTable(); mainPane.add(demoTable); mainPane.add(Filler.createVerticalStrut(10)); mainPane.add(new Label("Table A: A Table populated from a TableModel using the default renderer.")); mainPane.add(Filler.createVerticalStrut(30)); // Create a second table that has default header and column renders assigned. demoTable2 = createDemoTable(); demoTable2.setDefaultRenderer(Object.class, BASIC_RENDERER); demoTable2.getTableHeader().setDefaultRenderer(HEADER_RENDERER); mainPane.add(demoTable2); mainPane.add(Filler.createVerticalStrut(10)); mainPane.add(new Label("Table B: A Table where the DefaultHeaderRenderer " + " and DefaultCellRenderer for the table have been assigned.")); mainPane.add(Filler.createVerticalStrut(30)); demoTable3 = createDemoTable(); demoTable3.setColumnModel(createColumnModel()); mainPane.add(demoTable3); mainPane.add(Filler.createVerticalStrut(10)); mainPane.add(new Label("Table C: A Table using a TableColumn model to style the table. " + "The render for the Price column is changing the cell color based on the " + "numeric value in the cell.")); mainPane.add(Filler.createVerticalStrut(30)); ContainerPane horizontalContainer = new ContainerPane(); horizontalContainer.setOrientation(ContainerPane.ORIENTATION_HORIZONTAL); // Add the button and main panes to the // horizontal container pane. horizontalContainer.add(buttonPane); horizontalContainer.add(mainPane); // Create the outside container pane. This // containerPane will have the title pane on the // top and another pane below it. ContainerPane verticalContainer = new ContainerPane(); // Set this container pane to lay out its child // panes vertically. verticalContainer.setOrientation( ContainerPane.ORIENTATION_VERTICAL); // Add the title pane and then the horizontal // container pane to the vertical container // pane. verticalContainer.add(titlePane); verticalContainer.add(horizontalContainer); // Create the window Window window = new Window("Table Demonstration"); // Set the content of the window to be the vertical // (outer) container pane. window.setContent(verticalContainer); return window; } // The randomColor method returns a random Color object. // The boolean parameter "light" is used to // determine whether the color will be very light // or very dark. (In the example, dark borders // and light cell backgrounds are used. public Color randomColor(boolean light) { int red = ((int) Math.floor(Math.random() * 128)) + (light ? 128 : 0); int green = ((int) Math.floor(Math.random() * 128)) + (light ? 128 : 0); int blue = ((int) Math.floor(Math.random() * 128)) + (light ? 128 : 0); return new Color(red, green, blue); } }