Package DisplayProject

Source Code of DisplayProject.DisplayNodeRenderer

/*
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;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.beans.Expression;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
/**
*
* Custom ListCellRenderer for a JList to display DisplayNodes.
* We split the text and icons into separate JLabels so the on 
* selection, only the text has its background changed.
* @author Craig Mitchell
*
*/
@SuppressWarnings("serial")
public class DisplayNodeRenderer extends JPanel implements ListCellRenderer {
    public static final int SMALL_ICON = 1;
    public static final int LARGE_ICON = 2;
    private int imageSize = 32;
    private JLabel labelText;
    private JLabel labelIcon;
    private ArrayFieldModel model;
    private String firstColumn;

    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------
    public DisplayNodeRenderer(int size) {
        this(size, null);
    }

    public DisplayNodeRenderer(int size, ArrayFieldModel model ) {
        this.model = model;
        this.setLayout(new BorderLayout(2,2));
        this.labelText = new JLabel();
        this.labelIcon = new JLabel();
        switch (size){
          case SMALL_ICON:
              this.add(Box.createRigidArea(new Dimension(2,2)), java.awt.BorderLayout.WEST);
              this.add(Box.createRigidArea(new Dimension(2,2)), java.awt.BorderLayout.NORTH);
              this.add(this.labelIcon, java.awt.BorderLayout.CENTER);
              this.add(this.labelText, java.awt.BorderLayout.EAST);
              this.labelIcon.setHorizontalAlignment(JLabel.CENTER);
              this.labelText.setHorizontalAlignment(JLabel.LEFT);
              this.imageSize = 16;
              break;
          case LARGE_ICON:
              this.add(Box.createRigidArea(new Dimension(2,2)), java.awt.BorderLayout.WEST);
              this.add(Box.createRigidArea(new Dimension(2,2)), java.awt.BorderLayout.NORTH);
              this.add(this.labelIcon, java.awt.BorderLayout.CENTER);
              this.add(this.labelText, java.awt.BorderLayout.SOUTH);
              this.labelIcon.setHorizontalAlignment(JLabel.CENTER);
              this.labelText.setHorizontalAlignment(JLabel.CENTER);
              this.imageSize = 32;
              break;
        }
        this.firstColumn = "get" + this.model.getColumnName(0);
        this.setOpaque(false);
    }

    // ------------------------------------------------------------------------
    // Render the JPanel
    // ------------------------------------------------------------------------
    public Component getListCellRendererComponent(
                                JList list, // the list being redrawn
                                Object value, // value to display
                                int index, // cell index
                                boolean isSelected, // is the cell selected
                                boolean cellHasFocus) // the list and the cell have the focus
    {
      DisplayNode dn = (DisplayNode)value;
        if (dn.getDVNodeText()!= null) {
            this.labelText.setText(dn.getDVNodeText().toString());
        }
        else if (this.model != null){
            // this is a unique feature of Forte ListView, if the DVNodeText is null than the 1st column is used as the node text
            try {

                Expression expr = new Expression(value, this.firstColumn, new Object[0]);
                expr.execute();
                this.labelText.setText(expr.getValue().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            this.labelText.setText(((DisplayNode)value).toString());
        }
        UIutils.labelWidth(this.labelText);
        // Set float over text as the label is often truncated.  ISIS-349881.
        this.setToolTipText(this.labelText.getText());

        ImageIcon image = null;
        if (isSelected) {

            if (list.hasFocus()) {
                this.labelText.setBackground(list.getSelectionBackground());
                this.labelText.setForeground(list.getSelectionForeground());

                if (dn.getDVSelectedIcon() != null) {
                    image = new ImageIcon(dn.getDVSelectedIcon().getValue());
                }
                else if (dn.getDVLargeIcon() != null) {
                    image = new ImageIcon(dn.getDVLargeIcon().getValue());
                }
                else if (dn.getDVSmallIcon() != null) {
                    image = new ImageIcon(dn.getDVLargeIcon().getValue());
                }
            }
            else {
                //this._LabelText.setBackground(java.awt.Color.lightGray);
                //this._LabelText.setForeground(java.awt.Color.black);
                image = (dn.getDVLargeIcon()== null ? null : new ImageIcon(dn.getDVLargeIcon().getValue()));
            }
        }
        else {
            this.labelText.setBackground(list.getBackground());
            this.labelText.setForeground(list.getForeground());
            if (dn.getDVLargeIcon() != null) {
              image = new ImageIcon(dn.getDVLargeIcon().getValue());
            }
        }
        if (image != null) {
            image = new ImageIcon(image.getImage().getScaledInstance(this.imageSize, this.imageSize, Image.SCALE_DEFAULT));
            this.labelIcon.setIcon(image);
        }
        this.setEnabled(list.isEnabled());
        this.labelText.setFont(list.getFont());
        this.labelText.setOpaque(true);

        return this;
    }
}

TOP

Related Classes of DisplayProject.DisplayNodeRenderer

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.