labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r880850 - in /labs/magma/trunk/website-beansview/src: main/java/org/apache/magma/website/beansview/ test/java/org/apache/magma/website/beansview/ test/java/org/apache/magma/website/beansview/stuff/ test/resources/ test/resources/META-INF/
Date Mon, 16 Nov 2009 17:04:13 GMT
Author: simoneg
Date: Mon Nov 16 17:04:12 2009
New Revision: 880850

URL: http://svn.apache.org/viewvc?rev=880850&view=rev
Log:
Support for show list actions

Added:
    labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/stuff/DummyPersonConverter.java
    labs/magma/trunk/website-beansview/src/test/resources/
    labs/magma/trunk/website-beansview/src/test/resources/META-INF/
    labs/magma/trunk/website-beansview/src/test/resources/META-INF/magma.default.properties
Modified:
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyle.java
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyleTable.java
    labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java
    labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java?rev=880850&r1=880849&r2=880850&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/FormAction.java
Mon Nov 16 17:04:12 2009
@@ -17,12 +17,16 @@
 package org.apache.magma.website.beansview;
 
 import org.apache.magma.basics.MagmaException;
+import org.apache.magma.beans.MagmaBeanSupport;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+
 
 public class FormAction {
 
 	private String label;
 	private String method;
-	private boolean parametrized;
+	private boolean parametrized = true;
 	private boolean validate;
 
 	
@@ -77,4 +81,19 @@
 		return this;
 	}
 	
-}
+	public String getMethodUrl(MagmaBeanSupport bean) {
+		String meth = getMethod();
+		if (meth.startsWith("do")) {
+			meth = meth.substring(2);
+		} else if (meth.startsWith("handle")) {
+			meth = meth.substring(6);
+		}
+		if (bean != null && this.parametrized) {
+			Converter conv = Converters.getConverterFor(bean.getClass());
+			if (conv == null) throw new MagmaException("Cannot find a converter for the form action
on bean {0}", bean.getClass());
+			meth += "!";
+			meth += conv.to(bean);
+		}
+		return meth;
+	}
+ }

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyle.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyle.java?rev=880850&r1=880849&r2=880850&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyle.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyle.java
Mon Nov 16 17:04:12 2009
@@ -1,25 +1,18 @@
 package org.apache.magma.website.beansview;
 
-import java.io.IOException;
-import java.io.Writer;
 import java.util.List;
 
-import org.apache.commons.lang.StringEscapeUtils;
-import org.apache.magma.basics.LocalizableString;
-import org.apache.magma.beans.BeanData;
-import org.apache.magma.beans.MagmaBeanSupport;
 import org.apache.magma.beans.PropertyInfo;
 import org.apache.magma.view.tree.FieldNode;
 import org.apache.magma.view.tree.Node;
 import org.apache.magma.view.tree.RootNode;
-import org.apache.magma.view.tree.ViewUtils;
-import org.apache.magma.website.WebHandler;
 
 public abstract class ListStyle {
 
 	protected Class<?> myclass;
 	protected RootNode root;
 	protected boolean withLink = false;
+	protected boolean withAdditional = false;
 	protected StringBuilder buff;
 
 	public void setMyclass(Class<?> myclass) {
@@ -32,7 +25,11 @@
 
 	public void setWithLink(boolean withLink) {
 		this.withLink = withLink;
-	}	
+	}
+	
+	public void setWithAdditional(boolean withAdditional) {
+		this.withAdditional = withAdditional;
+	}
 	
 	public void generate() {
 		buff = new StringBuilder();
@@ -57,19 +54,36 @@
 				createFieldNodeHeader(fldn);
 			}
 		}
+		if (withAdditional) {
+			createFieldNodeHeader(null);			
+		}
 		closeHeaderRow();
 	}
 	protected abstract void openHeaderRow();
 	
+	/**
+	 * @param fldn may be null, for the last additional column
+	 */
 	protected abstract void openHeaderCell(FieldNode fldn);
 	
+	/**
+	 * @param fldn may be null, for the last additional column
+	 */
 	protected void createFieldNodeHeader(FieldNode fldn) {
 		openHeaderCell(fldn);
 		buff.append("${list.labelFor('");
-		buff.append(fldn.getId());
+		if (fldn != null) {
+			buff.append(fldn.getId());
+		} else {
+			buff.append("_additionals");
+		}
 		buff.append("')}");
 		closeHeaderCell(fldn);
 	}
+	
+	/**
+	 * @param fldn may be null, for the last additional column
+	 */
 	protected abstract void closeHeaderCell(FieldNode fldn);
 
 	protected abstract void closeHeaderRow();
@@ -85,7 +99,10 @@
 				FieldNode fldn = (FieldNode) node;
 				createFieldNodeCell(fldn);
 			}
