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

📄 xpmenuui.java

📁 Swing Windows XP 外观和感觉 BeanSoft 修改版, 2003年 原始的作者: XP 外观和感觉 by Stefan Krause - http://www.stefan
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * methods should call the parent methods so that the menu selection
	 * is correct.
	 *
	 * @see javax.swing.MenuSelectionManager
	 * @since 1.4
	 */
	protected class MouseInputHandler implements MouseInputListener {
		public void mouseClicked(MouseEvent e) {
		}

		/**
		 * Invoked when the mouse has been clicked on the menu. This
		 * method clears or sets the selection path of the
		 * MenuSelectionManager.
		 * 
		 * @param e the mouse event
		 */
		public void mousePressed(MouseEvent e) {
			JMenu menu = (JMenu) menuItem;
			if (!menu.isEnabled())
				return;

			MenuSelectionManager manager = MenuSelectionManager.defaultManager();
			if (menu.isTopLevelMenu()) {
				if (menu.isSelected()) {
					manager.clearSelectedPath();
				} else {
					Container cnt = menu.getParent();
					if (cnt != null && cnt instanceof JMenuBar) {
						MenuElement me[] = new MenuElement[2];
						me[0] = (MenuElement) cnt;
						me[1] = menu;
						manager.setSelectedPath(me);
					}
				}
			}

			MenuElement selectedPath[] = manager.getSelectedPath();
			if (selectedPath.length > 0 && selectedPath[selectedPath.length - 1] != menu.getPopupMenu()) {

				if (menu.isTopLevelMenu() || menu.getDelay() == 0) {
					appendPath(selectedPath, menu.getPopupMenu());
				} else {
					setupPostTimer(menu);
				}
			}
		}

		/**
		 * Invoked when the mouse has been released on the menu. Delegates the 
		 * mouse event to the MenuSelectionManager.
		 *
		 * @param e the mouse event
		 */
		public void mouseReleased(MouseEvent e) {
			JMenu menu = (JMenu) menuItem;
			if (!menu.isEnabled())
				return;
			MenuSelectionManager manager = MenuSelectionManager.defaultManager();
			manager.processMouseEvent(e);
			if (!e.isConsumed())
				manager.clearSelectedPath();
		}

		/**
		 * Invoked when the cursor enters the menu. This method sets the selected
		 * path for the MenuSelectionManager and handles the case
		 * in which a menu item is used to pop up an additional menu, as in a 
		 * hierarchical menu system.
		 * 
		 * @param e the mouse event; not used
		 */
		public void mouseEntered(MouseEvent e) {
			JMenu menu = (JMenu) menuItem;
			if (!menu.isEnabled())
				return;

			menu.putClientProperty("rollover",Boolean.TRUE);
			menu.repaint();
			MenuSelectionManager manager = MenuSelectionManager.defaultManager();
			MenuElement selectedPath[] = manager.getSelectedPath();
			if (!menu.isTopLevelMenu()) {
				if (!(selectedPath.length > 0 && selectedPath[selectedPath.length - 1] == menu.getPopupMenu())) {
					if (menu.getDelay() == 0) {
						appendPath(getPath(), menu.getPopupMenu());
					} else {
						manager.setSelectedPath(getPath());
						setupPostTimer(menu);
					}
				}
			} else {
				if (selectedPath.length > 0 && selectedPath[0] == menu.getParent()) {
					MenuElement newPath[] = new MenuElement[3];
					// A top level menu's parent is by definition 
					// a JMenuBar
					newPath[0] = (MenuElement) menu.getParent();
					newPath[1] = menu;
					newPath[2] = menu.getPopupMenu();
					manager.setSelectedPath(newPath);
				}
			}
			menu.repaint();
		}
		public void mouseExited(MouseEvent e) {
			JMenu menu = (JMenu) menuItem;
			menu.putClientProperty("rollover",Boolean.FALSE);
			menu.repaint();
		}

		/**
		 * Invoked when a mouse button is pressed on the menu and then dragged.
		 * Delegates the mouse event to the MenuSelectionManager.
		 *
		 * @param e the mouse event
		 * @see java.awt.event.MouseMotionListener#mouseDragged
		 */
		public void mouseDragged(MouseEvent e) {
			JMenu menu = (JMenu) menuItem;
			if (!menu.isEnabled())
				return;
			MenuSelectionManager.defaultManager().processMouseEvent(e);
		}
		public void mouseMoved(MouseEvent e) {
		}
	}

	private static class MenuHandler implements MenuListener {
		public void menuSelected(MenuEvent e) {
		}
		public void menuDeselected(MenuEvent e) {
		}
		public void menuCanceled(MenuEvent e) {
			JMenu m = (JMenu) e.getSource();
			MenuSelectionManager manager = MenuSelectionManager.defaultManager();
			if (manager.isComponentPartOfCurrentMenu(m))
				MenuSelectionManager.defaultManager().clearSelectedPath();
		}

	}

	/**
	 * As of Java 2 platform 1.4, this previously undocumented class
	 * is now obsolete. KeyBindings are now managed by the popup menu.
	 */
	public class ChangeHandler implements ChangeListener {
		public JMenu menu;
		public XPMenuUI ui;
		public boolean isSelected = false;
		public Component wasFocused;

		public ChangeHandler(JMenu m, XPMenuUI ui) {
			menu = m;
			this.ui = ui;
		}

		public void stateChanged(ChangeEvent e) {
		}
	}

	private class MenuDragMouseHandler implements MenuDragMouseListener {
		public void menuDragMouseEntered(MenuDragMouseEvent e) {
		}
		public void menuDragMouseDragged(MenuDragMouseEvent e) {
			if (menuItem.isEnabled() == false)
				return;

			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()) {
				JMenu menu = (JMenu) menuItem;
				MenuElement selectedPath[] = manager.getSelectedPath();
				if (!(selectedPath.length > 0 && selectedPath[selectedPath.length - 1] == menu.getPopupMenu())) {
					if (menu.isTopLevelMenu() || menu.getDelay() == 0 || e.getID() == MouseEvent.MOUSE_DRAGGED) {
						appendPath(path, menu.getPopupMenu());
					} else {
						manager.setSelectedPath(path);
						setupPostTimer(menu);
					}
				}
			} else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
				Component comp = manager.componentForPoint(e.getComponent(), e.getPoint());
				if (comp == null)
					manager.clearSelectedPath();
			}

		}
		public void menuDragMouseExited(MenuDragMouseEvent e) {
		}
		public void menuDragMouseReleased(MenuDragMouseEvent e) {
		}
	}

	static JPopupMenu getActivePopupMenu() {
		MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath();
		for (int i = path.length - 1; i >= 0; i--) {
			MenuElement elem = path[i];
			if (elem instanceof JPopupMenu) {
				return (JPopupMenu) elem;
			}
		}
		return null;
	}

	/**
	 * Handles the mnemonic handling for the JMenu and JMenuItems.
	 */
	private class MenuKeyHandler implements MenuKeyListener {

		// fields for handling duplicate mnemonics.
		private int indexes[];
		private char lastMnemonic;
		private int lastIndex;
		private int matches;

		/**
		 * Opens the SubMenu
		 */
		public void menuKeyTyped(MenuKeyEvent e) {
			if (DEBUG) {
				System.out.println("in XPMenuUI.menuKeyTyped for " + menuItem.getText());
			}
			if (!crossMenuMnemonic) {
				JPopupMenu pm = getActivePopupMenu();
				if (pm != null && pm != menuItem.getParent()) {
					return;
				}
			}

			int key = menuItem.getMnemonic();
			if (key == 0)
				return;
			MenuElement path[] = e.getPath();
			if (lower((char) key) == lower(e.getKeyChar())) {
				JPopupMenu popupMenu = ((JMenu) menuItem).getPopupMenu();
				MenuElement sub[] = popupMenu.getSubElements();
				if (sub.length > 0) {
					MenuSelectionManager manager = e.getMenuSelectionManager();
					MenuElement newPath[] = new MenuElement[path.length + 2];
					System.arraycopy(path, 0, newPath, 0, path.length);
					newPath[path.length] = popupMenu;
					newPath[path.length + 1] = sub[0];
					manager.setSelectedPath(newPath);
				}
				e.consume();
			}
		}

		/**
		 * Handles the mnemonics for the menu items. Will also handle duplicate mnemonics.
		 * Perhaps this should be moved into BasicPopupMenuUI. See 4670831
		 */
		public void menuKeyPressed(MenuKeyEvent e) {
			if (DEBUG) {
				System.out.println("in XPMenuUI.menuKeyPressed for " + menuItem.getText());
			}
			// Handle the case for Escape or Enter...
			char keyChar = e.getKeyChar();
			if (!Character.isLetterOrDigit(keyChar))
				return;

			MenuSelectionManager manager = e.getMenuSelectionManager();
			MenuElement path[] = e.getPath();
			MenuElement selectedPath[] = manager.getSelectedPath();

			for (int i = selectedPath.length - 1; i >= 0; i--) {
				if (selectedPath[i] == menuItem) {
					JPopupMenu popupMenu = ((JMenu) menuItem).getPopupMenu();
					MenuElement items[] = popupMenu.getSubElements();

					if (indexes == null || lastMnemonic != keyChar) {
						matches = 0;
						lastIndex = 0;
						indexes = new int[items.length];
						for (int j = 0; j < items.length; j++) {
							int key = ((JMenuItem) items[j]).getMnemonic();
							if (lower((char) key) == lower(keyChar)) {
								indexes[matches++] = j;
							}
						}
						lastMnemonic = keyChar;
					}
					if (matches == 0) {
						; // no op (consume)
					} else if (matches == 1) {
						// Invoke the menu action
						JMenuItem item = (JMenuItem) items[indexes[0]];
						if (!(item instanceof JMenu)) {
							// Let Submenus be handled by menuKeyTyped
							manager.clearSelectedPath();
							item.doClick();
						}
					} else {
						// Select the menu item with the matching mnemonic. If
						// the same mnemonic has been invoked then select the next
						// menu item in the cycle.
						if (lastIndex == matches) {
							// Take care of the situation in which the 
							// mnemonic wraps.
							lastIndex = 0;
						}
						MenuElement menuItem = items[indexes[lastIndex++]];

						MenuElement newPath[] = new MenuElement[path.length + 2];
						System.arraycopy(path, 0, newPath, 0, path.length);
						newPath[path.length] = popupMenu;
						newPath[path.length + 1] = menuItem;
						manager.setSelectedPath(newPath);
					}
					e.consume();
					return;
				}
			}
		}

		public void menuKeyReleased(MenuKeyEvent e) {
		}

		private char lower(char keyChar) {
			return Character.toLowerCase(keyChar);
		}
	}
	
	
}

⌨️ 快捷键说明

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