FileDocCategorySizeDatePackage
ItemMIDlet.javaAPI DocExample17751Fri Oct 19 23:20:32 BST 2001ora.ch4

ItemMIDlet

public class ItemMIDlet extends javax.microedition.midlet.MIDlet implements javax.microedition.lcdui.CommandListener, javax.microedition.lcdui.ItemStateListener

Fields Summary
private javax.microedition.lcdui.Display
display
protected boolean
started
private javax.microedition.lcdui.Command
exitCommand
private javax.microedition.lcdui.Command
backCommand
private javax.microedition.lcdui.List
examplesList
private javax.microedition.lcdui.Screen[]
screens
private String[]
examples
private javax.microedition.lcdui.AlertType[]
alertTypes
private String[]
alertTypeNames
Constructors Summary
Methods Summary
private voidaddCommands(javax.microedition.lcdui.Displayable d)

        d.addCommand(exitCommand);
        d.addCommand(backCommand);
        d.setCommandListener(this);
        if (d instanceof Form) {
            ((Form)d).setItemStateListener(this);
        }
    
public voidcommandAction(javax.microedition.lcdui.Command c, javax.microedition.lcdui.Displayable d)

        if (d == examplesList) {
            // New example selected
            int index = examplesList.getSelectedIndex();
            display.setCurrent(screens[index]);
        } else if (c == exitCommand) {
            // Exit. No need to call destroyApp
            // because it is empty.
            notifyDestroyed();
        } else if (c == backCommand) {
            // Go back to main selection list
            display.setCurrent(examplesList);
        } else if (c == List.SELECT_COMMAND) {
            // Selection made in the IMPLICIT LIST
            handleChoiceSelection((Choice)d);
        }
    
private javax.microedition.lcdui.ChoiceGroupcreateAlertChoiceGroup(boolean includeNone)

        ChoiceGroup group = new ChoiceGroup("Alert Type", Choice.EXCLUSIVE);
        int count = includeNone ? alertTypeNames.length : alertTypeNames.length - 1;
        for (int i = 0; i < count; i++) {
            group.append(alertTypeNames[i], null);
        }
        return group;
    
private javax.microedition.lcdui.ScreencreateAlertForm()

        final Form form = new Form("Alert");
        
        // A ChoiceGroup that allows the user to choose between
        // a modal Alert or an Alert with a finite timeout.
        final ChoiceGroup limitGroup = new ChoiceGroup("Timeout", Choice.EXCLUSIVE);
        limitGroup.append("Forever", null);
        limitGroup.append("Bounded 5s", null);
        form.append(limitGroup);
        
        // A Gauge that allows the user to choose the 
        // timeout for the Alert. This appears only if
        // the "Bounded" option is selected.
        final Gauge gauge = new Gauge(null, true, 30, 5);
        
        // A ChoiceGroup that lets the user pick the 
        // type of Alert
        final ChoiceGroup group = createAlertChoiceGroup(true); 
        form.append(group);  
        
        // A Command that is used to display the Alert
        final Command okCommand = new Command("OK", Command.OK, 0);
        
        // Add CommandListener for back/exit commands
        // and the OK command, which is specific to this Form
        form.addCommand(okCommand);
        form.addCommand(exitCommand);
        form.addCommand(backCommand);
        form.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                if (c == okCommand) {
                    // Create and display the Alert
                    
                    // Get the alert type from the 
                    // second ChoiceGroup
                    int selectedType = group.getSelectedIndex();
                    AlertType alertType = alertTypes[selectedType];
                    String name = alertTypeNames[selectedType];
                    
                    // Get the timeout. This is FOREVER if the
                    // first item in the first ChoiceGroup is
                    // selected. Otherwise it is the value in
                    // the Gauge
                    int timeout = Alert.FOREVER;
                    String timeoutString = "none";
                    if (limitGroup.isSelected(1)) {
                        // "Bounded" selected - get the timeout.
                        timeout = gauge.getValue() * 1000;
                        timeoutString = gauge.getValue() + "s";                        
                    }
                    
                    // Create the Alert and set the timeout
                    Alert alert = new Alert("Alert!",
                        "Alert type: " + name + "\nTimeout = " + timeoutString,
                        null, alertType);
                    alert.setTimeout(timeout);
                    
                    // Display the Alert.
                    display.setCurrent(alert);                               
                } else {
                    // Delegate others to the usual commandAction
                    ItemMIDlet.this.commandAction(c, d);
                }                
            }
        });
        
        // Set our own ItemStateListener
        form.setItemStateListener(new ItemStateListener() {
            public void itemStateChanged(Item item) {
                if (item == limitGroup) {
                    if (limitGroup.getSelectedIndex() == 0) {
                        // Get rid of the Gauge
                        form.delete(1);
                    } else {
                        // Add the Gauge
                        form.insert(1, gauge);
                    }
                } else if (item == gauge) {               
                    int value = gauge.getValue();
                    limitGroup.set(1, "Bounded " + value + "s", null);
                }
            }
        });
        return form;
    
