Create a context filter
In this task, a context filter is created that replaces the subject in the JSONObject context object with different text.
Code sample: Creating a context filter
import net.rim.blackberry.api.sendmenu.*;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.BorderFactory;
import org.json.me.JSONException;
import org.json.me.JSONObject;
public class ContextFilterDemo extends UiApplication
{
public static void main(String[] args)
{
ContextFilterDemo theApp = new ContextFilterDemo();
theApp.enterEventDispatcher();
}
public ContextFilterDemo()
{
pushScreen(new ContextFilterDemoScreen());
}
}
class ContextFilterDemoScreen extends MainScreen
{
private EditField _textToSend;
private String _textString;
public ContextFilterDemoScreen()
{
setTitle("Context Filter Demo");
_textToSend = new EditField("Send: ", "Type the text to send here",
255, Field.USE_ALL_WIDTH);
_textToSend.setBorder(BorderFactory.createBevelBorder(
new XYEdges(3, 3, 3, 3)));
_textToSend.setPadding(8, 8, 8, 8);
_textToSend.setMargin(15, 15, 15, 15);
add(_textToSend);
}
protected void makeMenu(Menu menu, int instance)
{
super.makeMenu(menu, instance);
_textString = _textToSend.getText();
JSONObject context = new JSONObject();
try
{
context.put(SendCommandContextKeys.TEXT, _textString);
context.put(SendCommandContextKeys.SUBJECT, "Your Text");
}
catch (JSONException e)
{
System.out.println(e.toString());
}
SendCommand[] sendCommands = SendCommandRepository.getInstance().get(
SendCommand.TYPE_TEXT, context, false);
if (sendCommands != null && sendCommands.length > 0)
{
MyContextFilter filter = new MyContextFilter();
for (int i = 0; i < sendCommands.length; i++)
{
sendCommands[i].setSendCommandContextFilter(filter);
}
menu.add(new SendCommandMenu(sendCommands, 0, 0));
}
}
}
class MyContextFilter implements SendCommandContextFilter
{
public JSONObject filterContext(SendCommand sendCommand)
{
JSONObject newContext = sendCommand.getContext();
try
{
newContext.put(SendCommandContextKeys.SUBJECT,
"Subject replaced by filter");
}
catch (JSONException e)
{
System.out.println(e.toString());
}
return newContext;
}
}
Next topic: Mais informações
Previous topic: Using context filters