1323 lines · 42.1 KB · Java
RawDownload
                new Rectangle(accSize.width,  accSize.height),                new Rectangle(checkSize.width, checkSize.height),                new Rectangle(arrowSize.width, arrowSize.height),                new Rectangle(labelSize.width, labelSize.height)        );    }     public ColumnAlignment getLTRColumnAlignment() {        return ColumnAlignment.LEFT_ALIGNMENT;    }     public ColumnAlignment getRTLColumnAlignment() {        return ColumnAlignment.RIGHT_ALIGNMENT;    }     protected void prepareForLayout(LayoutResult lr) {        lr.checkRect.width = checkSize.maxWidth;        lr.accRect.width = accSize.maxWidth;        lr.arrowRect.width = arrowSize.maxWidth;    }     /**     * Aligns the accelertor text and the check and arrow icons vertically     * with the center of the label rect.     */    private void alignAccCheckAndArrowVertically(LayoutResult lr) {        lr.accRect.y = (int)(lr.labelRect.y                + (float)lr.labelRect.height/2                - (float)lr.accRect.height/2);        fixVerticalAlignment(lr, lr.accRect);        if (useCheckAndArrow) {            lr.arrowRect.y = (int)(lr.labelRect.y                    + (float)lr.labelRect.height/2                    - (float)lr.arrowRect.height/2);            lr.checkRect.y = (int)(lr.labelRect.y                    + (float)lr.labelRect.height/2                    - (float)lr.checkRect.height/2);            fixVerticalAlignment(lr, lr.arrowRect);            fixVerticalAlignment(lr, lr.checkRect);        }    }     /**     * Fixes vertical alignment of all menu item elements if rect.y     * or (rect.y + rect.height) is out of viewRect bounds     */    private void fixVerticalAlignment(LayoutResult lr, Rectangle r) {        int delta = 0;        if (r.y < viewRect.y) {            delta = viewRect.y - r.y;        } else if (r.y + r.height > viewRect.y + viewRect.height) {            delta = viewRect.y + viewRect.height - r.y - r.height;        }        if (delta != 0) {            lr.checkRect.y += delta;            lr.iconRect.y += delta;            lr.textRect.y += delta;            lr.accRect.y += delta;            lr.arrowRect.y += delta;            lr.labelRect.y += delta;        }    }     private void doLTRColumnLayout(LayoutResult lr, ColumnAlignment alignment) {        // Set maximal width for all the five basic rects        // (three other ones are already maximal)        lr.iconRect.width = iconSize.maxWidth;        lr.textRect.width = textSize.maxWidth;         // Set X coordinates        // All rects will be aligned at the left side        calcXPositionsLTR(viewRect.x, leadingGap, gap, lr.checkRect,                lr.iconRect, lr.textRect);         // Tune afterCheckIconGap        if (lr.checkRect.width > 0) { // there is the afterCheckIconGap            lr.iconRect.x += afterCheckIconGap - gap;            lr.textRect.x += afterCheckIconGap - gap;        }         calcXPositionsRTL(viewRect.x + viewRect.width, leadingGap, gap,                lr.arrowRect, lr.accRect);         // Take into account minimal text offset        int textOffset = lr.textRect.x - viewRect.x;        if (!isTopLevelMenu && (textOffset < minTextOffset)) {            lr.textRect.x += minTextOffset - textOffset;        }         alignRects(lr, alignment);         // Set Y coordinate for text and icon.        // Y coordinates for other rects        // will be calculated later in layoutMenuItem.        calcTextAndIconYPositions(lr);         // Calculate valid X and Y coordinates for labelRect        lr.setLabelRect(lr.textRect.union(lr.iconRect));    }     private void doLTRComplexLayout(LayoutResult lr, ColumnAlignment alignment) {        lr.labelRect.width = labelSize.maxWidth;         // Set X coordinates        calcXPositionsLTR(viewRect.x, leadingGap, gap, lr.checkRect,                lr.labelRect);         // Tune afterCheckIconGap        if (lr.checkRect.width > 0) { // there is the afterCheckIconGap            lr.labelRect.x += afterCheckIconGap - gap;        }         calcXPositionsRTL(viewRect.x + viewRect.width,                leadingGap, gap, lr.arrowRect, lr.accRect);         // Take into account minimal text offset        int labelOffset = lr.labelRect.x - viewRect.x;        if (!isTopLevelMenu && (labelOffset < minTextOffset)) {            lr.labelRect.x += minTextOffset - labelOffset;        }         alignRects(lr, alignment);         // Center labelRect vertically        calcLabelYPosition(lr);         layoutIconAndTextInLabelRect(lr);    }     private void doRTLColumnLayout(LayoutResult lr, ColumnAlignment alignment) {        // Set maximal width for all the five basic rects        // (three other ones are already maximal)        lr.iconRect.width = iconSize.maxWidth;        lr.textRect.width = textSize.maxWidth;         // Set X coordinates        calcXPositionsRTL(viewRect.x + viewRect.width, leadingGap, gap,                lr.checkRect, lr.iconRect, lr.textRect);         // Tune the gap after check icon        if (lr.checkRect.width > 0) { // there is the gap after check icon            lr.iconRect.x -= afterCheckIconGap - gap;            lr.textRect.x -= afterCheckIconGap - gap;        }         calcXPositionsLTR(viewRect.x, leadingGap, gap, lr.arrowRect,                lr.accRect);         // Take into account minimal text offset        int textOffset = (viewRect.x + viewRect.width)                       - (lr.textRect.x + lr.textRect.width);        if (!isTopLevelMenu && (textOffset < minTextOffset)) {            lr.textRect.x -= minTextOffset - textOffset;        }         alignRects(lr, alignment);         // Set Y coordinates for text and icon.        // Y coordinates for other rects        // will be calculated later in layoutMenuItem.        calcTextAndIconYPositions(lr);         // Calculate valid X and Y coordinate for labelRect        lr.setLabelRect(lr.textRect.union(lr.iconRect));    }     private void doRTLComplexLayout(LayoutResult lr, ColumnAlignment alignment) {        lr.labelRect.width = labelSize.maxWidth;         // Set X coordinates        calcXPositionsRTL(viewRect.x + viewRect.width, leadingGap, gap,                lr.checkRect, lr.labelRect);         // Tune the gap after check icon        if (lr.checkRect.width > 0) { // there is the gap after check icon            lr.labelRect.x -= afterCheckIconGap - gap;        }         calcXPositionsLTR(viewRect.x, leadingGap, gap, lr.arrowRect, lr.accRect);         // Take into account minimal text offset        int labelOffset = (viewRect.x + viewRect.width)                        - (lr.labelRect.x + lr.labelRect.width);        if (!isTopLevelMenu && (labelOffset < minTextOffset)) {            lr.labelRect.x -= minTextOffset - labelOffset;        }         alignRects(lr, alignment);         // Center labelRect vertically        calcLabelYPosition(lr);         layoutIconAndTextInLabelRect(lr);    }     private void alignRects(LayoutResult lr, ColumnAlignment alignment) {        alignRect(lr.checkRect, alignment.getCheckAlignment(),                  checkSize.getOrigWidth());        alignRect(lr.iconRect, alignment.getIconAlignment(),                  iconSize.getOrigWidth());        alignRect(lr.textRect, alignment.getTextAlignment(),                  textSize.getOrigWidth());        alignRect(lr.accRect, alignment.getAccAlignment(),                  accSize.getOrigWidth());        alignRect(lr.arrowRect, alignment.getArrowAlignment(),                  arrowSize.getOrigWidth());    }     private void alignRect(Rectangle rect, int alignment, int origWidth) {        if (alignment == SwingConstants.RIGHT) {            rect.x = rect.x + rect.width - origWidth;        }        rect.width = origWidth;    }     protected void layoutIconAndTextInLabelRect(LayoutResult lr) {        lr.setTextRect(new Rectangle());        lr.setIconRect(new Rectangle());        SwingUtilities.layoutCompoundLabel(                mi, fm, text,icon, verticalAlignment, horizontalAlignment,                verticalTextPosition, horizontalTextPosition, lr.labelRect,                lr.iconRect, lr.textRect, gap);    }     private void calcXPositionsLTR(int startXPos, int leadingGap,                                   int gap, Rectangle... rects) {        int curXPos = startXPos + leadingGap;        for (Rectangle rect : rects) {            rect.x = curXPos;            if (rect.width > 0) {                curXPos += rect.width + gap;            }        }    }     private void calcXPositionsRTL(int startXPos, int leadingGap,                                   int gap, Rectangle... rects) {        int curXPos = startXPos - leadingGap;        for (Rectangle rect : rects) {            rect.x = curXPos - rect.width;            if (rect.width > 0) {                curXPos -= rect.width + gap;            }        }    }    /**     * Sets Y coordinates of text and icon     * taking into account the vertical alignment     */    private void calcTextAndIconYPositions(LayoutResult lr) {        if (verticalAlignment == SwingUtilities.TOP) {            lr.textRect.y  = (int)(viewRect.y                    + (float)lr.labelRect.height/2                    - (float)lr.textRect.height/2);            lr.iconRect.y  = (int)(viewRect.y                    + (float)lr.labelRect.height/2                    - (float)lr.iconRect.height/2);        } else if (verticalAlignment == SwingUtilities.CENTER) {            lr.textRect.y = (int)(viewRect.y                    + (float)viewRect.height/2                    - (float)lr.textRect.height/2);            lr.iconRect.y = (int)(viewRect.y                    + (float)viewRect.height/2                    - (float)lr.iconRect.height/2);        }        else if (verticalAlignment == SwingUtilities.BOTTOM) {            lr.textRect.y = (int)(viewRect.y                    + viewRect.height                    - (float)lr.labelRect.height/2                    - (float)lr.textRect.height/2);            lr.iconRect.y = (int)(viewRect.y                    + viewRect.height                    - (float)lr.labelRect.height/2                    - (float)lr.iconRect.height/2);        }    }     /**     * Sets labelRect Y coordinate     * taking into account the vertical alignment     */    private void calcLabelYPosition(LayoutResult lr) {        if (verticalAlignment == SwingUtilities.TOP) {            lr.labelRect.y  = viewRect.y;        } else if (verticalAlignment == SwingUtilities.CENTER) {            lr.labelRect.y = (int)(viewRect.y                    + (float)viewRect.height/2                    - (float)lr.labelRect.height/2);        } else if (verticalAlignment == SwingUtilities.BOTTOM) {            lr.labelRect.y  = viewRect.y + viewRect.height                    - lr.labelRect.height;        }    }     /**     * Returns parent of this component if it is not a top-level menu     * Otherwise returns null.     * @param menuItem the menu item whose parent will be returned.     * @return parent of this component if it is not a top-level menu     * Otherwise returns null.     */    public static JComponent getMenuItemParent(JMenuItem menuItem) {        Container parent = menuItem.getParent();        if ((parent instanceof JComponent) &&             (!(menuItem instanceof JMenu) ||               !((JMenu)menuItem).isTopLevelMenu())) {            return (JComponent) parent;        } else {            return null;        }    }     public static void clearUsedParentClientProperties(JMenuItem menuItem) {        clearUsedClientProperties(getMenuItemParent(menuItem));    }     public static void clearUsedClientProperties(JComponent c) {        if (c != null) {            c.putClientProperty(MAX_ARROW_WIDTH, null);            c.putClientProperty(MAX_CHECK_WIDTH, null);            c.putClientProperty(MAX_ACC_WIDTH, null);            c.putClientProperty(MAX_TEXT_WIDTH, null);            c.putClientProperty(MAX_ICON_WIDTH, null);            c.putClientProperty(MAX_LABEL_WIDTH, null);            c.putClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET, null);        }    }     /**     * Finds and returns maximal integer value in the given array.     * @param values array where the search will be performed.     * @return maximal vaule.     */    public static int max(int... values) {        int maxValue = Integer.MIN_VALUE;        for (int i : values) {            if (i > maxValue) {                maxValue = i;            }        }        return maxValue;    }     public static Rectangle createMaxRect() {        return new Rectangle(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);    }     public static void addMaxWidth(RectSize size, int gap, Dimension result) {        if (size.maxWidth > 0) {            result.width += size.maxWidth + gap;        }    }     public static void addWidth(int width, int gap, Dimension result) {        if (width > 0) {            result.width += width + gap;        }    }     public JMenuItem getMenuItem() {        return mi;    }     public JComponent getMenuItemParent() {        return miParent;    }     public Font getFont() {        return font;    }     public Font getAccFont() {        return accFont;    }     public FontMetrics getFontMetrics() {        return fm;    }     public FontMetrics getAccFontMetrics() {        return accFm;    }     public Icon getIcon() {        return icon;    }     public Icon getCheckIcon() {        return checkIcon;    }     public Icon getArrowIcon() {        return arrowIcon;    }     public String getText() {        return text;    }     public String getAccText() {        return accText;    }     public boolean isColumnLayout() {        return isColumnLayout;    }     public boolean useCheckAndArrow() {        return useCheckAndArrow;    }     public boolean isLeftToRight() {        return isLeftToRight;    }     public boolean isTopLevelMenu() {        return isTopLevelMenu;    }     public View getHtmlView() {        return htmlView;    }     public int getVerticalAlignment() {        return verticalAlignment;    }     public int getHorizontalAlignment() {        return horizontalAlignment;    }     public int getVerticalTextPosition() {        return verticalTextPosition;    }     public int getHorizontalTextPosition() {        return horizontalTextPosition;    }     public int getGap() {        return gap;    }     public int getLeadingGap() {        return leadingGap;    }     public int getAfterCheckIconGap() {        return afterCheckIconGap;    }     public int getMinTextOffset() {        return minTextOffset;    }     public Rectangle getViewRect() {        return viewRect;    }     public RectSize getIconSize() {        return iconSize;    }     public RectSize getTextSize() {        return textSize;    }     public RectSize getAccSize() {        return accSize;    }     public RectSize getCheckSize() {        return checkSize;    }     public RectSize getArrowSize() {        return arrowSize;    }     public RectSize getLabelSize() {        return labelSize;    }     protected void setMenuItem(JMenuItem mi) {        this.mi = mi;    }     protected void setMenuItemParent(JComponent miParent) {        this.miParent = miParent;    }     protected void setFont(Font font) {        this.font = font;    }     protected void setAccFont(Font accFont) {        this.accFont = accFont;    } 

Lines 501–1000 of 1323. Previous lines