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);
}
}