Create a context filter

In this task, a context filter is created that replaces the subject in the JSONObject context object with different text.

  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.sendmenu.SendCommandContextFilter;
  2. Override makeMenu() of the MainScreen class to create the menu for your application. Add the required code to set up a Send menu, such as invoking super.makeMenu(), creating and populating a context object, and retrieving a list of SendCommand objects.
    protected void makeMenu(Menu menu, int instance)
    {
        super.makeMenu(menu, instance);
    
        // add the required code to set up a Send menu here
    }
  3. Before you invoke add() to add the Send menu to the menu of your application, perform the following steps:
    1. Create a new object to represent a context filter. In this task, the MyContextFilter class is defined as a context filter for SendCommand objects.
      MyContextFilter filter = new MyContextFilter();
    2. Invoke setSendCommandContextFilter() of the SendCommand class to set the MyContextFilter object as the context filter for a SendCommand. In this task, the emailSendCommand variable represents a SendCommand object to send content using an email message.
      emailSendCommand.setSendCommandContextFilter(filter);
  4. Define the MyContextFilter class and implement the SendCommandContextFilter interface.
    class MyContextFilter implements SendCommandContextFilter
    {
  5. Implement filterContext(). This method is invoked when a BlackBerry device user clicks a Send menu item that has this context filter set.
    public JSONObject filterContext(SendCommand sendCommand)
    {
  6. Create a new JSONObject object. Invoke getContext() to retrieve the context object of the SendCommand object that the user clicked, and store it in the JSONObject.
        JSONObject newContext = sendCommand.getContext();
  7. In a try/catch block, alter the context object as appropriate. In this task, invoke put() of the JSONObject object to replace the subject text in the JSONObject. Catch a JSONException, which indicates that the specified key is null.
        try
        {
            newContext.put(SendCommandContextKeys.SUBJECT,
                           "Subject replaced by filter");
        }
        catch (JSONException e)
        {
            System.out.println(e.toString());
        }
    }

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

Was this information helpful? Send us your comments.