Skip to content

Commit

Permalink
Fixed so that deleted nodes are not cached, fixed highlight bug, fixe…
Browse files Browse the repository at this point in the history
…d so that position are not relocated
  • Loading branch information
Ruffy committed Feb 17, 2013
1 parent 2904da0 commit 93e1642
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 45 deletions.
32 changes: 21 additions & 11 deletions src/se/jc/library/util/SuspendableObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package se.jc.library.util;

import java.util.BitSet;
import java.util.Observable;

/**
Expand All @@ -14,36 +15,45 @@ public class SuspendableObservable extends Observable {

private boolean _needToNotify;
private boolean _suspended;
private BitSet _updatedFields;

@Override
public void notifyObservers() {
notifyObservers(null);
}

@Override
public void notifyObservers(Object data) {

if (!_suspended) {
super.notifyObservers(data);
super.notifyObservers(getUpdatedFields());
getUpdatedFields().clear();
_needToNotify = false;
} else {
_needToNotify = true;
}
}

public void setChangeAndNotifyObservers(Object data) {
public void notifyObservers(int changedData) {
getUpdatedFields().set(changedData);
notifyObservers();
}

public void setChangeAndNotifyObservers(int changeBit) {
setChanged();
notifyObservers(data);
notifyObservers(changeBit);
}

public void suspendBinding() {
_suspended = true;
}

public void resumeBinding(Object data) {
public void resumeBinding() {
_suspended = false;
if (_needToNotify) {
notifyObservers(data);
notifyObservers();
}
}

protected BitSet getUpdatedFields() {
if(_updatedFields == null)
{
_updatedFields = new BitSet();
}
return _updatedFields;
}
}
37 changes: 28 additions & 9 deletions src/se/jc/mindmap/model/MindMapItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
public class MindMapItem extends SuspendableObservable {

public static final int UPDATED_TEXT = 1;
public static final int UPDATED_CHILDREN = 2;
public static final int UPDATED_DELETED = 3;

private MindMapItem _parent;
private String _text;
private List<MindMapItem> _children;
Expand Down Expand Up @@ -44,20 +48,20 @@ public int getChildIndex(MindMapItem mindMapItem) {

public void removeChild(MindMapItem childToRemove) {
_children.remove(childToRemove);
setChangeAndNotifyObservers(this);
setChangeAndNotifyObservers(UPDATED_CHILDREN);
}

public void addChild(int index, MindMapItem childToAdd) {
_children.add(index, childToAdd);
childToAdd.setParent(this);
setChangeAndNotifyObservers(this);
setChangeAndNotifyObservers(UPDATED_CHILDREN);
}

public void addChild(MindMapItem childToAdd) {

_children.add(childToAdd);
childToAdd.setParent(this);
setChangeAndNotifyObservers(this);
setChangeAndNotifyObservers(UPDATED_CHILDREN);
}

public void addChild(String childText) {
Expand All @@ -70,7 +74,6 @@ public void addSibling(String childText) {
MindMapItem childToAdd = new MindMapItem(childText);
int currentIndex = _parent.getChildIndex(this);
_parent.addChild(currentIndex + 1, childToAdd);
setChangeAndNotifyObservers(this);
}
}

Expand All @@ -86,7 +89,7 @@ public void setChildren(List<MindMapItem> childrenToSet) {
_children.add(child);
}
}
setChangeAndNotifyObservers(this);
setChangeAndNotifyObservers(UPDATED_CHILDREN);
}

public List<MindMapItem> copyChildren() {
Expand Down Expand Up @@ -124,7 +127,21 @@ public String getText() {
public void setText(String text) {
this._text = text;

setChangeAndNotifyObservers(this);
setChangeAndNotifyObservers(UPDATED_TEXT);
}

public void notifyDelete()
{
setChangeAndNotifyObservers(UPDATED_DELETED);
}

public void notifyDeleteWithChildren()
{
for(MindMapItem child : getChildren())
{
child.notifyDeleteWithChildren();
}
setChangeAndNotifyObservers(UPDATED_DELETED);
}

public void deleteOne() {
Expand All @@ -134,13 +151,15 @@ public void deleteOne() {
_parent.addChild(0, child);
}
_parent.removeChild(this);
_parent.resumeBinding(_parent);
notifyDelete();
_parent.resumeBinding();
}
}

public void deleteBranch() {
if (_parent != null) {
_parent.removeChild(this);
notifyDeleteWithChildren();
}
}

Expand All @@ -167,8 +186,8 @@ public static void SwapItems(MindMapItem itemOne, MindMapItem itemTwo) {
} else {
itemTwo.clearParent();
}
itemOne.resumeBinding(itemOne);
itemTwo.resumeBinding(itemTwo);
itemOne.resumeBinding();
itemTwo.resumeBinding();
}

@Override
Expand Down
53 changes: 44 additions & 9 deletions src/se/jc/mindmap/view/MindMapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package se.jc.mindmap.view;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.graphics.PointF;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class MindMapView extends ZoomView {
private OnSelectedBulletChangeListener _selectedBulletChangeListener;
private MindMapItemActionRequestListener _mindMapActionListener;
private BulletManager _bulletManager;
private boolean _rootPositionInitialized;

public MindMapView(Context context, AttributeSet attrs) {
super(context, attrs);
Expand All @@ -45,6 +47,8 @@ public MindMapView(Context context, AttributeSet attrs) {

Bullet rootBullet = getBulletManager().getOrCreateBullet(_mindMapRoot);
setBulletRoot(rootBullet);


}

public MindMapView(Context context, AttributeSet attrs, int defStyle) {
Expand All @@ -60,21 +64,22 @@ public boolean onLongClick(View v) {
});
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (!_rootPositionInitialized) {
initializeRootPosition();
_rootPositionInitialized = true;
}
}

@Override
protected void onDraw(Canvas canvas) {
prepareCanvasZoom(canvas);

int width = getWidth();
int height = getHeight();
Rect itemBounds = getBulletRoot().getItemBounds();
int x = (width - itemBounds.width()) / 2;
int y = (height - itemBounds.height()) / 2;

getBulletRoot().setLayoutPosition(x, y);
getBulletRoot().updateLayout();

handleMovementAnimations(2000);

renderItemWithChildren(canvas, getBulletRoot());

Bullet moveBullet = getMoveBullet();
Expand All @@ -94,10 +99,25 @@ public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}
});

AnimatorSet animatorSet = new AnimatorSet();
for (ValueAnimator animator : allAnimators) {
animatorSet.play(animator);
}
animatorSet.addListener(new Animator.AnimatorListener() {
public void onAnimationStart(Animator animation) {
}

public void onAnimationEnd(Animator animation) {
invalidate();
}

public void onAnimationCancel(Animator animation) {
}

public void onAnimationRepeat(Animator animation) {
}
});
animatorSet.start();
}

Expand Down Expand Up @@ -300,15 +320,20 @@ private void setHoveredItem(Bullet hoveredItem) {

private void handleMoveItemHoverMove(PointF transformedPoint, Bullet moveBullet) {
Bullet foundBullet = getBulletAtCoordinate(getBulletRoot(), transformedPoint);
boolean hasLeftItem = getHoveredItem() != null && foundBullet != getHoveredItem();
if (hasLeftItem) {
getHoveredItem().hoverItemLeave(moveBullet);
}

boolean isHoveringOtherItem = foundBullet != null
&& foundBullet.getWrappedContent() != moveBullet.getWrappedContent();
if (isHoveringOtherItem) {
setHoveredItem(foundBullet);
foundBullet.hoverItem(moveBullet, transformedPoint);
} else if (getHoveredItem() != null) {
getHoveredItem().hoverItemLeave(moveBullet);
setHoveredItem(null);
}

}

private void updateMoveBulletPosition(Bullet moveBullet, PointF rawPoint) {
Expand Down Expand Up @@ -348,4 +373,14 @@ private BulletManager getBulletManager() {
}
return _bulletManager;
}

private void initializeRootPosition() {
int width = getWidth();
int height = getHeight();
Rect itemBounds = getBulletRoot().getItemBounds();
int x = (width - itemBounds.width()) / 2;
int y = (height - itemBounds.height()) / 2;

getBulletRoot().setLayoutPosition(x, y);
}
}
7 changes: 5 additions & 2 deletions src/se/jc/mindmap/view/component/Bullet.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ public void setSelected(boolean selected) {

private void renderChildConnection(Bullet child, Canvas canvasToRenderOn) {

Path path = getChildPath(child);
Path path = calculateBezierPath(child);
// Path path = getChildPath(child);
renderConnectionPath(path, canvasToRenderOn);
}

Expand Down Expand Up @@ -557,7 +558,9 @@ public void setWrappedContent(MindMapItem wrappedContent) {
}

private void clearCachedPaths() {
_cachedPaths.clear();
if (_cachedPaths.size() > 0) {
_cachedPaths.clear();
}
}

public void update(Observable observable, Object data) {
Expand Down
32 changes: 18 additions & 14 deletions src/se/jc/mindmap/view/component/BulletManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package se.jc.mindmap.view.component;

import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
Expand All @@ -18,21 +19,28 @@ public class BulletManager {

private Map<MindMapItem, Bullet> _bulletStore;
private Bullet _rootBullet;

public BulletManager() {
_bulletStore = new HashMap<MindMapItem, Bullet>();
}

public Bullet getOrCreateBullet(MindMapItem mindMapItem) {
if (!_bulletStore.containsKey(mindMapItem)) {
mindMapItem.addObserver(new Observer() {
public void update(Observable observable, Object data) {
reconstructBulletTree();
BitSet updateMap = (BitSet) data;
if (updateMap != null) {
if (updateMap.get(MindMapItem.UPDATED_CHILDREN)) {
reconstructBulletTree();
}
if (updateMap.get(MindMapItem.UPDATED_DELETED)) {
_bulletStore.remove(observable);
}
}
}
});
Bullet newBullet = Bullet.createBullet(mindMapItem);
for(MindMapItem itemChild : mindMapItem.getChildren())
{
for (MindMapItem itemChild : mindMapItem.getChildren()) {
Bullet childBullet = getOrCreateBullet(itemChild);
newBullet.getChildren().add(childBullet);
}
Expand All @@ -41,18 +49,14 @@ public void update(Observable observable, Object data) {
return _bulletStore.get(mindMapItem);
}

public void reconstructBulletTree()
{
public void reconstructBulletTree() {
reconstructBulletTree(getRootBullet());
}

public void reconstructBulletTree(Bullet bulletToReconstruct)
{
if(bulletToReconstruct != null)
{

public void reconstructBulletTree(Bullet bulletToReconstruct) {
if (bulletToReconstruct != null) {
bulletToReconstruct.getChildren().clear();
for(MindMapItem itemChild : bulletToReconstruct.getWrappedContent().getChildren())
{
for (MindMapItem itemChild : bulletToReconstruct.getWrappedContent().getChildren()) {
Bullet childBullet = getOrCreateBullet(itemChild);
bulletToReconstruct.getChildren().add(childBullet);
reconstructBulletTree(childBullet);
Expand Down

0 comments on commit 93e1642

Please sign in to comment.