Package DisplayProject.actions

Source Code of DisplayProject.actions.WidthPartner

/*
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.util.HashSet;
import java.util.Set;

import javax.swing.JComponent;

import DisplayProject.Constants;
import DisplayProject.GridCell;
import DisplayProject.LayoutManagerHelper;

/**
* Gets and sets the width partner on the target JComponent.
* Components in a Width partnership resize to the minimum size of the largest partner.
* <p>
* Invoking this method will remove the existing component from any existing width partnership,
* insert it into the chain of width partnerships in the new ring, and set the width policy
* for all components in the new ring to SP_TO_PARTNER. In addition, it will re-validate the
* container objects that need to be validated.
* <p>
* This has been revamped to allow the layout managers to take care of these partnerships.
*/
public class WidthPartner extends PendingAction {

    private JComponent partner;
    public WidthPartner(Component pComponent, JComponent partner) {
        super(pComponent);
        this.partner = partner;
    }
   
    public Component getPartner() {
        return this.partner;
    }
   
    public void performAction() {
      // TF:11/05/2009:Changed this to rely on the layout managers
      Set<Container> containersToValidate = new HashSet<Container>();
      JComponent thisComponent = (JComponent)this._component;
      GridCell cell = GridCell.get(thisComponent);
     
      // First, remove this component from it's old chain, if there is one.
      JComponent currentPartner = LayoutManagerHelper.getWidthPartner(thisComponent);
      if (currentPartner != null) {
        if (currentPartner == this.partner) {
          // do nothing
          return;
        }
        else {
          JComponent lastInChain = null;
          for (JComponent comp = currentPartner; comp != null && comp != thisComponent; comp = LayoutManagerHelper.getWidthPartner(comp)) {
            lastInChain = comp;
            // Also add the parent of the component to the containers needing laying out
            Container parent = comp.getParent();
            if (parent != null) {
              containersToValidate.add(parent);
            }
          }
          if (lastInChain != null) {
            // We need to set the partner of the last in the chain to the current partner, skipping this component
            LayoutManagerHelper.setWidthPartner(lastInChain, currentPartner);
          }
        }
      }
     
      // We now need to run around the partnership chain, possibly inserting this element in the chain,
      // setting all the size policies to WIDTH_PARTNER and marking all the containers to be validated
      cell.setWidthPolicy(Constants.SP_TO_PARTNER);
      LayoutManagerHelper.setWidthPartner(thisComponent, this.partner);
    JComponent lastInChain = null;
    for (JComponent comp = this.partner; comp != null && comp != thisComponent; comp = LayoutManagerHelper.getWidthPartner(comp)) {
      lastInChain = comp;
      // Also add the parent of the component to the containers needing laying out
      Container parent = comp.getParent();
      if (parent != null) {
        containersToValidate.add(parent);
      }
      // Force the width policy
      GridCell.get(comp).setWidthPolicy(Constants.SP_TO_PARTNER);
    }
    if (lastInChain != null) {
      // We need to set the partner of the last in the chain to the current partner, skipping this component
      LayoutManagerHelper.setWidthPartner(lastInChain, thisComponent);
    }
   
    // Now layout all the parents
    for (Container comp : containersToValidate) {
      comp.invalidate();
      comp.validate();
    }
    }

    public static void set(Component comp, JComponent partner){
        ActionMgr.addAction(new WidthPartner(comp, partner));
    }

    public static Component get(Component comp){
        WidthPartner action = ActionMgr.getAction(comp, WidthPartner.class);
        if (action != null) {
            return action.getPartner();
        }
        return LayoutManagerHelper.getWidthPartner((JComponent)comp);
    }
}
TOP

Related Classes of DisplayProject.actions.WidthPartner

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.