Package net.xoetrope.deprecated.swing

Source Code of net.xoetrope.deprecated.swing.XTable

package net.xoetrope.deprecated.swing;

import java.awt.Dimension;
import java.awt.ItemSelectable;
import java.awt.event.ItemListener;
import net.xoetrope.swing.*;

import net.xoetrope.xui.XAttributedComponent;
import net.xoetrope.xui.XModelHolder;
import net.xoetrope.xui.data.XModel;
import net.xoetrope.xui.style.XStyleComponent;
import java.awt.Container;
import java.awt.event.MouseListener;
import net.xoetrope.xui.XHashCode;
import net.xoetrope.xui.data.XRowSelector;

/**
* <p>Provides a simple read-only tables/grid component.</p>
* <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
* License:      see license.txt
* $Revision: 2.6 $
*/
public class XTable extends XScrollPane  implements XAttributedComponent, XModelHolder, ItemSelectable, XStyleComponent, XHashCode, XRowSelector
{
  private XTableRenderer content;
  private XModel model;

  /**
   * Create a new table
   */
  public XTable()
  {
    super();
    content = new XTableRenderer( this );
    setViewportView( content );
  }

  /**
   * implemented from XModelHolder interface
   */
  public void update()
  {
  }

  /**
   * Set the XModel which we will be generating the table from
   * @param xmodel The XModel of data
   */
  public void setModel( XModel xmodel )
  {
    model = xmodel;
    if ( model != null ) {
      model.get();
      content.setContent( xmodel );
    }

    checkSize();
  }

  /**
   * Get the underlying model.
   * @return the model
   */
  public XModel getModel()
  {
    return model;
  }

  /**
   * Recalculate and check the size of the content
   */
  public void checkSize()
  {
    Dimension d = content.calcSize();
    content.setPreferredSize( d );
    content.revalidate();

    repaint();
  }

  /**
   * Get the container that holds the components
   * @return the viewport pane
   */
  public Container getComponentPanel()
  {
    return getViewport();
  }

  /**
   * Set the type of component for a column
   * @param col the field index
   * @param className the class name
   */
  public void setComponentAt( int col, String className )
  {
    content.setComponentAt( col, className );
  }

  /**
   * Set the model's name
   * @return the name
   */
  public String getContent()
  {
    return model.getId();
  }

  /**
   * Creates the scroll pane's peer and sizes the child element.
   */
  public void addNotify()
  {
    super.addNotify();
    if ( content != null ) {
      Dimension d = content.calcSize();
      content.setBounds( 0, 0, Math.max( ( this.getSize().width - 20 ), d.width ), d.height );
      doLayout();
    }
  }

  /**
   * Set the general style of the XTable
   * @param style XStyle
   */
  public void setStyle( String style )
  {
    content.setStyle( style );
  }

  /**
   * Set the style of the header data
   * @param style XStyle
   */
  public void setHeaderStyle( String style )
  {
    content.setHeaderStyle( style );
  }

  /**
   * Set the style of the selected row
   * @param style XStyle
   */
  public void setSelectedStyle( String style )
  {
    content.setSelectedStyle( style );
  }

  /**
   * Set the style of the border
   * @param style XStyle
   */
  public void setBorderStyle( String style )
  {
    content.setBorderStyle( style );
  }

  /**
   * Check the if the table is interactive
   * @return true if the table supports user interaction
   */
  public boolean isInteractiveTable()
  {
    return content.isInteractiveTable();
  }

  /**
   * Set the user interaction state
   * @param state true for an user interactive table.
   */
  public void setInteractiveTable( boolean state )
  {
    content.setInteractiveTable( state );
  }

  /**
   * Sets the indexof the selected row
   * @param idx the new selected row
   */
  public void setSelectedRow( int idx )
  {
    content.setSelectedRow( idx );
  }

  /**
   * Get the index of the selected row
   * @return the index of the selected row
   */
  public int getSelectedRow()
  {
    return content.getSelectedRow();
  }

  /**
   * Move to the first row
   */
  public void first()
  {
    content.setSelectedRow( 0 );
  }

  /**
   * Move to the previous row
   */
  public void prev()
  {
    content.setSelectedRow( content.getSelectedRow()-1 );
  }

  /**
   * Move to the next row
   */
  public void next()
  {
    content.setSelectedRow( content.getSelectedRow()+1 );
  }

  /**
   * Move to the last row
   */
  public void last()
  {
    content.setSelectedRow( model.getNumChildren()-1 );
  }

  /**
   * Tie the model selection to this table's selection
   * @param doUpdate true to tie the selections together, false to ignore
   */
  public void setUpdateModelSelection( boolean doUpdate )
  {
    content.setUpdateModelSelection( doUpdate );
  }

