Skip to content

Commit

Permalink
Support level-wide menu resource
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisnelson committed Dec 16, 2013
1 parent e5a75d6 commit ae87dff
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,40 @@ protected void onCreate(Bundle savedInstanceState) {

Tree tree = getTree();
Level root = tree.getRoot();
Level second = new Level(tree);
Level twoChild = new Level(tree);
Level threeChild = new Level(tree, R.menu.sample); //This one has a level-wide menu. Down the road we will be able to be more efficient and not constantly reinflate things

//Card One
Card one = new Card(this);
one.setText("Dead End");
root.add(new Node(one.toView()));

// Card Two
Card two = new Card(this);
two.setText("Submenu");
root.add(new Node(two.toView(), second));
root.add(new Node(two.toView(), twoChild));

Card a = new Card(this);
a.setText("Click Listened to");
View viewA = a.toView();
viewA.setOnClickListener(new ClickListener());
second.add(new Node(viewA));
twoChild.add(new Node(viewA));

Card b = new Card(this);
b.setText("I have a menu");
View viewB = b.toView();
viewB.setOnFocusChangeListener(new FocusListener());
second.add(new Node(viewB, R.menu.sample));
twoChild.add(new Node(viewB, R.menu.sample));

//Card Three
Card three = new Card(this);
three.setText("Level wide submenu in here");
root.add(new Node(three.toView(), threeChild));

Card repeat = new Card(this);
repeat.setText("I have a menu");
threeChild.add(new Node(repeat.toView()));
threeChild.add(new Node(repeat.toView()));

tree.showRoot();
setContentView(tree);
Expand Down
4 changes: 1 addition & 3 deletions Lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />

<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<application android:allowBackup="true">

</application>

Expand Down
11 changes: 11 additions & 0 deletions Lib/src/main/java/com/kelsonprime/cardtree/CardTreeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,26 @@ public void onBackPressed() {

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//#TODO We should be able to make this more efficient and not constantly reinflate.
if(getCurrentNode().hasMenu()){
Log.d(TAG, "Preparing Node menu");
menu.clear();
Integer activeMenu = getCurrentNode().getMenuRes();
getMenuInflater().inflate(activeMenu, menu);
return true;
}else if(getCurrentLevel().hasMenu()){
Log.d(TAG, "Preparing level menu");
menu.clear();
Integer activeMenu = getCurrentLevel().getMenuRes();
getMenuInflater().inflate(activeMenu, menu);
return true;
}
return false;
}

private Level getCurrentLevel(){
return getTree().getCurrentLevel();
}
private Node getCurrentNode(){
return getTree().getCurrentNode();
}
Expand Down
20 changes: 19 additions & 1 deletion Lib/src/main/java/com/kelsonprime/cardtree/Level.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package com.kelsonprime.cardtree;

import android.util.Log;

import java.util.ArrayList;

public class Level extends ArrayList<Node> {
private static final String TAG = "Level";
private Tree parent;
private Integer menuRes;

public Level(Tree parent){
this(parent, null);
}

public Level(Tree parent, Integer menuRes){
super();
this.parent = parent;
this.menuRes = menuRes;
}

public boolean hasMenu(){
return menuRes != null;
}
public Integer getMenuRes() {
return menuRes;
}

void click(int pos){
Expand All @@ -16,7 +33,8 @@ void click(int pos){
private void click(Node node){
if(node.hasChild()){
parent.enterLevel(node.getChild());
}else if(node.hasMenu()){
}else if(node.hasMenu() || hasMenu()){
Log.d(TAG, "Sending showMenu");
parent.showMenu();
}else{
node.click();
Expand Down
5 changes: 3 additions & 2 deletions Lib/src/main/java/com/kelsonprime/cardtree/Tree.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.kelsonprime.cardtree;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;

import android.view.View;
import android.widget.AdapterView;

Expand Down Expand Up @@ -51,6 +50,8 @@ public Level getRoot() {
}

public void enterLevel(Level level) {
if(level.isEmpty())
throw new RuntimeException("Empty " + level.toString() + " cannot be entered");
if (level == this.root) {
backStack.clear();
} else {
Expand Down

0 comments on commit ae87dff

Please sign in to comment.