defaultlookandfeel.java.svn-base

来自「j2me设计的界面包」· SVN-BASE 代码 · 共 1,456 行 · 第 1/4 页

SVN-BASE
1,456
字号
    }

    /**
     * @inheritDoc
     */
    public Component getTabbedPaneCell(final TabbedPane tp,
            final String text, final Image icon, final boolean isSelected,
            final boolean cellHasFocus, final Style cellStyle,
            final Style tabbedPaneStyle, final int cellOffsetX,
            final int cellOffsetY, final Dimension cellsPreferredSize,
            final Dimension contentPaneSize) {


        Label cell = new Label(text) {

            public void paint(Graphics g) {
                int tPBorder = tp.getTabbedPaneBorderWidth();
                int focusMarkWidth = tPBorder * 2;
                int tabP = tp.getTabPlacement();
                // Initialize forground colors before calling to super.paint()
                if (isSelected && cellHasFocus) {
                    focusMarkWidth = tPBorder * 3;
                    this.getStyle().setFgSelectionColor(cellStyle.getFgSelectionColor());
                } else {
                    this.getStyle().setFgColor(cellStyle.getFgColor());
                }

                super.paint(g);

                if (!isSelected) {
                    g.setColor(0);
                    g.fillRect(getX(), getY(), getWidth(), getHeight(), (byte) 0x2f);
                }
                // coloring back the focus mark line
                g.setColor(getStyle().getFgColor());
                if (tabP == tp.TOP || tabP == tp.BOTTOM) {
                    if (tabP == tp.TOP) {
                        if (isSelected) {
                            g.fillRect(getX(), getY() + tPBorder, getWidth(), focusMarkWidth);// north

                        }
                        g.setColor(tabbedPaneStyle.getFgColor());
                        g.fillRect(getX(), getY(), getWidth(), tPBorder);//north line

                        g.fillRect(getX(), getY(), tPBorder, getHeight());// west line

                    } else {
                        if (isSelected) {
                            g.fillRect(getX(), getY() + getHeight() - focusMarkWidth, getWidth(), focusMarkWidth);// south

                        }
                        g.setColor(tabbedPaneStyle.getFgColor());
                        g.fillRect(getX(), getY() + getHeight() - tPBorder, getWidth(), tPBorder);//south line

                        g.fillRect(getX(), getY(), tPBorder, getHeight());// west line

                    }
                    int x = getX() - cellOffsetX + getWidth();
                    if (x == contentPaneSize.getWidth()) {
                        g.fillRect(x + cellOffsetX - tPBorder, getY(), tPBorder, getHeight());// east line

                    }
                    if (cellsPreferredSize.getWidth() < contentPaneSize.getWidth() && (getX() + getWidth() == cellsPreferredSize.getWidth())) {
                        g.fillRect(getX() + getWidth() - tPBorder, getY(), tPBorder, getHeight());
                    }
                } else { // LEFT or RIGHT

                    if (isSelected) {
                        g.fillRect(getX(), getY() + tPBorder, getWidth(), focusMarkWidth);// north

                    }
                    g.setColor(tabbedPaneStyle.getFgColor());
                    g.fillRect(getX(), getY(), getWidth(), tPBorder);
                    int y = getY() - cellOffsetY + getHeight();
                    if (y == contentPaneSize.getHeight()) {
                        g.fillRect(getX(), y + cellOffsetY - tPBorder, getWidth(), tPBorder);
                    }
                    if (cellsPreferredSize.getHeight() < contentPaneSize.getHeight() && (getY() + getHeight() == cellsPreferredSize.getHeight())) {
                        g.fillRect(getX(), getY() + getHeight() - tPBorder, getWidth(), tPBorder);//south line

                    }
                    if (tabP == tp.LEFT) {
                        g.fillRect(getX(), getY(), tPBorder, getHeight());// west border

                    } else {
                        g.fillRect(getX() + getWidth() - tPBorder, getY(), tPBorder, getHeight());// east border

                    }
                }
            }
        };

        cell.setCellRenderer(true);
        cell.getStyle().setBorder(null);
        cell.setIcon(icon);
        updateCellLook(tp, (Component) cell, isSelected);
        if (isSelected) {
            cellStyle.setBgSelectionColor(cellStyle.getBgColor());
        }

        cell.setAlignment(Label.CENTER);
        return cell;
    }

    private void updateCellLook(TabbedPane tp, Component c, boolean selected) {
        if (selected) {
//            c.getStyle().setBgColor(tp.getStyle().getBgSelectionColor());
            c.getStyle().setFgColor(tp.getStyle().getFgSelectionColor());
        } else {
//            c.getStyle().setBgColor(tp.getStyle().getBgColor());
            c.getStyle().setFgColor(tp.getStyle().getFgColor());
        }
        c.getStyle().setBgTransparency(tp.getStyle().getBgTransparency());
        c.getStyle().setFont(tp.getStyle().getFont());
    //c.setWidth(c.getPreferredW());
    //c.setHeight(c.getPreferredH());
    }

    /**
     * @inheritDoc
     */
    public void drawTabbedPaneContentPane(final TabbedPane tp,
            final Graphics g, final Rectangle rect,
            final Dimension cellsPreferredSize, final int numOfTabs,
            final int selectedTabIndex, final Dimension tabsSize,
            final int cellOffsetX, final int cellOffsetY) {
        int x = rect.getX();
        int y = rect.getY();
        int w = rect.getSize().getWidth();
        int h = rect.getSize().getHeight();
        int listPreferredW = cellsPreferredSize.getWidth();
        int listPreferredH = cellsPreferredSize.getHeight();
        int maxTabWidth = tabsSize.getWidth();
        int maxTabHeight = tabsSize.getHeight();
        int tPBorder = tp.getTabbedPaneBorderWidth();

        g.setColor(tp.getStyle().getBgColor());
        g.fillRect(x, y, w, h, tp.getStyle().getBgTransparency());

        // paint borders for TOP tab placement
        g.setColor(tp.getStyle().getFgColor());
        int tabP = tp.getTabPlacement();

        if (tabP == tp.TOP || tabP == tp.BOTTOM) {
            g.fillRect(x, y, tPBorder, h);// west border

            g.fillRect(x + w - tPBorder, y, tPBorder, h);// east border

            int relativeY = y;
            if (tabP == tp.BOTTOM) {
                relativeY = y + h - tPBorder;
                g.fillRect(x, y, w, tPBorder);// north border

            } else {
                g.fillRect(x, y + h - tPBorder, w, tPBorder);// south border

            }
            if (listPreferredW < w) {
                g.fillRect(listPreferredW - tPBorder, relativeY, w - listPreferredW, tPBorder);
            }
            for (int i = 0; i < numOfTabs; i++) {
                if (i != selectedTabIndex) {
                    g.fillRect((x - cellOffsetX + (maxTabWidth * i)), relativeY, maxTabWidth + tPBorder, tPBorder);
                }
            }
        } else {//if (tabP == LEFT || tabP == RIGHT) {

            g.fillRect(x, y, w, tPBorder);// north border

            g.fillRect(x, y + h - tPBorder, w, tPBorder);// south border

            int relativeX = x;
            if (tabP == tp.RIGHT) {
                g.fillRect(x, y, tPBorder, h);// west border

                relativeX = x + w - tPBorder;
            } else {
                g.fillRect(x + w - tPBorder, y, tPBorder, h);// east border

            }
            if (listPreferredH < h) {
                g.fillRect(relativeX, y + listPreferredH - tPBorder, tPBorder, h - listPreferredH + tPBorder);
            }
            for (int i = 0; i < numOfTabs; i++) {
                if (i != selectedTabIndex) {
                    g.fillRect(relativeX, (y - cellOffsetY + (maxTabHeight * i)), tPBorder, maxTabHeight + tPBorder);
                }
            }
        }
    }

    /**
     * Similar to getText() but works properly with password fields
     */
    private String getTextFieldString(TextField ta) {
        String text = (String) ta.getText();
        String displayText = "";
        if ((ta.getConstraint() & TextArea.PASSWORD) != 0) {
            // show the last character in a password field
            if (ta.isPendingCommit()) {
                if (text.length() > 0) {
                    for (int j = 0; j < text.length() - 1; j++) {
                        displayText += "*";
                    }
                    displayText += text.charAt(text.length() - 1);
                }
            } else {
                for (int j = 0; j < text.length(); j++) {
                    displayText += "*";
                }
            }
        } else {
            displayText = text;
        }
        return displayText;
    }
    
    /**
     * @inheritDoc
     */
    public void drawTextField(Graphics g, TextField ta) {
        setFG(g, ta);
        String text = (String) ta.getText();

        // display ******** if it is a password field
        String displayText = getTextFieldString(ta);

        Style style = ta.getStyle();
        int x = 0;
        int cursorCharPosition = ta.getCursorPosition();
        Font f = ta.getStyle().getFont();
        int cursorX = 0;
        int xPos = 0;
        if (cursorCharPosition > 0) {
            xPos = f.stringWidth(ta.getText().substring(0, cursorCharPosition));
            cursorX = ta.getX() + style.getPadding(Component.LEFT) + xPos;
            if (ta.getWidth() > (f.getHeight() * 2) && cursorX >= ta.getWidth() - f.getHeight()) {
                while (x + xPos >= ta.getWidth() - f.getHeight() * 2) {
                    x--;
                }
            }
        }

        g.drawString(displayText, ta.getX() + x + style.getPadding(Component.LEFT),
                ta.getY() + style.getPadding(Component.TOP));

        String inputMode = ta.getInputMode();
        int inputModeWidth = f.stringWidth(inputMode);
        if (ta.handlesInput() && ta.getWidth() / 2 > inputModeWidth) {
            int drawXPos = ta.getX() + style.getPadding(Component.LEFT);
            if (xPos < ta.getWidth() / 2) {
                // draw on the right side
                drawXPos = drawXPos + ta.getWidth() - inputModeWidth - style.getPadding(Component.RIGHT) - style.getPadding(Component.LEFT);
            }
            g.setColor(style.getFgSelectionColor());
            g.fillRect(drawXPos, ta.getY() + style.getPadding(Component.TOP), inputModeWidth,
                    ta.getHeight(), (byte) 140);
            g.setColor(style.getBgSelectionColor());
            g.drawString(inputMode, drawXPos, ta.getY() + style.getPadding(Component.TOP));
        }
    }

    /**
     * @inheritDoc
     */
    public Dimension getTextFieldPreferredSize(TextField ta) {
        return getTextAreaPreferredSize(ta);
    }

    /**
     * Draws the cursor of the text field, blinking is handled simply by avoiding
     * a call to this method.
     */
    public void drawTextFieldCursor(Graphics g, TextField ta) {
        Style style = ta.getStyle();
        Font f = style.getFont();

        // display ******** if it is a password field
        String displayText = getTextFieldString(ta);
        
        int xPos = 0;
        int cursorCharPosition = ta.getCursorPosition();
        if (cursorCharPosition > 0) {
            xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
        }
        int cursorX = ta.getX() + style.getPadding(Component.LEFT) + xPos;
        int cursorY = ta.getY() + style.getPadding(Component.TOP);
        int x = 0;
        if (ta.getWidth() > (f.getHeight() * 2) && cursorX >= ta.getWidth() - f.getHeight()) {
            while (x + xPos >= ta.getWidth() - f.getHeight() * 2) {
                x--;
            }
        }
        int oldColor = g.getColor();
        g.setColor(style.getFgSelectionColor());
        g.drawLine(cursorX + x, cursorY, cursorX + x, cursorY + f.getHeight());
        g.setColor(oldColor);
    }

    private int getAvaliableSpaceForText(Label l) {
        Style style = l.getStyle();
        int textSpaceW = l.getWidth() - style.getPadding(Label.RIGHT) - style.getPadding(Label.LEFT);
        Image icon = l.getIcon();

        if (l instanceof Button) {
            icon = getIconFromState((Button) l);
        }

        if (icon != null && (l.getTextPosition() == Label.RIGHT || l.getTextPosition() == Label.LEFT)) {
            textSpaceW = textSpaceW - icon.getWidth();
        }
        int preserveSpaceForState = 0;

        if (l instanceof RadioButton) {
            if (rButtonImages != null) {
                int index = ((RadioButton) l).isSelected() ? 1 : 0;
                preserveSpaceForState = rButtonImages[index].getWidth();
            } else {
                int height = style.getFont().getHeight();
                preserveSpaceForState = height + ((RadioButton) l).getGap();
            }
        }

        if (l instanceof CheckBox) {
            if (chkBoxImages != null) {
                int index = ((CheckBox) l).isSelected() ? 1 : 0;
                preserveSpaceForState = chkBoxImages[index].getWidth();
            } else {
                int height = style.getFont().getHeight();
                preserveSpaceForState = height + ((CheckBox) l).getGap();
            }
        }

        textSpaceW = textSpaceW - preserveSpaceForState;
        return textSpaceW;
    }

    /**
     * @inheritDoc
     */
    public void focusGained(Component cmp) {
        if(cmp instanceof Label) {
            Label l = (Label) cmp;
            Style style = l.getStyle();
            int txtW = style.getFont().stringWidth(l.getText());
            int textSpaceW = getAvaliableSpaceForText(l);
            if (txtW > textSpaceW && textSpaceW > 0) {
                ((Label) cmp).startTicker(tickerSpeed, true);
            }
        }
    }

    /**
     * @inheritDoc
     */
    public void focusLost(Component cmp) {
        if(cmp instanceof Label) {
            if (((Label) cmp).isTickerRunning()) {
                ((Label) cmp).stopTicker();
            }
        }
    }
}

⌨️ 快捷键说明

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