-		}			
+		}
+		if (withAdditional) {
+			createFieldNodeCell(null);
+		}
 		closeRow();
 		closeIterator();
 		closeBody();
@@ -111,19 +128,24 @@
 	
 	protected abstract void closeRow();
 	
-	
+	/**
+	 * @param fldn may be null, for the last additional column
+	 */	
 	protected void createFieldNodeCell(FieldNode fldn) {
 		openCell(fldn);
-		if (withLink) {
+		if (withLink && fldn != null) {
 			openLink(fldn);
 		}
 		createFieldNodeCellContent(fldn);
-		if (withLink) {
+		if (withLink && fldn != null) {
 			closeLink(fldn);
 		}
 		closeCell(fldn);
 	}
 	
+	/**
+	 * @param fldn may be null, for the last additional column
+	 */	
 	protected abstract void openCell(FieldNode fldn);
 	
 	protected void openLink(FieldNode fldn) {
@@ -134,10 +156,16 @@
 		buff.append("')}\">");
 	}
 	
+	/**
+	 * @param fldn may be null, for the last additional column
+	 */	
 	protected void createFieldNodeCellContent(FieldNode fldn) {
-		PropertyInfo prop = fldn.getProperty();
 		buff.append("${list.contentFor($row,'");
-		buff.append(fldn.getId());
+		if (fldn != null) {
+			buff.append(fldn.getId());
+		} else {
+			buff.append("_additionals");			
+		}
 		buff.append("')}");		
 	}
 	
@@ -145,6 +173,9 @@
 		buff.append("</a>");
 	}
 
+	/**
+	 * @param fldn may be null, for the last additional column
+	 */
 	protected abstract void closeCell(FieldNode fldn);	
 	
 	protected abstract void createFooter();
@@ -154,7 +185,7 @@
 	protected abstract void openOuterTable();
 	
 	protected String getStyleKey() {
-		return "WL" + this.withLink;
+		return "WL" + this.withLink + "WA" + this.withAdditional;
 	}
 
 }

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyleTable.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyleTable.java?rev=880850&r1=880849&r2=880850&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyleTable.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ListStyleTable.java
Mon Nov 16 17:04:12 2009
@@ -47,14 +47,14 @@
 
 	@Override
 	protected void openCell(FieldNode fldn) {
-		// TODO add call for additional classes
-		buff.append("<td class=\"MagmaList-Cell " + fldn.getId() + "\">");		
+		String addclass = fldn == null ? "additionals" : fldn.getId();
+		buff.append("<td class=\"MagmaList-Cell " + addclass + "\">");		
 	}
 
 	@Override
 	protected void openHeaderCell(FieldNode fldn) {
-		// TODO add call for additional classes
-		buff.append("<th class=\"MagmaList-HeaderCell " + fldn.getId() + "\">");
+		String addclass = fldn == null ? "additionals" : fldn.getId();
+		buff.append("<th class=\"MagmaList-HeaderCell " + addclass + "\">");
 	}
 
 	@Override

Modified: labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java?rev=880850&r1=880849&r2=880850&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java
(original)
+++ labs/magma/trunk/website-beansview/src/main/java/org/apache/magma/website/beansview/ShowList.java
Mon Nov 16 17:04:12 2009
@@ -45,6 +45,7 @@
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
@@ -62,6 +63,8 @@
 	protected String gototitle;
 	protected String[] layers = LIST;
 	protected ListStyle listStyle;
+	protected List<FormAction> actions = null;
+	
 	
 	public <T extends MagmaBeanSupport> ShowList(Class<T> myclass, Collection<?
extends T> list, String link, String... layers) {
 		this.list = list;
@@ -127,6 +130,7 @@
 		listStyle.setMyclass(myclass);
 		listStyle.setRoot(root);
 		listStyle.setWithLink(this.linkUrl != null);
+		listStyle.setWithAdditional(this.actions != null && this.actions.size() > 0);
 		String tplkey = listStyle.getClass().getName() + ":" + listStyle.getStyleKey();
 		
 		String tplval = root.getTemplate(tplkey);
@@ -205,10 +209,18 @@
 	}
 	
 	public final String labelFor(String nodeid) {
+		if (nodeid.equals("_additionals")) {
+			return labelForAdditionals();
+		}
 		RootNode root = BeanData.getFor(myclass).getViewTree(this.layers);
 		Node gennode = root.findById(nodeid);
 		return labelFor(gennode);
 	}
