8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.swing.plaf.basic;
27
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.beans.PropertyChangeEvent;
31 import java.beans.PropertyChangeListener;
32
33 import javax.swing.*;
34 import javax.swing.event.*;
35 import javax.swing.plaf.*;
36 import javax.swing.text.View;
37
38 import sun.swing.*;
39
40 /**
41 * BasicMenuItem implementation
42 *
43 * @author Georges Saab
44 * @author David Karlton
45 * @author Arnaud Weber
46 * @author Fredrik Lagerblad
47 */
131 private static final boolean VERBOSE = false; // show reuse hits/misses
132 private static final boolean DEBUG = false; // show bad params, misc.
133
134 static void loadActionMap(LazyActionMap map) {
135 // NOTE: BasicMenuUI also calls into this method.
136 map.put(new Actions(Actions.CLICK));
137 BasicLookAndFeel.installAudioActionMap(map);
138 }
139
140 /**
141 * Returns a new instance of {@code BasicMenuItemUI}.
142 *
143 * @param c a component
144 * @return a new instance of {@code BasicMenuItemUI}
145 */
146 public static ComponentUI createUI(JComponent c) {
147 return new BasicMenuItemUI();
148 }
149
150 public void installUI(JComponent c) {
151 menuItem = (JMenuItem) c;
152
153 installDefaults();
154 installComponents(menuItem);
155 installListeners();
156 installKeyboardActions();
157 }
158
159 /**
160 * Installs default properties.
161 */
162 protected void installDefaults() {
163 String prefix = getPropertyPrefix();
164
165 acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
166 // use default if missing so that BasicMenuItemUI can be used in other
167 // LAFs like Nimbus
168 if (acceleratorFont == null) {
169 acceleratorFont = UIManager.getFont("MenuItem.font");
170 }
657 } else {
658 g.setColor(holdc);
659 }
660 if (lh.useCheckAndArrow()) {
661 lh.getCheckIcon().paintIcon(lh.getMenuItem(), g,
662 lr.getCheckRect().x, lr.getCheckRect().y);
663 }
664 g.setColor(holdc);
665 }
666 }
667
668 private void paintAccText(Graphics g, MenuItemLayoutHelper lh,
669 MenuItemLayoutHelper.LayoutResult lr) {
670 if (!lh.getAccText().equals("")) {
671 ButtonModel model = lh.getMenuItem().getModel();
672 g.setFont(lh.getAccFontMetrics().getFont());
673 if (!model.isEnabled()) {
674 // *** paint the accText disabled
675 if (disabledForeground != null) {
676 g.setColor(disabledForeground);
677 SwingUtilities2.drawString(lh.getMenuItem(), g,
678 lh.getAccText(), lr.getAccRect().x,
679 lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
680 } else {
681 g.setColor(lh.getMenuItem().getBackground().brighter());
682 SwingUtilities2.drawString(lh.getMenuItem(), g,
683 lh.getAccText(), lr.getAccRect().x,
684 lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
685 g.setColor(lh.getMenuItem().getBackground().darker());
686 SwingUtilities2.drawString(lh.getMenuItem(), g,
687 lh.getAccText(), lr.getAccRect().x - 1,
688 lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
689 }
690 } else {
691 // *** paint the accText normally
692 if (model.isArmed()
693 || (lh.getMenuItem() instanceof JMenu
694 && model.isSelected())) {
695 g.setColor(acceleratorSelectionForeground);
696 } else {
697 g.setColor(acceleratorForeground);
698 }
699 SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(),
700 lr.getAccRect().x, lr.getAccRect().y +
701 lh.getAccFontMetrics().getAscent());
702 }
703 }
704 }
705
706 private void paintText(Graphics g, MenuItemLayoutHelper lh,
707 MenuItemLayoutHelper.LayoutResult lr) {
708 if (!lh.getText().equals("")) {
709 if (lh.getHtmlView() != null) {
710 // Text is HTML
711 lh.getHtmlView().paint(g, lr.getTextRect());
712 } else {
713 // Text isn't HTML
714 paintText(g, lh.getMenuItem(), lr.getTextRect(), lh.getText());
715 }
716 }
717 }
718
719 private void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh,
774 }
775
776 /**
777 * Renders the text of the current menu item.
778 *
779 * @param g graphics context
780 * @param menuItem menu item to render
781 * @param textRect bounding rectangle for rendering the text
782 * @param text string to render
783 * @since 1.4
784 */
785 protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) {
786 ButtonModel model = menuItem.getModel();
787 FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g);
788 int mnemIndex = menuItem.getDisplayedMnemonicIndex();
789
790 if(!model.isEnabled()) {
791 // *** paint the text disabled
792 if ( UIManager.get("MenuItem.disabledForeground") instanceof Color ) {
793 g.setColor( UIManager.getColor("MenuItem.disabledForeground") );
794 SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
795 mnemIndex, textRect.x, textRect.y + fm.getAscent());
796 } else {
797 g.setColor(menuItem.getBackground().brighter());
798 SwingUtilities2.drawStringUnderlineCharAt(menuItem, g, text,
799 mnemIndex, textRect.x, textRect.y + fm.getAscent());
800 g.setColor(menuItem.getBackground().darker());
801 SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
802 mnemIndex, textRect.x - 1, textRect.y +
803 fm.getAscent() - 1);
804 }
805 } else {
806 // *** paint the text normally
807 if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) {
808 g.setColor(selectionForeground); // Uses protected field.
809 }
810 SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text,
811 mnemIndex, textRect.x, textRect.y + fm.getAscent());
812 }
813 }
814
815 /**
816 * Returns a menu element path.
817 *
818 * @return a menu element path
819 */
820 public MenuElement[] getPath() {
821 MenuSelectionManager m = MenuSelectionManager.defaultManager();
822 MenuElement oldPath[] = m.getSelectedPath();
823 MenuElement newPath[];
824 int i = oldPath.length;
825 if (i == 0)
826 return new MenuElement[0];
827 Component parent = menuItem.getParent();
828 if (oldPath[i-1].getComponent() == parent) {
829 // The parent popup menu is the last so far
830 newPath = new MenuElement[i+1];
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.swing.plaf.basic;
27
28 import javax.swing.plaf.TextUIDrawing;
29 import java.awt.*;
30 import java.awt.event.*;
31 import java.beans.PropertyChangeEvent;
32 import java.beans.PropertyChangeListener;
33
34 import javax.swing.*;
35 import javax.swing.event.*;
36 import javax.swing.plaf.*;
37 import javax.swing.text.View;
38
39 import sun.swing.*;
40
41 /**
42 * BasicMenuItem implementation
43 *
44 * @author Georges Saab
45 * @author David Karlton
46 * @author Arnaud Weber
47 * @author Fredrik Lagerblad
48 */
132 private static final boolean VERBOSE = false; // show reuse hits/misses
133 private static final boolean DEBUG = false; // show bad params, misc.
134
135 static void loadActionMap(LazyActionMap map) {
136 // NOTE: BasicMenuUI also calls into this method.
137 map.put(new Actions(Actions.CLICK));
138 BasicLookAndFeel.installAudioActionMap(map);
139 }
140
141 /**
142 * Returns a new instance of {@code BasicMenuItemUI}.
143 *
144 * @param c a component
145 * @return a new instance of {@code BasicMenuItemUI}
146 */
147 public static ComponentUI createUI(JComponent c) {
148 return new BasicMenuItemUI();
149 }
150
151 public void installUI(JComponent c) {
152 super.installUI(c);
153 menuItem = (JMenuItem) c;
154
155 installDefaults();
156 installComponents(menuItem);
157 installListeners();
158 installKeyboardActions();
159 }
160
161 /**
162 * Installs default properties.
163 */
164 protected void installDefaults() {
165 String prefix = getPropertyPrefix();
166
167 acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
168 // use default if missing so that BasicMenuItemUI can be used in other
169 // LAFs like Nimbus
170 if (acceleratorFont == null) {
171 acceleratorFont = UIManager.getFont("MenuItem.font");
172 }
659 } else {
660 g.setColor(holdc);
661 }
662 if (lh.useCheckAndArrow()) {
663 lh.getCheckIcon().paintIcon(lh.getMenuItem(), g,
664 lr.getCheckRect().x, lr.getCheckRect().y);
665 }
666 g.setColor(holdc);
667 }
668 }
669
670 private void paintAccText(Graphics g, MenuItemLayoutHelper lh,
671 MenuItemLayoutHelper.LayoutResult lr) {
672 if (!lh.getAccText().equals("")) {
673 ButtonModel model = lh.getMenuItem().getModel();
674 g.setFont(lh.getAccFontMetrics().getFont());
675 if (!model.isEnabled()) {
676 // *** paint the accText disabled
677 if (disabledForeground != null) {
678 g.setColor(disabledForeground);
679 getTextUIDrawing().drawString(lh.getMenuItem(), g,
680 lh.getAccText(), lr.getAccRect().x,
681 lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
682 } else {
683 g.setColor(lh.getMenuItem().getBackground().brighter());
684 getTextUIDrawing().drawString(lh.getMenuItem(), g,
685 lh.getAccText(), lr.getAccRect().x,
686 lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
687 g.setColor(lh.getMenuItem().getBackground().darker());
688 getTextUIDrawing().drawString(lh.getMenuItem(), g,
689 lh.getAccText(), lr.getAccRect().x - 1,
690 lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
691 }
692 } else {
693 // *** paint the accText normally
694 if (model.isArmed()
695 || (lh.getMenuItem() instanceof JMenu
696 && model.isSelected())) {
697 g.setColor(acceleratorSelectionForeground);
698 } else {
699 g.setColor(acceleratorForeground);
700 }
701 getTextUIDrawing().drawString(lh.getMenuItem(), g, lh.getAccText(),
702 lr.getAccRect().x, lr.getAccRect().y +
703 lh.getAccFontMetrics().getAscent());
704 }
705 }
706 }
707
708 private void paintText(Graphics g, MenuItemLayoutHelper lh,
709 MenuItemLayoutHelper.LayoutResult lr) {
710 if (!lh.getText().equals("")) {
711 if (lh.getHtmlView() != null) {
712 // Text is HTML
713 lh.getHtmlView().paint(g, lr.getTextRect());
714 } else {
715 // Text isn't HTML
716 paintText(g, lh.getMenuItem(), lr.getTextRect(), lh.getText());
717 }
718 }
719 }
720
721 private void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh,
776 }
777
778 /**
779 * Renders the text of the current menu item.
780 *
781 * @param g graphics context
782 * @param menuItem menu item to render
783 * @param textRect bounding rectangle for rendering the text
784 * @param text string to render
785 * @since 1.4
786 */
787 protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) {
788 ButtonModel model = menuItem.getModel();
789 FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g);
790 int mnemIndex = menuItem.getDisplayedMnemonicIndex();
791
792 if(!model.isEnabled()) {
793 // *** paint the text disabled
794 if ( UIManager.get("MenuItem.disabledForeground") instanceof Color ) {
795 g.setColor( UIManager.getColor("MenuItem.disabledForeground") );
796 getTextUIDrawing().drawStringUnderlineCharAt(menuItem, g,text,
797 mnemIndex, textRect.x, textRect.y + fm.getAscent());
798 } else {
799 g.setColor(menuItem.getBackground().brighter());
800 getTextUIDrawing().drawStringUnderlineCharAt(menuItem, g, text,
801 mnemIndex, textRect.x, textRect.y + fm.getAscent());
802 g.setColor(menuItem.getBackground().darker());
803 getTextUIDrawing().drawStringUnderlineCharAt(menuItem, g,text,
804 mnemIndex, textRect.x - 1, textRect.y +
805 fm.getAscent() - 1);
806 }
807 } else {
808 // *** paint the text normally
809 if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) {
810 g.setColor(selectionForeground); // Uses protected field.
811 }
812 getTextUIDrawing().drawStringUnderlineCharAt(menuItem, g,text,
813 mnemIndex, textRect.x, textRect.y + fm.getAscent());
814 }
815 }
816
817 /**
818 * Returns a menu element path.
819 *
820 * @return a menu element path
821 */
822 public MenuElement[] getPath() {
823 MenuSelectionManager m = MenuSelectionManager.defaultManager();
824 MenuElement oldPath[] = m.getSelectedPath();
825 MenuElement newPath[];
826 int i = oldPath.length;
827 if (i == 0)
828 return new MenuElement[0];
829 Component parent = menuItem.getParent();
830 if (oldPath[i-1].getComponent() == parent) {
831 // The parent popup menu is the last so far
832 newPath = new MenuElement[i+1];
|