home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 22.0 KB | 772 lines |
- /*
- * @(#Grid.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
-
- package symantec.itools.db.awt;
-
- import java.awt.*;
- import java.util.*;
-
- /**
- * A component for displaying multiple data records.
- * Provides an array, or spreadsheet-like display, of specified data.
- * <p>
- * You can also specify predefined buttons that will appear at the bottom of the grid.
- * You can adjust column widths at runtime.
- * You can customize column heading text in code.
- */
- public class Grid extends TableView {
-
- // mediator stuff
-
- /**
- * The mediator used to bind this component with the data source.
- */
- public symantec.itools.db.beans.binding.Mediator m_Mediator;
- private String[] m_GetMethods={"getjdbcDataSource()"};
- private String[] m_SetMethods={"setjdbcDataSource(Value)"};
-
- Object m_DataSource = null;
- symantec.itools.db.beans.binding.QueryNavigator jdbcDataSource = null;
-
- //BS: added rvmd variable to store metadata when table driven off relationView
- symantec.itools.db.pro.RelationViewMetaData rvmd;
- symantec.itools.db.pro.RelationView relation,
- master;
-
-
- /**
- * Constructs a default Grid.
- */
- public Grid(){
- super();
- }
-
- /**
- * Constructs a Grid with the specified number of columns.
- * @param cols the requested number of columns
- */
- public Grid(int cols){
- super(cols);
- setColumnsNumber(cols);
- }
-
- /**
- * Constructs a Grid with the preferred height based on
- * specified number of rows.
- * @param rows the number of rows to show
- */
- public Grid(long rows){
- super(rows);
- setRowsNumber((int)rows);
- }
-
- /**
- * Constructs a Grid with the specified number of columns and
- * showing the specified number of rows.
- * @param cols the requested number of columns
- * @param rows the number of rows to show
- */
- public Grid(long rows, int cols){
- super(rows,cols);
- setColumnsNumber(cols);
- setRowsNumber((int)rows);
- }
-
- /**
- * Constructs a Grid with the specified number of columns and background color.
- * @param cols the requested number of columns
- * @param bg the requested background color
- */
- public Grid(int cols, Color bg) {
- super(cols,bg);
- setColumnsNumber(cols);
- }
-
- //BS: added constructor for grid driven through dbAware source
- // (DbaDataStore)
-
- /**
- * Constructs a new Grid using the specified RelationView.
- * The Grid will automatically setup the columns using meta
- * data from the RelationView.
- * @param relView the RelationView on which to drive the Grid
- * @exception java.sql.SQLException when the Grid cannot
- * be successfully setup due to a database problem
- */
- public Grid(symantec.itools.db.pro.RelationView relView)
- throws java.sql.SQLException
- {
- this(relView, null,0);
- }
-
- /**
- * Constructs a new Grid using the specified RelationView.
- * The Grid will automatically setup the columns using meta
- * data from the RelationView.
- * The Grid will have a preferred width wide enough to show
- * the requested number of columns.
- * @param relView the RelationView on which to drive the Grid
- * @param coltoshow the number of columns to show in the grid
- * @exception java.sql.SQLException when the Grid cannot
- * be successfully setup due to a database problem
- */
- public Grid(symantec.itools.db.pro.RelationView relView,int coltoshow)
- throws java.sql.SQLException
- {
-
- this(relView, null, coltoshow);
-
-
- }
-
-
- /**
- * Only here to support old projects.
- * The Grid will have a preferred width wide enough to show
- * the requested number of columns.
- * @param relView the detail view to attach to the Grid
- * @param masterView the master view
- * @param coltoshow the number of columns to show in the grid
- * @exception java.sql.SQLException when the Grid cannot
- * be successfully setup due to a database problem
- */
- public Grid(symantec.itools.db.pro.RelationView relView,
- symantec.itools.db.pro.RelationView masterView,
- int coltoshow)
- throws java.sql.SQLException
- {
-
- super();
-
- StringBuffer buffer = new StringBuffer(relView.getName());
-
- buffer.append("@");
-
- if (coltoshow == 0) {
- buffer.append("All");
- }
- else {
- buffer.append("1");
- for (int index = 1; index < coltoshow; index++) {
- buffer.append(",");
- buffer.append(String.valueOf(index+1));
- }
- }
-
- setDataBinding(buffer.toString());
-
- setColumnsNumber(coltoshow);
- }
-
- /**
- * Handles redrawing of this component on the screen.
- * <p>
- * This is a standard Java AWT method which gets called by the Java AWT
- * (repaint()) to handle repainting this component on the screen.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its [0,0] coordinate is this component's top-left corner.
- * <p>
- * Typically this method paints the background color to clear the component's
- * drawing space, sets graphics context to be the foreground color, and
- * then calls paint() to draw the component.
- * <p>
- * It is overridden here to prevent the flicker associated with the standard
- * update() method's repainting of the background before painting the
- * component itself.
- */
- public void update(Graphics g) {
- paint(g);
- }
-
-
- /**
- * Paints this component using the given graphics context.
- * <p>
- * This is a standard Java AWT method which typically gets called by the
- * AWT to handle painting this component. It paints this component using
- * the given graphics context.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its [0,0] coordinate is this component's
- * top-left corner.
- */
- public void paint(Graphics g) {
-
- if(java.beans.Beans.isDesignTime())
- {
- Dimension d = size();
-
- for (int colStart = 0, colNum = 1; colStart < d.width; colStart += 30, colNum++)
- {
- Color oldColor = g.getColor();
- g.setColor(Color.lightGray);
- g.fillRect(colStart, 0, 30, 10);
- g.setColor(Color.white);
- g.drawLine(colStart + 1, 2, colStart + 29, 2);
- g.drawLine(colStart + 2, 1, colStart + 2, 9);
- g.setColor(oldColor);
-
- g.drawLine(colStart, 0, colStart, d.height);
- }
- for (int rowStart = 0, rowNum = 1; rowStart < d.height; rowStart += 10, rowNum++)
- {
- Color oldColor = g.getColor();
- g.setColor(Color.lightGray);
- g.fillRect(0, rowStart, 30, 10);
- g.setColor(Color.white);
- g.drawLine(1, rowStart + 2, 29, rowStart + 2);
- g.drawLine(2, rowStart+1, 2, rowStart + 9);
- g.setColor(oldColor);
- g.drawLine( 0, rowStart, d.width, rowStart);
- }
-
- g.drawRect(0, 0,d.width - 1, d.height - 1);
- invalidate();
- //showScrollbars();
- }else{
- super.paint(g);
- }
- }
-
- //BS: added for backward compatibility
- /**
- * Returns the current number of rows.
- *
- * @see #getNumberOfCols
- */
- public int getNumberOfRows() {
- /* if (java.beans.Beans.isDesignTime()) {
- return m_NumberOfRows;
- }*/
- return rows();
- }
-
- /**
- * Returns the current number of columns.
- *
- * @see #getNumberOfRows
- */
- public int getNumberOfCols() {
- /* if (java.beans.Beans.isDesignTime()) {
- return m_NumberOfColumns;
- }*/
- return cols();
- }
-
- //BS: added for backward compatibility
- /**
- * Sets the width of a column.
- * @param col the one-relative column index
- * @param rightSide the new width of the column, in pixels
- */
- public void resizeColumn(int col, int rightSide){
- setColumnSize(col, rightSide);
- }
-
- /**
- * Sets the RelationView that this component is bound with.
- * @param relView the RelationView to bind with
- * @see #getRelationView
- * @exception java.sql.SQLException when a database error occurs
- */
- public void setRelationView(symantec.itools.db.pro.RelationView relView)
- throws java.sql.SQLException{
-
- name = relView.getName();
- relation = relView;
-
- DbaDataStore store = new DbaDataStore(relView, null);
-
- store.setColtoShow(0);
-
- DataSource ds = new DbDataSource(this, store, store, store);
-
- setDataSource(ds,1);
-
- }
-
- /**
- * Gets the RelationView that this component is bound with.
- * @return the RelationView currently bound with
- * @see #setRelationView
- */
- public symantec.itools.db.pro.RelationView getRelationView() {
- return relation;
- }
-
- /**
- * Initializes this Grid to the specified size.
- * @param rows the number of rows
- * @param cols the number of columns
- * @exception java.sql.SQLException when a database error occurs
- */
- public void setStaticSize(int rows, int cols)
- throws java.sql.SQLException{
-
-
- DataSource ds = new DefaultDataSource(this);
-
- setDataSource(ds,1);
-
- createColumns(cols);
-
- //setBackground(Color.white);
-
- try{
- for( int i=0; i < rows; i++){
- appendRow();
- }
-
- for(int i=0; i< cols;i++){
- setHeading( "Column:"+(i+1), i+1, 10);
- }
- } catch( Exception e){
- System.out.println( "EXCEPTION " + e);
- }
-
- preferredRowCount = (int)rows;
-
- }
-
- private int numOfRows = 0;
- private int numOfCols = 0;
-
- /**
- * Initializes this Grid to have the specified number of columns.
- * @param cols the number of columns
- * @see #getColumnsNumber
- */
- public void setColumnsNumber(int cols){
-
- if(java.beans.Beans.isDesignTime()){
- numOfCols = cols;
- }else{
-
- DataSource ds = new DefaultDataSource(this);
- setDataSource(ds,1);
- createColumns(cols);
-
- //were the row number set before ?
- //build them if yes
-
- if(numOfRows > 0)
- setRowsNumber(numOfRows);
-
- }
- }
-
- /**
- * Gets the number of columns this grid has.
- * @return the number of columns
- * @see #setColumnsNumber
- */
- public int getColumnsNumber(){
- if(java.beans.Beans.isDesignTime())
- return numOfCols;
- else
- return cols();
- }
-
- /**
- * Initializes this Grid to have the specified number of rows.
- * @param rows the number of rows
- * @see #getRowsNumber
- */
- public void setRowsNumber(int rows){
-
- if(java.beans.Beans.isDesignTime()){
- numOfRows = rows;
- }else{
- int cols = cols();
- if(cols > 0){
- try{
- for( int i=0; i < rows; i++){
- appendRow();
- }
-
- for(int i=0; i< cols;i++){
- setHeading( "Column:"+(i+1), i+1, 10);
- }
- } catch( Exception e){
- System.out.println( "EXCEPTION " + e);
- }
-
- preferredRowCount = (int)rows;
- }else{
- numOfRows = rows;
- }
- }
- }
-
- /**
- * Gets the number of rows this Grid has.
- * @return the number of rows
- * @see #setRowsNumber
- */
- public int getRowsNumber(){
- if(java.beans.Beans.isDesignTime())
- return numOfRows;
- else
- return rows();
- }
-
- /**
- * Sets whether the Grid has this button.
- * @param on <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #getAppendButton
- */
- public void setAppendButton(boolean on){
- if(on)
- addAppendButton();
- else
- removeAppendButton();
- }
-
- /**
- * Gets whether the Grid has this button.
- * @return <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #setAppendButton
- */
- public boolean getAppendButton(){
- return getHasAppendButton();
- }
-
- /**
- * Sets whether the Grid has this button.
- * @param on <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #getInsertButton
- */
- public void setInsertButton(boolean on){
- if(on)
- addInsertButton();
- else
- removeInsertButton();
- }
-
- /**
- * Gets whether the Grid has this button.
- * @return <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #setInsertButton
- */
- public boolean getInsertButton(){
- return getHasInsertButton();
- }
-
- /**
- * Sets whether the Grid has this button.
- * @param on <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #getGotoButton
- */
- public void setGotoButton(boolean on){
- if(on)
- addGotoButton();
- else
- removeGotoButton();
- }
-
- /**
- * Gets whether the Grid has this button.
- * @return <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #setGotoButton
- */
- public boolean getGotoButton(){
- return getHasGotoButton();
- }
-
- /**
- * Sets whether the Grid has this button.
- * @param on <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #getUndoButton
- */
- public void setUndoButton(boolean on){
- if(on)
- addUndoButton();
- else
- removeUndoButton();
- }
-
- /**
- * Gets whether the Grid has this button.
- * @return <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #setUndoButton
- */
- public boolean getUndoButton(){
- return getHasUndoButton();
- }
-
- /**
- * Sets whether the Grid has this button.
- * @param on <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #getRestartButton
- */
- public void setRestartButton(boolean on){
- if(on)
- addRestartButton();
- else
- removeRestartButton();
- }
-
- /**
- * Gets whether the Grid has this button.
- * @return <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #setRestartButton
- */
- public boolean getRestartButton(){
- return getHasRestartButton();
- }
-
- /**
- * Sets whether the Grid has this button.
- * @param on <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #getDeleteButton
- */
- public void setDeleteButton(boolean on){
- if(on)
- addDeleteButton();
- else
- removeDeleteButton();
- }
-
- /**
- * Gets whether the Grid has this button.
- * @return <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #setDeleteButton
- */
- public boolean getDeleteButton(){
- return getHasDeleteButton();
- }
-
- /**
- * Sets whether the Grid has this button.
- * @param on <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #getUndeleteButton
- */
- public void setUndeleteButton(boolean on){
- if(on)
- addUndeleteButton();
- else
- removeUndeleteButton();
- }
-
- /**
- * Gets whether the Grid has this button.
- * @return <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #setUndeleteButton
- */
- public boolean getUndeleteButton(){
- return getHasUndeleteButton();
- }
-
- /**
- * Sets whether the Grid has this button.
- * @param on <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #getSaveButton
- */
- public void setSaveButton(boolean on){
- if(on)
- addSaveButton();
- else
- removeSaveButton();
- }
-
- /**
- * Gets whether the Grid has this button.
- * @return <code>true</code> if the Grid has the button,
- * <code>false</code> otherwise
- * @see #setSaveButton
- */
- public boolean getSaveButton(){
- return getHasSaveButton();
- }
-
- /**
- * Sets the background color of this Grid's toolbar.
- * @param c the background color
- * @see #getToolbarBackground
- */
- public void setToolbarBackground(Color c){
- super.setToolbarBackground(c);
- }
-
- /**
- * Gets the background color of this Grid's toolbar.
- * @return the background color
- * @see #setToolbarBackground
- */
- public Color getToolbarBackground(){
- return super.getToolbarBackground();
- }
-
- /**
- * Sets the column which does not scroll. All columns to the left of the
- * locked column do not scroll, all of the columns to the right are
- * scrollable.
- * @param col the column to be locked, 0 if all columns should be scrollable
- * @see #getLockedColumn
- */
- public void setLockedColumn(int col) {
- super.setLockedColumn(col);
- }
-
- /**
- * Gets the column which does not scroll. All columns to the left of the
- * locked column do not scroll, all of the columns to the right are
- * scrollable.
- * @return the locked column, 0 if all columns are scrollable
- * @see #setLockedColumn
- */
- public int getLockedColumn() {
- return super.getLockedColumn();
- }
-
- /**
- * Returns the preferred size of this component.
- */
- public Dimension getPreferredSize() {
- if(java.beans.Beans.isDesignTime()){
- return new Dimension(200,200);
- }else{
- return super.getPreferredSize();
- }
- }
-
- /* Mediator used method
- */
- /**
- * Gets the QueryNavigator this component is using as a JDBC data source.
- * @return the data source
- * @see #setjdbcDataSource
- */
- public symantec.itools.db.beans.binding.QueryNavigator getjdbcDataSource(){
- //System.out.println("in getjdbcDataSource");
-
- return jdbcDataSource;
- }
-
-
- /* Mediator used method
- */
- /**
- * Sets the data source that this component should use.
- * @param Value a QueryNavigator or RelationView data source
- * @see #getjdbcDataSource
- * @exception java.sql.SQLException when a database error occurs
- */
- public void setjdbcDataSource(Object Value)
- throws java.sql.SQLException{
-
- //System.out.println("in setjdbcDataSource");
- if(m_DataSource != Value){
- m_DataSource = Value;
-
- if(m_DataSource instanceof symantec.itools.db.beans.binding.QueryNavigator){
- //if(jdbcDataSource != ((symantec.itools.db.beans.binding.QueryNavigator) Value)){
-
- jdbcDataSource = (symantec.itools.db.beans.binding.QueryNavigator) Value;
- JDBCDataSourceDataStore store = new JDBCDataSourceDataStore(jdbcDataSource);
-
- store.setColtoShow(0);
- //this is tricky: we have a DataBinding containing the columns, etc.
- //as set by the user, and a DataBinding as set by the setDataBinding method
- //this is because we drive the Grid of the datasource directly
- store.setColumnsNamesToShow(m_DataBinding);
- DataSource ds = new DbDataSource(this, store, store, store);
-
- setDataSource(ds,1);
- }else
- if(m_DataSource instanceof symantec.itools.db.pro.RelationView){
- relation = (symantec.itools.db.pro.RelationView) m_DataSource;
- name = relation.getName();
-
- DbaDataStore store = new DbaDataStore(relation, null);
-
- store.setColtoShow(0);
- store.setColumnsNamesToShow(m_DataBinding);
-
- DataSource ds = new DbDataSource(this, store, store, store);
-
- setDataSource(ds,1);
- }
- }
-
- }
-
- //String inputn = null;
-
- private void setupMediator(){
- m_Mediator = new symantec.itools.db.beans.binding.Mediator();
- m_Mediator.setOutput(this);
- m_Mediator.setSetMethods(m_SetMethods);
- m_Mediator.setGetMethods(m_GetMethods);
- }
-
-
- String m_DataBinding = "";
-
- /**
- * Sets the name of the data item to bind this component to.
- * @param name the data item name, like "MyTable@MyColumn"
- * @see #getDataBinding
- */
- public void setDataBinding(String name){
-
- if(java.beans.Beans.isDesignTime()){
- m_DataBinding = name;
- }else{
- m_DataBinding = name;
-
- setupMediator();
-
- StringTokenizer st = new StringTokenizer(name,"@");
- String n = null;
- if(st.hasMoreTokens()){
- n = st.nextToken();
- }
-
- m_Mediator.setDataBinding(n +"@CurrentDataSource");
-
- //let mediator stuff initialize...
- try{
- Thread.sleep(0);
- }catch(InterruptedException e){}
- }
-
-
- }
-
- /**
- * Gets the name of the data item this component is bound to.
- * @returns the data item name, like "MyTable@MyColumn"
- * @see #setDataBinding
- */
- public String getDataBinding()
- {
- if(java.beans.Beans.isDesignTime()){
- return m_DataBinding;
- }else{
- return m_Mediator.getDataBinding();
- }
-
- }
-
-
- }
-