+
+	protected String labelForAdditionals() {
+		return new LocalizableString("Actions").toString();
+	}
+	
 	
 	protected String labelFor(Node gennode) {
 		if (!(gennode instanceof FieldNode)) return "NA";
@@ -217,10 +229,29 @@
 	}
 	
 	public final String contentFor(MagmaBeanSupport bean, String nodeid) {
+		if (nodeid.equals("_additionals")) {
+			return contentForAdditionals(bean);
+		}
 		RootNode root = BeanData.getFor(myclass).getViewTree(this.layers);
 		Node gennode = root.findById(nodeid);
 		return contentFor(bean, gennode);
 	}
+
+	protected String contentForAdditionals(MagmaBeanSupport bean) {
+		if (this.actions == null || this.actions.size() == 0) return "";
+		StringBuilder ret = new StringBuilder();
+		for (FormAction action : this.actions) {
+			ret.append("<a");
+			ret.append(" class=\"Actions ");
+			ret.append(action.getMethod());
+			ret.append('"');
+			ret.append(" href=\"" + action.getMethodUrl(bean) + "\">");
+			ret.append(new LocalizableString(action.getLabel()).toString());
+			ret.append("</a>");
+		}
+		return ret.toString();
+	}
+	
 	
 	protected String contentFor(MagmaBeanSupport bean, Node gennode) {
 		if (!(gennode instanceof FieldNode)) return "NA";
@@ -244,4 +275,11 @@
 		return this.list;
 	}
 	
+	public ShowList addAction(FormAction action) {
+		if (this.actions == null) this.actions = new ArrayList<FormAction>();
+		this.actions.add(action);
+		return this;
+	}
+	
+	
 }

Modified: labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java?rev=880850&r1=880849&r2=880850&view=diff
==============================================================================
--- labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java
(original)
+++ labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/ShowListTest.java
Mon Nov 16 17:04:12 2009
@@ -86,6 +86,33 @@
 		assertStructure(result, "<ul","<li","<span", "Simone", "</span", "<span",
"Gianni", "</ul");
 		System.out.println(result);
 	}
+
+	@Test
+	public void actionsTest() throws Exception {
+		Person p = new Person();
+		p.setName("Simone");
+		p.setSurname("Gianni");
+		p.setEmail("simoneg@apache.org");
+		
+		List<Person> lst = new ArrayList<Person>();
+		lst.add(p);
+		lst.add(p);
+		
+		ShowList sbp = new ShowList(Person.class, lst);
+		sbp.addAction(new FormAction("Delete", "doDelete"));
+		sbp.addAction(new FormAction("Promote", "doPromote"));
+		
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		sbp.produce(baos);
+		
+		String result = new String(baos.toByteArray());
+
+		assertStructure(result, 
+				"<table","<tr",
+				"<td", "Simone", "</td", "<td", "Gianni", "<td", "additionals", "<a",
"href=\"Delete",
+				"</table");
+		System.out.println(result);
+	}
 	
 	
 }

Added: labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/stuff/DummyPersonConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/stuff/DummyPersonConverter.java?rev=880850&view=auto
==============================================================================
--- labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/stuff/DummyPersonConverter.java
(added)
+++ labs/magma/trunk/website-beansview/src/test/java/org/apache/magma/website/beansview/stuff/DummyPersonConverter.java
Mon Nov 16 17:04:12 2009
@@ -0,0 +1,33 @@
+package org.apache.magma.website.beansview.stuff;
+
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.string.BaseConverter;
+
+public class DummyPersonConverter extends BaseConverter<Person> {
+	
+	public DummyPersonConverter() {
+		super(Person.class);
+	}
+
+	public Person from(String value) {
+		return new Person();
+	}
+	
+	public String to(Person value) {
+		return "converted";
+	}
+	
+	public boolean converts(Class<?> clazz) {
+		return Person.class.isAssignableFrom(clazz);
+	}
+	
+	public Converter<Person> build(Class<? extends Person> clazz) {
+		return this;
+	}
+
+	public int getMaximumStringSize() {
+		return 1;
+	}
+	
+
+}

Added: labs/magma/trunk/website-beansview/src/test/resources/META-INF/magma.default.properties
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-beansview/src/test/resources/META-INF/magma.default.properties?rev=880850&view=auto
==============================================================================
--- labs/magma/trunk/website-beansview/src/test/resources/META-INF/magma.default.properties
(added)
+++ labs/magma/trunk/website-beansview/src/test/resources/META-INF/magma.default.properties
Mon Nov 16 17:04:12 2009
@@ -0,0 +1,15 @@
+#Licensed to the Apache Software Foundation (ASF) under one or more
+#contributor license agreements.  See the NOTICE file distributed with
+#this work for additional information regarding copyright ownership.
+#The ASF licenses this file to You under the Apache License, Version 2.0
+#(the "License"); you may not use this file except in compliance with
+#the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
+org.apache.magma.conversion.Converter.person=org.apache.magma.website.beansview.stuff.DummyPersonConverter



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message