Implementing the full screen display of property details

The SysPropForm class is responsible for displaying the details of the individual system properties. The SystemProperties class calls the constructor of the SysPropForm class when a command is issued.

The SysPropFullCanvas class displays the property details in full screen mode. It renders a full screen canvas and draws the details on it.

Implementing the SysPropForm class

To implement theSysPropForm class:

  1. Create the SysPropForm.java class file.

  2. Import the required classes.

    import javax.bluetooth.BluetoothStateException;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;
    import javax.bluetooth.LocalDevice;
    import javax.microedition.m3g.Graphics3D;
    import java.util.Hashtable;
  3. Set SysPropForm to extend Form and implement CommandListener. MIDlets use the CommandListener interface to receive high-level UI events from the platform.

    public class SysPropForm extends Form implements CommandListener {
  4. Create the required variables and the SysPropForm class constructor.

        private Command backCommand;
        private Command exitCommand;
        private SystemProperties midlet;
        private String[] items;
    
        //Constructor
        public SysPropForm(String title, SystemProperties midlet, String[] list) {
            super(title);
            items = list;
            this.midlet = midlet;
            backCommand = new Command("Back", Command.BACK, 1);
            exitCommand = new Command("Exit", Command.EXIT, 1);
            addCommand(backCommand);
            addCommand(exitCommand);
            setCommandListener(this);
        }
  5. Use the update method to handle the different details that are displayed. Most details are displayed the same way, but the Bluetooth and M3G 1.1 properties are special cases that need to be handled differently.

        //Draw the details of the property
        protected void update(String title, String[] list) {
            this.setTitle(title);
            items = list;
            this.deleteAll(); //Clear the screen
    
            //Special case for Bluetooth details
            if (midlet.bt) {
                try {
                    append("BT address: " + LocalDevice.getLocalDevice().getBluetoothAddress() + "\n");
                    append("Friendly name: " + LocalDevice.getLocalDevice().getFriendlyName() + "\n");
                } catch (BluetoothStateException ex) {
                    append("BluetoothStateException: " + ex.getMessage());
                }
            }
    
            //Default way of displaying details
            for (int i = 0; i < items.length; i++) {
                append(items[i] + ":\n");
                if (midlet.bt) {
                    append(LocalDevice.getProperty(items[i]) + "\n");
                } else if (midlet.m3g) {
                    // Properties "maxViewportWidth" and "maxViewportHeight" were
                    // added in M3G 1.1, in case of M3G 1.0 this MIDlet returns
                    // "null" for these properties.
                    // Property "m3gRelease" is not mandated by the specification, but
                    // it is a implementation specific property.
                    Hashtable g3dtable = new Hashtable();
                    g3dtable = Graphics3D.getProperties();
                    Object property = g3dtable.get(list[i]);
                    if (property != null) {
                        append(property.toString() + "\n");
                    } else {
                        append("null\n");
                    }
                } else if (midlet.index == 14) {
                    try {
                        append(System.getProperty(items[i]) + "\n");
                    } catch (SecurityException se) {
                        // In case of untrusted or trusted 3rd party MIDlet
                        // SecurityException should be thrown for all the
                        // PROTECTED_SYSPROPS properties.
                        append("SecurityException: " + se.getMessage());
                    }
                } else {
                    append(System.getProperty(items[i]) + "\n");
                }
            }
        }
  6. Implement the commandAction method for handling commands.

        //This is called when a command is issued
        public void commandAction(Command c, Displayable d) {
            if (c == backCommand) {
                midlet.showList();
                midlet.bt = false;
                midlet.m3g = false;
            }
            if (c == exitCommand) {
                midlet.quitApp();
            }
        }
    }

Implementing the SysPropFullCanvas class

To implement theSysPropFullCanvas class:

  1. Create the SysPropFullCanvas.java class file.

  2. Import the required classes.

    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.Font;
  3. Set SysPropFullCanvas to extend Canvas and implement CommandListener. MIDlets use the CommandListener interface to receive high-level UI events from the platform.

    public class SysPropFullCanvas extends Canvas implements CommandListener {
  4. Create the required variables and the SysPropFullCanvas class constructor.

        private SystemProperties midlet;
        private Command backCommand;
        private Command exitCommand;
        private int w, h, fontheight;
        private Font font;
        private String value;
    
        //Constructor
        public SysPropFullCanvas(SystemProperties midlet) {
            this.midlet = midlet;
            this.setFullScreenMode(true);
            backCommand = new Command("Back", Command.BACK, 1);
            exitCommand = new Command("Exit", Command.EXIT, 1);
            this.setCommandListener(this);
            this.addCommand(backCommand);
            this.addCommand(exitCommand);
            font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
            fontheight = font.getHeight();
        }
  5. Use the paint method to draw the background and call the update method.

        //Paint fullscreen background
        protected void paint(Graphics g) {
            w = this.getWidth();
            h = this.getHeight();
            g.setColor(0, 0, 0);
            g.fillRect(0, 0, w, h);
            g.setColor(255, 255, 255);
            g.setFont(font);
            update(SystemProperties.S60_5TH_SYSPROPS, g);
        }
  6. Implement the commandAction method for handling commands.

        public void commandAction(Command c, Displayable d) {
            if (c == backCommand) {
                midlet.showList();
            }
            if (c == exitCommand) {
                midlet.notifyDestroyed();
            }
        }
  7. Use the update method to draw the actual property details.

        //Draws the details
        protected void update(String[] list, Graphics g) {
            int length = list.length;
            for (int i = 0; i < length; i++) {
                value = System.getProperty(list[i]);
                if (value == null) {
                    value = "null";
                }
                g.drawString(list[i], 0, (i * 2) * fontheight, Graphics.LEFT | Graphics.TOP);
                g.drawString(value, 0, (i * 2) * fontheight + fontheight, Graphics.LEFT | Graphics.TOP);
            }
        }
    }