MenuItemLayoutHelper.java revision 619
/*
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* Calculates preferred size and layouts menu items.
*/
public class MenuItemLayoutHelper {
/* Client Property keys for calculation of maximal widths */
public static final StringUIClientPropertyKey MAX_ARROW_WIDTH =
new StringUIClientPropertyKey("maxArrowWidth");
public static final StringUIClientPropertyKey MAX_CHECK_WIDTH =
new StringUIClientPropertyKey("maxCheckWidth");
public static final StringUIClientPropertyKey MAX_ICON_WIDTH =
new StringUIClientPropertyKey("maxIconWidth");
public static final StringUIClientPropertyKey MAX_TEXT_WIDTH =
new StringUIClientPropertyKey("maxTextWidth");
public static final StringUIClientPropertyKey MAX_ACC_WIDTH =
new StringUIClientPropertyKey("maxAccWidth");
public static final StringUIClientPropertyKey MAX_LABEL_WIDTH =
new StringUIClientPropertyKey("maxLabelWidth");
private JComponent miParent;
private FontMetrics fm;
private FontMetrics accFm;
private boolean isColumnLayout;
private boolean useCheckAndArrow;
private boolean isLeftToRight;
private boolean isTopLevelMenu;
private int verticalAlignment;
private int horizontalAlignment;
private int verticalTextPosition;
private int horizontalTextPosition;
private int gap;
private int leadingGap;
private int afterCheckIconGap;
private int minTextOffset;
/**
* The empty protected constructor is necessary for derived classes.
*/
protected MenuItemLayoutHelper() {
}
}
this.useCheckAndArrow = useCheckAndArrow;
this.isLeftToRight = isLeftToRight;
}
private void setOriginalWidths() {
}
if (accelerator != null) {
if (modifiers > 0) {
}
if (keyCode != 0) {
} else {
}
}
return accText;
}
// In case of column layout, .checkIconFactory is defined for this UI,
// the icon is compatible with it and useCheckAndArrow() is true,
// then the icon is handled by the checkIcon.
+ ".checkIconFactory");
}
return icon;
}
int minimumTextOffset = 0;
if (minimumTextOffsetObject instanceof Integer) {
}
return minimumTextOffset;
}
int afterCheckIconGap = gap;
if (afterCheckIconGapObject instanceof Integer) {
}
return afterCheckIconGap;
}
return getCheckOffset(propertyPrefix);
} else {
return gap; // There is no any check icon
}
}
int checkIconOffset = gap;
if (checkIconOffsetObject instanceof Integer) {
}
return checkIconOffset;
}
protected void calcWidthsAndHeights() {
// iconRect
}
// accRect
}
// textRect
text = "";
// Text is HTML
} else {
// Text isn't HTML
}
}
if (useCheckAndArrow) {
// checkIcon
}
// arrowRect
}
}
// labelRect
if (isColumnLayout) {
} else {
}
}
protected void calcMaxWidths() {
if (isColumnLayout) {
curGap = 0;
}
} else {
// We shouldn't use current icon and text widths
// in maximal widths calculation for complex layout.
// If maxLabelWidth is wider
// than the widest icon + the widest text + gap,
// we should update the maximal text witdh
}
}
}
}
/**
* Calculates and returns maximal value through specified parent component
* client property.
*
* @param propertyName name of the property, which stores the maximal value.
* @param value a value which pretends to be maximal
* @return maximal value among the parent property and the value.
*/
// Get maximal value from parent client property
// Store new maximal width in parent client property
}
return value;
} else {
return maxValue;
}
}
/**
* Returns parent client property as int.
* @param propertyName name of the parent property.
* @return value of the property as int.
*/
}
value = 0;
}
}
public static boolean isColumnLayout(boolean isLeftToRight,
}
/**
* Answers should we do column layout for a menu item or not.
* We do it when a user doesn't set any alignments
* and text positions manually, except the vertical alignment.
*/
public static boolean isColumnLayout(boolean isLeftToRight,
int horizontalAlignment,
int verticalTextPosition) {
return false;
}
if (isLeftToRight) {
return false;
}
return false;
}
} else {
return false;
}
return false;
}
}
return true;
}
/**
* Calculates maximal text offset.
* It is required for some L&Fs (ex: Vista L&F).
* The offset is meaningful only for L2R column layout.
*
* @param viewRect the rectangle, the maximal text offset
* will be calculated for.
*/
if (!isColumnLayout || !isLeftToRight) {
return;
}
// Calculate the current text offset
}
}
// maximal text offset shouldn't be less than minimal text offset;
if (offset < minTextOffset) {
}
// Calculate and store the maximal text offset
}
/**
* Layout icon, text, check icon, accelerator text and arrow icon
* in the viewRect and return their positions.
*
* If horizontalAlignment, verticalTextPosition and horizontalTextPosition
* are default (user doesn't set any manually) the layouting algorithm is:
* Elements are layouted in the five columns:
* check icon + icon + text + accelerator text + arrow icon
*
* In the other case elements are layouted in the four columns:
* check icon + label + accelerator text + arrow icon
* Label is union of icon and text.
*
* The order of columns can be reversed.
* It depends on the menu item orientation.
*/
public LayoutResult layoutMenuItem() {
if (isColumnLayout()) {
if (isLeftToRight()) {
} else {
}
} else {
if (isLeftToRight()) {
} else {
}
}
return lr;
}
private LayoutResult createLayoutResult() {
return new LayoutResult(
);
}
public ColumnAlignment getLTRColumnAlignment() {
return ColumnAlignment.LEFT_ALIGNMENT;
}
public ColumnAlignment getRTLColumnAlignment() {
return ColumnAlignment.RIGHT_ALIGNMENT;
}
}
/**
* Aligns the accelertor text and the check and arrow icons vertically
* with the center of the label rect.
*/
if (useCheckAndArrow) {
}
}
/**
* Fixes vertical alignment of all menu item elements if rect.y
* or (rect.y + rect.height) is out of viewRect bounds
*/
int delta = 0;
if (r.y < viewRect.y) {
}
if (delta != 0) {
}
}
// Set maximal width for all the five basic rects
// (three other ones are already maximal)
// Set X coordinates
// All rects will be aligned at the left side
// Tune afterCheckIconGap
}
// Take into account minimal text offset
}
// Take into account the left side bearings for text and accelerator text.
// Set Y coordinate for text and icon.
// Y coordinates for other rects
// will be calculated later in layoutMenuItem.
// Calculate valid X and Y coordinates for labelRect
}
// Set X coordinates
// Tune afterCheckIconGap
}
// Take into account minimal text offset
}
// Take into account the left side bearing for accelerator text.
// The LSB for text is taken into account in layoutCompoundLabel() below.
// Center labelRect vertically
}
// Set maximal width for all the five basic rects
// (three other ones are already maximal)
// Set X coordinates
// Tune the gap after check icon
}
// Take into account minimal text offset
}
// Take into account the left side bearings for text and accelerator text.
// Set Y coordinates for text and icon.
// Y coordinates for other rects
// will be calculated later in layoutMenuItem.
// Calculate valid X and Y coordinate for labelRect
}
// Set X coordinates
// Tune the gap after check icon
}
// Take into account minimal text offset
}
// Take into account the left side bearing for accelerator text.
// The LSB for text is taken into account in layoutCompoundLabel() below.
// Center labelRect vertically
}
iconSize.getOrigWidth());
textSize.getOrigWidth());
accSize.getOrigWidth());
}
}
}
}
}
}
}
}
}
}
/**
* Takes into account the left side bearings for text and accelerator text
*/
if (lsb < 0) {
}
}
}
/**
* Takes into account the left side bearing for accelerator text
*/
if (lsb < 0) {
}
}
/**
* Sets Y coordinates of text and icon
* taking into account the vertical alignment
*/
}
}
}
/**
* Sets labelRect Y coordinate
* taking into account the vertical alignment
*/
}
}
/**
* 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.
*/
if ((parent instanceof JComponent) &&
return (JComponent) parent;
} else {
return null;
}
}
}
public static void clearUsedClientProperties(JComponent c) {
if (c != null) {
}
}
/**
* Finds and returns maximal integer value in the given array.
* @param values array where the search will be performed.
* @return maximal vaule.
*/
for (int i : values) {
if (i > maxValue) {
maxValue = i;
}
}
return maxValue;
}
public static Rectangle createMaxRect() {
}
}
}
if (width > 0) {
}
}
public JMenuItem getMenuItem() {
return mi;
}
public JComponent getMenuItemParent() {
return miParent;
}
return font;
}
public Font getAccFont() {
return accFont;
}
public FontMetrics getFontMetrics() {
return fm;
}
public FontMetrics getAccFontMetrics() {
return accFm;
}
return icon;
}
public Icon getCheckIcon() {
return checkIcon;
}
public Icon getArrowIcon() {
return arrowIcon;
}
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 setColumnLayout(boolean columnLayout) {
}
protected void setUseCheckAndArrow(boolean useCheckAndArrow) {
this.useCheckAndArrow = useCheckAndArrow;
}
protected void setLeftToRight(boolean leftToRight) {
}
protected void setTopLevelMenu(boolean topLevelMenu) {
}
}
protected void setVerticalAlignment(int verticalAlignment) {
this.verticalAlignment = verticalAlignment;
}
protected void setHorizontalAlignment(int horizontalAlignment) {
}
protected void setVerticalTextPosition(int verticalTextPosition) {
}
protected void setHorizontalTextPosition(int horizontalTextPosition) {
}
}
protected void setLeadingGap(int leadingGap) {
this.leadingGap = leadingGap;
}
protected void setAfterCheckIconGap(int afterCheckIconGap) {
this.afterCheckIconGap = afterCheckIconGap;
}
protected void setMinTextOffset(int minTextOffset) {
this.minTextOffset = minTextOffset;
}
}
}
}
}
}
}
}
/**
* Returns false if the component is a JMenu and it is a top
* level menu (on the menubar).
*/
boolean b = true;
b = false;
}
return b;
}
public static class LayoutResult {
public LayoutResult() {
}
}
public Rectangle getIconRect() {
return iconRect;
}
}
public Rectangle getTextRect() {
return textRect;
}
}
public Rectangle getAccRect() {
return accRect;
}
}
public Rectangle getCheckRect() {
return checkRect;
}
}
public Rectangle getArrowRect() {
return arrowRect;
}
}
public Rectangle getLabelRect() {
return labelRect;
}
}
return result;
}
}
public static class ColumnAlignment {
private int checkAlignment;
private int iconAlignment;
private int textAlignment;
private int accAlignment;
private int arrowAlignment;
public static final ColumnAlignment LEFT_ALIGNMENT =
new ColumnAlignment(
);
public static final ColumnAlignment RIGHT_ALIGNMENT =
new ColumnAlignment(
);
int textAlignment, int accAlignment,
int arrowAlignment) {
this.checkAlignment = checkAlignment;
this.iconAlignment = iconAlignment;
this.textAlignment = textAlignment;
this.accAlignment = accAlignment;
this.arrowAlignment = arrowAlignment;
}
public int getCheckAlignment() {
return checkAlignment;
}
public int getIconAlignment() {
return iconAlignment;
}
public int getTextAlignment() {
return textAlignment;
}
public int getAccAlignment() {
return accAlignment;
}
public int getArrowAlignment() {
return arrowAlignment;
}
}
public static class RectSize {
private int width;
private int height;
private int origWidth;
private int maxWidth;
public RectSize() {
}
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getOrigWidth() {
return origWidth;
}
public int getMaxWidth() {
return maxWidth;
}
}
}
public void setOrigWidth(int origWidth) {
}
public void setMaxWidth(int maxWidth) {
}
}
}
}