Package DisplayProject.actions

Source Code of DisplayProject.actions.Enabled

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

import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import DisplayProject.ArrayColumn;
import DisplayProject.ArrayColumnModel;
import DisplayProject.table.ArrayFieldCellHelper;
import DisplayProject.table.GenericCellEditor;
import Framework.ForteKeyboardFocusManager;
/**
* This action allows the set and get of the Enable state a widget. It is delegated to by {@link WidgetState}
*
*/
public class Enabled extends ReversableAction {
    private boolean enable;
    private GenericCellEditor editor;
    public Enabled(Component pComponent, boolean pEnable) {
        super(pComponent);
        this.enable = pEnable;
    }
    public Enabled(GenericCellEditor pComponent, boolean pEnable) {
        super(null);
        this.editor = pComponent;
        this.enable = pEnable;
    }
    public boolean isEnabled() {
        return this.enable;
    }
    public GenericCellEditor getEditor() {
        return this.editor;
    }
    public void performAction() {
        performAction(this.getComponent());
    }

    public void performAction(Component component) {
        if (this.editor != null) {
            this.editor.setEnabled(this.enable);
        }
        else {
            _setEnabled(component, this.enable);
        }
    }

    public static void _setEnabled(Component comp, boolean value) {
        if (comp instanceof JScrollPane) {
            JScrollPane sp = (JScrollPane)comp;
            sp.getHorizontalScrollBar().setEnabled(value);
            sp.getVerticalScrollBar().setEnabled(value);
            _setEnabled(sp.getViewport(), value);
            if (sp.getViewport().getView() != null)
                _setEnabled(sp.getViewport().getView(), value);
        }
        // CraigM:09/09/2008 - Treat JPopupMenu like JPanels
        else if (comp instanceof JPanel || comp instanceof JPopupMenu) {
            JComponent J = ((JComponent) comp);
            if (J.getComponentCount() > 0) {
                Component[] kids = J.getComponents();
                for (int i = 0; i < kids.length; i++)
                    _setEnabled(kids[i], value);
            }
        }
        //PM:22/11/07 added specific processing for columns in ArrayField
        JTable t = ArrayFieldCellHelper.getArrayField(comp);
       
        // CraigM:18/10/2008 - If we are modifying a component which is in a grid in the array field, then this will not affect the column
        if (t != null && ArrayFieldCellHelper.isInGridFieldInArrayField(comp) == false) {
            int col = ArrayFieldCellHelper.getArrayFieldColumn((JComponent)comp);
            ArrayColumnModel model = (ArrayColumnModel) t.getColumnModel();
            ArrayColumn column = (ArrayColumn)model.getRealColumn(col);
            if (column != null) {
              // CraigM:17/10/2008 - If the component is not focusable (like TextFields), then don't allow it to be editable
              if (value && Focusable.is(comp)) {
                column.setEditable(true);
              }
              else {
                column.setEditable(false);
              }
            }
        }
        // TF:8/4/08:Removed the else test, so that the enabled state is set on both the
        // underlying widget and the column if the checkbox is in a column
        comp.setEnabled(value);
       
        // TF:15/03/2009:FTL-12: If we're making a component disabled and that component currently
        // has the focus, we need to ensure that when the focus is transferred to the new owner that
        // it will be listed as being transferred by the application.
        if (!value && comp.isFocusOwner()) {
          ForteKeyboardFocusManager.setStateChangedTraversal();
        }
    }
   
    /**
     * This method sets the Enabled state on a compound widget and its children
     */
    public static void set(java.awt.Component comp, boolean value) {
        ActionMgr.addAction(new Enabled(comp, value));
    }
    public static void set(GenericCellEditor comp, boolean value) {
        ActionMgr.addAction(new Enabled(comp, value));
    }
    public static boolean is(Component comp){
        Enabled action = ActionMgr.getAction(comp, Enabled.class);
        if (action != null)
            return action.isEnabled();
        else
            return comp.isEnabled();
    }
    public static boolean is(GenericCellEditor comp){
        Enabled action = ActionMgr.getAction(comp, Enabled.class);
        if (action != null)
            return action.isEnabled();
        else
            return comp.isEnabled();
    }

    public PendingAction createReverseAction() {
        return new Enabled(this._component, is(this._component));
    }

    public boolean isSameAction(PendingAction a) {
        return (a instanceof Enabled) && (a._component == this._component);
    }
    @Override                //JVM 1.5
    public String toString() {
            return "Enabled [" + this._component.toString() +
                "] to [" + Boolean.toString(this.enable) + "]";
    }
    @Override
    public PendingAction applyActionTo(Component component) {
        PendingAction reverse = null;
        if (editor != null) {
            reverse = new Enabled(editor, editor.isEnabled());
        }
        else {
            reverse = new Enabled(component, component.isEnabled());
        }
        performAction(component);
        return reverse;
    }
}

TOP

Related Classes of DisplayProject.actions.Enabled

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.