Permalink
Browse files

cleaned up. Got rid of useless menu interface. Made Decorator. Now ha…

…ve truly flexible Menu framework
  • Loading branch information...
1 parent 7f67435 commit 7621e73af2daaacc58331c6acb56adce79953a4d @leadVisionary committed Jul 2, 2012
@@ -2,13 +2,12 @@
import java.util.Set;
+import com.visionarysoftwaresolutions.menu.composite.MenuComponent;
+
public interface AbstractMenuFactory {
- public abstract Menu createMenu(String name);
+ public abstract MenuComponent createMenu(String name);
- public abstract Set<Menu> getSubMenus(Menu menu);
-
- public abstract Menu addSubMenu(Menu menu, Menu newMenu);
-
- public abstract Menu addItemToMenu(Menu menu, String item);
+ public abstract Set<MenuComponent> getSubMenus(MenuComponent menu);
+ public abstract MenuComponent addSubMenu(MenuComponent menu, MenuComponent newMenu);
}
@@ -1,18 +0,0 @@
-package com.visionarysoftwaresolutions.menu;
-
-import com.visionarysoftwaresolutions.menu.domain.MenuIterator;
-
-public interface Menu {
-
- public abstract String getText();
-
- public abstract void add(Menu child);
-
- public abstract void remove(Menu child);
-
- public abstract boolean hasChildren();
-
- public abstract Menu getChild(String text);
-
- public abstract MenuIterator createIterator();
-}
@@ -0,0 +1,38 @@
+package com.visionarysoftwaresolutions.menu;
+
+import com.visionarysoftwaresolutions.menu.composite.MenuComponent;
+
+
+public abstract class MenuComponentDecorator extends MenuComponent {
+ private MenuComponent component;
+
+ public MenuComponentDecorator(MenuComponent toDecorate){
+ super(toDecorate.getText());
+ component = toDecorate;
+ }
+
+ @Override
+ public void add(MenuComponent toAdd){
+ component.add(toAdd);
+ }
+
+ @Override
+ public void remove(MenuComponent toRemove){
+ component.remove(toRemove);
+ }
+
+ @Override
+ public boolean hasChildren(){
+ return component.hasChildren();
+ }
+
+ @Override
+ public MenuComponent getChild(String text){
+ return component.getChild(text);
+ }
+
+ @Override
+ public MenuIterator createIterator(){
+ return component.createIterator();
+ }
+}
@@ -0,0 +1,47 @@
+package com.visionarysoftwaresolutions.menu;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import com.visionarysoftwaresolutions.menu.composite.MenuComponent;
+import com.visionarysoftwaresolutions.menu.composite.MenuComposite;
+import com.visionarysoftwaresolutions.menu.composite.MenuLeaf;
+import com.visionarysoftwaresolutions.util.NullChecker;
+
+public class MenuFactory implements AbstractMenuFactory {
+
+ @Override
+ public MenuComponent createMenu(String name){
+ return new MenuComposite(name);
+ }
+
+ @Override
+ public MenuComponent addSubMenu(MenuComponent menu, MenuComponent newMenu) {
+ NullChecker.checkNull(menu);
+ NullChecker.checkNull(newMenu);
+ menu.add(newMenu);
+ return menu;
+ }
+
+ @Override
+ public Set<MenuComponent> getSubMenus(MenuComponent menu){
+ NullChecker.checkNull(menu);
+ Set<MenuComponent> result = new LinkedHashSet<MenuComponent>();
+ MenuIterator iterator = menu.createIterator();
+ while(iterator.hasNext()){
+ MenuComponent contained = iterator.next();
+ if(contained instanceof MenuComposite)
+ {
+ result.add(contained);
+ }
+ }
+ return result;
+ }
+
+ public MenuComponent addItemToMenu(MenuComponent menu, String item){
+ NullChecker.checkNull(menu);
+ NullChecker.checkNull(item);
+ menu.add(new MenuLeaf(item));
+ return menu;
+ }
+}
@@ -1,20 +1,21 @@
-package com.visionarysoftwaresolutions.menu.domain;
+package com.visionarysoftwaresolutions.menu;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
-import com.visionarysoftwaresolutions.menu.Menu;
+import com.visionarysoftwaresolutions.menu.composite.MenuComponent;
-public class MenuIterator implements Iterator<Menu>{
- Collection<Menu> items = null;
- Iterator<Menu> current = null;
+
+public class MenuIterator implements Iterator<MenuComponent>{
+ Collection<MenuComponent> items = null;
+ Iterator<MenuComponent> current = null;
public MenuIterator(){
}
- public MenuIterator(Collection<Menu> collection){
+ public MenuIterator(Collection<MenuComponent> collection){
if(collection != null){
this.items = collection;
current = items.iterator();
@@ -27,11 +28,11 @@ public boolean hasNext() {
}
@Override
- public Menu next() {
+ public MenuComponent next() {
if(current == null){
throw new NoSuchElementException("No next element");
}
- return (Menu) current.next();
+ return current.next();
}
@Override
@@ -0,0 +1,33 @@
+package com.visionarysoftwaresolutions.menu.composite;
+
+import com.visionarysoftwaresolutions.menu.MenuIterator;
+import com.visionarysoftwaresolutions.util.NullChecker;
+
+public abstract class MenuComponent {
+ private String name;
+
+ public MenuComponent(String name){
+ setText(name);
+ }
+
+ public String getText()
+ {
+ return name;
+ }
+
+ public void setText(String name){
+ NullChecker.checkNull(name);
+ this.name = name;
+ }
+
+
+ public abstract void add(MenuComponent child);
+
+ public abstract void remove(MenuComponent child);
+
+ public abstract boolean hasChildren();
+
+ public abstract MenuComponent getChild(String text);
+
+ public abstract MenuIterator createIterator();
+}
@@ -1,26 +1,27 @@
-package com.visionarysoftwaresolutions.menu.domain;
+package com.visionarysoftwaresolutions.menu.composite;
import java.util.LinkedHashMap;
import java.util.Map;
-import com.visionarysoftwaresolutions.menu.Menu;
+import com.visionarysoftwaresolutions.menu.MenuIterator;
+import com.visionarysoftwaresolutions.util.NullChecker;
-class MenuComposite extends MenuComponent {
- private Map<String, Menu> components;
+public class MenuComposite extends MenuComponent {
+ private Map<String, MenuComponent> components;
public MenuComposite(String name) {
super(name);
- components = new LinkedHashMap<String, Menu>();
+ components = new LinkedHashMap<String, MenuComponent>();
}
@Override
- public void add(Menu child) {
+ public void add(MenuComponent child) {
NullChecker.checkNull(child);
components.put(child.getText(), child);
}
@Override
- public void remove(Menu child) {
+ public void remove(MenuComponent child) {
NullChecker.checkNull(child);
components.remove(child.getText());
}
@@ -31,7 +32,7 @@ public boolean hasChildren() {
}
@Override
- public Menu getChild(String text) {
+ public MenuComponent getChild(String text) {
return components.get(text);
}
@@ -1,20 +1,21 @@
-package com.visionarysoftwaresolutions.menu.domain;
+package com.visionarysoftwaresolutions.menu.composite;
-import com.visionarysoftwaresolutions.menu.Menu;
+import com.visionarysoftwaresolutions.menu.MenuIterator;
-class MenuLeaf extends MenuComponent {
+
+public class MenuLeaf extends MenuComponent {
public MenuLeaf(String name) {
super(name);
}
@Override
- public void add(Menu child) {
+ public void add(MenuComponent child) {
throw new UnsupportedOperationException("Menu Items shouldn't have sub menu items. Use a Menu instead!");
}
@Override
- public void remove(Menu child) {
+ public void remove(MenuComponent child) {
throw new UnsupportedOperationException("Menu Items don't have sub menu items. Use a Menu instead!");
}
@@ -24,7 +25,7 @@ public boolean hasChildren() {
}
@Override
- public Menu getChild(String text) {
+ public MenuComponent getChild(String text) {
throw new UnsupportedOperationException("Menu Items don't have sub menu items. Use a Menu instead!");
}
@@ -1,40 +0,0 @@
-package com.visionarysoftwaresolutions.menu.domain;
-
-import com.visionarysoftwaresolutions.menu.Menu;
-
-public abstract class MenuComponent implements Menu {
- protected String name;
-
- public MenuComponent(String name){
- NullChecker.checkNull(name);
- this.name = name;
- }
- /* (non-Javadoc)
- * @see com.visionarysoftwaresolutions.webui.treeview.Menu#add(com.visionarysoftwaresolutions.webui.treeview.Menu)
- */
- @Override
- public abstract void add(Menu child);
- /* (non-Javadoc)
- * @see com.visionarysoftwaresolutions.webui.treeview.Menu#remove(com.visionarysoftwaresolutions.webui.treeview.Menu)
- */
- @Override
- public abstract void remove(Menu child);
- /* (non-Javadoc)
- * @see com.visionarysoftwaresolutions.webui.treeview.Menu#hasChildren()
- */
- @Override
- public abstract boolean hasChildren();
- /* (non-Javadoc)
- * @see com.visionarysoftwaresolutions.webui.treeview.Menu#getText()
- */
- @Override
- public String getText()
- {
- return name;
- }
- /* (non-Javadoc)
- * @see com.visionarysoftwaresolutions.webui.treeview.Menu#getChild(java.lang.String)
- */
- @Override
- public abstract Menu getChild(String text);
-}
@@ -1,46 +0,0 @@
-package com.visionarysoftwaresolutions.menu.domain;
-
-import java.util.LinkedHashSet;
-import java.util.Set;
-
-import com.visionarysoftwaresolutions.menu.AbstractMenuFactory;
-import com.visionarysoftwaresolutions.menu.Menu;
-
-public class MenuFactory implements AbstractMenuFactory {
-
- @Override
- public Menu createMenu(String name){
- return new MenuComposite(name);
- }
-
- @Override
- public Menu addItemToMenu(Menu menu, String item){
- NullChecker.checkNull(menu);
- NullChecker.checkNull(item);
- menu.add(new MenuLeaf(item));
- return menu;
- }
-
- @Override
- public Menu addSubMenu(Menu menu, Menu newMenu) {
- NullChecker.checkNull(menu);
- NullChecker.checkNull(newMenu);
- menu.add(newMenu);
- return menu;
- }
-
- @Override
- public Set<Menu> getSubMenus(Menu menu){
- NullChecker.checkNull(menu);
- Set<Menu> result = new LinkedHashSet<Menu>();
- MenuIterator iterator = menu.createIterator();
- while(iterator.hasNext()){
- Menu contained = iterator.next();
- if(contained instanceof MenuComposite)
- {
- result.add(contained);
- }
- }
- return result;
- }
-}
@@ -1,4 +1,4 @@
-package com.visionarysoftwaresolutions.menu.domain;
+package com.visionarysoftwaresolutions.util;
public class NullChecker {
public static void checkNull(Object child) {
Oops, something went wrong.

0 comments on commit 7621e73

Please sign in to comment.