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

📄 officexpmenuui.java

📁 java lookandfeel office pack
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ====================================================================
 * 
 * Office Look and Feels License
 * http://sourceforge.net/projects/officelnfs
 *
 * Copyright (c) 2003-2005 Robert Futrell.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The names "Office Look And Feels" and "OfficeLnFs" must not
 *    be used to endorse or promote products derived from this software
 *    without prior written permission. For written permission, please
 *    contact robert_futrell@users.sourceforge.net.
 *
 * 4. Products derived from this software may not be called "OfficeLnFs"
 *    nor may "OfficeLnFs" appear in their names without prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */ 
package org.fife.plaf.OfficeXP;

import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.ComponentUI;
import javax.swing.text.View;

import com.sun.java.swing.plaf.windows.WindowsMenuUI;


/**
 * Component UI for a menu using the Office XP Look and Feel.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class OfficeXPMenuUI extends WindowsMenuUI {

	private boolean isMouseOver = false;

 	// These rects are used for painting and preferred size calculations.
	// They are reused to keep from having to reallocate them.
	protected static final Rectangle zeroRect = new Rectangle(0,0,0,0);
	protected static Rectangle iconRect = new Rectangle();
	protected static Rectangle textRect = new Rectangle();
	protected static Rectangle arrowIconRect = new Rectangle();
	protected static Rectangle viewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
	protected static Rectangle r = new Rectangle();


    protected MouseInputListener createMouseInputListener(JComponent c) {
		return new MouseInputHandler();
    }


	public static ComponentUI createUI(JComponent x) {
		return new OfficeXPMenuUI();
    }


	/**
	 * Horrible, horrible hack to get non-top level menus' heights to be the
	 * same as that of regular menu items.
	 */
	protected Dimension getPreferredMenuItemSize(JComponent c,
									Icon checkIcon,
									Icon arrowIcon,
									int defaultTextIconGap) {

		JMenu menu = (JMenu)c;
		Dimension d = null;

		if (menu.isTopLevelMenu()) {
			d = super.getPreferredMenuItemSize(menu, checkIcon,
							arrowIcon, defaultTextIconGap);
			d.width += 4;	// Allows for the "shadow" to be drawn.
			d.height = 22;
		}
		else { // Menu that's a menu item inside another menu.
			d = getPreferredNonTopLevelMenuSize(menu, checkIcon, arrowIcon,
										defaultTextIconGap);
		}

		return d;

	}




	/**
	 * Computes the preferred dimensions of a menu that is NOT a top-level
	 * menu.  This method is called from <code>getPreferredMenuItemSize</code>
	 * because its super implementation won't properly calculate the
	 * preferred width of a non-top level menu in this LnF (because we always
	 * have an icon's width of space on the left, etc.).
	 */
	protected Dimension getPreferredNonTopLevelMenuSize(JMenu menu,
										Icon checkIcon,
										Icon arrowIcon,
										int defaultTextIconGap) {

		Icon icon = (Icon)menu.getIcon();
		String text = menu.getText();
		Font font = menu.getFont();
		FontMetrics fm = menu.getFontMetrics(font);
		resetRects();

		layoutMenuItem(fm, text, icon, arrowIcon,
					viewRect, iconRect, textRect, arrowIconRect,
					text == null ? 0 : defaultTextIconGap,
					defaultTextIconGap
					);

		// Find the union of the icon and text rects.
		r.setBounds(textRect);
		r = SwingUtilities.computeUnion(iconRect.x, iconRect.y,
                                        iconRect.width, iconRect.height, r);


		// If the width of this menu's text is longer than the parent menu's
		// current longest text, update it.  This is so that other menu
		// items in the parent menu can have their accelerators align.

		// Get the parent, which stores the information.
		Container parent = menu.getParent();

		if (parent!=null && (parent instanceof JComponent)) {

			//Get widest text so far from parent, if no one exists null
			// is returned.
			JComponent p = (JComponent) parent;
			Integer maxTextWidth = (Integer) p.getClientProperty(
								OfficeXPMenuItemUI.MAX_TEXT_WIDTH);
			int maxTextValue = maxTextWidth!=null ?
									maxTextWidth.intValue() : 0;

			//Compare the text widths, and adjust the r.width to the widest.
			if (r.width < maxTextValue)
				r.width = maxTextValue;
			else {
				p.putClientProperty(OfficeXPMenuItemUI.MAX_TEXT_WIDTH,
								new Integer(r.width));
			}

			r.width += defaultTextIconGap;

		}

		// Add in the checkIcon
		r.width += 20;//checkIconRect.width;
		r.width += defaultTextIconGap;

		// Add in the arrowIcon
		r.width += defaultTextIconGap;
		r.width += 12;//arrowIconRect.width;

		// Add in the "padding" on either side of the menu item.
		r.width += 2*defaultTextIconGap;

		Insets insets = menu.getInsets();
		if(insets != null) {
			r.width += insets.left + insets.right;
			r.height += insets.top + insets.bottom;
		}

		// if the width is even, bump it up one. This is critical
		// for the focus dash line to draw properly
		if(r.width%2 == 0)
			r.width++;

		// if the height is even, bump it up one. This is critical
		// for the text to center properly
		if(r.height%2 == 0)
			r.height++;

		return new Dimension((int)r.getWidth(),
							OfficeXPMenuItemUI.MENU_ITEM_HEIGHT);

	}


    /**
     * Get the temporary flag to indicate if the mouse has entered the menu.
     */
    protected boolean isMouseOver() {
		return isMouseOver;
    }


	/**
	 * Compute and return the location of the icons origin, the
	 * location of origin of the text baseline, and a possibly clipped
	 * version of the compound labels string.  Locations are computed
	 * relative to the viewRect rectangle.
	 */
    protected String layoutMenuItem(FontMetrics fm, String text, Icon icon,
					Icon arrowIcon, Rectangle viewRect, Rectangle iconRect,
					Rectangle textRect, Rectangle arrowIconRect,
					int textIconGap, int menuItemGap) {

		OfficeXPMenuItemUI.layoutCompoundLabel(menuItem, fm, text, viewRect,
										iconRect, textRect);

		// Initialize the arrowIcon bounds rectangle width & height.
		if (arrowIcon != null) {
			arrowIconRect.width = arrowIcon.getIconWidth();
			arrowIconRect.height = arrowIcon.getIconHeight();
		}
		else {
			arrowIconRect.width = arrowIconRect.height = 0;
		}

		Rectangle labelRect = iconRect.union(textRect);

		// Position the Arrow Icon.
		int temp = viewRect.x;// + 6;
		textRect.x += temp;
		iconRect.x += temp;
		if (menuItem.getComponentOrientation().isLeftToRight()) {
			arrowIconRect.x = viewRect.x + viewRect.width - menuItemGap -
							arrowIconRect.width;
		}
		else {
			arrowIconRect.x = viewRect.x + menuItemGap;
		}

		arrowIconRect.y = 8;//labelRect.y + (labelRect.height/2) - arrowIconRect.height/2;

		return text;

	}


    protected void paintBackground(Graphics g, JMenuItem menuItem,
    									Color bgColor) {
		// If the user is running pre-Windows XP, don't do all this jazz.
		if (OfficeXPLookAndFeel.isClassicWindows()) {
			super.paintBackground(g, menuItem, bgColor);
		}
		JMenu menu = (JMenu)menuItem;
		// If this is a submenu, it should be painted just like menu items.
		if (!menu.isTopLevelMenu()) {

⌨️ 快捷键说明

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