private javax.microedition.lcdui.FormcreateChoiceGroupForm()

        Form form = new Form("ChoiceGroup");
        
        try {
            Image red = Image.createImage("/ora/ch4/resources/red.png");
            Image green = Image.createImage("/ora/ch4/resources/green.png");
            Image blue = Image.createImage("/ora/ch4/resources/blue.png");

            // Exclusive choice group
            String[] strings = new String[] { "Red", "Green", "Blue" };
            Image[] images = new Image[] { red, green, blue };
            ChoiceGroup exGroup = new ChoiceGroup("Choose one", ChoiceGroup.EXCLUSIVE,
                                                        strings, images);
            form.append(exGroup);

            // Multiple choice group
            ChoiceGroup multiGroup = new ChoiceGroup("Choose any", ChoiceGroup.MULTIPLE);
            form.append(multiGroup);
            multiGroup.append("Use SSL", null);
            multiGroup.append("Reconnect on failure", null);
            multiGroup.append("Enable tracing", null);
        } catch (IOException ex) {
            form.append("Failed to load images");
        }
        
        addCommands(form);
        return form;
    
private voidcreateCommands()

        exitCommand = new Command("Exit", Command.EXIT, 0);
        backCommand = new Command("Back", Command.BACK, 1);
    
private javax.microedition.lcdui.FormcreateDateFieldForm()

        Form form = new Form("DateField");
        
        // Get Calendar for the epoch date and time
        Calendar baseCal = Calendar.getInstance();
        Date baseDate = new Date(0);
        baseCal.setTime(baseDate);
        
        // Get Calendar for now and use the epoch
        // values to reset the date to the epoch.
        Calendar cal = Calendar.getInstance();
        Date now = new Date();       
        cal.setTime(now);
        cal.set(Calendar.YEAR, baseCal.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, baseCal.get(Calendar.MONTH));
        cal.set(Calendar.DATE, baseCal.get(Calendar.DATE));
        
        DateField timeOnly = new DateField("Time", DateField.TIME);
        DateField dateOnly = new DateField("Date", DateField.DATE);
        DateField both = new DateField("Both", DateField.DATE_TIME);
        
        timeOnly.setDate(cal.getTime());
        dateOnly.setDate(now);
        both.setDate(now);
        
        form.append(timeOnly);
        form.append(dateOnly);
        form.append(both);
       
        addCommands(form);
        return form;
    
private javax.microedition.lcdui.FormcreateGaugeForm()

        Form form = new Form("Gauge");
        
        form.append(new Gauge(null, true, 100, 50));
        form.append(new Gauge(null, true, 100, 25));
        form.append(new Gauge(null, false, 100, 50));
        
        addCommands(form);
        return form;
    
private javax.microedition.lcdui.FormcreateImageItemForm()

        Form form = new Form("ImageItem");
        
        try {
            Image red = Image.createImage("/ora/ch4/resources/red.png");
            Image blue = Image.createImage("/ora/ch4/resources/blue.png");

            // ImageItems with labels
            // (1)
            form.append(new ImageItem("Center", red, ImageItem.LAYOUT_CENTER, null));
            
            // (2)
            form.append(new ImageItem("Left", red, ImageItem.LAYOUT_LEFT, null));
            
            // (3)
            form.append(new ImageItem("Right", red, ImageItem.LAYOUT_RIGHT, null));
            
            // (4)
            form.append(new ImageItem("Default", red, ImageItem.LAYOUT_DEFAULT, null));
            
            // ImageItems with no labels
            // (5)
            form.append(new ImageItem(null, blue, 
                    ImageItem.LAYOUT_NEWLINE_BEFORE | 
                    ImageItem.LAYOUT_CENTER | 
                    ImageItem.LAYOUT_NEWLINE_AFTER, null));
            
            // (6)
            form.append(new ImageItem(null, blue, 
                    ImageItem.LAYOUT_NEWLINE_BEFORE | 
                    ImageItem.LAYOUT_DEFAULT | 
                    ImageItem.LAYOUT_NEWLINE_AFTER, null));
            
            // (7)
            form.append(new ImageItem(null, blue, 
                    ImageItem.LAYOUT_NEWLINE_BEFORE | 
                    ImageItem.LAYOUT_RIGHT | 
                    ImageItem.LAYOUT_NEWLINE_AFTER, null));
            
            // (8)
            form.append(new ImageItem(null, blue, ImageItem.LAYOUT_DEFAULT, null)); 
            form.append(new ImageItem(null, blue, ImageItem.LAYOUT_DEFAULT, null)); 
        } catch (IOException ex) {
            form.append("Failed to load images");
        }
        
        addCommands(form);
        return form;
    
