//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// J2ME-Lib source file
// Copyright (c) 2006 Elmar Sonnenschein / esoco GmbH
// Last Change: 17.10.2006 by eso
//
// J2ME-Lib 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 2.1 of the License, or (at your option)
// any later version.
//
// J2ME-Lib 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 J2ME-Lib; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or use the contact
// information from the GNU website http://www.gnu.org
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package de.esoco.j2me.ui;
import de.esoco.j2me.model.MutableHierarchyNode;
import de.esoco.j2me.model.HierarchyNode;
import de.esoco.j2me.resource.ResourceBundle;
import de.esoco.j2me.util.Executable;
import de.esoco.j2me.util.UserNotificationException;
/********************************************************************
* Abstract command subclass for commands that can be invoked on HierarchyNode
* instances. Implementations must implement the <code>process()</code> method
* for the actual node processing functionality. The node that is to be
* processed can be queried through the <code>getNode()</code> method or, in
* case of editable nodes, through the convenience method <code>
* getEditableNode()</code>.
*
* @author eso
* @version 1.1
* @see de.esoco.j2me.model.HierarchyNode
*/
public abstract class NodeCommand extends ExecutableCommand
{
//~ Instance fields --------------------------------------------------------
private boolean bEditableOnly = false;
private NodeController rNodeController = null;
//~ Constructors -----------------------------------------------------------
/***************************************
* Standard constructor that simply invokes super().
*
* @param sLabel The command label to be displayed in the UI
* @param nType One of the predefined command types
* @param nPriority The ordering priority of the command
*/
public NodeCommand(String sLabel, int nType, int nPriority)
{
super(sLabel, nType, nPriority);
}
/***************************************
* Constructs a command that can use a NodeController instance to check
* access to the node on which it is invoked. It does so by invoking the
* controllers <code>checkNodeAccess()</code> method. It can also be defined
* if the command may only be invoked on editable nodes. If this flag is
* TRUE the execute() method will throw a UserNotificationException if the
* command is invoked on a read-only node.
*
* @param sLabel The command label to be displayed in the UI
* @param nType One of the predefined command types
* @param nPriority The ordering priority of the command
* @param bEditableOnly TRUE if this command may only be invoked on editable
* nodes
* @param rController The NodeController to use for access checking or
* NULL to disable the check
*/
public NodeCommand(String sLabel,
int nType,
int nPriority,
boolean bEditableOnly,
NodeController rController)
{
this(sLabel, nType, nPriority);
this.rNodeController = rController;
this.bEditableOnly = bEditableOnly;
}
//~ Methods ----------------------------------------------------------------
/***************************************
* This method is invoked if the command is selected in a screen. It first
* checks if the command is only for editable nodes. If so, but the given
* node is not an instance of the MutableHierarchyNode instance it throws
* an UserNotificationException. It the second step this method will check
* if a NodeController has been set. It then will either invoke the
* controller's <code>checkNodeAccess()</code> method or directly invoke the
* <code>execute()</code> method without parameters from the executable
* interface.
*
* @param rArg The node for which the command has been selected
*
* @throws UserNotificationException If an error occurs during execution
*/
public final void execute(Object rArg) throws UserNotificationException
{
final HierarchyNode rNode = (HierarchyNode) rArg;
if (bEditableOnly && !(rArg instanceof MutableHierarchyNode))
{
ResourceBundle rRsrc = ResourceBundle.getCurrent();
throw new UserNotificationException(rRsrc.getString("JL_MtInfo"),
rRsrc.getString("JL_MsNoEdit"));
}
if (rNodeController != null)
{
rNodeController.checkNodeAccess(rNode, new Executable()
{
public void execute(Object rArg)
throws UserNotificationException
{
processNode(rNode);
}
});
}
else
{
processNode(rNode);
}
}
/***************************************
* Method that must be implemented by subclasses with the actual node
* processing of this command.
*
* @param rNode The node to process
*
* @throws UserNotificationException If an error occurs during processing
*/
protected abstract void processNode(HierarchyNode rNode)
throws UserNotificationException;
}