Source for javax.swing.plaf.multi.MultiLookAndFeel

   1: /* MultiLookAndFeel.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.swing.plaf.multi;
  39: 
  40: import java.util.Vector;
  41: 
  42: import javax.swing.JComponent;
  43: import javax.swing.LookAndFeel;
  44: import javax.swing.UIDefaults;
  45: import javax.swing.UIManager;
  46: import javax.swing.plaf.ComponentUI;
  47: 
  48: /**
  49:  * A look and feel that provides the ability to use auxiliary look and feels
  50:  * in addition to the primary look and feel.
  51:  */
  52: public class MultiLookAndFeel extends LookAndFeel {
  53: 
  54:   /**
  55:    * Creates a new instance of the look and feel.
  56:    */
  57:   public MultiLookAndFeel()
  58:   {
  59:   }
  60:   
  61:   /**
  62:    * Returns the name for the look and feel.
  63:    * 
  64:    * @return "Multiplexing Look and Feel".
  65:    */
  66:   public String getName()
  67:   {
  68:     return "Multiplexing Look and Feel";
  69:   }
  70:   
  71:   /**
  72:    * Returns an identifier for the look and feel.
  73:    * 
  74:    * @return "Multiplex".
  75:    */
  76:   public String getID()
  77:   {
  78:     return "Multiplex"; 
  79:   }
  80:   
  81:   /**
  82:    * Returns a description of the look and feel.
  83:    * 
  84:    * @return A description of the look and feel.
  85:    */
  86:   public String getDescription()
  87:   {
  88:     return "Allows multiple UI instances per component instance";    
  89:   }
  90:   
  91:   /**
  92:    * Returns <code>false</code> to indicate that this look and feel is not 
  93:    * native to any platform.
  94:    * 
  95:    * @return <code>false</code>.
  96:    */
  97:   public boolean isNativeLookAndFeel()
  98:   {
  99:     return false;    
 100:   }
 101: 
 102:   /**
 103:    * Returns <code>true</code> always, since this look and feel is supported on
 104:    * all platforms.
 105:    * 
 106:    * @return <code>true</code>.
 107:    */
 108:   public boolean isSupportedLookAndFeel()
 109:   {
 110:     return true;
 111:   }
 112:   
 113:   /**
 114:    * Creates and returns the UI defaults for this look and feel.
 115:    * 
 116:    * @return The UI defaults.
 117:    */
 118:   public UIDefaults getDefaults()
 119:   {
 120:     UIDefaults defaults = new UIDefaults();
 121:     defaults.put("ButtonUI", "javax.swing.plaf.multi.MultiButtonUI");
 122:     defaults.put("CheckBoxUI", "javax.swing.plaf.multi.MultiButtonUI");
 123:     defaults.put("CheckBoxMenuItemUI", "javax.swing.plaf.multi.MultiMenuItemUI");
 124:     defaults.put("ColorChooserUI", 
 125:         "javax.swing.plaf.multi.MultiColorChooserUI");
 126:     defaults.put("ComboBoxUI", "javax.swing.plaf.multi.MultiComboBoxUI");
 127:     defaults.put("DesktopPaneUI", "javax.swing.plaf.multi.MultiDesktopPaneUI");
 128:     defaults.put("DesktopIconUI", "javax.swing.plaf.multi.MultiDesktopIconUI");
 129:     defaults.put("EditorPaneUI", "javax.swing.plaf.multi.MultiTextUI");
 130:     defaults.put("FileChooserUI", "javax.swing.plaf.multi.MultiFileChooserUI");
 131:     defaults.put("FormattedTextFieldUI", "javax.swing.plaf.multi.MultiTextUI");
 132:     defaults.put("InternalFrameUI", 
 133:         "javax.swing.plaf.multi.MultiInternalFrameUI");
 134:     defaults.put("LabelUI", "javax.swing.plaf.multi.MultiLabelUI");
 135:     defaults.put("ListUI", "javax.swing.plaf.multi.MultiListUI");
 136:     defaults.put("MenuItemUI", "javax.swing.plaf.multi.MultiMenuItemUI");
 137:     defaults.put("MenuUI", "javax.swing.plaf.multi.MultiMenuItemUI");
 138:     defaults.put("MenuBarUI", "javax.swing.plaf.multi.MultiMenuBarUI");
 139:     defaults.put("OptionPaneUI", "javax.swing.plaf.multi.MultiOptionPaneUI");
 140:     defaults.put("PanelUI", "javax.swing.plaf.multi.MultiPanelUI");
 141:     defaults.put("PasswordFieldUI", "javax.swing.plaf.multi.MultiTextUI");
 142:     defaults.put("PopupMenuUI", "javax.swing.plaf.multi.MultiPopupMenuUI");
 143:     defaults.put("PopupMenuSeparatorUI", 
 144:         "javax.swing.plaf.multi.MultiSeparatorUI");
 145:     defaults.put("ProgressBarUI", "javax.swing.plaf.multi.MultiProgressBarUI");
 146:     defaults.put("RadioButtonUI", "javax.swing.plaf.multi.MultiButtonUI");
 147:     defaults.put("RadioButtonMenuItemUI", 
 148:         "javax.swing.plaf.multi.MultiMenuItemUI");
 149:     defaults.put("RootPaneUI", "javax.swing.plaf.multi.MultiRootPaneUI");
 150:     defaults.put("ScrollBarUI", "javax.swing.plaf.multi.MultiScrollBarUI");
 151:     defaults.put("ScrollPaneUI", "javax.swing.plaf.multi.MultiScrollPaneUI");
 152:     defaults.put("SeparatorUI", "javax.swing.plaf.multi.MultiSeparatorUI");
 153:     defaults.put("SliderUI", "javax.swing.plaf.multi.MultiSliderUI");
 154:     defaults.put("SpinnerUI", "javax.swing.plaf.multi.MultiSpinnerUI");
 155:     defaults.put("SplitPaneUI", "javax.swing.plaf.multi.MultiSplitPaneUI");
 156:     defaults.put("TabbedPaneUI", "javax.swing.plaf.multi.MultiTabbedPaneUI");
 157:     defaults.put("TableHeaderUI", "javax.swing.plaf.multi.MultiTableHeaderUI");
 158:     defaults.put("TableUI", "javax.swing.plaf.multi.MultiTableUI");
 159:     defaults.put("TextAreaUI", "javax.swing.plaf.multi.MultiTextUI");
 160:     defaults.put("TextFieldUI", "javax.swing.plaf.multi.MultiTextUI");
 161:     defaults.put("TextPaneUI", "javax.swing.plaf.multi.MultiTextUI");
 162:     defaults.put("ToggleButtonUI", "javax.swing.plaf.multi.MultiButtonUI");
 163:     defaults.put("ToolBarSeparatorUI", 
 164:         "javax.swing.plaf.multi.MultiSeparatorUI");
 165:     defaults.put("ToolBarUI", "javax.swing.plaf.multi.MultiToolBarUI");
 166:     defaults.put("ToolTipUI", "javax.swing.plaf.multi.MultiToolTipUI");
 167:     defaults.put("ViewportUI", "javax.swing.plaf.multi.MultiViewportUI");
 168:     return defaults;
 169:   }
 170:   
 171:   /**
 172:    * Creates the UI delegates for the <code>target</code> component and
 173:    * returns a multiplexing UI delegate (<code>mui</code>) if there are
 174:    * multiple delegates.
 175:    * 
 176:    * @param mui  a multiplexing UI delegate appropriate for the component.
 177:    * @param uis  a vector into which the UI delegates will be added.
 178:    * @param target  the target component.
 179:    * 
 180:    * @return A UI delegate.
 181:    */
 182:   public static ComponentUI createUIs(ComponentUI mui, Vector uis, 
 183:                                       JComponent target)
 184:   {
 185:     // get primary UI delegate for 'target', and add it to uis
 186:     ComponentUI ui = null;
 187:     LookAndFeel primary = UIManager.getLookAndFeel();
 188:     if (primary != null) 
 189:     {
 190:       ui = UIManager.getUI(target);
 191:       uis.add(ui);
 192:     }
 193:     // for any auxiliary look and feels in use, get the UI delegate and add 
 194:     // it to uis
 195:     LookAndFeel[] auxlafs = UIManager.getAuxiliaryLookAndFeels();
 196:     for (int i = 0; i < auxlafs.length; i++)
 197:     {
 198:       LookAndFeel auxlaf = auxlafs[i];
 199:       // FIXME: here I call getDefaults() to get the UI delegate from the 
 200:       // auxiliary look and feel.  But getDefaults() creates a new set of
 201:       // defaults every time it is called, which is wasteful.  Unfortunately
 202:       // I cannot find another way to get the UI delegate, so I'm doing it
 203:       // anyway...
 204:       UIDefaults defaults = auxlaf.getDefaults();
 205:       ui = defaults.getUI(target);
 206:       if (ui != null)
 207:         uis.add(ui);
 208:     }
 209:     // if uis contains more than 1 delegate, return mui, otherwise return 
 210:     // the primary delegate
 211:     if (uis.size() > 1)
 212:       return mui;
 213:     else
 214:       return ui;    
 215:   }
 216:   
 217:   /**
 218:    * Returns an array containing the same {@link ComponentUI} instances as
 219:    * <code>uis</code>.  If <code>uis</code> is <code>null</code>, a zero-length
 220:    * array is returned.
 221:    * 
 222:    * @param uis  a list of {@link ComponentUI} references (<code>null</code> 
 223:    *             permitted).
 224:    * 
 225:    * @return An array containing the same {@link ComponentUI} instances as
 226:    *         <code>uis</code>, or <code>null</code> if <code>uis</code> is
 227:    *         empty.  
 228:    */
 229:   protected static ComponentUI[] uisToArray(Vector uis)
 230:   {
 231:     if (uis == null) 
 232:       return new ComponentUI[0];
 233:     int size = uis.size();
 234:     if (size == 0) 
 235:       return null;
 236:     ComponentUI[] result = new ComponentUI[size];
 237:     uis.copyInto(result);
 238:     return result;    
 239:   }
 240: 
 241: }