Save This Page
Home » openjdk-7 » javax » swing » plaf » synth » [javadoc | source]
    1   /*
    2    * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    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   package javax.swing.plaf.synth;
   26   
   27   import java.awt;
   28   import java.beans;
   29   import javax.swing;
   30   import javax.swing.plaf;
   31   import javax.swing.plaf.basic;
   32   import sun.swing.MenuItemLayoutHelper;
   33   
   34   /**
   35    * Provides the Synth L&F UI delegate for
   36    * {@link javax.swing.JMenu}.
   37    *
   38    * @author Georges Saab
   39    * @author David Karlton
   40    * @author Arnaud Weber
   41    * @since 1.7
   42    */
   43   public class SynthMenuUI extends BasicMenuUI
   44                            implements PropertyChangeListener, SynthUI {
   45       private SynthStyle style;
   46       private SynthStyle accStyle;
   47   
   48       /**
   49        * Creates a new UI object for the given component.
   50        *
   51        * @param x component to create UI object for
   52        * @return the UI object
   53        */
   54       public static ComponentUI createUI(JComponent x) {
   55           return new SynthMenuUI();
   56       }
   57   
   58       /**
   59        * @inheritDoc
   60        */
   61       @Override
   62       protected void installDefaults() {
   63           updateStyle(menuItem);
   64       }
   65   
   66       /**
   67        * @inheritDoc
   68        */
   69       @Override
   70       protected void installListeners() {
   71           super.installListeners();
   72           menuItem.addPropertyChangeListener(this);
   73       }
   74   
   75       private void updateStyle(JMenuItem mi) {
   76           SynthStyle oldStyle = style;
   77           SynthContext context = getContext(mi, ENABLED);
   78   
   79           style = SynthLookAndFeel.updateStyle(context, this);
   80           if (oldStyle != style) {
   81               String prefix = getPropertyPrefix();
   82               defaultTextIconGap = style.getInt(
   83                              context, prefix + ".textIconGap", 4);
   84               if (menuItem.getMargin() == null ||
   85                            (menuItem.getMargin() instanceof UIResource)) {
   86                   Insets insets = (Insets)style.get(context, prefix + ".margin");
   87   
   88                   if (insets == null) {
   89                       // Some places assume margins are non-null.
   90                       insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
   91                   }
   92                   menuItem.setMargin(insets);
   93               }
   94               acceleratorDelimiter = style.getString(context, prefix +
   95                                               ".acceleratorDelimiter", "+");
   96   
   97               if (MenuItemLayoutHelper.useCheckAndArrow(menuItem)) {
   98                   checkIcon = style.getIcon(context, prefix + ".checkIcon");
   99                   arrowIcon = style.getIcon(context, prefix + ".arrowIcon");
  100               } else {
  101                   // Not needed in this case
  102                   checkIcon = null;
  103                   arrowIcon = null;
  104               }
  105   
  106               ((JMenu)menuItem).setDelay(style.getInt(context, prefix +
  107                                                       ".delay", 200));
  108               if (oldStyle != null) {
  109                   uninstallKeyboardActions();
  110                   installKeyboardActions();
  111               }
  112           }
  113           context.dispose();
  114   
  115           SynthContext accContext = getContext(mi, Region.MENU_ITEM_ACCELERATOR,
  116                                                ENABLED);
  117   
  118           accStyle = SynthLookAndFeel.updateStyle(accContext, this);
  119           accContext.dispose();
  120       }
  121   
  122       /**
  123        * @inheritDoc
  124        */
  125       @Override
  126       public void uninstallUI(JComponent c) {
  127           super.uninstallUI(c);
  128           // Remove values from the parent's Client Properties.
  129           JComponent p = MenuItemLayoutHelper.getMenuItemParent((JMenuItem) c);
  130           if (p != null) {
  131               p.putClientProperty(
  132                       SynthMenuItemLayoutHelper.MAX_ACC_OR_ARROW_WIDTH, null);
  133           }
  134       }
  135   
  136       /**
  137        * @inheritDoc
  138        */
  139       @Override
  140       protected void uninstallDefaults() {
  141           SynthContext context = getContext(menuItem, ENABLED);
  142           style.uninstallDefaults(context);
  143           context.dispose();
  144           style = null;
  145   
  146           SynthContext accContext = getContext(menuItem,
  147                                        Region.MENU_ITEM_ACCELERATOR, ENABLED);
  148           accStyle.uninstallDefaults(accContext);
  149           accContext.dispose();
  150           accStyle = null;
  151   
  152           super.uninstallDefaults();
  153       }
  154   
  155       /**
  156        * @inheritDoc
  157        */
  158       @Override
  159       protected void uninstallListeners() {
  160           super.uninstallListeners();
  161           menuItem.removePropertyChangeListener(this);
  162       }
  163   
  164       /**
  165        * @inheritDoc
  166        */
  167       @Override
  168       public SynthContext getContext(JComponent c) {
  169           return getContext(c, getComponentState(c));
  170       }
  171   
  172       SynthContext getContext(JComponent c, int state) {
  173           Region region = SynthLookAndFeel.getRegion(c);
  174           return SynthContext.getContext(SynthContext.class, c, region,
  175                                          style, state);
  176       }
  177   
  178       SynthContext getContext(JComponent c, Region region) {
  179           return getContext(c, region, getComponentState(c, region));
  180       }
  181   
  182       private SynthContext getContext(JComponent c, Region region, int state) {
  183           return SynthContext.getContext(SynthContext.class, c,
  184                                          region, accStyle, state);
  185       }
  186   
  187       private int getComponentState(JComponent c) {
  188           int state;
  189   
  190           if (!c.isEnabled()) {
  191               return DISABLED;
  192           }
  193           if (menuItem.isArmed()) {
  194               state = MOUSE_OVER;
  195           }
  196           else {
  197               state = SynthLookAndFeel.getComponentState(c);
  198           }
  199           if (menuItem.isSelected()) {
  200               state |= SELECTED;
  201           }
  202           return state;
  203       }
  204   
  205       private int getComponentState(JComponent c, Region region) {
  206           return getComponentState(c);
  207       }
  208   
  209       /**
  210        * @inheritDoc
  211        */
  212       @Override
  213       protected Dimension getPreferredMenuItemSize(JComponent c,
  214                                                        Icon checkIcon,
  215                                                        Icon arrowIcon,
  216                                                        int defaultTextIconGap) {
  217           SynthContext context = getContext(c);
  218           SynthContext accContext = getContext(c, Region.MENU_ITEM_ACCELERATOR);
  219           Dimension value = SynthGraphicsUtils.getPreferredMenuItemSize(
  220                   context, accContext, c, checkIcon, arrowIcon,
  221                   defaultTextIconGap, acceleratorDelimiter,
  222                   MenuItemLayoutHelper.useCheckAndArrow(menuItem),
  223                   getPropertyPrefix());
  224           context.dispose();
  225           accContext.dispose();
  226           return value;
  227       }
  228   
  229       /**
  230        * Notifies this UI delegate to repaint the specified component.
  231        * This method paints the component background, then calls
  232        * the {@link #paint(SynthContext,Graphics)} method.
  233        *
  234        * <p>In general, this method does not need to be overridden by subclasses.
  235        * All Look and Feel rendering code should reside in the {@code paint} method.
  236        *
  237        * @param g the {@code Graphics} object used for painting
  238        * @param c the component being painted
  239        * @see #paint(SynthContext,Graphics)
  240        */
  241       @Override
  242       public void update(Graphics g, JComponent c) {
  243           SynthContext context = getContext(c);
  244   
  245           SynthLookAndFeel.update(context, g);
  246           context.getPainter().paintMenuBackground(context,
  247                             g, 0, 0, c.getWidth(), c.getHeight());
  248           paint(context, g);
  249           context.dispose();
  250       }
  251   
  252       /**
  253        * Paints the specified component according to the Look and Feel.
  254        * <p>This method is not used by Synth Look and Feel.
  255        * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
  256        *
  257        * @param g the {@code Graphics} object used for painting
  258        * @param c the component being painted
  259        * @see #paint(SynthContext,Graphics)
  260        */
  261       @Override
  262       public void paint(Graphics g, JComponent c) {
  263           SynthContext context = getContext(c);
  264   
  265           paint(context, g);
  266           context.dispose();
  267       }
  268   
  269       /**
  270        * Paints the specified component. This implementation does nothing.
  271        *
  272        * @param context context for the component being painted
  273        * @param g the {@code Graphics} object used for painting
  274        * @see #update(Graphics,JComponent)
  275        */
  276       protected void paint(SynthContext context, Graphics g) {
  277           SynthContext accContext = getContext(menuItem,
  278                                                Region.MENU_ITEM_ACCELERATOR);
  279           // Refetch the appropriate check indicator for the current state
  280           String prefix = getPropertyPrefix();
  281           Icon checkIcon = style.getIcon(context, prefix + ".checkIcon");
  282           Icon arrowIcon = style.getIcon(context, prefix + ".arrowIcon");
  283           SynthGraphicsUtils.paint(context, accContext, g, checkIcon, arrowIcon,
  284                 acceleratorDelimiter, defaultTextIconGap, getPropertyPrefix());
  285           accContext.dispose();
  286       }
  287   
  288       /**
  289        * @inheritDoc
  290        */
  291       @Override
  292       public void paintBorder(SynthContext context, Graphics g, int x,
  293                               int y, int w, int h) {
  294           context.getPainter().paintMenuBorder(context, g, x, y, w, h);
  295       }
  296   
  297       /**
  298        * @inheritDoc
  299        */
  300       @Override
  301       public void propertyChange(PropertyChangeEvent e) {
  302           if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  303               updateStyle((JMenu)e.getSource());
  304           }
  305       }
  306   }

Save This Page
Home » openjdk-7 » javax » swing » plaf » synth » [javadoc | source]