If you’ve succumbed to not being able to get Spring Web Flow to work with your other framework of choice (I am only using ICEfaces as an example because its fresh in my head) and have to figure out how to reimplement row selection for tables and such, since you can’t readily use SWF’s OneSelectionTrackingListDataModel et al (i.e. result-type=”dataModel” in a flow configuration), there is of course an AOP solution. What can’t it do?!

I have an aspect skeleton sitting around for this, and of course you can use it if you want. (Fuck, you can use the whole library. I just have to get my SCM back online.) You just have to have everything else in place to make it go, like load-time weaving or whatever. Anyway, it’s basically the Decorator pattern, something AOP introductions… are. Here it is…

/**
 * LDSYS Java Library
 *
 * Copyright (C) 1997-2010 Christopher G. Stach II
 *
 * This file is part of LDSYS Java Library.
 *
 * LDSYS Java Library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * LDSYS Java Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with LDSYS Java Library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 **/

package net.ldsys.view.jsf;

import java.io.Serializable;

import javax.faces.event.ActionEvent;

/**
 * Description: This should be subclassed and used like this: <code>
 * &#064;Aspect
 * public class SelectableRowAspect {
 *     &#064;DeclareMixin(&quot;@javax.persistence.Entity your.package.model..*&quot;)
 *     public static SelectableRow createDelegate(Object row) {
 *         return net.ldsys.view.jsf.SelectableRowAspect.createDelegate(row);
 *     }
 * }
 * </code>
 *
 * @author cgs
 */
// @Aspect
public class SelectableRowAspect {

	/**
	 * Description: What do you think it is?
	 *
	 * @author cgs
	 */
	public interface SelectableRow
		extends Serializable
	{

		/**
		 * @return the selected
		 */
		boolean isSelected();

		/**
		 * @param selected
		 */
		void setSelected(boolean selected);

		/**
		 * @param actionEvent
		 */
		void toggleSelected(ActionEvent actionEvent);

	}

	/**
	 * Description: What do you think it is?
	 *
	 * @author cgs
	 */
	public static class SelectableRowImpl
		implements SelectableRow
	{

		private static final long serialVersionUID = 1L;

		private boolean _selected;

		/**
		 *
		 */
		public SelectableRowImpl() {
			super();
		}

		/**
		 * @see net.ldsys.view.jsf.SelectableRowAspect.SelectableRow#isSelected()
		 */
		public boolean isSelected() {
			return _selected;
		}

		/**
		 * @see net.ldsys.view.jsf.SelectableRowAspect.SelectableRow#setSelected(boolean)
		 */
		public void setSelected(boolean selected) {
			_selected = selected;
		}

		/**
		 * @see net.ldsys.view.jsf.SelectableRowAspect.SelectableRow#toggleSelected(javax.faces.event.ActionEvent)
		 */
		public void toggleSelected(ActionEvent actionEvent) {
			_selected = !_selected;
		}

	}

	/**
	 * @param row
	 * @return the SelectableRow implementation
	 */
	// @DeclareMixin(&quot;@javax.persistence.Entity
	// your.package.model..*&quot;)
	public static SelectableRow createDelegate(Object row) {
		return new SelectableRowImpl();
	}

}

Using it is basically what the Javadoc says. One would look like this:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareMixin;

@Aspect
public class SelectableRowAspect
	extends net.ldsys.view.jsf.SelectableRowAspect
{

	@DeclareMixin("@javax.persistence.Entity tld.domain.some.package.model..*")
	public static SelectableRow createDelegate(Object row) {
		return net.ldsys.view.jsf.SelectableRowAspect.createDelegate(row);
	}

	/**
	 *
	 */
	public SelectableRowAspect() {
		super();
	}

}

That applies your row selection aspect on all of your entities. You just stuff them into a list and you can use the selection mechanism in things, kind of like this (this is a fucked up example, so don’t copy and paste thinking that it will work):

<ice:dataTable id="someTableData"
    value="#{someTableModel.rows}"
    var="user"
    varStatus="status">

    <ice:rowSelector id="someTableRowSelector"
        immediate="true"
        toggleOnClick="false"
        toggleOnInput="false"
        value="#{something.selected}" />

    <columnblahblah>

        <ice:commandLink action="#{somePopup.open}"
            actionListener="#{something.toggleSelected}">

            <ice:outputText value="#{status.index + 1}" />

        </ice:commandLink>

    </columnblahblah>

    [...]

</ice:dataTable>

There. Now you can click on a row and it will be selected for your popup.

  • Digg
  • Delicious
  • StumbleUpon
  • Technorati Favorites
  • Reddit
  • Yahoo Buzz
  • Twitter
  • DZone
  • Google Bookmarks
  • LinkedIn
  • Amazon Wish List
  • Share/Bookmark