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

📄 list.java

📁 j2me设计的界面包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                continue;            
            }
            calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true), i <= getSelectedIndex());
            
            // if the renderer is in the clipping region
            if(pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
                Object value = model.getItemAt(i);
                Component cmp = renderer.getListCellRendererComponent(this, value, i, false);
                cmp.setCellRenderer(true);
                Dimension size = pos.getSize();
                renderComponent(g, cmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                shouldBreak = true;
            } else {
                //this is relevant only if the List is not fixed.
                if(shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
                    break;
                }
            }
        }
        if(!paintFocusBehindList){
            paintFocus(g, width, pos, rendererSize);
        }else{
            calculateComponentPosition(getSelectedIndex(), width, pos, rendererSize, getElementSize(true), true);
        }
        Dimension size = pos.getSize();
        //if the animation has finished draw the selected element
        if (animationPosition == 0 && model.getSize() > 0) {
            Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(selection), selection, true);
            renderComponent(g, selected, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
        }

        g.translate(-xTranslate, -yTranslate);
    }

    private void paintFocus(Graphics g, int width, Rectangle pos, Dimension rendererSize){
        calculateComponentPosition(getSelectedIndex(), width, pos, rendererSize, getElementSize(true), true);
        Dimension size = pos.getSize();

        Component cmp = renderer.getListFocusComponent(this);
        if (cmp != null) {
            cmp.setCellRenderer(true);
            int x = pos.getX();
            int y = pos.getY();

            // prevent focus animation from working during a drag operation
            if (orientation == VERTICAL) {
                y -= animationPosition;
            } else {
                x -= animationPosition;
            }
            renderComponent(g, cmp, x, y, size.getWidth(), size.getHeight());
        }
    
    }
    /**
     * Renders the current component on the screen
     */
    private void renderComponent(Graphics g, Component cmp, int x, int y, int width, int height) {
        cmp.setWidth(width);
        cmp.setHeight(height);
        cmp.setX(x);
        cmp.setY(y);
        cmp.paintInternal(g);
    }

    /**
     * @inheritDoc
     */
    protected String getUIID() {
        return id;
    }

    /**
     * Invoked to indicate interest in future selection events
     * 
     * @param l the selection listener to be added
     */
    public void addSelectionListener(SelectionListener l) {
        model.addSelectionListener(l);
    }

    /**
     * Invoked to indicate no further interest in future selection events
     * 
     * @param l the selection listener to be removed
     */
    public void removeSelectionListener(SelectionListener l) {
        model.removeSelectionListener(l);
    }

    /**
     * Allows binding a listener to user selection actions
     * 
     * @param l the action listener to be added
     */
    public void addActionListener(ActionListener l) {
        dispatcher.addListener(l);
    }

    /**
     * Allows binding a listener to user selection actions
     * 
     * @param l the action listener to be removed
     */
    public void removeActionListener(ActionListener l) {
        dispatcher.removeListener(l);
    }

    /**
     * @inheritDoc
     */
    protected void fireActionEvent() {
        super.fireActionEvent();
        dispatcher.fireActionEvent(new ActionEvent(this));
    }

    /**
     * A list can start handling input implicitly upon gaining focus, this can
     * make for a more intuitive UI when no other focus elements exist or when 
     * their use case is infrequent. However, it might be odd in some cases 
     * where the list "steals" focus.
     * 
     * @param inputOnFocus true is a list can start handling input 
     * implicitly upon gaining focus
     */
    public void setInputOnFocus(boolean inputOnFocus) {
        this.inputOnFocus = inputOnFocus;
    }

    /**
     * This method determines if the animated focus is drawn on top of the List
     * or behind the List when moving.
     * 
     * @param paintFocusBehindList
     */
    public void setPaintFocusBehindList(boolean paintFocusBehindList) {
        this.paintFocusBehindList = paintFocusBehindList;
    }

    
    /**
     * @inheritDoc
     */
    void focusGainedInternal() {
        if (inputOnFocus) {
            setHandlesInput(true);
        }
    }

    /**
     * @inheritDoc
     */
    public void focusLostInternal() {
    }

    /**
     * Returns the gap between items
     * 
     * @return the gap between items
     */
    public int getItemGap() {
        return itemGap;
    }

    /**
     * Set the gap between items
     * 
     * @param itemGap the gap between items
     */
    public void setItemGap(int itemGap) {
        this.itemGap = itemGap;
    }

    /**
     * The rendering prototype is optionally used in calculating the size of the 
     * List and is recommended for performance reasons. You should invoke it with an object
     * representing a theoretical value in the list which will be used to calculate
     * the size required for each element in the list.
     * <p>This allows list size calculations to work across look and feels and allows
     * developers to predetermin size for list elements. 
     * <p>e.g. For a list of Strings which you would like to always be 5 characters wide
     * you can use a prototype "XXXXX" which would use the preferred size of the XXXXX 
     * String to determin the size of the list element. E.g. for a list of dates you can use
     * new Date(30, 12, 00) etc..
     * 
     * @param renderingPrototype a value that can be passed to the renderer to indicate the preferred
     * size of a list component.
     */
    public void setRenderingPrototype(Object renderingPrototype) {
        this.renderingPrototype = renderingPrototype;
    }
    
    /**
     * See set rendering prototype
     * 
     * @see #setRenderingPrototype(java.lang.Object)
     * @return the value of the rendering prototype
     */
    public Object getRenderingPrototype() {
        return renderingPrototype;
    }

    /**
     * Calculates the default size for elements on the list
     * 
     * @return the default dimension for elements in a list
     */
    Dimension getElementSize(boolean selected) {
        if(selected) {
            if(selectedElemSize == null) {
                // don't keep element size if there are no elements and no prototype...
                if(renderingPrototype == null) {
                    if (model.getSize() == 0) {
                        // put a sensible value as default when there are no elements or rendering prototype
                        return new Label("XXXXXX").getPreferredSize();
                    }
                }
                selectedElemSize = calculateElementSize(true);
            }
            return selectedElemSize;
        } else {
            if(elemSize == null) {
                // don't keep element size if there are no elements and no prototype...
                if(renderingPrototype == null) {
                    if (model.getSize() == 0) {
                        // put a sensible value as default when there are no elements or rendering prototype
                        return new Label("XXXXXX").getPreferredSize();
                    }
                }
                elemSize = calculateElementSize(false);
            }
            return elemSize;
        }
    }

    /**
     * Calculates the size of an element based on a forumla or on rendering prototype
     */
    private Dimension calculateElementSize(boolean selected) {
        if(renderingPrototype != null) {
            Component unselected = renderer.getListCellRendererComponent(this, renderingPrototype, 0, selected);
            return unselected.getPreferredSize();
        }
        int width = 0;
        int height = 0;
        int elements = Math.min(5, model.getSize());
        for(int iter = 0 ; iter < elements ; iter++) {
            Component cmp = renderer.getListCellRendererComponent(this, model.getItemAt(iter), iter, selected);
            Dimension d = cmp.getPreferredSize();
            width = Math.max(width, d.getWidth());
            height = Math.max(height, d.getHeight());
        }
        return new Dimension(width, height);
    }

    /**
     * @inheritDoc
     */
    public void pointerDragged(int x, int y) {
        if (isSmoothScrolling()) {
            super.pointerDragged(x, y);
        } else {
            model.setSelectedIndex(pointerSelect(x, y));
        }
    }

    private int pointerSelect(int x, int y) {
        int selectedIndex = -1;
        Dimension elemSize = getElementSize(false);
        y = y - getAbsoluteY();
        x = x - getAbsoluteX();
        if (fixedSelection < FIXED_NONE_BOUNDRY) {
            if (orientation == VERTICAL) {
                selectedIndex = y / (elemSize.getHeight() + itemGap);
            } else {
                selectedIndex = x / (elemSize.getWidth() + itemGap);
            }
        } else {
            int numOfcomponents = getModel().getSize();
            Rectangle pos = new Rectangle();
            Style style = getStyle();
            int width = getWidth() - style.getPadding(RIGHT) - style.getPadding(LEFT) - getSideGap();
            if (isScrollableX()) {
                width = Math.max(width, getPreferredW() - style.getPadding(RIGHT) - style.getPadding(LEFT) - getSideGap());
            }
            Dimension rendererSize = getElementSize(false);
            Dimension selectedSize = getElementSize(true);
            for (int i = 0; i < numOfcomponents; i++) {
                calculateComponentPosition(i, width, pos, rendererSize, selectedSize, true);
                if (pos.contains(x, y)) {
                    selectedIndex = i;
                    break;
                }
            }
        }

        if (selectedIndex < 0 || selectedIndex >= size()) {
            return -1;
        }
        return selectedIndex;
    }

    /**
     * @inheritDoc
     */
    public void pointerReleased(int x, int y) {
        if (isDragActivated()) {
            super.pointerReleased(x, y);
            return;
        }
        int selection = pointerSelect(x, y);
        if (selection > -1) {
            model.setSelectedIndex(selection);
            fireActionEvent();
        }
    }

    /**
     * @inheritDoc
     */
    protected Dimension calcPreferredSize() {
        return UIManager.getInstance().getLookAndFeel().getListPreferredSize(this);
    }

    /**
     * Allows adding an element to a list if the underlying model supports this, notice that
     * it is an optional operation and if the model does not support it (default list model does)
     * then this operation may failed.
     * 
     * @param item the item to be added to a list model
     */
    public void addItem(Object item) {
        model.addItem(item);
    }

    /**
     * Indicates whether selection is fixable to place in which case all the
     * elements in the list move and selection stays in place. 
     * 
     * @return one of: FIXED_NONE, FIXED_TRAIL, FIXED_LEAD, FIXED_CENTER, FIXED_NONE_CYCLIC
     */
    public int getFixedSelection() {
        return fixedSelection;
    }

    /**
     * Indicates whether selection is fixable to place in which case all the
     * elements in the list move and selection stays in place. 
     * 
     * @param fixedSelection one of: FIXED_NONE, FIXED_TRAIL, FIXED_LEAD, 
     * FIXED_CENTER, FIXED_NONE_CYCLIC
     */
    public void setFixedSelection(int fixedSelection) {
        this.fixedSelection = fixedSelection;
    }

    /**
     * @inheritDoc
     */
    public boolean animate() {
        // parent is performing the animation we shouldn't do anything in this case
        // this is the scrolling animation which we don't want to interfear with
        boolean parentFinished = super.animate();
        if (animationPosition != 0) {
            if (animationPosition < 0) {
                animationPosition = Math.min(listMotion.getValue() - destination, 0);
            } else {
                animationPosition = Math.max(destination - listMotion.getValue(), 0);
            }
            return true;
        }
        return parentFinished;
    }

    /**
     * Setting the surrounding border gap
     * 
     * @param borderGap number of pixels for the gap
     */
    public void setBorderGap(int borderGap) {
        this.borderGap = borderGap;
    }

    /**
     * Getting the surrounding border gap
     * 
     * @return border gap in pixels
     */
    public int getBorderGap() {
        return borderGap;
    }

    /**
     * @inheritDoc
     */
    protected String paramString() {
        String elemSizeStr = "element size = ";
        if (elemSize != null) {
            elemSizeStr += elemSize.toString();
        }
        return super.paramString() + ", " + elemSizeStr +
                ", itemGap = " + itemGap +
                ", orientation = " + orientation +
                ", selected index = " + getSelectedIndex() +
                ", size = " + size();
    }
}

⌨️ 快捷键说明

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