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

📄 menuitemui.java

📁 用于java swing的皮肤软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

	/**	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 menuWidth= menuItem.getWidth();
		int menuHeight= menuItem.getHeight();

		if (menuItem.isOpaque())
		{
			g.setColor(menuItem.getBackground());
			g.fillRect(0, 0, menuWidth, menuHeight);

			if (model.isArmed()
				|| (menuItem instanceof JMenu && model.isSelected()))
			{
				g.setColor(bgColor);
				g.fillRect(1, 1, menuWidth-2, menuHeight-2);
			}
			g.setColor(oldColor);
		}
	}

	
	/**	Renders the text of the current menu item.
	 * 
	 * 	@param	g			Graphics context
	 * 	@param	menuItem	Menu item to render
	 * 	@param	textRect	Bounding rectangle for rendering the text
	 * 	@param 	text		String to render
	 */
	protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text)
	{
		ButtonModel model= menuItem.getModel();
		FontMetrics fm= g.getFontMetrics();
		int mnemIndex= menuItem.getDisplayedMnemonicIndex();

		if (!model.isEnabled())
		{
			// *** paint the text disabled
			if (UIManager.get("MenuItem.disabledForeground") instanceof Color)
			{
				g.setColor(UIManager.getColor("MenuItem.disabledForeground"));
				BasicGraphicsUtils.drawStringUnderlineCharAt(
					g,
					text,
					mnemIndex,
					textRect.x,
					textRect.y + fm.getAscent());
			}
			else
			{
				g.setColor(menuItem.getBackground().brighter());
				BasicGraphicsUtils.drawStringUnderlineCharAt(
					g,
					text,
					mnemIndex,
					textRect.x,
					textRect.y + fm.getAscent());
				g.setColor(menuItem.getBackground().darker());
				BasicGraphicsUtils.drawStringUnderlineCharAt(
					g,
					text,
					mnemIndex,
					textRect.x - 1,
					textRect.y + fm.getAscent() - 1);
			}
		}
		else
		{
			// *** paint the text normally
			if (model.isArmed()
				|| (menuItem instanceof JMenu && model.isSelected()))
			{
				g.setColor(selectionForeground); // Uses protected field.
			}
			BasicGraphicsUtils.drawStringUnderlineCharAt(
				g,
				text,
				mnemIndex,
				textRect.x,
				textRect.y + fm.getAscent());
		}
	}


	/** 
	 * 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. 
	 */
	private String layoutMenuItem(FontMetrics fm, String text, FontMetrics fmAccel, String acceleratorText,
		Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment,
		int verticalTextPosition, int horizontalTextPosition, Rectangle viewRect, Rectangle iconRect,
		Rectangle textRect, Rectangle acceleratorRect, Rectangle checkIconRect, Rectangle arrowIconRect,
		int textIconGap, int menuItemGap, int maxTextWidth, int maxAccWidth, boolean arrowUsed,
		boolean iconUsed)
	{
		
		viewRect.x=0;
		viewRect.y=0;
		viewRect.width=10;
		viewRect.height=20;
		
		if(icon!=null)
		{
			iconRect.x=6+(16-icon.getIconWidth())/2;
			iconRect.y=(viewRect.height-icon.getIconHeight())/2;
			iconRect.width=16;
			iconRect.height=icon.getIconHeight();
		}
		
		if(checkIcon!=null)
		{
			checkIconRect.x=6+(16-checkIcon.getIconWidth())/2;
			checkIconRect.y=(viewRect.height-checkIcon.getIconHeight())/2;;
			checkIconRect.width=16;
			checkIconRect.height=checkIcon.getIconHeight();
		}
						
		if(iconUsed || !(menuItem.getParent() instanceof JMenuBar))
			textRect.x=26;
		else
			textRect.x=6;
			
		textRect.y=(viewRect.height-fm.getAscent()-fm.getDescent())/2;
		textRect.width=fm.stringWidth(text);
		textRect.height=fm.getAscent()+fm.getDescent();
		
		if(iconUsed || !(menuItem.getParent() instanceof JMenuBar))
			acceleratorRect.x=20+maxTextWidth+10;
		else
			acceleratorRect.x=maxTextWidth+10;
			
		if(maxAccWidth==0)
			acceleratorRect.x-=10;
			
		acceleratorRect.y=(viewRect.height-fmAccel.getAscent()-fmAccel.getDescent())/2;
		acceleratorRect.width=fmAccel.stringWidth(acceleratorText);
		acceleratorRect.height=fmAccel.getAscent()+fmAccel.getDescent();

		if(arrowIcon!=null && arrowUsed)
		{		
			arrowIconRect.x=acceleratorRect.x+maxAccWidth+14-arrowIcon.getIconWidth();
			arrowIconRect.y=(viewRect.height-arrowIcon.getIconHeight())/2;
			arrowIconRect.width=arrowIcon.getIconWidth();
			arrowIconRect.height=arrowIcon.getIconHeight();
		}
		else
		{
			arrowIconRect.x=acceleratorRect.x+maxAccWidth;
			if(!(menuItem instanceof JMenu))
				arrowIconRect.x+=24;
				
			arrowIconRect.y=0;
			arrowIconRect.width=0;
			arrowIconRect.height=viewRect.height;
		}		
					
		viewRect.width=arrowIconRect.x+arrowIconRect.width+10;
		
		return text;
	}
	
	
	/*
	 * 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;
	}


	/**	Returns the menu tree path to the associated menu item */
	public MenuElement[] getPath()
	{
		MenuSelectionManager m= MenuSelectionManager.defaultManager();
		MenuElement oldPath[]= m.getSelectedPath();
		MenuElement newPath[];
		int i= oldPath.length;
		if (i == 0)
			return new MenuElement[0];
		Component parent= menuItem.getParent();
		if (oldPath[i - 1].getComponent() == parent)
		{
			// The parent popup menu is the last so far
			newPath= new MenuElement[i + 1];
			System.arraycopy(oldPath, 0, newPath, 0, i);
			newPath[i]= menuItem;
		}
		else
		{
			// A sibling menuitem is the current selection
			// 
			//  This probably needs to handle 'exit submenu into 
			// a menu item.  Search backwards along the current
			// selection until you find the parent popup menu,
			// then copy up to that and add yourself...
			int j;
			for (j= oldPath.length - 1; j >= 0; j--)
			{
				if (oldPath[j].getComponent() == parent)
					break;
			}
			newPath= new MenuElement[j + 2];
			System.arraycopy(oldPath, 0, newPath, 0, j + 1);
			newPath[j + 1]= menuItem;
			/*
			System.out.println("Sibling condition -- ");
			System.out.println("Old array : ");
			printMenuElementArray(oldPath, false);
			System.out.println("New array : ");
			printMenuElementArray(newPath, false);
			*/
		}
		return newPath;
	}

	void printMyMenuElementArray(MenuElement path[], boolean dumpStack)
	{
		System.out.println("Path is(");
		int i, j;
		for (i= 0, j= path.length; i < j; i++)
		{
			for (int k= 0; k <= i; k++)
				System.out.print("  ");
			MenuElement me= (MenuElement) path[i];
			if (me instanceof JMenuItem)
				System.out.println(((JMenuItem) me).getText() + ", ");
			else if (me == null)
				System.out.println("NULL , ");
			else
				System.out.println("" + me + ", ");
		}
		System.out.println(")");

		if (dumpStack == true)
			Thread.dumpStack();
	}
	
	
	protected class MouseInputHandler implements MouseInputListener
	{
		public void mouseClicked(MouseEvent e)
		{
		}
		public void mousePressed(MouseEvent e)
		{
		}
		public void mouseReleased(MouseEvent e)
		{
			MenuSelectionManager manager= MenuSelectionManager.defaultManager();
			Point p= e.getPoint();
			if (p.x >= 0
				&& p.x < menuItem.getWidth()
				&& p.y >= 0
				&& p.y < menuItem.getHeight())
			{
				doClick(manager);
			}
			else
			{
				manager.processMouseEvent(e);
			}
		}
		public void mouseEntered(MouseEvent e)
		{
			MenuSelectionManager manager= MenuSelectionManager.defaultManager();
			int modifiers= e.getModifiers();
			// 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2	    
			if ((modifiers
				& (InputEvent.BUTTON1_MASK
					| InputEvent.BUTTON2_MASK
					| InputEvent.BUTTON3_MASK))
				!= 0)
			{
				MenuSelectionManager.defaultManager().processMouseEvent(e);
			}
			else
			{
				manager.setSelectedPath(getPath());
			}
		}
		public void mouseExited(MouseEvent e)
		{
			MenuSelectionManager manager= MenuSelectionManager.defaultManager();

			int modifiers= e.getModifiers();
			// 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
			if ((modifiers
				& (InputEvent.BUTTON1_MASK
					| InputEvent.BUTTON2_MASK
					| InputEvent.BUTTON3_MASK))
				!= 0)
			{
				MenuSelectionManager.defaultManager().processMouseEvent(e);
			}
			else
			{

				MenuElement path[]= manager.getSelectedPath();
				if (path.length > 1)
				{
					MenuElement newPath[]= new MenuElement[path.length - 1];
					int i, c;
					for (i= 0, c= path.length - 1; i < c; i++)
						newPath[i]= path[i];
					manager.setSelectedPath(newPath);
				}
			}
		}

		public void mouseDragged(MouseEvent e)
		{
			MenuSelectionManager.defaultManager().processMouseEvent(e);
		}
		public void mouseMoved(MouseEvent e)
		{
		}
	}

	private class MenuDragMouseHandler implements MenuDragMouseListener
	{
		public void menuDragMouseEntered(MenuDragMouseEvent e)
		{
		}
		public void menuDragMouseDragged(MenuDragMouseEvent e)
		{
			MenuSelectionManager manager= e.getMenuSelectionManager();
			MenuElement path[]= e.getPath();
			manager.setSelectedPath(path);
		}
		public void menuDragMouseExited(MenuDragMouseEvent e)
		{
		}
		public void menuDragMouseReleased(MenuDragMouseEvent e)
		{
			MenuSelectionManager manager= e.getMenuSelectionManager();
			MenuElement path[]= e.getPath();
			Point p= e.getPoint();
			if (p.x >= 0
				&& p.x < menuItem.getWidth()
				&& p.y >= 0
				&& p.y < menuItem.getHeight())
			{
				doClick(manager);
			}
			else
			{
				manager.clearSelectedPath();
			}
		}
	}

	private class MenuKeyHandler implements MenuKeyListener
	{
		public void menuKeyTyped(MenuKeyEvent e)
		{
			if (DEBUG)
			{
				System.out.println(
					"in BasicMenuItemUI.menuKeyTyped for " + menuItem.getText());
			}
			int key= menuItem.getMnemonic();
			if (key == 0)
				return;
			if (lower(key) == lower((int) (e.getKeyChar())))
			{
				MenuSelectionManager manager= e.getMenuSelectionManager();
				doClick(manager);
				e.consume();
			}
		}
		public void menuKeyPressed(MenuKeyEvent e)
		{
			if (DEBUG)
			{
				System.out.println(
					"in BasicMenuItemUI.menuKeyPressed for " + menuItem.getText());
			}
		}
		public void menuKeyReleased(MenuKeyEvent e)
		{
		}

		private int lower(int ascii)
		{
			if (ascii >= 'A' && ascii <= 'Z')
				return ascii + 'a' - 'A';
			else
				return ascii;
		}

	}

	private class PropertyChangeHandler implements PropertyChangeListener
	{
		public void propertyChange(PropertyChangeEvent e)
		{
			String name= e.getPropertyName();

			if (name.equals("labelFor")
				|| name.equals("displayedMnemonic")
				|| name.equals("accelerator"))
			{
				updateMyAcceleratorBinding();
			}
			else if (
				name.equals("text")
					|| "font".equals(name)
					|| "foreground".equals(name))
			{
				// remove the old html view client property if one
				// existed, and install a new one if the text installed
				// into the JLabel is html source.
				JMenuItem lbl= ((JMenuItem) e.getSource());
				String text= lbl.getText();
				BasicHTML.updateRenderer(lbl, text);
			}
		}
	}

	private static class ClickAction extends AbstractAction
	{
		public void actionPerformed(ActionEvent e)
		{
			JMenuItem mi= (JMenuItem) e.getSource();
			MenuSelectionManager.defaultManager().clearSelectedPath();
			mi.doClick();
		}
	}

	/**
	 * Call this method when a menu item is to be activated.
	 * This method handles some of the details of menu item activation
	 * such as clearing the selected path and messaging the 
	 * JMenuItem's doClick() method.
	 *
	 * @param msm  A MenuSelectionManager. The visual feedback and 
	 *             internal bookkeeping tasks are delegated to 
	 *             this MenuSelectionManager. If <code>null</code> is
	 *             passed as this argument, the 
	 *             <code>MenuSelectionManager.defaultManager</code> is
	 *             used.
	 * @see MenuSelectionManager
	 * @see JMenuItem#doClick(int)
	 * @since 1.4
	 */
	protected void doClick(MenuSelectionManager msm)
	{
		// Auditory cue
		if (!isInternalFrameSystemMenu())
		{
			ActionMap map= menuItem.getActionMap();
			if (map != null)
			{
				Action audioAction= map.get(getPropertyPrefix() + ".commandSound");
				//		if (audioAction != null) {
				//			 // pass off firing the Action to a utility method
				//			 BasicLookAndFeel lf = (BasicLookAndFeel)
				//									 UIManager.getLookAndFeel();
				//			 lf.playSound(audioAction);
				//		}
			}
		}
		// Visual feedback
		if (msm == null)
		{
			msm= MenuSelectionManager.defaultManager();
		}
		msm.clearSelectedPath();
		menuItem.doClick(0);
	}

	/** 
	 * This is to see if the menu item in question is part of the 
	 * system menu on an internal frame.
	 * The Strings that are being checked can be found in 
	 * MetalInternalFrameTitlePaneUI.java,
	 * WindowsInternalFrameTitlePaneUI.java, and
	 * MotifInternalFrameTitlePaneUI.java.
	 *
	 * @since 1.4
	 */
	private boolean isInternalFrameSystemMenu()
	{
		String actionCommand= menuItem.getActionCommand();
		if ((actionCommand == "Close")
			|| (actionCommand == "Minimize")
			|| (actionCommand == "Restore")
			|| (actionCommand == "Maximize"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**	Convenience function for determining ComponentOrientation.  Helps us
	 * 	avoid having Munge directives throughout the code.
	 */
	static boolean isLeftToRight(Component c)
	{
		return c.getComponentOrientation().isLeftToRight();
	}
}

⌨️ 快捷键说明

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