  /**
   * Sets the bounds for this component
   * @param x x/left
   * @param y y/top
   * @param w width
   * @param h height
   */
  public void setBounds( int x, int y, int w, int h )
  {
    Dimension d = content.calcSize();
    content.setPreferredSize( d );
    content.revalidate();
    super.setBounds( x, y, w, h );
    validate();
  }

  /**
   * Set the table column width;
   * @param fieldIdx the field index
   * @param w the new column width
   */
  public void setColWidth( int fieldIdx, int w )
  {
    content.setColWidth( fieldIdx, w );
  }

  /**
   * Set one or more attributes of the component. Currently this handles the
   * attributes
   * <OL>
   * <LI>headingStyle, value=the table header style</LI>
   * <LI>selectionStyle, value=the selected row style</LI>
   * <LI>interactive, value=true|false</LI>
   * <LI>updateModel, value=true|false update the underlying model selection (the selected row)</LI>
   * </OL>
   * @param attribName the attribute name
   * @param attribValue the attribute value
   * @return 0 for success, non zero for failure or to require some further action
   */
  public int setAttribute( String attribName, Object attribValue )
  {
    String attribNameLwr = attribName.toLowerCase();
    String attribValueStr = (String)attribValue;
    String attribValueLwr = null;
    if ( attribValue != null )
      attribValueLwr = attribValueStr.toLowerCase();
    if ( attribNameLwr.equals( "headingstyle" ))
      setHeaderStyle( attribValueStr );
    else if ( attribNameLwr.equals( "selectionstyle" ))
      setSelectedStyle( attribValueStr );
    else if ( attribNameLwr.equals( "borderStyle" ))
      setBorderStyle( attribValueStr );
    else if ( attribNameLwr.equals( "interactive" ))
      setInteractiveTable( attribValue.equals( "true" ));
    else if ( attribNameLwr.equals( "updatemodel" ))
      setUpdateModelSelection( attribValueLwr.equals( "true" ));
    else return super.setAttribute( attribName, attribValue );   
   
    return 0;
  }

  /**
   * Gets a field value object from the currently selected row
   * @param fieldIdx the field offset
   * @return the value
   */
  public Object getValue( int fieldIdx )
  {
    int row = content.getCurrentRow();
    return getValue( row, fieldIdx );
  }

  /**
   * Gets a field value object from the specified row
   * @param rowIdx the row offset
   * @param fieldIdx the field offset
   * @return the value
   */
  public Object getValue( int rowIdx, int fieldIdx )
  {
    XModel rowModel = model.get( rowIdx + content.getFirstRow());
    Object fieldModel = rowModel.get( fieldIdx );
    if ( fieldModel instanceof XModel )
      return ( ( XModel )fieldModel ).get();
    return fieldModel;
  }

  /**
   * Gets a field value as a string from the currently selected row
   * @param fieldIdx the field offset
   * @return the value
   */
  public String getFieldValue( int fieldIdx )
  {
    return ( String )getValue( fieldIdx );

  }
  /**
   * Gets a field value as a string from the specified row
   * @param rowIdx the row offset
   * @param fieldIdx the field offset
   * @return the value
   */
  public String getFieldValue( int rowIdx, int fieldIdx )
  {
    return ( String )getValue( rowIdx, fieldIdx );
  }

  /**
   * Adds the specified item listener to receive item events from
   * this list.  Item events are sent in response to user input, but not
   * in response to calls to <code>select</code> or <code>deselect</code>.
   * If listener <code>l</code> is <code>null</code>,
   * no exception is thrown and no action is performed.
   *
   * @param         l the item listener
   * @see           java.awt.event.ItemEvent
   * @see           java.awt.event.ItemListener
   * @since         JDK1.1
   */
  public synchronized void addItemListener( ItemListener l )
  {
    content.addItemListener( l );
  }

  /**
   * Removes the specified item listener so that it no longer
   * receives item events from this list.
   * If listener <code>l</code> is <code>null</code>,
   * no exception is thrown and no action is performed.
   *
   * @param           l the item listener
   * @see             java.awt.event.ItemEvent
   * @see             java.awt.event.ItemListener
   * @since           JDK1.1
   */
  public synchronized void removeItemListener( ItemListener l )
  {
    content.removeItemListener( l );
  }

  /**
   * Returns the selected items on the list in an array of objects.
   * @see ItemSelectable
   * @return the selected item/row
   */
  public Object[] getSelectedObjects()
  {
    return content.getSelectedObjects();
  }

  /**
   * Add a mouse listener
   * @param l the new mouse listener
   */
  public void addMouseListener( MouseListener l )
  {
    content.addMouseListener( l );
  }

  /**
   * Get the hash code to be used by the event handler for this component
   * @return the hash code
   */
  public long getComponentHashCode()
  {
    return content.hashCode();
  }
}
TOP

Related Classes of net.xoetrope.deprecated.swing.XTable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.