es.emergya.ui.base.ImageButtonCell.java Source code

Java tutorial

Introduction

Here is the source code for es.emergya.ui.base.ImageButtonCell.java

Source

/*
 * Copyright (C) 2010, Emergya (http://www.emergya.es)
 *
 * @author <a href="mailto:jlrodriguez@emergya.es">Juan Lus Rodrguez</a>
 * @author <a href="mailto:marias@emergya.es">Mara Arias</a>
 * @author <a href="mailto:fario@emergya.es">Flix del Ro Beningno</a>
 *
 * This file is part of GoFleet
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As a special exception, if you link this library with other files to
 * produce an executable, this library does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * This exception does not however invalidate any other reasons why the
 * executable file might be covered by the GNU General Public License.
 */
/*
 * 18/06/2009
 */
package es.emergya.ui.base;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * Class that mimic the behavior of a JButton inside a JTable To use it,
 * instance it just as a JButton, and set the cell renderer and editor of the
 * JTable to use the ones generated by this class.
 * <br>
 * Example: 
 * <br>
 * <code>
 * {@link ImageButtonCell} myButton = new {@link ImageButtonCell}(myIcon);
 * mybutton.addActionListener(myActionListener);
 * mytable.getColumn("acolumn").setCellRenderer(myButton.getRenderer());
 * mytable.getColumn("acolumn").setCellEditor(myButton.getEditor());
 * </code>
 * 
 * @author fario
 * 
 */
public class ImageButtonCell extends JButton {

    private static final long serialVersionUID = -585641714809075119L;
    private static final Log LOG = LogFactory.getLog(ImageButtonCell.class);
    private TableCellRenderer renderer;
    private TableCellEditor editor;
    private JButton jbutton;
    private int row;
    private boolean useTextFromCell;

    /**
     * 
     * @param icon
     *            Can be null
     * @param text
     *            Can be null
     * @param actionCommand
     */
    public ImageButtonCell(Icon icon, String text, String actionCommand) {
        super(text, icon);
        this.setActionCommand(actionCommand);
    }

    public boolean isUseTextFromCell() {
        return useTextFromCell;
    }

    public void setUseTextFromCell(boolean useTextFromCell) {
        this.useTextFromCell = useTextFromCell;
    }

    public ImageButtonCell() {
        super();
    }

    /**
     * 
     * @return
     */
    public TableCellRenderer getRenderer() {
        if (renderer == null)
            renderer = (TableCellRenderer) new ImageButtonCell.ButtonCellRenderer();
        return renderer;
    }

    public TableCellEditor getEditor() {
        if (editor == null)
            editor = (TableCellEditor) new ImageButtonCell.ButtonCellEditor();
        return editor;
    }

    public int getRow() {
        return row;
    }

    /**
     * Calling "this" from an internal class has indeterminate behavior
     * 
     * @return The jbutton instance
     */
    private ImageButtonCell self() {
        return this;
    }

    private JButton untoggledRepresentation() {
        if (jbutton == null) {
            jbutton = new JButton(this.getText(), this.getIcon());
            jbutton.setBackground(this.getBackground());
            jbutton.setForeground(this.getForeground());
            jbutton.setMargin(this.getMargin());
            jbutton.setFont(this.getFont());
            jbutton.setBorder(this.getBorder());
            jbutton.setContentAreaFilled(this.isContentAreaFilled());
            jbutton.setPressedIcon(this.getPressedIcon());
        }
        return jbutton;
    }

    private class ButtonCellRenderer extends DefaultTableCellRenderer {

        private static final long serialVersionUID = 5276880803119755896L;

        public ButtonCellRenderer() {
            this.setHorizontalAlignment(SwingConstants.CENTER);
        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            JButton rt = isSelected ? self() : self().untoggledRepresentation();
            if (useTextFromCell && value != null)
                rt.setText(value.toString());
            return rt;
        }
    }

    private class ButtonCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {

        private static final long serialVersionUID = 1815891418239257620L;
        private Object old_value;

        public ButtonCellEditor() {
            self().addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            LOG.debug("Row: " + ((ImageButtonCell) self()).row);
            fireEditingStopped();
        }

        // Implement the one CellEditor method that AbstractCellEditor doesn't.
        public Object getCellEditorValue() {
            return old_value;
        }

        // Implement the one method defined by TableCellEditor.
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
                int column) {
            old_value = value;
            self().row = row;
            if (useTextFromCell && value != null)
                self().setText(value.toString());
            return self();
        }
    }

}