Author: nbelaevski
Date: 2007-07-30 19:08:02 -0400 (Mon, 30 Jul 2007)
New Revision: 1939
Added:
trunk/framework/test/src/test/java/org/richfaces/model/
trunk/framework/test/src/test/java/org/richfaces/model/StackingTreeDataModelTest.java
trunk/framework/test/src/test/java/org/richfaces/model/entity/
trunk/framework/test/src/test/java/org/richfaces/model/entity/Directory.java
trunk/framework/test/src/test/java/org/richfaces/model/entity/File.java
trunk/framework/test/src/test/java/org/richfaces/model/entity/Named.java
trunk/framework/test/src/test/java/org/richfaces/model/entity/Project.java
Modified:
trunk/framework/api/src/main/java/org/richfaces/model/StackingTreeModel.java
Log:
- StackingTreeDataModelTest.java extended & model entity classes added
- StackingTreeModel few bugs fixed
Modified: trunk/framework/api/src/main/java/org/richfaces/model/StackingTreeModel.java
===================================================================
---
trunk/framework/api/src/main/java/org/richfaces/model/StackingTreeModel.java 2007-07-30
23:06:53 UTC (rev 1938)
+++
trunk/framework/api/src/main/java/org/richfaces/model/StackingTreeModel.java 2007-07-30
23:08:02 UTC (rev 1939)
@@ -5,6 +5,7 @@
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
@@ -29,6 +30,8 @@
private String modelId;
private Object modelKey;
+ private Object rowData;
+
private StackingTreeModelDataProvider dataProvider;
protected StackingTreeModel stackingTreeModel;
@@ -52,6 +55,12 @@
return dataModel;
}
+ public boolean isEmpty() {
+ //TODO optimize that
+ return getDataModel().getRowCount() == 0;
+ }
+
+
public StackingTreeModel(String id, String var, StackingTreeModelDataProvider
dataProvider) {
super();
this.id = id;
@@ -75,6 +84,8 @@
Map map = facesContext.getExternalContext().getRequestMap();
map.put(var, varObject);
}
+
+ rowData = null;
}
public void leaveModel() {
@@ -87,6 +98,8 @@
varObject = map.get(var);
map.put(var, object);
}
+
+ rowData = object;
}
public void setupModel(Object object) {
@@ -112,8 +125,20 @@
* @see org.richfaces.model.AbstractTreeDataModel#isLeaf()
*/
public boolean isLeaf() {
- // TODO Auto-generated method stub
- return false;
+ if (stackingTreeModel != null && stackingTreeModel != this) {
+ return stackingTreeModel.isLeaf();
+ }
+
+ Collection values = this.models.values();
+ for (Iterator iterator = values.iterator(); iterator.hasNext();) {
+ StackingTreeModel stackingTreeModel = (StackingTreeModel) iterator.next();
+
+ if (!stackingTreeModel.isEmpty()) {
+ return false;
+ }
+ }
+
+ return true;
}
protected void doWalk(FacesContext context, DataVisitor dataVisitor,
@@ -131,7 +156,7 @@
}
dataModel.setRowKey(localRowKey);
setupModel(dataModel.getRowData(), context);
- System.out.println(dataModel.getRowData() + (last ? " * " : "") +
" - " + argumentKey);
+ //System.out.println(dataModel.getRowData() + (last ? " * " : "")
+ " - " + argumentKey);
processElement(context, dataVisitor, argument, argumentKey, last);
}
@@ -249,10 +274,6 @@
private DataVisitor dataVisitor;
- public DataVisitor getDataVisitor() {
- return dataVisitor;
- }
-
public ShiftingDataVisitor(DataVisitor dataVisitor) {
super();
this.dataVisitor = dataVisitor;
@@ -369,8 +390,11 @@
* @see javax.faces.model.DataModel#getRowData()
*/
public Object getRowData() {
- // TODO Auto-generated method stub
- return null;
+ if (stackingTreeModel != null && stackingTreeModel != this) {
+ return stackingTreeModel.getRowData();
+ }
+
+ return rowData;
}
/*
Added:
trunk/framework/test/src/test/java/org/richfaces/model/StackingTreeDataModelTest.java
===================================================================
--- trunk/framework/test/src/test/java/org/richfaces/model/StackingTreeDataModelTest.java
(rev 0)
+++
trunk/framework/test/src/test/java/org/richfaces/model/StackingTreeDataModelTest.java 2007-07-30
23:08:02 UTC (rev 1939)
@@ -0,0 +1,167 @@
+/**
+ *
+ */
+package org.richfaces.model;
+
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+import org.ajax4jsf.model.DataVisitor;
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+import org.richfaces.model.entity.Directory;
+import org.richfaces.model.entity.File;
+import org.richfaces.model.entity.Named;
+import org.richfaces.model.entity.Project;
+
+
+/**
+ * @author Nick Belaevski
+ * mailto:nbelaevski@exadel.com
+ * created 30.07.2007
+ *
+ */
+public class StackingTreeDataModelTest extends AbstractAjax4JsfTestCase {
+
+ public StackingTreeDataModelTest(String name) {
+ super(name);
+ }
+
+ private StackingTreeModel stackingTreeModel;
+
+ /* (non-Javadoc)
+ * @see org.ajax4jsf.tests.AbstractAjax4JsfTestCase#setUp()
+ */
+ public void setUp() throws Exception {
+ super.setUp();
+ this.stackingTreeModel = new StackingTreeModel();
+ StackingTreeModel projectsModel = new StackingTreeModel("project",
"project", new StackingTreeModelDataProvider() {
+
+ private Map data = null;
+
+ public Object getData() {
+ if (data == null) {
+ data = new LinkedHashMap();
+ Project projectA = new Project("projectA", 10);
+
+ Directory adir1 = new Directory("ADir1", 50);
+
+ adir1.addFile(new File("AFile1", 60));
+ adir1.addFile(new File("AFile2", 61));
+
+ Directory adir2 = new Directory("ADir2", 101);
+
+ projectA.addDirectory(adir1);
+ projectA.addDirectory(adir2);
+
+ data.put(projectA.getName(), projectA);
+
+ Project projectB = new Project("projectB", 501);
+
+ Directory bdir1 = new Directory("BDir1", 600);
+ Directory bdir2 = new Directory("BDir2", 700);
+ Directory bdir3 = new Directory("BDir3", 801);
+
+ projectB.addDirectory(bdir1);
+ projectB.addDirectory(bdir2);
+ projectB.addDirectory(bdir3);
+
+ data.put(projectB.getName(), projectB);
+ }
+ return data;
+ }
+
+ });
+
+ final ValueBinding dirVB =
application.createValueBinding("#{project.directories}");
+ StackingTreeModel directoriesModel = new StackingTreeModel("directory",
"directory", new StackingTreeModelDataProvider() {
+ public Object getData() {
+ return dirVB.getValue(facesContext);
+ }
+ });
+
+ final ValueBinding fileVB =
application.createValueBinding("#{directory.files}");
+ StackingTreeModel filesModel = new StackingTreeModel("file",
"file", new StackingTreeModelDataProvider() {
+ public Object getData() {
+ return fileVB.getValue(facesContext);
+ }
+ });
+
+ directoriesModel.addStackingModel(filesModel);
+ projectsModel.addStackingModel(directoriesModel);
+ this.stackingTreeModel.addStackingModel(projectsModel);
+ }
+
+ /* (non-Javadoc)
+ * @see org.ajax4jsf.tests.AbstractAjax4JsfTestCase#tearDown()
+ */
+ public void tearDown() throws Exception {
+ super.tearDown();
+ this.stackingTreeModel = null;
+ }
+
+ public void testWalk() throws Exception {
+ StackingTreeDataModelTestVisitor1 visitor1 = new StackingTreeDataModelTestVisitor1();
+ this.stackingTreeModel.walk(facesContext, visitor1, null, null);
+ assertEquals(9, visitor1.getCounter());
+ }
+
+ class StackingTreeDataModelTestVisitor1 implements DataVisitor, LastElementAware {
+
+ private boolean last;
+ private int tag = 0;
+ private int counter = 0;
+
+ public void process(FacesContext context, Object rowKey, Object argument)
+ throws IOException {
+
+ stackingTreeModel.setRowKey(rowKey);
+ assertNull(stackingTreeModel.getTreeNode());
+ Object rowData = stackingTreeModel.getRowData();
+ assertNotNull(rowData);
+ assertTrue(rowData instanceof Named);
+
+ Named named = (Named) rowData;
+
+ if (tag == 0) {
+ tag = named.getTag();
+ } else {
+ int currentTag = named.getTag();
+ assertTrue(currentTag > tag);
+ this.tag = currentTag;
+ }
+
+ assertFalse(this.tag % 10 == 1 ^ last);
+
+ if (named instanceof Directory) {
+ if ("ADir1".equals(named.getName())) {
+ assertFalse(stackingTreeModel.isLeaf());
+ } else {
+ assertTrue(stackingTreeModel.isLeaf());
+ }
+ } else if (named instanceof Project) {
+ assertFalse(stackingTreeModel.isLeaf());
+ } else if (named instanceof File) {
+ assertTrue(stackingTreeModel.isLeaf());
+ }
+
+ counter++;
+ }
+
+ public void resetLastElement() {
+ this.last = false;
+ }
+
+ public void setLastElement() {
+ this.last = true;
+ }
+
+ public int getCounter() {
+ return counter;
+ }
+ }
+}
+
Added: trunk/framework/test/src/test/java/org/richfaces/model/entity/Directory.java
===================================================================
--- trunk/framework/test/src/test/java/org/richfaces/model/entity/Directory.java
(rev 0)
+++
trunk/framework/test/src/test/java/org/richfaces/model/entity/Directory.java 2007-07-30
23:08:02 UTC (rev 1939)
@@ -0,0 +1,30 @@
+/**
+ *
+ */
+package org.richfaces.model.entity;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Nick Belaevski
+ * mailto:nbelaevski@exadel.com
+ * created 30.07.2007
+ *
+ */
+public class Directory extends Named {
+
+ public Directory(String name, int tag) {
+ super(name, tag);
+ }
+
+ private List files = new ArrayList();
+
+ public List getFiles() {
+ return files;
+ }
+
+ public void addFile(File file) {
+ this.files.add(file);
+ }
+}
Added: trunk/framework/test/src/test/java/org/richfaces/model/entity/File.java
===================================================================
--- trunk/framework/test/src/test/java/org/richfaces/model/entity/File.java
(rev 0)
+++ trunk/framework/test/src/test/java/org/richfaces/model/entity/File.java 2007-07-30
23:08:02 UTC (rev 1939)
@@ -0,0 +1,18 @@
+/**
+ *
+ */
+package org.richfaces.model.entity;
+
+/**
+ * @author Nick Belaevski
+ * mailto:nbelaevski@exadel.com
+ * created 30.07.2007
+ *
+ */
+public class File extends Named {
+
+ public File(String name, int tag) {
+ super(name, tag);
+ }
+
+}
Added: trunk/framework/test/src/test/java/org/richfaces/model/entity/Named.java
===================================================================
--- trunk/framework/test/src/test/java/org/richfaces/model/entity/Named.java
(rev 0)
+++ trunk/framework/test/src/test/java/org/richfaces/model/entity/Named.java 2007-07-30
23:08:02 UTC (rev 1939)
@@ -0,0 +1,27 @@
+/**
+ *
+ */
+package org.richfaces.model.entity;
+
+/**
+ * @author Nick Belaevski
+ * mailto:nbelaevski@exadel.com
+ * created 30.07.2007
+ *
+ */
+public class Named {
+ private String name;
+ private int tag;
+ public String getName() {
+ return name;
+ }
+ public int getTag() {
+ return tag;
+ }
+ public Named(String name, int tag) {
+ super();
+ this.name = name;
+ this.tag = tag;
+ }
+
+}
Added: trunk/framework/test/src/test/java/org/richfaces/model/entity/Project.java
===================================================================
--- trunk/framework/test/src/test/java/org/richfaces/model/entity/Project.java
(rev 0)
+++ trunk/framework/test/src/test/java/org/richfaces/model/entity/Project.java 2007-07-30
23:08:02 UTC (rev 1939)
@@ -0,0 +1,29 @@
+/**
+ *
+ */
+package org.richfaces.model.entity;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Nick Belaevski
+ * mailto:nbelaevski@exadel.com
+ * created 30.07.2007
+ *
+ */
+public class Project extends Named {
+ public Project(String name, int tag) {
+ super(name, tag);
+ }
+
+ private List directories = new ArrayList();
+
+ public List getDirectories() {
+ return directories;
+ }
+
+ public void addDirectory(Directory directory) {
+ directories.add(directory);
+ }
+}