Package DisplayProject.table

Source Code of DisplayProject.table.TextFieldCellRenderer

/*
Copyright (c) 2003-2008 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.Component;
import java.awt.SystemColor;
import java.awt.image.BufferedImage;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableCellRenderer;

import DisplayProject.UIutils;
import DisplayProject.controls.MultiLineTextField;
import DisplayProject.factory.TextEditFactory;
import Framework.EventManager;


/**
* Cell renderer for the {@link DisplayProject.controls.MultiLineTextField}.  We create a new JScrollPane and {@link DisplayProject.controls.MultiLineTextField}
* so we don't interfere with the TextFieldCellEditor.
*
* @author Craig Mitchell
* @since 17/09/2007
*/
@SuppressWarnings("serial")
public class TextFieldCellRenderer extends JTextArea implements TableCellRenderer, ArrayFieldEmptyCellRenderer {
    private JScrollPane sp;
    private JScrollPane spPainter;
    private boolean isPrinting;
    private boolean printingRemoveScrollBars;

    public TextFieldCellRenderer(JScrollPane pScrollPane) {
      // CraigM:25/07/2008 - Force any pending actions on the widget through (like colour changes), so when we clone the widget, we get the latest info
      UIutils.processGUIActions(pScrollPane);
        MultiLineTextField tf = (MultiLineTextField)pScrollPane.getViewport().getComponent(0);

        this.sp = new JScrollPane(tf.cloneComponent());
        ArrayFieldCellHelper.setUpCellRenderer(pScrollPane, this.sp); // CraigM: 28/03/2008
        this.sp.setPreferredSize(pScrollPane.getPreferredSize());
        this.sp.setMinimumSize(pScrollPane.getMinimumSize());
        this.sp.setSize(pScrollPane.getSize());
        this.sp.setHorizontalScrollBarPolicy(pScrollPane.getHorizontalScrollBarPolicy());
        this.sp.setVerticalScrollBarPolicy(pScrollPane.getVerticalScrollBarPolicy());
        this.isPrinting = false;
    }

    private MultiLineTextField getMultiLineTextField() {
        return (MultiLineTextField)this.sp.getViewport().getComponent(0);
    }

    public Component getTableCellRendererComponent(JTable table, Object obj,
            boolean isSelected, boolean hasFocus, int row, int column) {
        // TF:10/10/07:We don't want this code to fire events, which it will do by calling removeUpdate on the document
        boolean oldValue = EventManager.isPostingEnabled();
        EventManager.disableEventPosting();
        // TF:23/9/07:Made this code null-aware, especially as this can be called when trying to set the component renderer
        this.getMultiLineTextField().setText(obj == null ? "" : obj.toString());
        if (oldValue) {
            EventManager.enableEventPosting();
        }

        if (isSelected) {
            this.getMultiLineTextField().setBackgroundTemporarily(table.getSelectionBackground());
        }
        else {
            if (this.getMultiLineTextField().getOverriddenBackgroundColor() != null) {
                this.getMultiLineTextField().setBackgroundTemporarily(this.getMultiLineTextField().getOverriddenBackgroundColor());
            }
            else {
                this.getMultiLineTextField().setBackgroundTemporarily(SystemColor.window);
            }
        }
       
        if (this.isPrinting && this.sp.getViewport().getComponentCount() > 0) {
          MultiLineTextField tf = this.getMultiLineTextField();
        sp.setSize(tf.getPreferredSize());
        sp.setMinimumSize(tf.getPreferredSize());
        sp.setPreferredSize(tf.getPreferredSize());
       
        if (printingRemoveScrollBars) {
          sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
          sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        }
            sp.setBorder(null);
        }
        return this.sp;
    }
   
    /**
     * This will be called to indicate that we will be printing the arrayfield that this renderer is currently in
     */
    public void setPrinting(boolean removeScrollBars) {
      this.isPrinting = true;
      this.printingRemoveScrollBars = removeScrollBars;
    }

    /**
     * CraigM:22/01/2009 - Create a buffered image of the MultiLineTextField.
     */
    public BufferedImage getImage(int width) {
    int height = this.sp.getMinimumSize().height;
    // TF:27/11/2009:Changed this to use the passed width
//    int width = this.sp.getMinimumSize().width;
        MultiLineTextField tf = (MultiLineTextField)this.sp.getViewport().getComponent(0);

    if (this.spPainter == null) {
      MultiLineTextField newTF = TextEditFactory.newTextField("painter", tf.getRows(), tf.getColumns());
        this.spPainter = new JScrollPane(newTF);
        this.spPainter.setHorizontalScrollBarPolicy(this.sp.getHorizontalScrollBarPolicy());
        this.spPainter.setVerticalScrollBarPolicy(this.sp.getVerticalScrollBarPolicy());
        newTF.setForeground(tf.getForeground());
        newTF.setBackground(tf.getBackground());
      }
   
    return ArrayFieldCellHelper.createBufferedImage(this.spPainter, width, height);
    }
}
TOP

Related Classes of DisplayProject.table.TextFieldCellRenderer

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.