Package DisplayProject.actions

Source Code of DisplayProject.actions.Parent

/*
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 java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.lang.reflect.Method;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JViewport;
import javax.swing.MenuElement;

import DisplayProject.CompoundField;
import DisplayProject.Constants;
import DisplayProject.DisplayNode;
import DisplayProject.GridCell;
import DisplayProject.GridField;
import DisplayProject.GridFieldLayout;
import DisplayProject.PanelLayoutManager;
import DisplayProject.PictureField;
import DisplayProject.UIutils;
import DisplayProject.WindowFormLayout;
import DisplayProject.WindowUsageMap;
import DisplayProject.controls.ArrayField;
import DisplayProject.controls.CompoundGraphic;
import DisplayProject.controls.Line;
import DisplayProject.controls.MenuList;
import DisplayProject.controls.Panel;
import DisplayProject.controls.PictureGraphic;
import DisplayProject.controls.Point;
import DisplayProject.controls.TabFolder;
import DisplayProject.controls.TextGraphic;
import DisplayProject.events.ClientEventManager;
import DisplayProject.table.ArrayFieldCellHelper;
import Framework.ErrorMgr;
import Framework.UsageException;

/**
* This pending action implements the Forte Parent attribute
* @author Peter
* PM:01/8/07 Added overload on set() for JScrollpane and CompoundField
* TF:04/1/08 Ensured consistent return types from overloads of getter
*/
public class Parent extends PendingAction {
    private Component parent;
    private boolean preventRedraw = false;
    private DisplayNode dnChild, dnParent;

    public Parent(Component pComponent, Component pParent) {
        super(pComponent);
        this.parent = pParent;
        // Also let the client event manager know about this relationship so it can
        // manage the events properly.
        ClientEventManager.addChildForObject((JComponent) pComponent, parent);
    }

    public Parent(Component pComponent, Component pParent, boolean preventRedraw) {
      this(pComponent, pParent);
      this.preventRedraw = preventRedraw;
    }
   
    /**
     * Create a relationship between display nodes. We must do this as a pending action as it can affect the
     * display, and also the parent might not be fully set up as RootNode is a pending action
     */
    public Parent(DisplayNode pChild, DisplayNode pParent) {
        super(null);
        this.dnChild = pChild;
        this.dnParent = pParent;
    }

    public void performAction() {
        if (this._component != null) {
            if (this._component instanceof MenuElement){
                if (parent == null){
                    if (this._component.getParent() != null) {
                        this._component.getParent().remove(this._component);
                    }
                } else {
                    if (parent instanceof JMenu){
                        if (this._component instanceof MenuList)
                            ((JMenu)parent).add((MenuList)this._component);
                        else
                            ((JMenu)parent).add((JMenuItem)this._component);
                    } else if (parent instanceof JPopupMenu)
                        ((JPopupMenu)parent).add((JMenuItem)this._component);
                    //PM:5/11/07 added support for menu bar
                    else if (parent instanceof JMenuBar){
                        ((JMenuBar)parent).add((JMenu)this._component);
                    }
            // Perhaps this should be revalidate()?
                    parent.repaint();
                }

            } else {
                // TF:20/8/07:Adapted to use new GridField static methods
                setComponentParent(((JComponent) this._component), ((JComponent) parent), this.preventRedraw);

                if (parent != null) {
                    // TF,PM,CM: This forces the container to layout its children.  13/07/2007.
                    if (parent instanceof JComponent) { // We really should always be a Container
                        ((JComponent)parent).revalidate();
                    }
                    parent.repaint();
                }
            }
        } else if (dnChild != null) {
            dnChild.setParent(dnParent);
        }
    }

    /**
     * Sets the parent of a widget
     * @param comp
     * @param parent
     */
    public static void set(ButtonGroup comp, Component parent) {
        Enumeration<AbstractButton> em = comp.getElements();
        while (em.hasMoreElements()) {
            JRadioButtonMenuItem jrbmi = (JRadioButtonMenuItem)em.nextElement();
            set((MenuElement) jrbmi, parent);
        }
    }

