//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// J2ME-Lib source file
// Copyright (c) 2006 Elmar Sonnenschein / esoco GmbH
// Last Change: 10.11.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.storage.StorageException;
import de.esoco.j2me.util.Executable;
import de.esoco.j2me.util.ProcessingQueue;
import de.esoco.j2me.util.TextUtil;
import de.esoco.j2me.util.UserNotificationException;
import java.util.Hashtable;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
/********************************************************************
* Simple implementation of the NodeController interface based on a TextBox.
*
* @author eso
*/
public class BasicNodeController implements NodeController
{
//~ Instance fields --------------------------------------------------------
/** Cache for node images */
private Hashtable aNodeImages = new Hashtable();
/** Reference to the HierarchyView this controller is associated with */
private HierarchyView rView;
//~ Methods ----------------------------------------------------------------
/***************************************
* This basic implementation grants access to all nodes, it simply invokes
* rInvokeIfGranted.execute() through ProcessingQueue.schedule().
*
* @see NodeController#checkNodeAccess(HierarchyNode, Executable)
*/
public void checkNodeAccess(HierarchyNode rNode,
Executable rInvokeIfGranted)
{
ProcessingQueue.schedule(rInvokeIfGranted);
}
/***************************************
* To check if a particular node may be deleted. This method displays a
* confirmation message and invokes the given Executable instance through
* <code>ProcessingQueue.schedule()</code> if the user confirms the request.
*
* @param rNode The node that will be deleted by invoking the
* runnable
* @param rInvokeIfAllowed The executable to invoke if deletion is allowed
*/
public void checkNodeDelete(HierarchyNode rNode,
final Executable rInvokeIfAllowed)
{
Object[] aMsgArgs = new Object[] { rNode.getTitle() };
String sMsg = TextUtil.formatMessage(getString("JL_MsDelNode"),
aMsgArgs);
MessageScreen.showConfirmation(getString("JL_MtDelNode"), sMsg, true,
new Executable()
{
public void execute(Object rArg)
throws UserNotificationException
{
// will only be invoked if the user selects Ok
ProcessingQueue.schedule(rInvokeIfAllowed);
}
});
}
/***************************************
* @see NodeController#createLeafEditor(MutableHierarchyNode)
*/
public Displayable createLeafEditor(MutableHierarchyNode rForNode)
{
return new TextBox(rForNode.getTitle(), new String(rForNode.getData()),
rForNode.getMaximumSize(), TextField.ANY);
}
/***************************************
* @see NodeController#createLeafViewer(HierarchyNode)
*/
public Displayable createLeafViewer(HierarchyNode rForNode)
{
Form aForm = new Form(rForNode.getTitle());
aForm.append(new String(rForNode.getData()));
return aForm;
}
/***************************************
* @see NodeController#editFinished(Displayable, MutableHierarchyNode)
*/
public void editFinished(Displayable rEditor, MutableHierarchyNode rNode)
throws StorageException
{
rNode.setData(((TextBox) rEditor).getString().getBytes());
}
/***************************************
* To return the image for a certain Node.
*
* @param sNodeType The node type code (always available)
* @param rNode The node for which the image shall be returned (may be
* NULL during node creation)
*
* @return The Image for the node or NULL if no Image shall be displayed
*
* @see NodeController#getNodeImage(String, HierarchyNode)
*/
public Image getNodeImage(String sNodeType, HierarchyNode rNode)
{
return getNodeImage(sNodeType);
}
/***************************************
* Always returns NULL.
*
* @see NodeController#getNodeListEditCommands(HierarchyNode)
*/
public Command[] getNodeListEditCommands(HierarchyNode rNode)
{
return null;
}
/***************************************
* Returns the view that this controller is associated with.
*
* @return A HierarchyView instance
*/
public HierarchyView getView()
{
return rView;
}
/***************************************
* Associates this controller with a particular view. This method is invoked
* by the view constructor when the controller is handed over as a
* parameter.
*
* @param rView The view this controller belongs to
*/
public void setView(HierarchyView rView)
{
this.rView = rView;
}
/***************************************
* @see NodeController#updateLeafViewer(Displayable, HierarchyNode)
*/
public void updateLeafViewer(Displayable rViewer, HierarchyNode rNode)
{
((Form) rViewer).delete(0);
((Form) rViewer).append(new String(rNode.getData()));
}
/***************************************
* Internal method to return the image for a certain node type from an
* internal cache. If the image is not in the cache already it will be read
* from the application resource by using the type code string with"I"
* appended as the resource key.
*
* @param sNodeType The node type code
*
* @return The Image for the node or NULL if no Image shall be displayed
*/
protected Image getNodeImage(String sNodeType)
{
Image rImage = (Image) aNodeImages.get(sNodeType);
if (rImage == null)
{
// if the image is not cached already get it from the resource (by
// appending "I" to the node type key) and store it in the HashTable
rImage = ResourceBundle.getCurrent().getImage(sNodeType + "I");
if (rImage != null)
{
aNodeImages.put(sNodeType, rImage);
}
}
return rImage;
}
/***************************************
* Internal method as shortcut to {@link ResourceBundle#getString(String)}.
*
* @see ResourceBundle#getString(String)
*/
protected String getString(String sKey)
{
return ResourceBundle.getCurrent().getString(sKey);
}
}