From 8fce2f820c15938e973238e553d8ad493eab6d7c Mon Sep 17 00:00:00 2001 From: Tor Norbye Date: Mon, 13 Jun 2011 17:27:48 -0700 Subject: [PATCH] Add two more layout actions for linear layouts Add two more actions: (1) Clear All Weights: This removes all the layout weights in a layout, and converts and 0-sized views to wrap_content. (2) Assign All Weight: This adds all the weight in the layout to the selected view(s) and removes it from the remaining views. Change-Id: Id2a27299d99f77ef4056b7e1745373d52a9331f7 --- .../ide/common/layout/LinearLayoutRule.java | 111 +++++++++++++-------- .../com/android/ide/common/layout/allweight.png | Bin 0 -> 460 bytes .../com/android/ide/common/layout/clearweights.png | Bin 0 -> 573 bytes 3 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/allweight.png create mode 100644 eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/clearweights.png diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java index 47c54998b..00b8e5301 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java @@ -67,6 +67,8 @@ public class LinearLayoutRule extends BaseLayoutRule { 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$ @@ -78,6 +80,10 @@ public class LinearLayoutRule extends BaseLayoutRule { 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. @@ -184,7 +190,8 @@ public class LinearLayoutRule extends BaseLayoutRule { 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) { @@ -198,45 +205,16 @@ public class LinearLayoutRule extends BaseLayoutRule { 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); } } }); @@ -245,8 +223,61 @@ public class LinearLayoutRule extends BaseLayoutRule { 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); + } } } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/allweight.png b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/allweight.png new file mode 100644 index 0000000000000000000000000000000000000000..506c66320feaa77b80924c77a0842a473721b7ba GIT binary patch literal 460 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|*pj^6T^Kw; z@XqUVHb4>10*}aI1_o|n5N2eUHAey{$X?><>&kwcjgNs@xJ2>VA)t_KW=KSdbAE1a zYF-JD%fR4Vl$uzQnxasiS(2gP?&%wlqL<1Jv`*R6#WBR<^wCMSMTZ=CTIG%BsKqph zELpA)8gsFLrOzcdc(!CvVPk=U=li8M&%76T`RPwXeJ! zy*}2+CA?#3xci&0_P#xX;K~cdO}m{_T15(W_RFX@)Ff&~v2{2-nYp|*&!1s-iC(8? zoq|zDRlb}**O?$Em&FerGR{8t?iy28BAY7f+D^^&p}&$_j26$ler`&`ssqm$gFknz zOfg!ob7}qhg1>vrgz~rFexo#%E&h8ocg5`-2hO~{udk&Rt?GO#>8f;RV^!qUy%|0| u3A3WJrg?Pj@>%5IX1bNB@3`{6w>8Wc8GP)rQkFjgg`=mdpUXO@geCyN0j*L1 literal 0 HcmV?d00001 diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/clearweights.png b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/clearweights.png new file mode 100644 index 0000000000000000000000000000000000000000..ad27c174d3b87be32fc64deb44c2dff04b5f4549 GIT binary patch literal 573 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|*pj^6T^Kw; z@XqUVHb4>10*}aI1_o|n5N2eUHAey{$X?><>&kwcjgNs}Q*MSs4NyonGbEzKIX^cy zHLnE7WngeFN=+Bh zGAAkJ$~m;gcCkw9-!gT3a4aGr{PxNqRf8i5$%n5Qa;i*Sd+rjq4ogt0!@L6zLMoe# zr;AhvhJ{U4V{R{=Y5afA|M`~Ef9vl5bdS~%lNLNsV;5h4Sho218H4f|-r~b&C6;J1 zT>NFB-uJl7e)d^|;yb*XxfX0xuV=5bwRLsvd^|(v^p3rAC#FhzPErv1QCQmAArP{Q zVO5()>5sQ>Y#AbFEfVr56K1FrV`WfkIwhpAih;%WaqPU$e?Lv~Tz|cYWyY5(hCOyR zE-rGx97-Ox%TjN0W~bw7uRsx*`7+zrM|dxc++SP4@Zjk`JBI!HYTo^jXUvF@yI22*YE7&SDV? zV+^sh-+$n)#qFC1*Jj;ZRV!+|{(pn<&0B9W1Q?%Go~z$%-=iQUBxhUi0E#$IS3j3^ HP6