    public static void set(JComponent comp, Component parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(JComponent comp, Component parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, parent, preventRedraw));
    }
    public static void set(JComponent comp, JComponent parent) {
      ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(JComponent comp, JComponent parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, parent, preventRedraw));
    }
    public static void set(GridField comp, JComponent parent) {
      ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(GridField comp, JComponent parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, parent, preventRedraw));
    }
    public static void set(JComponent comp, CompoundField parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
    public static void set(JComponent comp, CompoundField parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }
    public static void set(JComponent comp, GridField parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
    public static void set(JComponent comp, GridField parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }
    public static void set(CompoundGraphic comp, JComponent parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
    public static void set(CompoundGraphic comp, JComponent parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }
    public static void set(CompoundGraphic comp, CompoundGraphic parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
    public static void set(CompoundGraphic comp, CompoundGraphic parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }
    public static void set(TextGraphic comp, CompoundGraphic parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
    public static void set(TextGraphic comp, CompoundGraphic parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }
    public static void set(CompoundField comp, CompoundField parent) {
      ActionMgr.addAction(new Parent((Component)comp, (Component)parent));
    }
    public static void set(CompoundField comp, CompoundField parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent((Component)comp, (Component)parent, preventRedraw));
    }
    public static void set(JComponent comp, Panel parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
    public static void set(JComponent comp, Panel parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }

    public static void set(JComponent comp, ArrayField parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
    public static void set(JComponent comp, ArrayField parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }

    public static void set(JComponent comp, TabFolder parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
    public static void set(JComponent comp, TabFolder parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }

    public static void set(MenuList comp, Component parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(MenuList comp, Component parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, parent, preventRedraw));
    }

    public static void set(Panel comp, TabFolder parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(Panel comp, JComponent parent) {
      ActionMgr.addAction(new Parent(comp, parent));
    }
   
    public static void set(PictureField comp, GridField parent) { //PM:12/9/07
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(TextGraphic comp, GridField parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(PictureGraphic comp, GridField parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(GridField comp, GridField parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(GridField comp, Panel parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(TextGraphic comp, Panel parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(TextGraphic comp, Panel parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, parent, preventRedraw));
    }
    public static void set(Line comp, CompoundGraphic parent) {
      ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(GridField comp, Component parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(Panel comp, GridField parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(GridField comp, CompoundField field) {
        if (field instanceof JComponent)
            ActionMgr.addAction(new Parent(comp, (JComponent) field));
    }

    public static void set(Panel comp, CompoundField field) {
        if (field instanceof JComponent)
            ActionMgr.addAction(new Parent(comp, (JComponent) field));
    }
    public static void set(Panel comp, CompoundField field, boolean preventRedraw) {
      if (field instanceof JComponent)
        ActionMgr.addAction(new Parent(comp, (JComponent) field, preventRedraw));
    }

    public static void set(JMenuBar comp, Component parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(JMenu comp, Component parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(JMenuItem comp, JMenu parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(JCheckBoxMenuItem comp, Component parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(JMenuItem comp, Component parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(JFrame comp, Component parent) {
        ActionMgr.addAction(new Parent(getFromFromFrame(comp), parent));
    }

    public static void set(JFrame comp, Panel parent) {
        ActionMgr.addAction(new Parent(getFromFromFrame(comp), parent));
    }

    public static void set(JFrame comp, CompoundField parent) {
        ActionMgr.addAction(new Parent(getFromFromFrame(comp), (JComponent)parent));
    }
    public static void set(JFrame comp, GridField parent) {
        ActionMgr.addAction(new Parent(getFromFromFrame(comp), parent));
    }

    public static void set(CompoundField comp, JComponent parent) {
      ActionMgr.addAction(new Parent((Component)comp, (Component)parent));
    }
   
    public static void set(JComponent comp, CompoundGraphic parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent(comp, (Component)parent, preventRedraw));
    }
   
    public static void set(JComponent comp, CompoundGraphic parent) {
      ActionMgr.addAction(new Parent(comp, (Component)parent));
    }
   

    //was in old code
//  public static void set(JFrame comp, CompoundField parent){
//    ActionMgr.addAction(new Parent(UIutils.getForm(comp), (Component)parent));
//  }

    public static void set(Line comp, Panel parent) {
        ActionMgr.addAction(new Parent((Component)comp, parent));
    }

    public static void set(Line comp, Panel parent, boolean preventRedraw) {
      ActionMgr.addAction(new Parent((Component)comp, parent, preventRedraw));
    }
   
    public static void set(Point comp, Panel parent) {
        ActionMgr.addAction(new Parent((Component)comp, parent));
    }
    public static void set(Panel comp, Panel parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    // I don't think this is used.  Calls should just go to: set(JComponent comp, Component parent)
    // CraigM: 17/02/2008.
//    public static void set(JScrollPane pane, CompoundField parentField) {
//        if (parentField instanceof JComponent) {
//            ActionMgr.addAction(new Parent(pane, (JComponent) parentField));
//        }
//    }

    public static void set(JMenu comp, JMenu parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    public static void set(JCheckBoxMenuItem comp, JMenu parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }
    public static void set(MenuElement comp, Component parent) {
        if ((comp instanceof JMenu) || (comp instanceof JMenuItem)
                || (comp instanceof JRadioButtonMenuItem)
                || (comp instanceof JCheckBoxMenuItem)
                || (comp instanceof JMenuBar))
            ActionMgr.addAction(new Parent((JComponent) comp, parent));
    }

    public static void set(DisplayNode comp, DisplayNode parent) {
        ActionMgr.addAction(new Parent(comp, parent));
    }

    /**
     * returns ths parent of the DisplayNode. If the node has no parent it return null.
     * @param comp
     * @return
     */
    public static DisplayNode get(final DisplayNode comp) {
        if(comp == null)
            return null;

        // TF:29/7/07: Because of the interaction between the display nodes (eg parent can be set
        // using the FirstChild, NextSibling, Parent, etc actions) we can only be certain that we
        // have the correct parent by processing the actions and returning the true parent
        UIutils.processGUIActions();
        return (DisplayNode) comp.getParent();
    }

    /**
     * Returns the Parent of a Widget
     * @param comp
     * @return
     *
     * Switched param from JComponent to Component and handled.  CraigM 30/11/2007.
     */
    public static Container get(Component comp) {
        if(comp == null)
            return null;

        if(comp instanceof Container == false)
            return null;

        Parent action = (Parent) ActionMgr.getAction(comp, Parent.class);
        if (action != null)
            return (JComponent) action.parent;
        else {
            Container p;
            ArrayField af = ArrayFieldCellHelper.getArrayField(comp);
           
            // If the component is contained in an ArrayField, then the ArrayField is the parent.  CraigM: 27/03/2008.
            // TF:27/10/2008:Changed this logic to return the array field as the parent only if there is no real
            // parent, to cater for grids within array fields.
            p = comp.getParent();
            if (p == null && af != null) {
              p = af;
            }
            else {
              if (p instanceof JViewport) {
                  p = p.getParent();
              }
            }
            return p;
        }
    }

//  /**
//  * returns the parent of the container
//  * @param comp
//  * @return
//  */
//    public static Container get(Container comp) {
//      return get((Component)comp);
//    }

    /**
     * Return the containing widget for the passed Array Field. This is necessary as
     * a ArrayField is both a CompoundField and a Component, so is used to remove
     * ambiguity when a array field is passed to the Parent call.
     */
    public static Container get(ArrayField comp) {
        return get((Component) comp);
    }

    /**
     * Return the containing widget for the passed Grid Field. This is necessary as
     * a GridField is both a CompoundField and a Component, so is used to remove
     * ambiguity when a grid field is passed to the Parent call.
     */
    public static Container get(GridField comp) {
        return get((Component) comp);
    }

    /**
     * Return the containing widget for the passed Panel. This is necessary as
     * a Panel is both a CompoundField and a Component, so is used to remove
     * ambiguity when a grid field is passed to the Parent call.
     */
    public static Container get(Panel comp) {
        return get((Component) comp);
    }

    public static Container get(CompoundField comp) {
        return get((Component)comp);
    }

    /**
     * returns the parent of the MenuElement
     * @param comp
     * @return
     */
    public static Container get(MenuElement comp) {
        if(comp == null)
            return null;
        JComponent menu = null;
        if ((comp instanceof JMenu) || (comp instanceof JMenuBar)
                || (comp instanceof JMenuItem)
                || (comp instanceof JCheckBoxMenuItem)
                || (comp instanceof JRadioButtonMenuItem))
            menu = (JComponent)comp;
        else
            return null;
        Parent action = (Parent) ActionMgr.getAction(menu, Parent.class);
        if (action != null)
            return (Container) action.parent;
        else {
          // DKL:25/06/2008: return real invoker as a parent menu to emulate Forte processing.
          Container parentContainer = menu.getParent();
          if (parentContainer instanceof JPopupMenu) {
            //Assume we are in sub menu. So invoker field holds the real parent
        JPopupMenu popupMenu = (JPopupMenu) parentContainer;
        return (Container) popupMenu.getInvoker();
      }
          return parentContainer;
        }
    }

    /**
     * Returns the Parent of a Nested window
     * @param comp
     * @return
     */
    public static Container get(JFrame comp) {
        if(comp == null)
            return null;

        Parent action = (Parent) ActionMgr.getAction(comp, Parent.class);
        if (action != null)
            return (JComponent) action.parent;
        else {
            Class<? extends JFrame> parentClass = comp.getClass();
            try {
                Method formMethod = parentClass.getMethod("getForm", new Class[0]);
                JPanel theForm = ((JPanel) formMethod.invoke(comp, new Object[0]));
                if (theForm.getParent() == comp.getLayeredPane()){ //PM:23/4/08 if the window is not nested, the parent should be null
                  return null;
                } else {
                  return theForm.getParent();
                }
            } catch (Exception e) {
                RuntimeException errorVar = new RuntimeException("The widget: " + comp.getName() + " has no member: 'Form'");
                ErrorMgr.addError(errorVar);
                throw errorVar;
            }
        }
    }

    /**
     * Reparent the child window into the specified parent window
     * @param pParent - the new parent. If this is null, the child is removed from this container
     * @param pChild - the child to re-parent. This parameter cannot be null
     */
    public static void setComponentParent(JComponent pChild, JFrame pParent, boolean preventRedraw) {
        if (pParent == null) {
            setComponentParent(pChild, (Container) null, preventRedraw);
            return;
        }
        if (pChild == null)
            return;
        if (pChild.getParent() == pParent)
            return;

        Class<? extends JFrame> parentClass = pParent.getClass();
        try {
            Method formMethod = parentClass.getMethod("getForm", new Class[0]);
            JPanel parentForm = ((JPanel) formMethod.invoke(pParent,
                    new Object[0]));
            setComponentParent(pChild, parentForm, preventRedraw);
        } catch (Exception e) {
            throw new RuntimeException("The widget: " + pChild.getName()
                    + " cannot be parented with: " + pParent.getName());
        }
    }

    /**
     * Copy all the usages (for type usage) from oldWindow to newWindow.  CraigM: 17/06/2008.
     *
     * @param oldWindow
     * @param newWindow
     */
    public static void copyUsageMap(JFrame oldWindow, JFrame newWindow) {//PM:13/01/2009:made public
      WindowUsageMap usageOld = WindowUsage.getWindowUsageMap(oldWindow);
      WindowUsageMap usageNew = WindowUsage.getWindowUsageMap(newWindow);
      usageNew.addAll(usageOld);
    }

    /**
     * @param pChild
     * @param pCurrentParent
     * @param pNewParent
     *
     * CraigM: 24/04/2008: Added pNewParent attribute.
     */
    private static void moveChildOffWindow(JComponent pChild, Container pCurrentParent, Container pNewParent) {
        //PM:9/10/07 store declared JFrame as a client property on the form
        Container declaredWindow = (Container)pChild.getClientProperty("qq_DeclaredWindow");
        //AD:14/12/2008: FTL-1 Get the original Frame based on the current parent rather than layout manager
        JFrame frame = UIutils.getWindowForComponent(pCurrentParent);
        if (declaredWindow ==  null){
            declaredWindow = frame;
            pChild.putClientProperty("qq_DeclaredWindow", declaredWindow);
        }
        Insets i = ((WindowFormLayout)pChild.getLayout()).getLayoutInsets(pChild);
        pChild.setLayout(new PanelLayoutManager(true));
        ((Panel)pChild).setInsets(i);
       
        // TF:28/04/2008:These rules only seem to apply if the size policy of the form has not been explicitly set
        GridCell existingCell = GridCell.getExisting(pChild);
        Container c = ((JComponent)pCurrentParent).getTopLevelAncestor();
        if (existingCell == null) {
          // The child must have been on a window. If the old window was resizable, the
          // default layout policy = SP_EXPLICIT and a default size of 4000x3000 mils
          // which is 384x288 pixels. If the window was not resizable the default size
          // policy is Natural. In both cases, the location is (0, 0) after compensating for insets
          if (c instanceof JFrame && (!((JFrame)c).isUndecorated()) && ((JFrame)c).isResizable()) {
              GridField.getConstraints(pChild).setWidthPolicy(Constants.SP_EXPLICIT);
              GridField.getConstraints(pChild).setHeightPolicy(Constants.SP_EXPLICIT);
              pChild.setSize(384, 288);
          }
          else {
              GridField.getConstraints(pChild).setWidthPolicy(Constants.SP_NATURAL);
              GridField.getConstraints(pChild).setHeightPolicy(Constants.SP_NATURAL);
          }
        }
        else {
          if (existingCell.getWidthPolicy() == Constants.SP_TO_PARENT || existingCell.getHeightPolicy() == Constants.SP_TO_PARENT) {
            pChild.setMinimumSize(null);
            pChild.setPreferredSize(null);
            pChild.setSize(0, 0);
          }
        }
       
        // Copy all the UsageStates from the child to the new parent window.  CraigM: 24/04/2008
        // TF:20/04/2009:DET-95:Testing in Forte showed that the usage maps of the child are not related to
        // the usage maps of the parent.
//        JFrame oldWindow = UIutils.getWindowForComponent(pCurrentParent);
//        JFrame newWindow = UIutils.getWindowForComponent(pNewParent);
//        if (newWindow != null){ //PM:28/4/08 added null check
//          Parent.copyUsageMap(oldWindow, newWindow);
//        }
        pChild.setName(c.getClass().getName().concat(".").concat(pChild.getName()));
        pChild.setLocation(0, 0);
        // We also need to reset the minimum size, which can be set on a windowformlayout
        // but should not be set on a panel, to force it to be recalculated.
        pChild.setMinimumSize(null);
        pChild.invalidate();
    }

    /**
     * Reparent the child component into the specified container
     *
     * @param pParent -
     *            the new parent. If this is null, the child is removed from
     *            this container
     * @param pChild -
     *            the child to re-parent. This parameter cannot be null
     */
    public static void setComponentParent(JComponent pChild, Container pParent, boolean preventRedraw) {
        if (pChild == null) {
            UsageException errorVar = new UsageException("pChild should not be null");
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }

        Container oldParent = pChild.getParent();

        if (pParent == null) {
            // TF:13/8/07:Setting the parent widget to null didn't remove this child from the parent
            Container parent = pChild.getParent();
            if (parent != null) {
                parent.remove(pChild);
                if (parent.getParent() != null) {
                    parent.getParent().invalidate();
                }
                // TF:25/9/07:If we were a child of a window, remove the window form layout
                if (pChild.getLayout() instanceof WindowFormLayout) {
                    moveChildOffWindow(pChild, oldParent, pParent);
                }
            }
            return;
        }
        if (pChild.isAncestorOf(pParent)) {
            return;
        }
        if (pChild.getParent() == pParent)
            return;

        // TF:21/11/07:If the child has the default button for the panel, and
        // the new child doesn't have a default button, set the old default
        // button to be the new default button.
        JButton defaultButton = null;
        JRootPane root = pChild.getRootPane();
        if (root != null && root.getDefaultButton() != null &&
                (pChild == root.getDefaultButton() || pChild.isAncestorOf(root.getDefaultButton()))) {

            defaultButton = root.getDefaultButton();
        }

        if (oldParent != null) {
            oldParent.remove(pChild);
            if (oldParent.getParent() != null)
                oldParent.getParent().validate();
        }

        // If we're a window form and we're being nested onto a normal window,
        // we need to change the layout manager to be a panel layout manager
        if (pChild.getLayout() instanceof WindowFormLayout) {
            // TF:25/9/07:Moved this logic into a common method
            moveChildOffWindow(pChild, oldParent, pParent);
        }

        /*
         * special case for split panes
         * They need a constraining but we don't know which one so we assume
         * JSplitPane.RIGHT
         */
        if (pParent instanceof JSplitPane){
            JSplitPane split = (JSplitPane)pParent;
            split.add(pChild, JSplitPane.RIGHT);
            return;
        }

        if (pParent instanceof JScrollPane){
            ((JScrollPane)pParent).setViewportView(pChild);
            if (defaultButton != null && ((JScrollPane)pParent).getRootPane().getDefaultButton() == null) {
                ((JScrollPane)pParent).getRootPane().setDefaultButton(defaultButton);
            }
            return;
        }
        /*
         * if we dont have a grid field or a gridbaglayout just add the component
         */
        if (!((pParent.getLayout() instanceof GridFieldLayout) ||
                (pParent.getLayout() instanceof GridBagLayout))) {

          // TF:Mar 11, 2010:Refactored this code to include the call to setComponentZOrder
          // in the parent.add method for efficiency
//            pParent.add(pChild);
//            try {
//                if (!(pParent instanceof JMenuBar)) {
//                    pParent.setComponentZOrder(pChild, 0);
//                }
//            } catch (IllegalArgumentException ex) {
//            }
          if (!(pParent instanceof JMenuBar)) {
            pParent.add(pChild, 0);
          }
          else {
            pParent.add(pChild);
          }
            if (defaultButton != null && pChild.getRootPane().getDefaultButton() == null) {
              pChild.getRootPane().setDefaultButton(defaultButton);
            }
            //            pParent.repaint();
            if (!preventRedraw) {
              ((JComponent)pParent).doLayout();
            }
            return;
        }
        if (pParent instanceof GridField) {
            GridField gf = (GridField) pParent;
            gf.add(pChild);
        } else {
            pParent.add(pChild);
            pParent.setComponentZOrder(pChild, 0);
        }
        //PM:28/4/08 added additional null check
        if (defaultButton != null && pChild.getRootPane() != null && pChild.getRootPane().getDefaultButton() == null) {
            pChild.getRootPane().setDefaultButton(defaultButton);
        }
        pParent.validate();
    }

    @Override
    public String toString() {
        if (this.dnChild != null) {
            return "Parent[" + this.dnChild.toString() + "] to ["
                    + this.dnParent + "]";
        } else {
            return "Parent[" + this._component.toString() + "] to ["
                    + this.parent + "]";
        }
    }

    private static JComponent getFromFromFrame(JFrame frame){
        JPanel form = UIutils.getForm(frame);

        return form;
    }
}
TOP

Related Classes of DisplayProject.actions.Parent

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.