⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manager.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * Appends a names of all the MIDlets in a suite to a Form, one per line.     *     * @param midletSuite suite of MIDlets     * @param form form to append to     */    private void appendMIDletsToForm(MIDletSuite midletSuite, Form form) {        int numberOfMidlets;        MIDletInfo midletInfo;        numberOfMidlets = midletSuite.getNumberOfMIDlets();        for (int i = 1; i <= numberOfMidlets; i++) {            midletInfo = new MIDletInfo(                             midletSuite.getProperty("MIDlet-" + i));            form.append(midletInfo.name);            form.append("\n");        }    }    /**     * Read the set of MIDlet names, icons and classes     * Fill in the list.     */    private void setupList() {        if (mlist == null) {            mlist = new List(Resource.getString("Main Screen"),                              Choice.IMPLICIT);            // Add each midlet            for (int i = 0; i < mcount; i++) {                mlist.append(" " + minfo[i].displayName, minfo[i].icon);            }        }    }            /**     * Read in and create a MIDletInfo for each MIDlet suite.     */    private void readMIDletSuiteInfo() {        String[] suiteNames;        MIDletSuite midletSuite;        int i;        int numberOfMidlets;        String nth;        String attr;        byte[] rawPng;        suiteNames = installer.list();        for (i = 0; i < suiteNames.length; i++) {            try {                midletSuite = installer.getMIDletSuite(suiteNames[i]);                numberOfMidlets = midletSuite.getNumberOfMIDlets();                if (numberOfMidlets > 1) {                    addMIDletSuite(new MIDletSuiteInfo(suiteNames[i],                                                       midletSuite));                }                for (int j = 1; j <= numberOfMidlets; j++) {                    nth = "MIDlet-"+ j;                    attr = midletSuite.getProperty(nth);                    if (attr == null || attr.length() == 0) {                        break;                    }                                        addMIDletSuite(new MIDletSuiteInfo(suiteNames[i],                                                       midletSuite, attr));                }                if (numberOfMidlets == 1) {                    minfo[mcount - 1].singleMidlet = true;                    minfo[mcount - 1].icon = getSingleSuiteIcon();                }            } catch (Exception e) {                // move on to the next suite            }        }    }    /**     * Add a MIDlet suite to the list.     * @param info MIDlet suite information to add to MIDlet     */    private void addMIDletSuite(MIDletSuiteInfo info) {        if (mcount >= minfo.length) {            MIDletSuiteInfo[] n = new MIDletSuiteInfo[mcount+4];            System.arraycopy(minfo, 0, n, 0, mcount);            minfo = n;        }        minfo[mcount++] = info;    }    /**     * Simple attribute storage for MIDlet suites     */    private class MIDletSuiteInfo {        /** Midlet suite interface for displaying its info. */        MIDletSuite midletSuite;        /** Storage name of the MIDlet suite. */        String storageName;        /** Display name of the MIDlet suite. */        String displayName;        /** Name of the MIDlet to run. */        String midletToRun;        /** Icon for this suite. */        Image icon;        /** Icon name for the first midlet. */        String iconName;        /** Is this single MIDlet MIDlet suite. */        boolean singleMidlet = false;        /**         * Constructs a MIDletSuiteInfo object for a suite         *         * @param theStorageName name the installer has for this suite         * @param theMidletSuite MIDletSuite object for this suite         */        MIDletSuiteInfo(String theStorageName, MIDletSuite theMidletSuite) {            midletSuite = theMidletSuite;            displayName = midletSuite.getProperty(Installer.SUITE_NAME_PROP);            if (displayName == null) {                displayName = theStorageName;            }            icon = getSuiteIcon();            storageName = theStorageName;        }        /**         * Constructs a MIDletSuiteInfo object for a MIDlet of a suite         *         * @param theStorageName name the installer has for this suite         * @param theMidletSuite MIDletSuite object for this suite         * @param attr an MIDlet-<n> value for the MIDlet         */        MIDletSuiteInfo(String theStorageName, MIDletSuite theMidletSuite,                        String attr) {            MIDletInfo midletInfo = new MIDletInfo(attr);            midletSuite = theMidletSuite;            displayName = midletInfo.name;            icon = getEmptyIcon();            iconName = midletInfo.icon;            storageName = theStorageName;        }    }    /** A class to install a suite in a background thread. */    private class BackgroundInstaller implements Runnable, InstallListener {        /** Parent manager. */        private Manager parent;        /** JAD URL to install from. */        private String jadUrl;        /** Name of MIDlet suite. */        private String name;        /**         * Message for the user after the current install completes         * successfully.         */        private String successMessage;        /** Flag to overwrite the current suite. */        private boolean overwrite;        /** State of the install. */        InstallState installState;        /** Signals that the user wants the install to continue. */        boolean continueInstall;        /**         * Construct a BackgroundInstaller.         *         * @param theParent parent Mangager of this object         * @param theJadUrl where to get the JAD.         * @param theName name of the MIDlet suite         * @param theSuccessMessage message to display to user upon success         * @param overwriteFlag if true the current suite should be         *                      overwritten without asking the user.         */        private BackgroundInstaller(Manager theParent, String theJadUrl,                String theName, String theSuccessMessage,                boolean overwriteFlag) {            parent = theParent;            jadUrl = theJadUrl;            name = theName;            successMessage = theSuccessMessage;            overwrite = overwriteFlag;        }        /**         * Run the installer.         */        public void run() {            try {                parent.installer.install(jadUrl, overwrite, this);                parent.displaySuccessMessage(successMessage);                /*                 * We need to prevent "flashing" on fast development                 * platforms.                 */                while ((System.currentTimeMillis() -                        parent.lastDisplayChange) < parent.ALERT_TIMEOUT);                parent.destroyApp(false);                parent.notifyDestroyed();                return;            } catch (Throwable ex) {                String msg;                if (parent.installer != null &&                        parent.installer.wasStopped()) {                    /*                     * We need to prevent "flashing" on fast development                     * platforms.                     */                    while ((System.currentTimeMillis() -                        parent.lastDisplayChange) < parent.ALERT_TIMEOUT);                    parent.display.setCurrent(parent.mlist);                    return;                }                if (ex instanceof InvalidJadException) {                    msg = translateJadException((InvalidJadException)ex,                                                name, jadUrl);                } else if (ex instanceof IOException) {                    msg = Resource.getString(                          "The connection dropped and the installation " +                          "did not complete. Please try installing again.");                } else {                    msg = ex.getMessage();                }                            Alert a = new Alert(Resource.getString("Install Error"),                                     msg, null, AlertType.ERROR);                a.setTimeout(Alert.FOREVER);                parent.display.setCurrent(a, parent.mlist);            }        }        /**         * Called with the current state of the install so the user can be         * asked to override the warning. Calls the parent to display the         * warning to the user and then waits on the state object for         * user's response.         *         * @param state current state of the install.         *         * @return true if the user wants to continue,         *         false to stop the install         */        public boolean warnUser(InstallState state) {            boolean temp;            installState = state;            InvalidJadException e = installState.getLastException();            if (e.getReason() == InvalidJadException.UNAUTHORIZED) {                parent.getUsernameAndPassword();            } else {                parent.warnUser(name, jadUrl, e);            }            synchronized (installState) {                try {                    installState.wait();                } catch (InterruptedException ie) {                    // ignore                }            }            installState = null;            temp = continueInstall;            continueInstall = false;            return temp;        }        /**         * Called with the current status of the install.         * Changes the status alert box text based on the status.         *         * @param status current status of the install.         */        public void updateStatus(int status) {            parent.updateStatus(status);        }    }    /** A class to get the install list in a background thread. */    private class BackgroundInstallListGetter implements Runnable {        /** Parent manager. */        private Manager parent;        /** URL of the list. */        private String url;        /**         * Construct a BackgroundInstallListGetter.         *         * @param theParent parent Mangager of this object         * @param theUrl where to get the list of suites to install.         */        private BackgroundInstallListGetter(Manager theParent,                                            String theUrl) {            parent = theParent;            url = theUrl;        }        /**         * Get the list of suites for the user to install.         * The suites that are listed         * are the links on a web page that end with .jad.         */        public void run() {            StreamConnection conn = null;            InputStreamReader in = null;            String errorMessage;            long startTime;            startTime = System.currentTimeMillis();            try {                conn = (StreamConnection)Connector.open(url, Connector.READ);                in = new InputStreamReader(conn.openInputStream());                try {                    displayConnectingForm("Getting Install List", "", url);                    parent.installList =                        SuiteDownloadInfo.getDownloadInfoFromPage(in);                                        if (parent.installList.size() > 0) {                        parent.installListBox = new List(Resource.getString(                                                "Select one to install:"),                                                Choice.IMPLICIT);                        // Add each suite                        for (int i = 0; i < parent.installList.size(); i++) {                            SuiteDownloadInfo suite =                                (SuiteDownloadInfo)installList.elementAt(i);                            parent.installListBox.append(suite.label,                                                         (Image)null);                        }                        parent.installListBox.addCommand(backCmd);                        parent.installListBox.addCommand(installCmd);                        parent.installListBox.setCommandListener(parent);                        /*                         * We need to prevent "flashing" on fast development                         * platforms.                          */                        while (System.currentTimeMillis() - startTime <                             parent.ALERT_TIMEOUT);                        display.setCurrent(parent.installListBox);                        return;                    }                    errorMessage = Resource.getString(                        "No MIDlet Suites found. " +                        "Check the URL to make sure it is correct.");                } catch (Exception ex) {                    errorMessage = ex.getMessage();                }            } catch (Exception ex) {                errorMessage = Resource.getString(                    "The connection failed. Please check the website " +                    "URL and try again.");            } finally {                try {                    conn.close();                    in.close();                } catch (Exception e) {                    // ignore                }            }            Alert a = new Alert(Resource.getString("Error"),                                 errorMessage, null, AlertType.ERROR);            a.setTimeout(Alert.FOREVER);            display.setCurrent(a, parent.mlist);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -