defaultlookandfeel.java.svn-base

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

SVN-BASE
1,456
字号
                    break;
            }
        } else if (l.getAlignment() == Component.RIGHT) {
            switch (l.getTextPosition()) {
                case Label.LEFT:
                case Label.RIGHT:
                    x = l.getX() + l.getWidth() - style.getPadding(Component.RIGHT) -
                            (preserveSpaceForState + ((icon != null) ? (icon.getWidth() + gap) : 0) +
                            font.stringWidth(text));
                    x = Math.max(x, l.getX() + style.getPadding(Component.LEFT) + preserveSpaceForState);

                    y = y + (l.getHeight() - (style.getPadding(Component.TOP) +
                            style.getPadding(Component.BOTTOM) +
                            Math.max(((icon != null) ? icon.getHeight() : 0),
                            fontHeight))) / 2;
                    break;
                case Label.BOTTOM:
                case Label.TOP:
                    x = l.getX() + l.getWidth() - style.getPadding(Component.RIGHT) -
                            preserveSpaceForState - (Math.max(((icon != null) ? (icon.getWidth()) : 0),
                            font.stringWidth(text)));
                    x = Math.max(x, l.getX() + style.getPadding(Component.LEFT) + preserveSpaceForState);
                    y = y + (l.getHeight() - (style.getPadding(Component.TOP) +
                            style.getPadding(Component.BOTTOM) +
                            ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
                    break;
            }
        }


        int textSpaceW = l.getWidth() - style.getPadding(Label.RIGHT) - style.getPadding(Label.LEFT);

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

        if (stateIcon != null) {
            textSpaceW = textSpaceW - stateIcon.getWidth();
        } else {
            textSpaceW = textSpaceW - preserveSpaceForState;
        }

        if (icon == null) { // no icon only string 
            drawLabelString(g, l, text, x, y, textSpaceW, preserveSpaceForState);
        } else {
            //int strHeight = l.getStyle().getFont().getHeight();
            int strWidth = l.getStyle().getFont().stringWidth(text);
            int iconWidth = icon.getWidth();
            int iconHeight = icon.getHeight();
            int iconStringWGap;
            int iconStringHGap;

            switch (l.getTextPosition()) {
                case Label.LEFT:
                    if (iconHeight > fontHeight) {
                        iconStringHGap = (iconHeight - fontHeight) / 2;
                        //drawLabelStringValign(g, l, text, x, y, iconStringHGap, iconHeight);
                        strWidth = drawLabelStringValign(g, l, text, x, y, iconStringHGap, iconHeight, textSpaceW, fontHeight, preserveSpaceForState);

                        g.drawImage(icon, x + strWidth + gap, y);
                    } else {
                        iconStringHGap = (fontHeight - iconHeight) / 2;
                        //drawLabelString(g, l, text, x, y);
                        strWidth = drawLabelString(g, l, text, x, y, textSpaceW, preserveSpaceForState);

                        g.drawImage(icon, x + strWidth + gap, y + iconStringHGap);
                    }
                    break;
                case Label.RIGHT:
                    if (iconHeight > fontHeight) {
                        iconStringHGap = (iconHeight - fontHeight) / 2;
                        g.drawImage(icon, x, y);
                        //drawLabelStringValign(g, l, text, x+ iconWidth + gap, y, iconStringHGap, iconHeight);
                        drawLabelStringValign(g, l, text, x + iconWidth + gap, y, iconStringHGap, iconHeight, textSpaceW, fontHeight, preserveSpaceForState);
                    } else {
                        iconStringHGap = (fontHeight - iconHeight) / 2;
                        g.drawImage(icon, x, y + iconStringHGap);
                        //drawLabelString(g, l, text, x + iconWidth + gap, y);
                        drawLabelString(g, l, text, x + iconWidth + gap, y, textSpaceW, preserveSpaceForState);
                    }
                    break;
                case Label.BOTTOM:
                    if (iconWidth > strWidth) { //center align the smaller

                        iconStringWGap = (iconWidth - strWidth) / 2;
                        g.drawImage(icon, x, y);
                        //drawLabelString(g, l, text, x + iconStringWGap, y + iconHeight + gap);
                        drawLabelString(g, l, text, x + iconStringWGap, y + iconHeight + gap, textSpaceW, preserveSpaceForState);
                    } else {
                        iconStringWGap = (strWidth - iconWidth) / 2;
                        g.drawImage(icon, x + iconStringWGap, y);
                        //drawLabelString(g, l, text, x, y + iconHeight + gap);
                        drawLabelString(g, l, text, x, y + iconHeight + gap, textSpaceW, preserveSpaceForState);
                    }
                    break;
                case Label.TOP:
                    if (iconWidth > strWidth) { //center align the smaller

                        iconStringWGap = (iconWidth - strWidth) / 2;
//                        drawLabelString(g, l, text, x + iconStringWGap, y);
                        drawLabelString(g, l, text, x + iconStringWGap, y, textSpaceW, preserveSpaceForState);
                        g.drawImage(icon, x, y + fontHeight + gap);
                    } else {
                        iconStringWGap = (strWidth - iconWidth) / 2;
//                        drawLabelString(g, l, text, x, y);
                        drawLabelString(g, l, text, x, y, textSpaceW, preserveSpaceForState);
                        g.drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
                    }
                    break;
            }
        }
    }

    /**
     * Implements the drawString for the text component and adjust the valign
     * assuming the icon is in one of the sides
     */
    private int drawLabelStringValign(Graphics g, Label l, String str, int x, int y, int iconStringHGap, int iconHeight, int textSpaceW, int fontHeight, int preserveSpaceForState) {

        switch (l.getVerticalAlignment()) {
            case Component.TOP:
                return drawLabelString(g, l, str, x, y, textSpaceW, preserveSpaceForState);
            case Component.CENTER:
                return drawLabelString(g, l, str, x, y + iconHeight / 2 - fontHeight / 2, textSpaceW, preserveSpaceForState);
            default:
                return drawLabelString(g, l, str, x, y + iconStringHGap, textSpaceW, preserveSpaceForState);
        }
    }

    /**
     * Implements the drawString for the text component and adjust the valign
     * assuming the icon is in one of the sides
     */
    private int drawLabelString(Graphics g, Label l, String text, int x, int y, int textSpaceW, int preserveSpaceForState) {
        Style style = l.getStyle();

        int cx = g.getClipX();
        int cy = g.getClipY();
        int cw = g.getClipWidth();
        int ch = g.getClipHeight();

        int txtX;
        if (l.getTextPosition() == l.RIGHT) {
            txtX = l.getX() + (l.getWidth() - style.getPadding(l.LEFT) - textSpaceW);
            g.clipRect(l.getX() + (l.getWidth() - style.getPadding(l.LEFT) - textSpaceW), cy, textSpaceW, ch);
        } else {
            txtX = x + preserveSpaceForState;
        }

        g.clipRect(txtX, cy, textSpaceW, ch);

        if (l.isTickerRunning()) {
            if (l.getShiftText() > 0) {
                if (l.getShiftText() > textSpaceW) {
                    l.setShiftText(x - l.getX() - style.getFont().stringWidth(text));
                }
            } else if ((x) + l.getShiftText() + style.getFont().stringWidth(text) < txtX) {
                l.setShiftText(textSpaceW - (x - txtX));// + style.getFont().stringWidth(text));

            }
        }
        //g.drawString(text, l.getShiftText() + x, y);
        int drawnW = drawLabelText(g, l, text, x, y, textSpaceW);

        g.setClip(cx, cy, cw, ch);

        return drawnW;
    }

    protected int drawLabelText(Graphics g, Label l, String text, int x, int y, int textSpaceW) {

        Font f = l.getStyle().getFont();
        if (!l.isTickerRunning()) {
            int txtW = f.stringWidth(text);
            //if there is no space to draw the text add ... at the end
            if (txtW > textSpaceW && textSpaceW > 0) {
                if (l.isEndsWith3Points()) {
                    String points = "...";
                    int index = 1;
                    String tmp = text.substring(0, index);
                    int pointsW = f.stringWidth(points);
                    while (f.stringWidth(tmp) + pointsW < textSpaceW) {
                        index++;
                        if (index >= text.length()) {
                            break;
                        }
                        tmp = text.substring(0, index);
                    }
                    tmp = text.substring(0, index - 1) + points;
                    text = tmp;
                }
                
            }
        }

        g.drawString(text, l.getShiftText() + x, y);
        return Math.min(f.stringWidth(text), textSpaceW);
    }

    private Image getIconFromState(Button b) {
        Image icon = null;
        switch (b.getState()) {
            case Button.STATE_DEAFULT:
                icon = b.getIcon();
                break;
            case Button.STATE_PRESSED:
                icon = b.getPressedIcon();
                if (icon == null) {
                    icon = b.getIcon();
                }
                break;
            case Button.STATE_ROLLOVER:
                icon = b.getRolloverIcon();
                if (icon == null) {
                    icon = b.getIcon();
                }
                break;
        }
        return icon;
    }

    /**
     * @inheritDoc
     */
    public long findDayAt(int x, int y, com.sun.lwuit.Calendar cal, Component mv) {
        Calendar calendar = Calendar.getInstance();
        long firstOfMonth = getMonthExtent(calendar, cal.getDate(), true);

        int w = mv.getWidth();

        Font f = mv.getStyle().getFont();
        int fh = f.getHeight();

        int labelH = (fh + DAY_SPACE_H);

        int dayWidth = (w / 7);
        long currentDay = firstOfMonth;
        calendar.setTime(new Date(currentDay));
        int dayX = (int) (mv.getAbsoluteX() + (dayWidth * (calendar.get(java.util.Calendar.DAY_OF_MONTH) % 7)));
        int dayY = mv.getAbsoluteY() + labelH;

        while (!(dayX < x && dayX + dayWidth > x && dayY < y && dayY + labelH > y)) {
            currentDay += DAY;
            calendar.setTime(new Date(currentDay));
            dayX = (int) (mv.getAbsoluteX() + (dayWidth * (calendar.get(java.util.Calendar.DAY_OF_MONTH) % 7)));
            if (currentDay % 7 == 0) {
                dayX = mv.getAbsoluteX();
                dayY += labelH;
            }
            if (dayX > mv.getX() + mv.getWidth() && dayY > mv.getY() + mv.getHeight()) {
                return currentDay;
            }
        }
        return currentDay;
    }

    private void paintDates(Graphics g, Date date, Component mv, long sd, Font f,
            int dayWidth, int dayHeight, int labelH,
            int fix, int fh, long selectedDay) {
        Calendar calendar = Calendar.getInstance();
        date.setTime(sd);
        long firstOfMonth = getMonthExtent(calendar, date, true);

        date.setTime(sd);
        long endOfMonth = getMonthExtent(calendar, date, false);

        long day = firstOfMonth;
        int d = 1;
        int sats = 0;
        int dx;
        g.setColor(mv.getStyle().getFgColor());
        do {
            date.setTime(day);
            calendar.setTime(date);
            String dayString = Integer.toString(d);

            int dow = calendar.get(Calendar.DAY_OF_WEEK);

            dx = mv.getX() + (dayWidth * (dow - 1)) + fix;//(d < 10 ? fix : 0);

            int dy = mv.getY() + ((sats) * dayHeight) + labelH;

            if (day == selectedDay) {
                g.setColor(mv.getStyle().getBgSelectionColor());
                g.fillRoundRect(dx - 2, dy - 2,
                        f.stringWidth("22") + 4, fh + 4, 4, 4);
                g.setColor(mv.getStyle().getFgSelectionColor());
                g.drawString(dayString, dx, dy);
            } else {
                g.drawString(dayString, dx, dy);
            }

            g.setColor(mv.getStyle().getFgColor());


            day += DAY;
            d++;

            if (dow == Calendar.SATURDAY) {
                sats++;
                g.setColor(mv.getStyle().getBgColor());
                g.fillRect(mv.getX() + 2, dy + fh + 3, mv.getWidth() - 4, 1);
                g.setColor(mv.getStyle().getFgColor());
            }
        } while (day <= endOfMonth);
    }

    /*
     * Finds the first or last day of the month prior or ahead of the given date.
     */
    private long getMonthExtent(Calendar calendar, Date d, boolean first) {
        long day = d.getTime();
        d.setTime(day);
        calendar.setTime(d);
        long origDay = day;

        int currentMonth = calendar.get(Calendar.MONTH);
        int adjacentMonth;
        do {
            long adjacentDay = day + ((first ? -1 : 1) * DAY);
            d.setTime(adjacentDay);
            calendar.setTime(d);
            adjacentMonth = calendar.get(Calendar.MONTH);
            if (currentMonth == adjacentMonth) {
                day = adjacentDay;
            }
        } while (currentMonth == adjacentMonth);

        d.setTime(origDay);
        calendar.setTime(d);
        return day;
    }

    /**
     * @inheritDoc
     */
    public Dimension getComboBoxPreferredSize(ComboBox cb) {
        int gap = 2;
        int width = 0;
        int height = 0;

        ListModel model = cb.getModel();
        ListCellRenderer renderer = cb.getRenderer();

        int numOfcomponents = model.getSize();
        for (int i = 0; i < numOfcomponents; i++) {
            Object value = model.getItemAt(i);
            Component cmp = renderer.getListCellRendererComponent(cb, value, i, model.getSelectedIndex() == i);
            height = Math.max(height, cmp.getPreferredH());
            width = Math.max(width, cmp.getPreferredW());
        }
        if (comboImage != null) {
            height = Math.max(height, comboImage.getHeight());
            width += comboImage.getWidth();
        } else {
            width += cb.getStyle().getFont().getHeight();
        }
        return new Dimension(width + gap + cb.getStyle().getPadding(Component.RIGHT) + cb.getStyle().getPadding(Component.LEFT), height + cb.getStyle().getPadding(Component.TOP) + cb.getStyle().getPadding(Component.BOTTOM));
    }

    /**
     * @inheritDoc
     */
    public void drawTabbedPane(Graphics g, TabbedPane tp) {

⌨️ 快捷键说明

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