implementation.java

来自「j2me设计的界面包」· Java 代码 · 共 905 行 · 第 1/3 页

JAVA
905
字号
        // this can happen when traversing from the native form to the current form        // caused by a keypress        if(keyCode != lastKeyPressed) {            return;        }        addInputEvent(createKeyEvent(keyCode, false));    }    protected  void keyRepeatedInternal(final int keyCode){    }        protected  void pointerDragged(final int x, final int y){        Form current = Display.getInstance().getCurrentInternal();        if(current == null){            return;        }        //send the drag events to the form only after latency of 7 drag events,         //most touch devices are too sensitive and send too many drag events.        //7 is just a latency const number that is pretty good for most devices        //this may be tuned for specific devices.        dragActivationCounter++;        if(dragActivationCounter < 7){            return;        }        addInputEvent(createPointerDragEvent(x, y));    }    protected  void pointerPressed(final int x,final int y){        Form current = Display.getInstance().getCurrentInternal();        if(current == null){            return;        }        addInputEvent(createPointerEvent(x, y, true));    }    protected  void pointerReleased(final int x,final int y){        dragActivationCounter = 0;        Form current = Display.getInstance().getCurrentInternal();        if(current == null){            return;        }        addInputEvent(createPointerEvent(x, y, false));    }    protected void sizeChanged(int w, int h){        Form current = Display.getInstance().getCurrentInternal();        if(current == null || currentTextBox != null/*patch for a WTK bug*/){            return;        }        if(w == current.getWidth() && h == current.getHeight()) {            return;        }                    addInputEvent(createSizeChangedEvent(w, h));    }    /**     * Used by the flush functionality which doesn't care much about component     * animations     */    public boolean shouldEDTSleepNoFormAnimation() {        Display d = Display.getInstance();        Vector animationQueue = d.getAnimationQueue();        synchronized(Display.lock){            return (animationQueue == null || animationQueue.size() == 0) &&                    inputEvents.size() == 0 &&                    paintQueueFill == 0 &&                    d.hasNoSerialCallsPending();        }    }    /**     * Returns true for a case where the EDT has nothing at all to do     */    public boolean shouldEDTSleep() {        Display d = Display.getInstance();        Vector animationQueue = d.getAnimationQueue();        Form current = d.getCurrentInternal();        return (current == null || (!current.hasAnimations())) &&                (animationQueue == null || animationQueue.size() == 0) &&                inputEvents.size() == 0 &&                paintQueueFill == 0 &&                d.hasNoSerialCallsPending();    }    /**     * Run is used both to invoke the main EDT loop               */    public void run() {        mainEDTLoop();    }    /**     * This method represents the event thread for the UI library on which      * all events are carried out. It differs from the MIDP event thread to      * prevent blocking of actual input and drawing operations. This also     * enables functionality such as "true" modal dialogs etc...     */    private void mainEDTLoop() {        Vector animationQueue = Display.getInstance().getAnimationQueue();        try {            synchronized(Display.lock){                // when there is no current form the EDT is useful only                // for features such as call serially                while(Display.getInstance().getCurrentInternal() == null) {                    if(shouldEDTSleep()) {                        Display.lock.wait();                    }                    // paint transition or intro animations and don't do anything else if such                    // animations are in progress...                    if(animationQueue != null && animationQueue.size() > 0) {                        paintTransitionAnimation();                        continue;                    }                    processSerialCalls();                }            }        } catch(Throwable err) {            err.printStackTrace();            Dialog.show("Error", "An internal application error occured: " + err.toString(), "OK", null);        }                while(true) {            try {                // wait indefinetly but no more than the framerate if                // there are no animations... If animations exist then                // only wait for the framerate                if(shouldEDTSleep()) {                    synchronized(Display.lock){                        Display.lock.wait();                    }                }                 edtLoopImpl();            } catch(Throwable err) {                err.printStackTrace();                Dialog.show("Error", "An internal application error occured: " + err.toString(), "OK", null);            }        }    }    /**     * Implementation of the event dispatch loop content     */    public void edtLoopImpl() {        long time = System.currentTimeMillis();        try {            Vector animationQueue = Display.getInstance().getAnimationQueue();            // transitions shouldn't be bound by framerate            if(animationQueue == null || animationQueue.size() == 0) {                // prevents us from waking up the EDT too much and                 // thus exhausting the systems resources. The + 1                // prevents us from ever waiting 0 milliseconds which                // is the same as waiting with no time limit                long currentTime = System.currentTimeMillis() + 1;                while(currentTime - time < framerateLock) {                    synchronized(Display.lock){                        Display.lock.wait(Math.min(1, framerateLock - (currentTime - time)));                    }                    currentTime = System.currentTimeMillis() + 1;                }            } else {                // paint transition or intro animations and don't do anything else if such                // animations are in progress...                paintTransitionAnimation();                return;            }        } catch(InterruptedException ignor) {}        while(inputEvents.size() > 0 && !block) {            int[] i = (int[])inputEvents.elementAt(0);            inputEvents.removeElementAt(0);            handleEvent(i);        }                paintDirty();        // draw the animations        Display.getInstance().getCurrentInternal().repaintAnimations();                // check key repeat events        if(!block && keyRepeatCharged && nextKeyRepeatEvent <= System.currentTimeMillis()) {            Display.getInstance().getCurrentInternal().keyRepeated(keyRepeatValue);            nextKeyRepeatEvent = System.currentTimeMillis() + keyRepeatNextIntervalTime;        }        processSerialCalls();    }        /**     * Restores the menu in the given form     */    private void restoreMenu(Form f) {        if(f != null) {            f.restoreMenu();        }    }        private void paintTransitionAnimation() {        Vector animationQueue = Display.getInstance().getAnimationQueue();        Animation ani = (Animation) animationQueue.elementAt(0);        if (!ani.animate()) {            animationQueue.removeElementAt(0);            if (ani instanceof Transition) {                Form current = Display.getInstance().getCurrentInternal();                Form source = (Form) ((Transition)ani).getSource();                restoreMenu(source);                Form f = (Form) ((Transition)ani).getDestination();                restoreMenu(f);                if (source == null || source == current || source == Display.getInstance().getCurrent()) {                    Display.getInstance().setCurrentForm(f);                }                ((Transition) ani).cleanup();                if (animationQueue.size() > 0) {                    ani = (Animation) animationQueue.elementAt(0);                    if (ani instanceof Transition) {                        ((Transition) ani).initTransition();                    }                }                return;            }        }        ani.paint(wrapper);        flushGraphics();        if(transitionDelay > 0) {            // yield for a fraction, some devices don't "properly" implement            // flush and so require the painting thread to get CPU too.            try {                synchronized(Display.lock){                    Display.lock.wait(transitionDelay);                }            } catch (InterruptedException ex) {                ex.printStackTrace();            }        }    }    /**     * Used by the EDT to process all the calls submitted via call serially     */    private void processSerialCalls() {        Display.getInstance().processSerialCalls();    }    /**     * Invoked on the EDT to propagate the event     */    private void handleEvent(int[] ev) {        switch(ev[0]) {        case KEY_PRESSED:            Display.getInstance().getCurrentInternal().keyPressed(ev[1]);            break;        case KEY_RELEASED:            Display.getInstance().getCurrentInternal().keyReleased(ev[1]);            break;        case POINTER_PRESSED:            Display.getInstance().getCurrentInternal().pointerPressed(ev[1], ev[2]);            break;        case POINTER_RELEASED:            Display.getInstance().getCurrentInternal().pointerReleased(ev[1], ev[2]);            break;        case POINTER_DRAGGED:            Display.getInstance().getCurrentInternal().pointerDragged(ev[1], ev[2]);            break;        case SIZE_CHANGED:            Display.getInstance().getCurrentInternal().sizeChanged(ev[1], ev[2]);            break;        }    }    void blockEvents(boolean block){        this.block = block;    }    /**     * This method is used for text input purposes only     */    public void commandAction(Command c, Displayable d) {        if (d == currentTextBox) {            if (c == CONFIRM_COMMAND) {                // confirm                String text = currentTextBox.getString();                currentTextComponent.onEditComplete(text);                currentTextComponent.fireActionEvent();            }            currentTextBox = null;            Display.getInstance().setCurrentForm(currentTextComponent.getComponentForm());            waitForEdit.setDone(true);        }    }    public void saveTextBox() {        String text = currentTextBox.getString();        currentTextComponent.onEditComplete(text);        currentTextComponent.fireActionEvent();        currentTextBox = null;        waitForEdit.setDone(true);    }}

⌨️ 快捷键说明

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