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 height partner on the target JComponent.
* Components in a Height partnership resize to the minimum size of the largest partner.
* <p>
* Invoking this method will remove the existing component from any existing height partnership,
* insert it into the chain of height partnerships in the new ring, and set the height 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 HeightPartner extends PendingAction {
private JComponent partner;
public HeightPartner(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.getHeightPartner(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.getHeightPartner(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.setHeightPartner(lastInChain, currentPartner);
}
}
}
// We now need to run around the partnership chain, possibly inserting this element in the chain,
// setting all the size policies to TO_PARTNER and marking all the containers to be validated
cell.setHeightPolicy(Constants.SP_TO_PARTNER);
LayoutManagerHelper.setHeightPartner(thisComponent, this.partner);
JComponent lastInChain = null;
for (JComponent comp = this.partner; comp != null && comp != thisComponent; comp = LayoutManagerHelper.getHeightPartner(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 height policy
GridCell.get(comp).setHeightPolicy(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.setHeightPartner(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 HeightPartner(comp, partner));
}
public static Component get(Component comp){
HeightPartner action = ActionMgr.getAction(comp, HeightPartner.class);
if (action != null) {
return action.getPartner();
}
return LayoutManagerHelper.getHeightPartner((JComponent)comp);
}
}