Package DisplayProject.table

Source Code of DisplayProject.table.FormattedCellRenderer

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.table;

import java.awt.Color;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.text.Format;
import java.text.ParseException;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.PlainDocument;

import DisplayProject.DataField;
import DisplayProject.DataFieldFormatterFactory;
import DisplayProject.FixedLengthDocument;
import DisplayProject.controls.ArrayField;

/**
* FormattedCellRender renders the table cell with a clone of the passed in DataField.
*
* @author Craig Mitchell
* @since 22/03/2008
*/
public class FormattedCellRenderer implements TableCellRenderer, ArrayFieldEmptyCellRenderer, StateColourForRowDisplayer {
    protected DataField dataFieldOriginal;  // Stored to do the formatting of the text
    protected DataField dataFieldCloned; // Stored to do the drawing of the component
    protected DataField dataFieldPainter; // Stored to do painting of empty rows
    // PM:30 Oct 2008:added capability to allow specific row dependent colours and states
    protected StateColourForRow stateColour;

    /**
     * Main constructor.  Creates a renderer based on pDataField.
     * @param pDataField
     */
    public FormattedCellRenderer(DataField pDataField) {
      this();
      this.dataFieldOriginal = pDataField;
      this.dataFieldCloned = pDataField.cloneComponentForRenderer();
      ArrayFieldCellHelper.setUpCellRenderer(this.dataFieldOriginal, this.dataFieldCloned); // CraigM: 28/03/2008
      // TF:29 Oct 2008:Set this up to handle fixed length documents
      if (this.dataFieldOriginal.getDocument() instanceof FixedLengthDocument) {
        FixedLengthDocument doc = (FixedLengthDocument)this.dataFieldOriginal.getDocument();
        this.dataFieldCloned.setDocument(new FixedLengthDocument(doc.getMaxLength()));
      }
      else {
        this.dataFieldCloned.setDocument(new PlainDocument()); // we can't reuse the document
      }
      this.dataFieldCloned.setFormatterFactory(new DataFieldFormatterFactory()); // we can't reuse the formatter
      // TF:29 Oct 2008:removed the caret
    this.dataFieldCloned.getCaret().setVisible(false);
   
//TF:DEBUG      System.out.println("Renderer: orig  " + this.dataFieldOriginal .getName()+" [" + this.dataFieldOriginal.hashCode() + "]");
//TF:DEBUG      System.out.println("Renderer: clone " + this.dataFieldCloned.getName()+" [" + this.dataFieldCloned.hashCode() + "]");

    }

    /**
     * Constructor provided for visual editors
     */
    public FormattedCellRenderer() {
      super();
    }

    /**
     * Provided for backwards compatibility.
     * @param pFormat
     * @param pAlignment
     */
    public FormattedCellRenderer(Format pFormat, int pAlignment) {
      this(new DataField(pFormat));
      this.dataFieldCloned.setHorizontalAlignment(pAlignment);
    this.dataFieldCloned.getCaret().setVisible(false);
    }
   
    /**
     * Provided for backwards compatibility.
     * @param pFormat
     * @param pAlignment
     */
    public FormattedCellRenderer(DefaultFormatter pFormat, int pAlignment) {
      this(new DataField(pFormat));
      this.dataFieldCloned.setHorizontalAlignment(pAlignment);
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
     
      // This null check is for the visual editors
      if (this.dataFieldCloned == null) {
        this.dataFieldCloned = new DataField();
        this.dataFieldOriginal = new DataField();
      }
     
      //PM:29/4/08 Set correct border
      this.dataFieldCloned.setBorder(this.dataFieldOriginal.getBorder());

      try {
         this.dataFieldCloned.setValue(this.dataFieldOriginal.getFormatter().valueToString(value));
    } catch (ParseException e) {
      // shouldn't ever get here
      this.dataFieldCloned.setValue(value);
    }

    // Draw the row highlight
    // TF:04/03/2010: only change the back/foreground if highlight the selected row or not
      if (isSelected && ((ArrayField) table).isHighlightSelectedRow()) {
        this.dataFieldCloned.setForeground(table.getSelectionForeground());
           this.dataFieldCloned.setBackground(table.getSelectionBackground());
      }
      // Reset the colours
      else {
        this.dataFieldCloned.setForeground(this.getForegroundForRow(row));
        this.dataFieldCloned.setBackground(this.getBackgroundForRow(row));
      }
     
        // TF:14/12/2009:DET-143:Set the tool tip information as well
        this.dataFieldCloned.setToolTipText(this.dataFieldOriginal.getToolTipText());
       
      return this.dataFieldCloned;
    }
    /**
     * Get the component used for rendering
     * @return
     */
    //PM:30/4/08 added access to the component that is used for rendering
  public DataField getDataFieldOriginalComponent() {
    return dataFieldOriginal;
  }

  public StateColourForRow getStateColour() {
    return stateColour;
  }

  public void setStateColour(StateColourForRow stateColour) {
    this.stateColour = stateColour;
  }
  // PM:31 Oct 2008:added customisation point for cell background colour

    private Color getBackgroundForRow(int row) {
      Color colour = null;
    if (this.stateColour != null){
      colour = this.stateColour.getBackgroundForRow(row);
    }
    if (colour == null){
      colour = this.dataFieldOriginal.getBackground();
    }
    return colour;
  }
  // PM:31 Oct 2008:added customisation point for cell foreground colour

  private Color getForegroundForRow(int row) {
    Color colour = null;
    if (this.stateColour != null){
      colour = this.stateColour.getForegroundForRow(row);
    }
    if (colour == null){
      colour = this.dataFieldOriginal.getForeground();
    }
    return colour;
  }
 
    /**
     * CraigM:19/01/2009 - Create a buffered image of the DataField.
     */
    public BufferedImage getImage(int width) {
    int height = this.dataFieldOriginal.getMinimumSize().height;
    // TF:27/11/2009:Changed this to use the passed width
//    int width = this.dataFieldOriginal.getMinimumSize().width;

    if (this.dataFieldPainter == null) {
        this.dataFieldPainter = new DataField();
      }
    this.dataFieldPainter.setForeground(this.dataFieldOriginal.getForeground());
    this.dataFieldPainter.setBackground(this.dataFieldOriginal.getBackground());
    this.dataFieldPainter.setBorder(this.dataFieldOriginal.getBorder());
   
    return ArrayFieldCellHelper.createBufferedImage(this.dataFieldPainter, width, height);
    }
}
TOP

Related Classes of DisplayProject.table.FormattedCellRenderer

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.