?? basicmenuitemui.java
字號:
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 = SwingUtilities2.stringWidth( menuItem, 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); int checkIconOffset = menuItemGap; Object checkIconOffsetObject = UIManager.get(getPropertyPrefix() + ".checkIconOffset"); if (checkIconOffsetObject instanceof Integer) { checkIconOffset = (Integer) checkIconOffsetObject; } if( BasicGraphicsUtils.isLeftToRight(menuItem) ) { /* get minimum text offset. It is defined for LTR case only. */ int minimumTextOffset = 0; Object minimumTextOffsetObject = UIManager.get(getPropertyPrefix() + ".minimumTextOffset"); if (minimumTextOffsetObject instanceof Integer) { minimumTextOffset = (Integer) minimumTextOffsetObject; } 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 + checkIconOffset; textRect.x += checkIconOffset + checkIconRect.width; textRect.x = Math.max(textRect.x, minimumTextOffset); iconRect.x += checkIconOffset + 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 { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void mouseClicked(MouseEvent e) { getHandler().mouseClicked(e); } public void mousePressed(MouseEvent e) { getHandler().mousePressed(e); } public void mouseReleased(MouseEvent e) { getHandler().mouseReleased(e); } public void mouseEntered(MouseEvent e) { getHandler().mouseEntered(e); } public void mouseExited(MouseEvent e) { getHandler().mouseExited(e); } public void mouseDragged(MouseEvent e) { getHandler().mouseDragged(e); } public void mouseMoved(MouseEvent e) { getHandler().mouseMoved(e); } } private static class Actions extends UIAction { private static final String CLICK = "doClick"; Actions(String key) { super(key); } 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()) { BasicLookAndFeel.playSound(menuItem, getPropertyPrefix() + ".commandSound"); } // 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; } } // BasicMenuUI subclasses this. class Handler implements MenuDragMouseListener, MouseInputListener, PropertyChangeListener { // // 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 && path[path.length-1] == menuItem) { 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) { } // // MenuDragListener // 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(); } } // // PropertyChangeListener // public void propertyChange(PropertyChangeEvent e) { String name = e.getPropertyName(); if (name == "labelFor" || name == "displayedMnemonic" || name == "accelerator") { updateAcceleratorBinding(); } else if (name == "text" || "font" == name || "foreground" == 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); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -