Package DisplayProject.actions

Source Code of DisplayProject.actions.Visible

/*
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.Container;
import java.awt.Dimension;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import org.apache.log4j.Logger;

import DisplayProject.ArrayColumn;
import DisplayProject.ArrayColumnModel;
import DisplayProject.controls.ArrayField;
import DisplayProject.controls.Panel;
import DisplayProject.controls.TabFolder;
import DisplayProject.table.ArrayFieldCellHelper;
import DisplayProject.table.GenericCellEditor;
import Framework.ForteKeyboardFocusManager;

/**
* This pending action is used internally by the WidgetState pending action
*
*/
public class Visible extends ReversableAction implements ColumnAction {
    private static Logger _log = Logger.getLogger(Visible.class);
    private boolean visible;

    private GenericCellEditor editor;

    private ArrayColumn column;

    public Visible(Component pComponent, boolean pVisible) {
        super(pComponent);
        this.visible = pVisible;
    }

    public Visible(GenericCellEditor pComponent, boolean pVisible) {
        super((Component) null);
        this.editor = pComponent;
        this.visible = pVisible;
    }

    public Visible(ArrayColumn column, boolean pVisible) {
        super((Component) null);
        this.editor = null;
        this.visible = pVisible;
        this.column = column;
    }

    public boolean isVisible() {
        return this.visible;
    }

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

    public ArrayColumn getColumn() {
        return this.column;
    }

    public void performAction() {
        performAction(this.getComponent());
    }

    public void performAction(Component component) {
        if (this.column != null) {
            this.column.setVisible(this.visible);
        } else if (this.editor != null) {
            this.editor.setVisible(this.visible);
        } else {
            _setVisible(component, this.visible);
        }
    }

    public static void set(Component comp, boolean value) {
        // if this component is a window but the Form attribute.parent does not equal this component,
        // it's something like the form has been reparented, but the state is still being set on the window
        // so we really need to set it on the form instead.
        if (comp instanceof JFrame) {
            try {
                Class<?> clazz = comp.getClass();
                Field formField = clazz.getField("Form");
                if (formField != null) {
                    formField.setAccessible(true);
                    Component c = (Component) formField.get(comp);
                    if (c.getParent() != comp) {
                        Visible.set(c, value);
                        return;
                    }
                }
            } catch (NoSuchFieldException e) {
            } catch (IllegalAccessException e) {
            }
        }
        // Only set the visible state if we are not the current visible state. This stops unnecessary pack calls. Craig Mitchell: 20/01/2007.
        if (Visible.is(comp) != value || comp instanceof JPopupMenu) {  // DET-118, Allow JPopup menus to ignore this test.
            ActionMgr.addAction(new Visible(comp, value));
        }

        // Tab panes are an exception to the rule, as they can be not visible, but still want to be removed. CraigM 05/08/2007
        else if (comp instanceof JPanel &&
                    comp.getParent() != null &&
                    comp.getParent() instanceof JTabbedPane) {
            ActionMgr.addAction(new Visible(comp, value));
        }
    }

    public static void set(GenericCellEditor comp, boolean value) {
        ActionMgr.addAction(new Visible(comp, value));
    }

    public static void set(ArrayColumn column, boolean value) {
        ActionMgr.addAction(new Visible(column, value));
    }

    public static void _setVisible(Component comp, boolean value) {
        Container parent = comp.getParent();
        // TF:25/05/2009:Tidied this up
        if (comp.isVisible() && value && !(parent instanceof TabFolder )) {//PM:15/01/2009:always do this if it is a tab page
          // TF:30/11/2009:DET-118:Added check in for the popup menu
          if (comp instanceof JPopupMenu == false) {
            return;
          }
        }

        if (!comp.isVisible() && !value) {

            // Don't skip over doing this if we are a tabbed folder,
            //as a tab can be not visible because it is not selected,
            //but we still want to make it invisible to remove it. CraigM 05/08/2007.
          // TF:25/05/2009:I'm pretty sure Craig wanted to use "OR"s here
//          if (comp instanceof JPanel == false &&
//            parent == null &&
//            parent instanceof JTabbedPane == false) {
          if (comp instanceof JPopupMenu == false) {    // DET-118, ensured we could hit the special code later for the popup menu
            if (comp instanceof JPanel == false ||
                parent instanceof JTabbedPane == false) {
              return;
            }
          }
        }
        if (comp instanceof JComponent) {
          JComponent jcomp = (JComponent)comp;
            ArrayField af = ArrayFieldCellHelper.getArrayField(jcomp);
            if (af != null) {
              // CraigM:02/07/2008 - Clean up of code to use helper class
              int col = ArrayFieldCellHelper.getArrayFieldColumn(jcomp);
                ((ArrayColumnModel)af.getColumnModel()).show(col, value);
            }

            if (comp instanceof JTabbedPane) {
                /*
                 * JTabbedPane first three kids are the tab controls and the scroll buttons
                 * so don't set their visibility, the TabPane will do this
                 * TF:26/5/09:This only applies in some cases (ie SCROLL_TAB_LAYOUT) so we
                 * iterate through the tabs differently
                 */
                JTabbedPane tp = ((JTabbedPane) comp);
                tp.setVisible(value);
                int tabs = tp.getTabCount();
                if (tabs > 0) {
                  // TF:26/05/2009:Unfortunately, as we're setting the visibility, this may change
                  // the component count and indexes, so remember the pages first.
                  List<Component> tabPages = new ArrayList<Component>(tabs);
                  for (int i = 0; i < tabs; i++) {
                    tabPages.add(tp.getComponentAt(i));
                  }
                  for (Component c : tabPages) {
                    _setVisible(c, value);
                  }
                }
            }
            // CraigM:09/09/2008 - Java doesn't allow hiding of popup menus, so just make its size 0
            else if (comp instanceof JPopupMenu) {
              if (value) {
                ((JPopupMenu)comp).setPreferredSize(null);
              }
              else {
                ((JPopupMenu)comp).setPreferredSize(new Dimension(0, 0));
              }
            }
            else if (comp instanceof Panel){
              Panel jp = ((Panel) comp);
                /*
                 * if the JPanel was or is part of a JTabbedPane
                 */
             
              _log.debug("Panel "+ jp.getCaption() + " parent: " + ((parent == null) ? "null" : parent.getClass().getName()));
                if (parent instanceof TabFolder ) {
                  if (value) {
                    ((TabFolder)parent).showTab(jp); //PM:14/01/2009:moved functionality to TabFolder
                    _log.debug("Tab: "+jp.getCaption()+" is now visible");
                  } else {
                    ((TabFolder)parent).hideTab(jp);//PM:14/01/2009:moved functionality to TabFolder
                    _log.debug("Tab: "+jp.getCaption()+" is now hidden");
                  }
                } else {
                  comp.setVisible(value);
                }
            } else if (comp instanceof JPanel) {
               
                    comp.setVisible(value);
                   
            } else {
                /*
                 * handle the cases where the widget is in a scroll pane
                 */
                if ((parent != null)
                        && (parent.getParent() != null)
                        && (parent.getParent() instanceof JScrollPane))
                  parent.getParent().setVisible(value);
                comp.setVisible(value);
            }
        } else {
            comp.setVisible(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();
        }

        if ((parent != null)&&(parent.isValid()))
          parent.invalidate();
    }


    public static boolean is(Component comp) {
        boolean vis = false;
        Visible action = (Visible) ActionMgr.getAction(comp, Visible.class);
        if (action != null)
            vis = action.isVisible();
        else
            vis = comp.isVisible();
        return vis;
    }

    public static boolean is(GenericCellEditor comp) {
        boolean vis = false;
        Visible action = (Visible) ActionMgr.getAction(comp, Visible.class);
        if (action != null)
            vis = action.isVisible();
        else
            vis = comp.isEnabled();
        return vis;
    }

    public static boolean is(ArrayColumn col) {
        boolean vis = false;
        Visible action = (Visible) ActionMgr.getAction(col, Visible.class);
        if (action != null)
            vis = action.isVisible();
        else
            vis = col.isVisible();
        return vis;
    }

    //    public boolean requiresPack() {
    //        return true;
    //    }

    public PendingAction createReverseAction() {
        return new Visible(this._component, this._component.isVisible());
    }

    public boolean isSameAction(PendingAction a) {
        return (a instanceof Visible) && (a._component == this._component);
    }

    public String toString() {
        return "Visible[" + this.visible + "] to Column=" + this.column
                + " editor=" + this.editor + " component="
                + this.getComponent();
    }

    @Override
    public PendingAction applyActionTo(Component component) {
        PendingAction result;
        if (editor != null) {
            result = new Visible(editor, Visible.is(editor));
        }
        else if (column != null) {
            result = new Visible(column, column.isVisible());
        }
        else {
            result = new Visible(component, component.isVisible());
        }
        performAction(component);
        return result;
    }
}
TOP

Related Classes of DisplayProject.actions.Visible

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.