private voidcreateList()

        examplesList = new List("Select Example", List.IMPLICIT);
        for (int i = 0; i < examples.length; i++) {
            examplesList.append(examples[i], null);
        } 
        examplesList.setCommandListener(this);
    
private javax.microedition.lcdui.ScreencreateListExample()

        List list = new List("List", List.IMPLICIT);

        try {
            Image red = Image.createImage("/ora/ch4/resources/red.png");
            Image green = Image.createImage("/ora/ch4/resources/green.png");
            Image blue = Image.createImage("/ora/ch4/resources/blue.png");
            
            list.append("Red", red);
            list.append("Green", green);
            list.append("Blue", blue);
        } catch (IOException ex) {
            Form form = new Form("Error");
            form.append("Failed to load images");
            return form;
        }
        
        addCommands(list);
        return list;
    
private voidcreateScreens()

        screens = new Screen[examples.length];
        screens[0] = createStringsForm();
        screens[1] = createTextFieldForm();
        screens[2] = createDateFieldForm();
        screens[3] = createImageItemForm();
        screens[4] = createGaugeForm();
        screens[5] = createChoiceGroupForm();
        screens[6] = createListExample();
        screens[7] = createAlertForm();
        screens[8] = createSoundsForm();
    
private javax.microedition.lcdui.ScreencreateSoundsForm()

        final Form form = new Form("Sounds");
        
        // A ChoiceGroup that lets the user pick the 
        // type of sound
        final ChoiceGroup group = createAlertChoiceGroup(false); 
        form.append(group);  

        addCommands(form);
        
        // Set our own ItemStateListener
        form.setItemStateListener(new ItemStateListener() {
            public void itemStateChanged(Item item) {
                // Get the alert type from the ChoiceGroup
                int selectedType = group.getSelectedIndex();
                AlertType alertType = alertTypes[selectedType];
                boolean result = alertType.playSound(display);
                System.out.println("A sound was " + (result ? "" : "not ")
                                    + "played.");
                
            }
        });
        return form;
    
private javax.microedition.lcdui.FormcreateStringsForm()

        Form form = new Form("StringItem");
        
        form.append(new StringItem("State ", "OK"));
        form.append(new StringItem(null, "No label\n"));
        form.append(new StringItem(null, "Line\nbreak"));
        form.append(new StringItem("Label", "Text."));
        form.append(new StringItem("Label2 ", "Text2."));
        
        addCommands(form);
        return form;
    
private javax.microedition.lcdui.FormcreateTextFieldForm()

        Form form = new Form("TextField");
        
        form.append(new TextField("Any", null, 8, TextField.ANY));
        form.append(new TextField("Phone", "1234567890", 10, TextField.PHONENUMBER));
        form.append(new TextField("Number", "12345", 8, TextField.NUMERIC));
        form.append(new TextField("Password", null, 8, TextField.PASSWORD | TextField.NUMERIC));
        
        addCommands(form);
        return form;
    
protected voiddestroyApp(boolean unconditional)

    
private voidhandleChoiceSelection(javax.microedition.lcdui.Choice choice)

        int count = choice.size();
        boolean[] states = new boolean[count];
        int selCount = choice.getSelectedFlags(states);
        if (selCount > 0) {
            System.out.println("Selected items:");
            for (int i = 0; i < count; i++) {
                if (states[i]) {
                    System.out.println("\t" + choice.getString(i));
                }
            }
        } else {
            System.out.println("No selected items.");
        }
        int selectedIndex = choice.getSelectedIndex();
        System.out.println("Selected index is " + selectedIndex);        
    
public voiditemStateChanged(javax.microedition.lcdui.Item item)

        
        if (item instanceof TextField) {
            System.out.println("Text field content: <" +
                            ((TextField)item).getString() + ">");
        } else if (item instanceof DateField) {
            DateField df = (DateField)item;
            Date date = df.getDate();
            if (date != null) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                System.out.println("Date field content set to " + date);                
            } else {
                System.out.println("Date field content set to null");
            }
        } else if (item instanceof Gauge) {
            int value = ((Gauge)item).getValue();
            System.out.println("Gauge value set to " + value);
        } else if (item instanceof ChoiceGroup) {
            handleChoiceSelection((Choice)item);
        } else {
            System.out.println("Item state changed in " + item);
        }
    
protected voidpauseApp()

    
protected voidstartApp()


       
        if (!started) {
            started = true;
            display = Display.getDisplay(this);
            
            // Create the common commands
            createCommands();
            
            // Create the screens
            createScreens();
            
            // Create the list of examples
            createList();
            
            // Start with the List
            display.setCurrent(examplesList);
        }