Package DisplayProject.table

Source Code of DisplayProject.table.ArrayFieldCellEditor$EditorLayoutPanel

/*
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.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.util.EventObject;

import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;

import DisplayProject.Constants;
import DisplayProject.GridCell;
import DisplayProject.GridField;
import DisplayProject.GridFieldLayout;

/**
* All cell editor components in an ArrayField are wrapped in this cell editor.
* This cell editors main job is to stop the components sizing to parent (which
* is the default behaviour for a JTable).  It does this by putting the cell
* component in a JPanel, so the JPanel will expand to the cell size, leaving
* the actual component to be the requested size.  The requested size is
* determined in ArrayFieldCellHelper.getGBCFill().
*
* @see ArrayFieldCellRenderer
*
* @author Craig Mitchell
* @since 24/08/2007
*/
@SuppressWarnings("unused")
public class ArrayFieldCellEditor implements ArrayFieldTableCellEditor {
  private Color bgColour = null;
    private Color fgColour = null;

    // TF:12/10/07:Extracted this to a named inner class so we can get access to the real component if needed
    // CraigM:16/10/2008 - Switched from extending a JPanel to extend a GridField.
    @SuppressWarnings("serial")
    public static class EditorLayoutPanel extends GridField {
        private Component comp;
        public EditorLayoutPanel(Component pComp) {
            comp = pComp;
            this.setOpaque(false);
            this.setLayout(new GridFieldLayout()); // CraigM:16/10/2008 - Set the layout for a GridField
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = ArrayFieldCellHelper.getGBCFill(comp);

            // CraigM:16/10/2008 - Size ourselves to our parent (the array column) so the component will be centered
            this.setHeightPolicy(Constants.SP_TO_PARENT);
            this.setWidthPolicy(Constants.SP_TO_PARENT);
            // TF:21/10/2008:Changed this to use grid cells to obey the width and height policies
            GridCell cell = new GridCell(gbc);
            if (comp instanceof JComponent) {
              GridCell oldCell = GridCell.get((JComponent)comp);
              cell.setWidthPolicy(oldCell.getWidthPolicy());
              cell.setHeightPolicy(oldCell.getHeightPolicy());
            }
            // this.add(comp, gbc);
            this.add(comp, cell);
        }
        @Override
        public void requestFocus() {
            comp.requestFocus();
        }

        @Override
        public boolean requestFocusInWindow() {
            return comp.requestFocusInWindow();
        }
        public Component getEditorComponent() {
            return comp;
        }
    }

    private TableCellEditor editor;

    public ArrayFieldCellEditor(TableCellEditor pEditor) {
        this.editor = pEditor;
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        // TF:27/9/07:Added null check in
        if (editor != null) {
            Component comp = this.editor.getTableCellEditorComponent(table, value, isSelected, row, column);
            //TF:03-11-08 This code is redundant.
            //PM:22/11/07 bg colour from colour change
//            if (this.bgColour != null) {
//                comp.setBackground(this.bgColour);
//            }
//            if (this.fgColour != null) {
//                comp.setForeground(this.fgColour);
//            }
            // Wrap the component in a panel to stop it sizing to parent
            // TF:29 Oct 2008:If the current row size is too small to accomodate our grid, we need to increase
            // the row height. If we don't we enter an infinite loop of grid sizing up, row sizing down...
            Component result = new EditorLayoutPanel(comp);
            if (result.getMinimumSize().height + table.getRowMargin() > table.getRowHeight()) {
              table.setRowHeight(table.getRowMargin() + result.getMinimumSize().height);
            }
           
            if (result instanceof JComponent) {
              ((JComponent)result).setComponentPopupMenu(table.getComponentPopupMenu());
            }
            return result;
        }
        return null;
    }

    public void addCellEditorListener(CellEditorListener l) {
        this.editor.addCellEditorListener(l);
    }

    public void cancelCellEditing() {
        this.editor.cancelCellEditing();
    }

    public Object getCellEditorValue() {
        return this.editor.getCellEditorValue();
    }

    public boolean isCellEditable(EventObject anEvent) {
        return this.editor == null ? false : this.editor.isCellEditable(anEvent);
    }

    public void removeCellEditorListener(CellEditorListener l) {
        this.editor.removeCellEditorListener(l);
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return this.editor.shouldSelectCell(anEvent);
    }

    public boolean stopCellEditing() {
        return this.editor.stopCellEditing();
    }

    public TableCellEditor getEditor() {
        return this.editor;
    }

    public void setBackground(Color c) {
        bgColour = c;
    }

    public void setForeground(Color c) {
        fgColour = c;
    }

  public Component getComponent() {
    if (this.getEditor() != null) {
      return ((ArrayFieldTableCellEditor)this.getEditor()).getComponent();
    }
    return null;
  }
}
TOP

Related Classes of DisplayProject.table.ArrayFieldCellEditor$EditorLayoutPanel

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.