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

📄 display.java

📁 j2me设计的界面包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @return true if we are currently in the event dispatch thread; 
     * otherwise false
     */
    public boolean isEdt() {
        return edt == Thread.currentThread();
    }
    
    /**
     * Plays sound for the dialog
     */
    void playDialogSound(final int type) {
        implementation.playDialogSound(type);
    }
    
    /**
     * Causes the runnable to be invoked on the event dispatch thread. This method
     * returns immediately and will not wait for the serial call to occur 
     * 
     * @param r runnable (NOT A THREAD!) that will be invoked on the EDT serial to
     * the paint and key handling events 
     * @throws IllegalStateException if this method is invoked on the event dispatch thread (e.g. during
     * paint or event handling).
     */
    public void callSerially(Runnable r){
        if(isEdt()) {
            throw new IllegalStateException("Call serially must never be invoked from the EDT");
        }
        synchronized(lock) {
            pendingSerialCalls.addElement(r);
            lock.notify();
        }
    }
    
    
    /**
     * Identical to callSerially with the added benefit of waiting for the Runnable method to complete.
     * 
     * @param r runnable (NOT A THREAD!) that will be invoked on the EDT serial to
     * the paint and key handling events 
     * @throws IllegalStateException if this method is invoked on the event dispatch thread (e.g. during
     * paint or event handling).
     */
    public void callSeriallyAndWait(Runnable r){
        RunnableWrapper c = new RunnableWrapper(r, 0);
        callSerially(c);
        synchronized(lock) {
            while(!c.isDone()) {
                try {
                    lock.wait();
                } catch(InterruptedException err) {}
            }
        }
    }
    
    /**
     * Allows us to "flush" the edt to allow any pending transitions and input to go
     * by before continuing with our other tasks.
     */
    void flushEdt() {
        while(!implementation.shouldEDTSleepNoFormAnimation()) {
            implementation.edtLoopImpl();
        }
    }
    
    boolean hasNoSerialCallsPending() {
        return pendingSerialCalls.size() == 0;
    }
    
    /**
     * Used by the EDT to process all the calls submitted via call serially
     */
    void processSerialCalls() {
        int size = pendingSerialCalls.size();
        if(size > 0) {
            Runnable[] array = new Runnable[size];

            // copy all elements to an array and remove them otherwise invokeAndBlock from
            // within a callSerially() can cause an infinite loop...
            for(int iter = 0 ; iter < size ; iter++) {
                array[iter] = (Runnable)pendingSerialCalls.elementAt(iter);
            }

            pendingSerialCalls.removeAllElements();

            for(int iter = 0 ; iter < size ; iter++) {
                array[iter].run();
            }

            // after finishing an event cycle there might be serial calls waiting
            // to return.
            synchronized(lock){
                lock.notify();
            }
        }
    }

    /**
     * Invokes runnable and blocks the current thread, if the current thread is the
     * edt it will still be blocked however a separate thread would be launched
     * to perform the duties of the EDT while it is blocked. Once blocking is finished
     * the EDT would be restored to its original position. This is very similar to the
     * "foxtrot" Swing toolkit and allows coding "simpler" logic that requires blocking
     * code in the middle of event sensitive areas.
     * 
     * @param r runnable (NOT A THREAD!) that will be invoked synchroniously by this method
     */
    public void invokeAndBlock(Runnable r){
        if(isEdt()) {
            synchronized(lock) {
                // this class allows a runtime exception to propogate correctly out of the
                // internal thread
                RunnableWrapper w = new RunnableWrapper(r, 1);
                Thread t = new Thread(w);
                t.start();

                // loop over the EDT until the thread completes then return
                while(t.isAlive()) {
                    try {
                        if(implementation.shouldEDTSleep()) {
                            lock.wait(50);
                        } else {
                            implementation.edtLoopImpl();
                        }
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                // if the thread thew an exception we need to throw it onwards
                if(w.getErr() != null) {
                    throw w.getErr();
                }
            }
        } else {
            r.run();
        }
    }

    /**
     * Indicates if this is a touch screen device that will return pen events,
     * defaults to true if the device has pen events but can be overriden by
     * the developer.
     */
    public boolean isTouchScreenDevice() {
        return touchScreen;
    }
    
    /**
     * Indicates if this is a touch screen device that will return pen events,
     * defaults to true if the device has pen events but can be overriden by
     * the developer.
     */
    public void setTouchScreenDevice(boolean touchScreen) {
        this.touchScreen = touchScreen;
    }
    
    /**
     * Displays the given Form on the screen.
     * 
     * @param newForm the Form to Display
     */
    void setCurrent(final Form newForm){
        if(edt == null) {
            throw new IllegalStateException("Initialize must be invoked before setCurrent!");
        }
        
        if(editingText) {
            switch(showDuringEdit) {
                case SHOW_DURING_EDIT_ALLOW_DISCARD:
                    break;
                case SHOW_DURING_EDIT_ALLOW_SAVE:
                    implementation.saveTextBox();
                    break;
                case SHOW_DURING_EDIT_EXCEPTION:
                    throw new IllegalStateException("Show during edit");
                case SHOW_DURING_EDIT_IGNORE:
                    return;
                case SHOW_DURING_EDIT_SET_AS_NEXT:
                    current = newForm;
                    return;
            }
        }
        
        if(!isEdt()) {
            callSerially(new RunnableWrapper(newForm, null));
            return;
        }
        
        if(current != null){
            current.hidePopups();
            if(current.isInitialized()) {
                current.deinitializeImpl();
            }
        }
        if(!newForm.isInitialized()) {
            newForm.initComponentImpl();
        }
        
        newForm.setShouldCalcPreferredSize(true);
        newForm.layoutContainer();

        synchronized(lock) {
            boolean transitionExists = false;
            Form current = this.current;
            if(animationQueue != null && animationQueue.size() > 0) {
                Object o = animationQueue.lastElement();
                if(o instanceof Transition) {
                    current = (Form)((Transition)o).getDestination();
                }
            }

            // make sure the fold menu occurs as expected then set the current
            // to the correct parent!
            if(current != null && current instanceof Dialog && ((Dialog)current).isMenu()) {
                Transition t = current.getTransitionOutAnimator();
                if(t != null) {
                    // go back to the parent form first
                    initTransition(t.copy(), current, ((Dialog)current).getPreviousForm());
                }
                current = ((Dialog)current).getPreviousForm();
            }
            
            // prevent the transition from occuring from a form into itself
            if(newForm != current) {
                if((current != null && current.getTransitionOutAnimator() != null) || newForm.getTransitionInAnimator() != null) {
                    if(animationQueue == null) {
                        animationQueue = new Vector();
                    }
                    // prevent form transitions from breaking our dialog based
                    // transitions which are a bit sensitive
                    if(current != null && (!(newForm instanceof Dialog))) {
                        Transition t = current.getTransitionOutAnimator();
                        if(current != null && t != null) {
                            initTransition(t.copy(), current, newForm);
                            transitionExists = true;
                        }
                    }
                    if(current != null && !(current instanceof Dialog)) {
                        Transition t = newForm.getTransitionInAnimator();
                        if(t != null) {
                            initTransition(t.copy(), current, newForm);
                            transitionExists = true;
                        }
                    }
                }
            } 
            lock.notify();
            
            if(!transitionExists) {
                if(animationQueue == null || animationQueue.size() == 0) {
                    setCurrentForm(newForm);
                } else {
                    // we need to add an empty transition to "serialize" this
                    // screen change...
                    Transition t = CommonTransitions.createEmpty();
                    initTransition(t, current, newForm);
                }
            }
        }
    }
    
    /**
     * Initialize the transition and add it to the queue
     */
    private void initTransition(Transition transition, Form source, Form dest) {
        dest.setVisible(true);
        transition.init(source, dest);
        animationQueue.addElement(transition);
        if(animationQueue.size() == 1) {
            transition.initTransition();
        }
    }
    
    void setCurrentForm(Form newForm){
        if(this.current != null){
            this.current.setVisible(false);
        }
        this.current = newForm;
        this.current.setVisible(true);
        
        implementation.confirmControlsView();
        
        if(isEdt() && (this.current.getWidth() != implementation.getDisplayWidth() ||
           this.current.getHeight() != implementation.getDisplayHeight())){
           this.current.setSize(new Dimension(implementation.getDisplayWidth(),implementation.getDisplayHeight()));
           this.current.setShouldCalcPreferredSize(true);

⌨️ 快捷键说明

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