Package DisplayProject.actions

Source Code of DisplayProject.actions.VisibleColumns

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

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import DisplayProject.ArrayColumn;
import DisplayProject.DataField;
import DisplayProject.UIutils;
import DisplayProject.controls.ArrayField;
import DisplayProject.controls.DropList;
import DisplayProject.controls.FillInField;
import DisplayProject.table.ArrayFieldCellHelper;

/**
* Gets and sets the width of a widget based on the average width of a character by the number of columns.
*
*/
public class VisibleColumns extends PendingAction {

    private int columns;
    public VisibleColumns(Component pComponent, int value) {
        super(pComponent);
        this.columns = value;
    }

    public void performAction() {
        if (this._component instanceof JTextArea){
            JTextArea tc = (JTextArea)this._component;
            Dimension currentSize = ((JScrollPane)tc.getParent().getParent()).getPreferredSize();
            FontMetrics fm = tc.getFontMetrics(tc.getFont());
            int[] charWidths = fm.getWidths();
            int width = 0;
            for (int i = 0; i < charWidths.length; i++){
                width += charWidths[i];
            }
            width /= charWidths.length;
            ((JScrollPane)tc.getParent().getParent()).setPreferredSize(new Dimension(width * this.columns, currentSize.height));
        }
        else if (this._component instanceof DropList) {
          // CraigM:21/08/2008 - DropLists cannot be sized with columns, however, FillInFields can.
        }
        else {
            // TF: Made this handle the generic case as well as the specific
            Dimension minSize = this._component.getMinimumSize();
            int width = UIutils.averageCharacter((JComponent)this._component) * this.columns;

            this._component.setSize(width, this._component.getHeight());
            this._component.setMinimumSize(new Dimension(width, minSize.height));
            this._component.setPreferredSize(new Dimension(width, minSize.height));

            // CraigM:16/06/2008 - Update the column model if we are in an array field
            ArrayField af = ArrayFieldCellHelper.getArrayField(this._component);

            if (af != null) {
              int col = ArrayFieldCellHelper.getArrayFieldColumn(this._component);
              ArrayColumn tc = (ArrayColumn)af.getColumnModel().getColumn(col);
              tc.setWidth(width);
              tc.setMinWidth(width);
              tc.setPreferredWidth(width);
              tc.setAssignedWidth(width);
            }
        }
    }

    public int getColumns() {
        return columns;
    }

    public static void set(JComponent comp, double value){
      set(comp, (int)value);
    }
    public static void set(JComponent comp, int value) {
        ActionMgr.addAction(new VisibleColumns(comp, value));
    }
    /**
     * Returns the Row of a Widget, based from 1
     * @param comp
     * @return
     */
    public static int get(JComponent comp) {
        VisibleColumns action = ActionMgr.getAction(comp, VisibleColumns.class);
        if (action != null) {
            return action.getColumns();
        }
        if (comp instanceof JTextArea) {
            JTextArea tc = (JTextArea)comp;
            FontMetrics fm = tc.getFontMetrics(tc.getFont());
            int[] charWidths = fm.getWidths();
            int width = 0;
            for (int i = 0; i < charWidths.length; i++){
                width += charWidths[i];
            }
            width /= charWidths.length;
            return ((JScrollPane)tc.getParent().getParent()).getPreferredSize().width / width;
        }
        //TF:6/7/07:Added the Visible columns for a data field.  CraigM:21/08/2008:Added FillInField.
        else if (comp instanceof DataField || comp instanceof FillInField) {
            // CraigM:21/08/2008 - Cater for the drop arrow in FillInFields
            int currentSize = comp instanceof FillInField ? comp.getWidth()-20 : comp.getWidth();
            FontMetrics fm = comp.getFontMetrics(comp.getFont());
            int[] charWidths = fm.getWidths();
            double width = 0;
            for (int i = 0; i < charWidths.length; i++){
                width += charWidths[i];
            }
            width /= charWidths.length;
            return (int)(currentSize / width);
        }
        else if (comp instanceof DropList) {
          // CraigM:21/08/2008 - DropLists cannot be sized with columns, however, FillInFields can.
        }
        return 0;
    }
    //PM:6/04/08
//    public static int get(CharacterField comp){
//        return get((JComponent)comp);
//    }

}
TOP

Related Classes of DisplayProject.actions.VisibleColumns

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.