/**
* C-Nery - A home automation web application for C-Bus.
* Copyright (C) 2008,2009,2012 Dave Oxley <dave@daveoxley.co.uk>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.daveoxley.cnery.pages;
import com.daveoxley.cnery.entities.AbstractCondition;
import com.daveoxley.seam.SelectionService;
import com.daveoxley.seam.exceptions.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Scope;
import org.richfaces.component.UITree;
import org.richfaces.event.TreeSelectionChangeEvent;
/**
*
* @author dave
*/
@Scope(ScopeType.PAGE)
public abstract class TreeBean<E extends AbstractCondition,N extends TreeNode> {
private N currentSelection = null;
public void selectionChanged(TreeSelectionChangeEvent selectionChangeEvent) throws EntityNotFoundException {
// considering only single selection
List<Object> selection = new ArrayList<Object>(selectionChangeEvent.getNewSelection());
if (selection.isEmpty()) {
currentSelection = null;
}
else {
Object currentSelectionKey = selection.get(0);
UITree tree = (UITree) selectionChangeEvent.getSource();
Object storedKey = tree.getRowKey();
tree.setRowKey(currentSelectionKey);
currentSelection = (N) tree.getRowData();
tree.setRowKey(storedKey);
}
SelectionService ss = (SelectionService)Component.getInstance(getSelectionServiceClass());
if (currentSelection != null && currentSelection.getEntity() != null)
ss.setSelectedId(currentSelection.getEntity().getId());
else
ss.setSelectedId(null);
}
protected abstract Class<? extends SelectionService> getSelectionServiceClass();
public N getCurrentSelection() { return currentSelection; }
protected void clearSelection() {
currentSelection = null;
}
public boolean getActionsDisabled() {
return currentSelection == null;
}
public boolean getNewActionDisabled() {
return getActionsDisabled();
}
public boolean getEditActionDisabled() {
return getActionsDisabled() || currentSelection.getEntity() == null;
}
}