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

📄 basicmenuitemui.java

📁 java jdk 1.4的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        Rectangle checkIconRect,         Rectangle arrowIconRect,         int textIconGap,        int menuItemGap        )    {        SwingUtilities.layoutCompoundLabel(                            menuItem, fm, text, icon, verticalAlignment,                             horizontalAlignment, verticalTextPosition,                             horizontalTextPosition, viewRect, iconRect, textRect,                             textIconGap);        /* Initialize the acceelratorText bounds rectangle textRect.  If a null          * or and empty String was specified we substitute "" here          * and use 0,0,0,0 for acceleratorTextRect.         */        if( (acceleratorText == null) || acceleratorText.equals("") ) {            acceleratorRect.width = acceleratorRect.height = 0;            acceleratorText = "";        }        else {            acceleratorRect.width = SwingUtilities.computeStringWidth( fmAccel, acceleratorText );            acceleratorRect.height = fmAccel.getHeight();        }        /* Initialize the checkIcon bounds rectangle's width & height.         */	if( useCheckAndArrow()) {	    if (checkIcon != null) {		checkIconRect.width = checkIcon.getIconWidth();		checkIconRect.height = checkIcon.getIconHeight();	    } 	    else {		checkIconRect.width = checkIconRect.height = 0;	    }	    	    /* 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);        if( BasicGraphicsUtils.isLeftToRight(menuItem) ) {            textRect.x += menuItemGap;            iconRect.x += menuItemGap;            // Position the Accelerator text rect            acceleratorRect.x = viewRect.x + viewRect.width - arrowIconRect.width                              - menuItemGap - acceleratorRect.width;                        // Position the Check and Arrow Icons             if (useCheckAndArrow()) {                checkIconRect.x = viewRect.x + menuItemGap;                textRect.x += menuItemGap + checkIconRect.width;                iconRect.x += menuItemGap + checkIconRect.width;                arrowIconRect.x = viewRect.x + viewRect.width - menuItemGap                                  - arrowIconRect.width;            }        } else {            textRect.x -= menuItemGap;            iconRect.x -= menuItemGap;            // Position the Accelerator text rect            acceleratorRect.x = viewRect.x + arrowIconRect.width + menuItemGap;            // Position the Check and Arrow Icons             if (useCheckAndArrow()) {                checkIconRect.x = viewRect.x + viewRect.width - menuItemGap                                  - checkIconRect.width;                textRect.x -= menuItemGap + checkIconRect.width;                iconRect.x -= menuItemGap + checkIconRect.width;                      arrowIconRect.x = viewRect.x + menuItemGap;            }        }        // Align the accelertor text and the check and arrow icons vertically        // with the center of the label rect.          acceleratorRect.y = labelRect.y + (labelRect.height/2) - (acceleratorRect.height/2);        if( useCheckAndArrow() ) {            arrowIconRect.y = labelRect.y + (labelRect.height/2) - (arrowIconRect.height/2);            checkIconRect.y = labelRect.y + (labelRect.height/2) - (checkIconRect.height/2);        }        /*        System.out.println("Layout: text="+menuItem.getText()+"\n\tv="                           +viewRect+"\n\tc="+checkIconRect+"\n\ti="                           +iconRect+"\n\tt="+textRect+"\n\tacc="                           +acceleratorRect+"\n\ta="+arrowIconRect+"\n");        */                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;    }    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 printMenuElementArray(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) {            MenuSelectionManager manager = e.getMenuSelectionManager();            MenuElement path[] = e.getPath();            manager.setSelectedPath(path);        }        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 {		/**	 * Handles the mnemonic key typed in the MenuItem if this menuItem is in	 * a standalone popup menu. This invocation normally 	 * handled in BasicMenuUI.MenuKeyHandler.menuKeyPressed. Ideally, the 	 * MenuKeyHandlers for both BasicMenuItemUI and BasicMenuUI can be consolidated	 * into BasicPopupMenuUI but that would require an semantic change. This	 * would result in a performance win since we can shortcut a lot of the needless	 * processing from MenuSelectionManager.processKeyEvent(). See 4670831.	 */        public void menuKeyTyped(MenuKeyEvent e) {	    if (DEBUG) {		System.out.println("in BasicMenuItemUI.menuKeyTyped for " + menuItem.getText());	    }            int key = menuItem.getMnemonic();            if(key == 0 || e.getPath().length != 2) // Hack! Only proceed if in a JPopupMenu                return;            if(lower((char)key) == lower(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 char lower(char keyChar) {	    return Character.toLowerCase(keyChar);        }    }    private class PropertyChangeHandler implements PropertyChangeListener {	public void propertyChange(PropertyChangeEvent e) {	    String name = e.getPropertyName();	    if (name.equals("labelFor") || name.equals("displayedMnemonic") ||		name.equals("accelerator")) {		updateAcceleratorBinding();	    } 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;	}     }}

⌨️ 快捷键说明

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