Package DisplayProject.table

Source Code of DisplayProject.table.PasswordCellRenderer

/*
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 javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.PlainDocument;

import DisplayProject.FixedLengthDocument;
import DisplayProject.PasswordDataField;

/**
* PasswordCellRenderer renders the table cell with a clone of the passed in PasswordDataField.
*  - Cloned/Based from FormattedCellRenderer
* @author C.Yeh
* @since 09/09/09
*/
public class PasswordCellRenderer implements TableCellRenderer, ArrayFieldEmptyCellRenderer, StateColourForRowDisplayer {
    protected PasswordDataField passwordDataFieldOriginal;  // Stored to do the formatting of the text
    protected PasswordDataField passwordDataFieldCloned; // Stored to do the drawing of the component
    protected PasswordDataField passwordDataFieldPainter; // 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 PasswordCellRenderer(PasswordDataField pDataField) {
      this();
      this.passwordDataFieldOriginal = pDataField;
      this.passwordDataFieldCloned = pDataField.cloneComponentForRenderer();
      ArrayFieldCellHelper.setUpCellRenderer(this.passwordDataFieldOriginal, this.passwordDataFieldCloned); // CraigM: 28/03/2008
      // TF:29 Oct 2008:Set this up to handle fixed length documents
     
      if (this.passwordDataFieldOriginal.getDocument() instanceof FixedLengthDocument) {
        FixedLengthDocument doc = (FixedLengthDocument)this.passwordDataFieldOriginal.getDocument();
        this.passwordDataFieldCloned.setDocument(new FixedLengthDocument(doc.getMaxLength()));
      }
      else {
        this.passwordDataFieldCloned.setDocument(new PlainDocument()); // we can't reuse the document
      }
    this.passwordDataFieldCloned.getCaret().setVisible(false);
    }

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


    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.passwordDataFieldCloned == null) {
        this.passwordDataFieldCloned = new PasswordDataField();
        this.passwordDataFieldOriginal = new PasswordDataField();
      }
     
      //PM:29/4/08 Set correct border
      this.passwordDataFieldCloned.setBorder(this.passwordDataFieldOriginal.getBorder());

    this.passwordDataFieldCloned.setValue(value);

    // Draw the row highlight
      if (isSelected) {
        this.passwordDataFieldCloned.setForeground(table.getSelectionForeground());
           this.passwordDataFieldCloned.setBackground(table.getSelectionBackground());
      }
      // Reset the colours
      else {
        this.passwordDataFieldCloned.setForeground(this.getForegroundForRow(row));
        this.passwordDataFieldCloned.setBackground(this.getBackgroundForRow(row));
      }
     
        // TF:14/12/2009:DET-143:Allowed the tool tip information to show
      this.passwordDataFieldCloned.setToolTipText(this.passwordDataFieldOriginal.getToolTipText());

      return this.passwordDataFieldCloned;
    }
    /**
     * Get the component used for rendering
     * @return
     */
    //PM:30/4/08 added access to the component that is used for rendering
  public PasswordDataField getDataFieldOriginalComponent() {
    return passwordDataFieldOriginal;
  }

  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.passwordDataFieldOriginal.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.passwordDataFieldOriginal.getForeground();
    }
    return colour;
  }
 
    /**
     * CraigM:19/01/2009 - Create a buffered image of the DataField.
     */
    public BufferedImage getImage(int width) {
    int height = this.passwordDataFieldOriginal.getMinimumSize().height;
    // TF:27/11/2009:Changed this to use the passed width
//    int width = this.passwordDataFieldOriginal.getMinimumSize().width;

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

Related Classes of DisplayProject.table.PasswordCellRenderer

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.