private static final String ACTION_WEIGHT = "_weight"; //$NON-NLS-1$
private static final String ACTION_DISTRIBUTE = "_distribute"; //$NON-NLS-1$
private static final String ACTION_BASELINE = "_baseline"; //$NON-NLS-1$
+ private static final String ACTION_CLEAR = "_clear"; //$NON-NLS-1$
+ private static final String ACTION_DOMINATE = "_dominate"; //$NON-NLS-1$
private static final URL ICON_HORIZONTAL =
LinearLayoutRule.class.getResource("hlinear.png"); //$NON-NLS-1$
LinearLayoutRule.class.getResource("distribute.png"); //$NON-NLS-1$
private static final URL ICON_BASELINE =
LinearLayoutRule.class.getResource("baseline.png"); //$NON-NLS-1$
+ private static final URL ICON_CLEAR_WEIGHTS =
+ LinearLayoutRule.class.getResource("clearweights.png"); //$NON-NLS-1$
+ private static final URL ICON_DOMINATE =
+ LinearLayoutRule.class.getResource("allweight.png"); //$NON-NLS-1$
/**
* Add an explicit Orientation toggle to the context menu.
final Boolean newValue) {
parentNode.editXml("Change Weight", new INodeHandler() {
public void handle(INode n) {
- if (action.getId().equals(ACTION_WEIGHT)) {
+ String id = action.getId();
+ if (id.equals(ACTION_WEIGHT)) {
String weight =
children.get(0).getStringAttr(ANDROID_URI, ATTR_LAYOUT_WEIGHT);
if (weight == null || weight.length() == 0) {
ATTR_LAYOUT_WEIGHT, weight);
}
}
- } else if (action.getId().equals(ACTION_DISTRIBUTE)) {
- // Any XML to get weight sum?
- String weightSum = parentNode.getStringAttr(ANDROID_URI,
- ATTR_WEIGHT_SUM);
- double sum = -1.0;
- if (weightSum != null) {
- // Distribute
- try {
- sum = Double.parseDouble(weightSum);
- } catch (NumberFormatException nfe) {
- // Just keep using the default
- }
- }
- INode[] targets = parentNode.getChildren();
- int numTargets = targets.length;
- double share;
- if (sum <= 0.0) {
- // The sum will be computed from the children, so just
- // use arbitrary amount
- share = 1.0;
- } else {
- share = sum / numTargets;
- }
- String value = formatFloatAttribute((float) share);
- String sizeAttribute = isVertical(parentNode) ?
- ATTR_LAYOUT_HEIGHT : ATTR_LAYOUT_WIDTH;
- for (INode target : targets) {
- target.setAttribute(ANDROID_URI, ATTR_LAYOUT_WEIGHT, value);
- // Also set the width/height to 0dp to ensure actual equal
- // size (without this, only the remaining space is
- // distributed)
- if (VALUE_WRAP_CONTENT.equals(
- target.getStringAttr(ANDROID_URI, sizeAttribute))) {
- target.setAttribute(ANDROID_URI,
- sizeAttribute, VALUE_ZERO_DP);
- }
- }
+ } else if (id.equals(ACTION_DISTRIBUTE)) {
+ distributeWeights(parentNode, parentNode.getChildren());
+ } else if (id.equals(ACTION_CLEAR)) {
+ clearWeights(parentNode);
+ } else if (id.equals(ACTION_CLEAR) || id.equals(ACTION_DOMINATE)) {
+ clearWeights(parentNode);
+ distributeWeights(parentNode,
+ children.toArray(new INode[children.size()]));
} else {
- assert action.getId().equals(ACTION_BASELINE);
+ assert id.equals(ACTION_BASELINE);
}
}
});
actions.add(MenuAction.createSeparator(50));
actions.add(MenuAction.createAction(ACTION_DISTRIBUTE, "Distribute Weights Evenly",
null, actionCallback, ICON_DISTRIBUTE, 60));
+ actions.add(MenuAction.createAction(ACTION_DOMINATE, "Assign All Weight",
+ null, actionCallback, ICON_DOMINATE, 70));
actions.add(MenuAction.createAction(ACTION_WEIGHT, "Change Layout Weight", null,
- actionCallback, ICON_WEIGHTS, 70));
+ actionCallback, ICON_WEIGHTS, 80));
+ actions.add(MenuAction.createAction(ACTION_CLEAR, "Clear All Weights",
+ null, actionCallback, ICON_CLEAR_WEIGHTS, 90));
+ }
+ }
+
+ private void distributeWeights(INode parentNode, INode[] targets) {
+ // Any XML to get weight sum?
+ String weightSum = parentNode.getStringAttr(ANDROID_URI,
+ ATTR_WEIGHT_SUM);
+ double sum = -1.0;
+ if (weightSum != null) {
+ // Distribute
+ try {
+ sum = Double.parseDouble(weightSum);
+ } catch (NumberFormatException nfe) {
+ // Just keep using the default
+ }
+ }
+ int numTargets = targets.length;
+ double share;
+ if (sum <= 0.0) {
+ // The sum will be computed from the children, so just
+ // use arbitrary amount
+ share = 1.0;
+ } else {
+ share = sum / numTargets;
+ }
+ String value = formatFloatAttribute((float) share);
+ String sizeAttribute = isVertical(parentNode) ?
+ ATTR_LAYOUT_HEIGHT : ATTR_LAYOUT_WIDTH;
+ for (INode target : targets) {
+ target.setAttribute(ANDROID_URI, ATTR_LAYOUT_WEIGHT, value);
+ // Also set the width/height to 0dp to ensure actual equal
+ // size (without this, only the remaining space is
+ // distributed)
+ if (VALUE_WRAP_CONTENT.equals(target.getStringAttr(ANDROID_URI, sizeAttribute))) {
+ target.setAttribute(ANDROID_URI, sizeAttribute, VALUE_ZERO_DP);
+ }
+ }
+ }
+
+ private void clearWeights(INode parentNode) {
+ // Clear attributes
+ String sizeAttribute = isVertical(parentNode)
+ ? ATTR_LAYOUT_HEIGHT : ATTR_LAYOUT_WIDTH;
+ for (INode target : parentNode.getChildren()) {
+ target.setAttribute(ANDROID_URI, ATTR_LAYOUT_WEIGHT, null);
+ String size = target.getStringAttr(ANDROID_URI, sizeAttribute);
+ if (size != null && size.startsWith("0")) { //$NON-NLS-1$
+ target.setAttribute(ANDROID_URI, sizeAttribute, VALUE_WRAP_CONTENT);
+ }
}
}