diff --git a/src/snap/view/ParentView.java b/src/snap/view/ParentView.java index 4f7d1efb..811bd73d 100644 --- a/src/snap/view/ParentView.java +++ b/src/snap/view/ParentView.java @@ -29,7 +29,11 @@ public class ParentView extends View { // Whether this view is performing layout private boolean _inLayout, _inLayoutDeep; - + + // PropChange Listener for Child changes to propagate changes when there is DeepChangeListener + private PropChangeListener _childPCL; + private DeepChangeListener _childDCL; + // Constants for properties public static final String Child_Prop = "Child"; @@ -67,7 +71,10 @@ public ParentView() /** * Adds the given child to the end of this view's children list. */ - protected void addChild(View aChild) { addChild(aChild, getChildCount()); } + protected void addChild(View aChild) + { + addChild(aChild, getChildCount()); + } /** * Adds the given child to this view's children list at the given index. @@ -75,7 +82,7 @@ public ParentView() protected void addChild(View aChild, int anIndex) { // If child already has parent, remove from parent - if (aChild.getParent()!=null) + if (aChild.getParent() != null) aChild.getParent().removeChild(aChild); // Add child to children list and set child's parent to this view @@ -89,7 +96,7 @@ protected void addChild(View aChild, int anIndex) setNeedsLayoutDeep(true); // If this view has child prop listeners, add to this child as well - if (_childPCL!=null) { + if (_childPCL != null) { aChild.addPropChangeListener(_childPCL); aChild.addDeepChangeListener(_childDCL); } @@ -108,7 +115,7 @@ protected View removeChild(int anIndex) child.setParent(null); // If this view has child prop listeners, clear from child - if (_childPCL!=null) { + if (_childPCL != null) { child.removePropChangeListener(_childPCL); child.removeDeepChangeListener(_childDCL); } @@ -129,7 +136,8 @@ protected View removeChild(int anIndex) protected int removeChild(View aChild) { int index = indexOfChild(aChild); - if (index>=0) removeChild(index); + if (index >= 0) + removeChild(index); return index; } @@ -138,7 +146,7 @@ protected int removeChild(View aChild) */ protected void removeChildren() { - for (int i=getChildCount()-1; i>=0; i--) + for (int i = getChildCount() - 1; i >= 0; i--) removeChild(i); } @@ -148,7 +156,8 @@ protected void removeChildren() protected void setChildren(View ... theChildren) { removeChildren(); - for (View c : theChildren) addChild(c); + for (View c : theChildren) + addChild(c); } /** @@ -159,10 +168,10 @@ public View getChild(String aName) for (View child : getChildren()) { if (aName.equals(child.getName())) return child; - if (child instanceof ParentView && child.getOwner()==getOwner()) { - ParentView par = (ParentView)child; + if (child instanceof ParentView && child.getOwner() == getOwner()) { + ParentView par = (ParentView) child; View n = par.getChild(aName); - if (n!=null) + if (n != null) return n; } } @@ -172,7 +181,10 @@ public View getChild(String aName) /** * Returns the index of the given child in this view's children list. */ - public int indexOfChild(View aChild) { return _children.indexOf(aChild); } + public int indexOfChild(View aChild) + { + return _children.indexOf(aChild); + } /** * Returns the last child of this view. @@ -227,11 +239,20 @@ public E[] getChildrenForClass(Class aClass) */ protected View getFocusNext(View aChild) { - int ind = aChild!=null? indexOfChild(aChild) : -1; - for (int i=ind+1,iMax=getChildCount();i=0;i--) { View child = getChild(i); - if (child.isFocusable()) return child; - ParentView par = child instanceof ParentView? (ParentView)child : null; if (par==null) continue; - if (par.getFocusPrev(null)!=null) - return par.getFocusPrev(null); + int ind = aChild != null ? indexOfChild(aChild) : getChildCount(); + for (int i = ind - 1; i >= 0; i--) { + + View child = getChild(i); + if (child.isFocusable()) + return child; + + ParentView par = child instanceof ParentView ? (ParentView) child : null; + if (par == null) + continue; + + View focusPrev = par.getFocusPrev(null); + if (focusPrev != null) + return focusPrev; } return getFocusPrev(); @@ -259,13 +288,14 @@ protected View getFocusPrev(View aChild) public void setFont(Font aFont) { // If no change and not null, just return - if (SnapUtils.equals(aFont, _font) && aFont!=null) return; + if (SnapUtils.equals(aFont, _font) && aFont != null) return; // Do normal version super.setFont(aFont); // Let all children that inherrit font know - for (int i=0,iMax=getChildCount();i0 && getHeight()>0) { + if (getWidth() > 0 && getHeight() > 0) { layoutImpl(); layoutFloatingViews(); } @@ -453,16 +490,18 @@ protected void layoutImpl() { } protected void layoutFloatingViews() { // If no floating, just return - if (getChildrenManaged().length==getChildCount()) return; + if (getChildrenManaged().length == getChildCount()) return; double viewW = getWidth(); double viewH = getHeight(); // Layout floating (unmanaged + leaning) children - for (View child : getChildren()) { if (child.isManaged()) continue; + for (View child : getChildren()) { + + if (child.isManaged()) continue; // Get child lean, grow, margin and current bounds HPos leanX = child.getLeanX(); - VPos leanY = child.getLeanY(); if (leanX==null && leanY==null) continue; + VPos leanY = child.getLeanY(); if (leanX == null && leanY == null) continue; Insets marg = child.getMargin(); boolean growX = child.isGrowWidth(); boolean growY = child.isGrowHeight(); @@ -472,14 +511,14 @@ protected void layoutFloatingViews() double childH = child.getHeight(); // Handle LeanX: If grow, make width fill parent (minus margin). Set X for lean, width, margin. - if (leanX!=null) { + if (leanX != null) { if (growX) childW = viewW - marg.getWidth(); childX = marg.left + (viewW - marg.getWidth() - childW) * ViewUtils.getAlignX(leanX); } // Handle LeanY: If grow, make height fill parent (minus margin). Set Y for lean, height, margin. - if (leanY!=null) { + if (leanY != null) { if (growY) childH = viewH - marg.getHeight(); childY = marg.top + (viewH - marg.getHeight() - childH) * ViewUtils.getAlignY(leanY); @@ -499,13 +538,17 @@ public void layoutDeep() _inLayoutDeep = true; // Do layout - if (_needsLayout) layout(); + if (_needsLayout) + layout(); // Do layout deep (several times, if necessary) - for (int i=0;_needsLayoutDeep;i++) { + for (int i = 0; _needsLayoutDeep; i++) { _needsLayoutDeep = false; layoutDeepImpl(); - if (i==5) { System.err.println("ParentView.layoutDeep: Too many calls to relayout inside layout"); break; } + if (i == 5) { + System.err.println("ParentView.layoutDeep: Too many calls to relayout inside layout"); + break; + } } // Clear flags @@ -518,9 +561,11 @@ public void layoutDeep() protected void layoutDeepImpl() { for (View child : getChildren()) - if (child instanceof ParentView) { ParentView par = (ParentView)child; + if (child instanceof ParentView) { + ParentView par = (ParentView)child; if (par._needsLayout || par._needsLayoutDeep) - par.layoutDeep(); } + par.layoutDeep(); + } } /** @@ -530,10 +575,11 @@ protected void layoutDeepImpl() public void processPropChange(PropChange aPC, Object oldVal, Object newVal) { String pname = aPC.getPropName(); - if (pname==Child_Prop) { - int ind = aPC.getIndex(); - if (newVal!=null) addChild((View)newVal, ind); - else removeChild(ind); + if (pname == Child_Prop) { + int index = aPC.getIndex(); + if (newVal != null) + addChild((View) newVal, index); + else removeChild(index); } // Do normal version @@ -550,7 +596,7 @@ public void addDeepChangeListener(DeepChangeListener aDCL) super.addDeepChangeListener(aDCL); // If child listeners not yet set, create/add for children - if (_childPCL==null) { + if (_childPCL == null) { _childPCL = pc -> childDidPropChange(pc); _childDCL = (lsnr,pc) -> childDidDeepChange(lsnr,pc); for (View child : getChildren()) { @@ -570,7 +616,7 @@ public void removeDeepChangeListener(DeepChangeListener aDCL) super.removeDeepChangeListener(aDCL); // If no more deep listeners, remove - if (!_pcs.hasDeepListener() && _childPCL!=null) { + if (!_pcs.hasDeepListener() && _childPCL != null) { for (View child : getChildren()) { child.removePropChangeListener(_childPCL); child.removeDeepChangeListener(_childDCL); @@ -579,19 +625,21 @@ public void removeDeepChangeListener(DeepChangeListener aDCL) } } - // PropChange Listener for Child changes to propogate changes when there is DeepChangeListener - PropChangeListener _childPCL; - DeepChangeListener _childDCL; - /** * Property change listener implementation to forward changes on to deep listeners. */ - protected void childDidPropChange(PropChange aPC) { _pcs.fireDeepChange(this, aPC); } + protected void childDidPropChange(PropChange aPC) + { + _pcs.fireDeepChange(this, aPC); + } /** * Deep property change listener implementation to forward to this View's deep listeners. */ - protected void childDidDeepChange(Object aLsnr, PropChange aPC) { _pcs.fireDeepChange(aLsnr, aPC); } + protected void childDidDeepChange(Object aLsnr, PropChange aPC) + { + _pcs.fireDeepChange(aLsnr, aPC); + } /** * Called when ViewTheme changes. @@ -627,7 +675,7 @@ protected XMLElement toXMLView(XMLArchiver anArchiver) protected void toXMLChildren(XMLArchiver anArchiver, XMLElement anElement) { if (this instanceof ViewHost) - ViewHost.toXMLGuests((ViewHost)this, anArchiver, anElement); + ViewHost.toXMLGuests((ViewHost) this, anArchiver, anElement); } /** @@ -635,9 +683,8 @@ protected void toXMLChildren(XMLArchiver anArchiver, XMLElement anElement) */ public View fromXML(XMLArchiver anArchiver, XMLElement anElement) { - // Unarchive shape and children and return - fromXMLView(anArchiver, anElement); // Unarchive shape - fromXMLChildren(anArchiver, anElement); // Unarchive children + fromXMLView(anArchiver, anElement); + fromXMLChildren(anArchiver, anElement); return this; } @@ -655,6 +702,6 @@ protected void fromXMLView(XMLArchiver anArchiver, XMLElement anElement) protected void fromXMLChildren(XMLArchiver anArchiver, XMLElement anElement) { if (this instanceof ViewHost) - ViewHost.fromXMLGuests((ViewHost)this, anArchiver, anElement); + ViewHost.fromXMLGuests((ViewHost) this, anArchiver, anElement); } } \ No newline at end of file