< prev index next >

## src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLabelUI.java

Print this page

      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 sun.swing.SwingUtilities2;
      29 import sun.swing.DefaultLookup;
      30 import sun.swing.UIAction;
      31 import sun.awt.AppContext;
      32
      33 import javax.swing.*;
      34 import javax.swing.plaf.*;
      35 import javax.swing.text.View;
      36
      37 import java.awt.event.ActionEvent;
      38 import java.awt.event.ActionListener;

      39 import java.awt.event.KeyEvent;
      40 import java.awt.Component;
      41 import java.awt.Container;
      42 import java.awt.Dimension;
      43 import java.awt.Rectangle;
      44 import java.awt.Insets;
      45 import java.awt.Color;
      46 import java.awt.Graphics;
      47 import java.awt.Font;
      48 import java.awt.FontMetrics;
      49 import java.beans.PropertyChangeEvent;
      50 import java.beans.PropertyChangeListener;
      51
      52 /**
      53  * A Windows L&amp;F implementation of LabelUI.  This implementation
      54  * is completely static, i.e. there's only one UIView implementation
      55  * that's shared by all JLabel objects.
      56  *
      57  * @author Hans Muller
      58  */
     385     /**
     386      * Registers keyboard actions.
     387      *
     388      * @param l an instance of {@code JLabel}
     389      */
     390     protected void installKeyboardActions(JLabel l) {
     391         int dka = l.getDisplayedMnemonic();
     392         Component lf = l.getLabelFor();
     393         if ((dka != 0) && (lf != null)) {
     394             LazyActionMap.installLazyActionMap(l, BasicLabelUI.class,
     395                                                "Label.actionMap");
     396             InputMap inputMap = SwingUtilities.getUIInputMap
     397                             (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
     398             if (inputMap == null) {
     399                 inputMap = new ComponentInputMapUIResource(l);
     400                 SwingUtilities.replaceUIInputMap(l,
     401                                 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
     402             }
     403             inputMap.clear();
     404             inputMap.put(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), false), "press");

     405         }
     406         else {
     407             InputMap inputMap = SwingUtilities.getUIInputMap
     408                             (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
     409             if (inputMap != null) {
     410                 inputMap.clear();
     411             }
     412         }
     413     }
     414
     415     /**
     416      * Uninstalls default properties.
     417      *
     418      * @param c an instance of {@code JLabel}
     419      */
     420     protected void uninstallDefaults(JLabel c){
     421     }
     422
     423     /**
     424      * Unregisters listeners.
     503             String key = getName();
     504             if (key == PRESS) {
     505                 doPress(label);
     506             }
     507             else if (key == RELEASE) {
     508                 doRelease(label, e.getActionCommand() != null);
     509             }
     510         }
     511
     512         private void doPress(JLabel label) {
     513             Component labelFor = label.getLabelFor();
     514             if (labelFor != null && labelFor.isEnabled()) {
     515                 InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
     516                 if (inputMap == null) {
     517                     inputMap = new InputMapUIResource();
     518                     SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap);
     519                 }
     520                 int dka = label.getDisplayedMnemonic();
     521                 putOnRelease(inputMap, dka, BasicLookAndFeel
     522                         .getFocusAcceleratorKeyMask());

     523                 // Need this when the sticky keys are enabled
     524                 putOnRelease(inputMap, dka, 0);
     525                 // Need this if ALT is released before the accelerator
     526                 putOnRelease(inputMap, KeyEvent.VK_ALT, 0);
     527                 label.requestFocus();
     528             }
     529         }
     530
     531         private void doRelease(JLabel label, boolean isCommand) {
     532             Component labelFor = label.getLabelFor();
     533             if (labelFor != null && labelFor.isEnabled()) {
     534                 if (label.hasFocus()) {
     535                     InputMap inputMap = SwingUtilities.getUIInputMap(label,
     536                             JComponent.WHEN_FOCUSED);
     537                     if (inputMap != null) {
     538                         // inputMap should never be null.
     539                         int dka = label.getDisplayedMnemonic();
     540                         removeOnRelease(inputMap, dka, BasicLookAndFeel
     541                                 .getFocusAcceleratorKeyMask());

     542                         removeOnRelease(inputMap, dka, 0);
     543                         removeOnRelease(inputMap, KeyEvent.VK_ALT, 0);
     544                     }
     545                     inputMap = SwingUtilities.getUIInputMap(label,
     546                             JComponent.WHEN_IN_FOCUSED_WINDOW);
     547                     if (inputMap == null) {
     548                         inputMap = new InputMapUIResource();
     549                         SwingUtilities.replaceUIInputMap(label,
     550                                 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
     551                     }
     552                     int dka = label.getDisplayedMnemonic();
     553                     if (isCommand) {
     554                         putOnRelease(inputMap, KeyEvent.VK_ALT, 0);
     555                     } else {
     556                         putOnRelease(inputMap, dka, BasicLookAndFeel
     557                                 .getFocusAcceleratorKeyMask());

     558                         // Need this when the sticky keys are enabled
     559                         putOnRelease(inputMap, dka, 0);
     560                     }
     561                     if (labelFor instanceof Container &&
     562                             ((Container) labelFor).isFocusCycleRoot()) {
     563                         labelFor.requestFocus();
     564                     } else {
     565                         SwingUtilities2.compositeRequestFocus(labelFor);
     566                     }
     567                 } else {
     568                     InputMap inputMap = SwingUtilities.getUIInputMap(label,
     569                             JComponent.WHEN_IN_FOCUSED_WINDOW);
     570                     int dka = label.getDisplayedMnemonic();
     571                     if (inputMap != null) {
     572                         if (isCommand) {
     573                             removeOnRelease(inputMap, dka, BasicLookAndFeel
     574                                     .getFocusAcceleratorKeyMask());

     575                             removeOnRelease(inputMap, dka, 0);
     576                         } else {
     577                             removeOnRelease(inputMap, KeyEvent.VK_ALT, 0);
     578                         }
     579                     }
     580                 }
     581             }
     582         }
     583
     584         private void putOnRelease(InputMap inputMap, int keyCode, int modifiers) {
     585             inputMap.put(KeyStroke.getKeyStroke(keyCode, modifiers, true),
     586                     RELEASE);
     587         }
     588
     589         private void removeOnRelease(InputMap inputMap, int keyCode, int modifiers) {
     590             inputMap.remove(KeyStroke.getKeyStroke(keyCode, modifiers, true));
     591         }
     592
     593     }
     594 }
      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 sun.swing.SwingUtilities2;
      29 import sun.swing.DefaultLookup;
      30 import sun.swing.UIAction;
      31 import sun.awt.AppContext;
      32
      33 import javax.swing.*;
      34 import javax.swing.plaf.*;
      35 import javax.swing.text.View;
      36
      37 import java.awt.event.ActionEvent;
      38 import java.awt.event.ActionListener;
      39 import java.awt.event.InputEvent;
      40 import java.awt.event.KeyEvent;
      41 import java.awt.Component;
      42 import java.awt.Container;
      43 import java.awt.Dimension;
      44 import java.awt.Rectangle;
      45 import java.awt.Insets;
      46 import java.awt.Color;
      47 import java.awt.Graphics;
      48 import java.awt.Font;
      49 import java.awt.FontMetrics;
      50 import java.beans.PropertyChangeEvent;
      51 import java.beans.PropertyChangeListener;
      52
      53 /**
      54  * A Windows L&amp;F implementation of LabelUI.  This implementation
      55  * is completely static, i.e. there's only one UIView implementation
      56  * that's shared by all JLabel objects.
      57  *
      58  * @author Hans Muller
      59  */
     386     /**
     387      * Registers keyboard actions.
     388      *
     389      * @param l an instance of {@code JLabel}
     390      */
     391     protected void installKeyboardActions(JLabel l) {
     392         int dka = l.getDisplayedMnemonic();
     393         Component lf = l.getLabelFor();
     394         if ((dka != 0) && (lf != null)) {
     395             LazyActionMap.installLazyActionMap(l, BasicLabelUI.class,
     396                                                "Label.actionMap");
     397             InputMap inputMap = SwingUtilities.getUIInputMap
     398                             (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
     399             if (inputMap == null) {
     400                 inputMap = new ComponentInputMapUIResource(l);
     401                 SwingUtilities.replaceUIInputMap(l,
     402                                 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
     403             }
     404             inputMap.clear();
     405             inputMap.put(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), false), "press");
     406             inputMap.put(KeyStroke.getKeyStroke(dka,
     407                     BasicLookAndFeel.getFocusAcceleratorKeyMask() |
     408                             InputEvent.ALT_GRAPH_DOWN_MASK, false), "press");
     409         }
     410         else {
     411             InputMap inputMap = SwingUtilities.getUIInputMap
     412                             (l, JComponent.WHEN_IN_FOCUSED_WINDOW);
     413             if (inputMap != null) {
     414                 inputMap.clear();
     415             }
     416         }
     417     }
     418
     419     /**
     420      * Uninstalls default properties.
     421      *
     422      * @param c an instance of {@code JLabel}
     423      */
     424     protected void uninstallDefaults(JLabel c){
     425     }
     426
     427     /**
     428      * Unregisters listeners.
     507             String key = getName();
     508             if (key == PRESS) {
     509                 doPress(label);
     510             }
     511             else if (key == RELEASE) {
     512                 doRelease(label, e.getActionCommand() != null);
     513             }
     514         }
     515
     516         private void doPress(JLabel label) {
     517             Component labelFor = label.getLabelFor();
     518             if (labelFor != null && labelFor.isEnabled()) {
     519                 InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
     520                 if (inputMap == null) {
     521                     inputMap = new InputMapUIResource();
     522                     SwingUtilities.replaceUIInputMap(label, JComponent.WHEN_FOCUSED, inputMap);
     523                 }
     524                 int dka = label.getDisplayedMnemonic();
     525                 putOnRelease(inputMap, dka, BasicLookAndFeel
     526                         .getFocusAcceleratorKeyMask());
     527                 putOnRelease(inputMap, dka, BasicLookAndFeel
     528                         .getFocusAcceleratorKeyMask() |
     529                         InputEvent.ALT_GRAPH_DOWN_MASK);
     530                 // Need this when the sticky keys are enabled
     531                 putOnRelease(inputMap, dka, 0);
     532                 // Need this if ALT is released before the accelerator
     533                 putOnRelease(inputMap, KeyEvent.VK_ALT, 0);
     534                 label.requestFocus();
     535             }
     536         }
     537
     538         private void doRelease(JLabel label, boolean isCommand) {
     539             Component labelFor = label.getLabelFor();
     540             if (labelFor != null && labelFor.isEnabled()) {
     541                 if (label.hasFocus()) {
     542                     InputMap inputMap = SwingUtilities.getUIInputMap(label,
     543                             JComponent.WHEN_FOCUSED);
     544                     if (inputMap != null) {
     545                         // inputMap should never be null.
     546                         int dka = label.getDisplayedMnemonic();
     547                         removeOnRelease(inputMap, dka, BasicLookAndFeel
     548                                 .getFocusAcceleratorKeyMask());
     549                         removeOnRelease(inputMap, dka, BasicLookAndFeel
     550                                 .getFocusAcceleratorKeyMask() |
     551                                 InputEvent.ALT_GRAPH_DOWN_MASK);
     552                         removeOnRelease(inputMap, dka, 0);
     553                         removeOnRelease(inputMap, KeyEvent.VK_ALT, 0);
     554                     }
     555                     inputMap = SwingUtilities.getUIInputMap(label,
     556                             JComponent.WHEN_IN_FOCUSED_WINDOW);
     557                     if (inputMap == null) {
     558                         inputMap = new InputMapUIResource();
     559                         SwingUtilities.replaceUIInputMap(label,
     560                                 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
     561                     }
     562                     int dka = label.getDisplayedMnemonic();
     563                     if (isCommand) {
     564                         putOnRelease(inputMap, KeyEvent.VK_ALT, 0);
     565                     } else {
     566                         putOnRelease(inputMap, dka, BasicLookAndFeel
     567                                 .getFocusAcceleratorKeyMask());
     568                         putOnRelease(inputMap, dka, BasicLookAndFeel
     569                                 .getFocusAcceleratorKeyMask() |
     570                                 InputEvent.ALT_GRAPH_DOWN_MASK);
     571                         // Need this when the sticky keys are enabled
     572                         putOnRelease(inputMap, dka, 0);
     573                     }
     574                     if (labelFor instanceof Container &&
     575                             ((Container) labelFor).isFocusCycleRoot()) {
     576                         labelFor.requestFocus();
     577                     } else {
     578                         SwingUtilities2.compositeRequestFocus(labelFor);
     579                     }
     580                 } else {
     581                     InputMap inputMap = SwingUtilities.getUIInputMap(label,
     582                             JComponent.WHEN_IN_FOCUSED_WINDOW);
     583                     int dka = label.getDisplayedMnemonic();
     584                     if (inputMap != null) {
     585                         if (isCommand) {
     586                             removeOnRelease(inputMap, dka, BasicLookAndFeel
     587                                     .getFocusAcceleratorKeyMask());
     588                             removeOnRelease(inputMap, dka, BasicLookAndFeel
     589                                     .getFocusAcceleratorKeyMask() |
     590                                     InputEvent.ALT_GRAPH_DOWN_MASK);
     591                             removeOnRelease(inputMap, dka, 0);
     592                         } else {
     593                             removeOnRelease(inputMap, KeyEvent.VK_ALT, 0);
     594                         }
     595                     }
     596                 }
     597             }
     598         }
     599
     600         private void putOnRelease(InputMap inputMap, int keyCode, int modifiers) {
     601             inputMap.put(KeyStroke.getKeyStroke(keyCode, modifiers, true),
     602                     RELEASE);
     603         }
     604
     605         private void removeOnRelease(InputMap inputMap, int keyCode, int modifiers) {
     606             inputMap.remove(KeyStroke.getKeyStroke(keyCode, modifiers, true));
     607         }
     608
     609     }
     610 }

< prev index next >