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

📄 officexpmenuitemui.java

📁 java lookandfeel office pack
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			if (menuItem.getComponentOrientation().isLeftToRight()) {
				acceleratorRect.x = viewRect.x + viewRect.width - 12/*arrowIconRect.width*/
									- menuItemGap - acceleratorRect.width;
			}
			else {
				acceleratorRect.x = viewRect.x + 20;
			}
			acceleratorRect.y = textRect.y;
		}

		Rectangle labelRect = iconRect.union(textRect);

        return text;

    }


    public void paint(final Graphics g, final JComponent c) {
        paintMenuItem(g, c, checkIcon, arrowIcon,
                      selectionBackground, selectionForeground,
                      defaultTextIconGap);
    }



    /**
     * Draws the background of the menu item.
     *
     * @param g the paint graphics
     * @param menuItem menu item to be painted
     * @param bgColor selection background color
     */
	protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {

		ButtonModel model = menuItem.getModel();
		Color oldColor = g.getColor();
		int menuItemWidth = menuItem.getWidth();
		int menuItemHeight = menuItem.getHeight();

		paintUnarmedBackground(g, menuItem);

		// 20061120 fabio: Highlight shouldn't be painted when item is disabled
		if (model.isArmed() && menuItem.isEnabled()) {

			int width = menuItemWidth - 3;
			int height = menuItemHeight - 2;

			g.setColor(UIManager.getColor("OfficeLnF.HighlightBorderColor"));
			g.drawRect(1,0, width,height);
			g.setColor(UIManager.getColor("OfficeLnF.HighlightColor"));
			g.fillRect(2,1, width-1,height-1);

		} else {
			// Do nothing; the background has already been painted above.
		}

		// Add the white line to the bottom item.  Note that this CANNOT be added as
		// a part of the popup menu's border because of Office XP's menu item design;
		// there's an empty line between each menu item, but the top and bottom empty
		// lines are pure background color (no "khaki" on the left).  If you can think
		// of a simpler way to do it, then by all means, go ahead.
		JPopupMenu popupMenu = (JPopupMenu)menuItem.getParent();
		if (popupMenu.getComponentIndex(menuItem) == popupMenu.getComponentCount()-1) {
			g.setColor(menuItem.getBackground());
			int y = menuItemHeight - 1;
			// Do whole line to cover both LTR and RTL.
			g.drawLine(0,y, menuItemWidth-1,y);
		}

		g.setColor(oldColor);

    }


	protected void paintCheck(Graphics g, JMenuItem menuItem) {
		ButtonModel model = menuItem.getModel();
		if(menuItem.isSelected()) {
			int x = iconRect.x-2;
			if (!menuItem.getComponentOrientation().isLeftToRight()) {
				x++; // ???
			}
			int y = 1;
			int width = 19;//20;//checkIconRect.width;
			int height = 19;//20;//checkIconRect.height;
			Color oldColor = g.getColor();
			g.setColor(UIManager.getColor("OfficeLnF.HighlightBorderColor"));
			g.drawRect(x,y, width,height);
			if (model.isArmed())
				g.setColor(UIManager.getColor("OfficeXPLnF.PressedHighlightColor"));
			else
				g.setColor(UIManager.getColor("OfficeXPLnF.CheckBoxHighlightColor"));
			g.fillRect(x+1,y+1, width-1,height-1);
			g.setColor(oldColor);
			checkIcon.paintIcon(menuItem, g, x+6,y+6);//8,7);//checkIconRect.x, checkIconRect.y);
		}
	}


	protected void paintIcon(Graphics g, JMenuItem menuItem) {
		OfficeXPUtilities.paintMenuItemIcon(g, menuItem, iconRect);
	}


	protected void paintMenuItem(Graphics g, JComponent c,
                                     Icon checkIcon, Icon arrowIcon,
                                     Color background, Color foreground,
                                     int defaultTextIconGap) {

		JMenuItem b = (JMenuItem) c;
		ButtonModel model = b.getModel();

		int menuWidth = b.getWidth();
		int menuHeight = b.getHeight();

		resetRects();

		viewRect.setBounds( 0, 0, menuWidth, menuHeight );

		Font holdf = g.getFont();
		Font f = c.getFont();
		g.setFont(f);
		FontMetrics fm = c.getFontMetrics(f);

		// get Accelerator text
		KeyStroke accelerator =  b.getAccelerator();
		String acceleratorText = "";
		if (accelerator != null) {
			int modifiers = accelerator.getModifiers();
			if (modifiers > 0) {
				acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
				acceleratorText += "+";
			}

			int keyCode = accelerator.getKeyCode();
			if (keyCode != 0)
				acceleratorText += KeyEvent.getKeyText(keyCode);
            	else
				acceleratorText += accelerator.getKeyChar();
		}

		// layout the text and icon
		String text = layoutMenuItem(
			fm, b.getText(), acceleratorText, b.getIcon(),
			checkIcon, arrowIcon,
			viewRect, iconRect, textRect, acceleratorRect,
			b.getText() == null ? 0 : defaultTextIconGap,
			defaultTextIconGap
		);

		// Paint background
		paintBackground(g, b, background);

		Color holdc = g.getColor();

		// Paint the Check
		boolean isCheckOrRadio = (c instanceof JCheckBoxMenuItem) ||
							(c instanceof JRadioButtonMenuItem);
		if (checkIcon != null && isCheckOrRadio)
			paintCheck(g, menuItem);

		// Paint the Icon
		if(b.getIcon()!=null && !isCheckOrRadio)
			paintIcon(g, menuItem);

		// Draw the Text
		paintText(g, b, textRect, text);

		// Draw the Accelerator Text
		if(!acceleratorText.equals("")) {

			//Get the maxAccWidth from the parent to calculate the offset.
			int accOffset = 0;
			Container parent = menuItem.getParent();
			if (parent != null && parent instanceof JComponent) {

				JComponent p = (JComponent) parent;
				Integer maxValueInt = (Integer) p.getClientProperty(OfficeXPMenuItemUI.MAX_ACC_WIDTH);
				int maxValue = maxValueInt != null ?
								maxValueInt.intValue() : acceleratorRect.width;

				//Calculate the offset, with which the accelerator texts will be drawn.
				accOffset = maxValue - acceleratorRect.width;

			}

			// Ensure all accelerators are right-aligned for RTL.
			if (!c.getComponentOrientation().isLeftToRight()) {
				Integer maxValueInt = null;
				if (parent!=null && parent instanceof JComponent) {
					maxValueInt = (Integer)((JComponent)b.getParent()).
						getClientProperty(OfficeXPMenuItemUI.MAX_ACC_WIDTH);
				}
				int maxValue = maxValueInt!=null ? maxValueInt.intValue() :
											acceleratorRect.width;
				accOffset = 0;
				acceleratorRect.x = 20 + maxValue - acceleratorRect.width;
			}

			if(!model.isEnabled()) {
				// *** paint the acceleratorText disabled

				if ( disabledForeground != null ) {
					g.setColor( disabledForeground );
					OfficeXPGraphicsUtils.drawString(g,fm,acceleratorText,0,
									acceleratorRect.x - accOffset,
									acceleratorRect.y + fm.getAscent());
				}
				else {
					g.setColor(b.getBackground().brighter());
					OfficeXPGraphicsUtils.drawString(g,fm,acceleratorText,0,
									acceleratorRect.x - accOffset,
									acceleratorRect.y + fm.getAscent());
					g.setColor(b.getBackground().darker());
					OfficeXPGraphicsUtils.drawString(g,fm,acceleratorText,0,
									acceleratorRect.x - accOffset - 1,
									acceleratorRect.y + fm.getAscent() - 1);
				}

			}
			else {
				// *** paint the acceleratorText normally
				g.setColor( acceleratorForeground );
				OfficeXPGraphicsUtils.drawString(g,fm,acceleratorText, 0,
									acceleratorRect.x - accOffset,
									acceleratorRect.y + fm.getAscent());
			}

		}

		g.setColor(holdc);
		g.setFont(holdf);

	}


    /**
     * Method which renders the text of the current menu item.
     *
     * @param g Graphics context
     * @param menuItem Current menu item to render
     * @param textRect Bounding rectangle to render the text.
     * @param text String to render
     */
	protected void paintText(Graphics g, JMenuItem menuItem,
						Rectangle textRect, String text) {

		ButtonModel model = menuItem.getModel();

		if(!model.isEnabled()) {
			OfficeXPGraphicsUtils.paintText(g, menuItem, textRect, text, 0);
		}
		else {
			FontMetrics fm = menuItem.getFontMetrics(g.getFont());
			int mnemonicIndex = menuItem.getDisplayedMnemonicIndex();
			Color oldColor = g.getColor();
			OfficeXPGraphicsUtils.drawStringUnderlineCharAt(g,fm,text,mnemonicIndex,
							textRect.x, textRect.y + fm.getAscent());
			g.setColor(oldColor);
		}

    }


	protected void paintUnarmedBackground(Graphics g, JMenuItem menuItem) {
		OfficeXPUtilities.paintMenuItemBackground(g, menuItem);
	}


    protected void resetRects() {
        iconRect.setBounds(zeroRect);
        textRect.setBounds(zeroRect);
        acceleratorRect.setBounds(zeroRect);
        viewRect.setBounds(0,0,Short.MAX_VALUE, Short.MAX_VALUE);
        r.setBounds(zeroRect);
    }


	/*
	 * Returns false if the component is a JMenu and it is a top
	 * level menu (on the menubar).
	 */
	private boolean useCheckAndArrow(){
		boolean b = true;
		if((menuItem instanceof JMenu) && (((JMenu)menuItem).isTopLevelMenu()))
			b = false;
		return b;
	}


	private class RMouseInputHandler extends MouseInputHandler {

		public void mouseExited(MouseEvent mouseEvent) {
			MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
			int mask = InputEvent.BUTTON1_MASK | InputEvent.BUTTON2_MASK |
					InputEvent.BUTTON3_MASK;
			if ((mouseEvent.getModifiers() & mask) != 0) {
				MenuSelectionManager.defaultManager().processMouseEvent(mouseEvent);
			}
			else {
				MenuElement path[] = menuSelectionManager.getSelectedPath();
				if (path.length > 1 && !(path[path.length-1] instanceof JPopupMenu)) {
					MenuElement newPath[] = new MenuElement[path.length-1];
					for (int i = 0; i < path.length - 1; i++)
						newPath[i] = path[i];
					menuSelectionManager.setSelectedPath(newPath);
				}
			}
		}

	}


}

⌨️ 快捷键说明

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