Source for javax.swing.UIManager

   1: /* UIManager.java -- 
   2:    Copyright (C) 2002, 2003, 2004, 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: 
  39: package javax.swing;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Dimension;
  43: import java.awt.Font;
  44: import java.awt.Insets;
  45: import java.beans.PropertyChangeListener;
  46: import java.io.Serializable;
  47: import java.util.Locale;
  48: 
  49: import javax.swing.border.Border;
  50: import javax.swing.plaf.ComponentUI;
  51: import javax.swing.plaf.metal.MetalLookAndFeel;
  52: 
  53: public class UIManager implements Serializable
  54: {
  55:   public static class LookAndFeelInfo
  56:   {
  57:     String name, clazz;
  58:     
  59:     public LookAndFeelInfo(String name, 
  60:                String clazz)
  61:     {
  62:       this.name  = name;
  63:       this.clazz = clazz;
  64:     }
  65: 
  66:     public String getName()
  67:     {
  68:       return name;
  69:     }
  70:     
  71:     public String getClassName()
  72:     {
  73:       return clazz;
  74:     }
  75: 
  76:     /**
  77:      * Returns a String representation of the LookAndFeelInfo object.
  78:      *
  79:      * @return a String representation of the LookAndFeelInfo object
  80:      */
  81:     public String toString()
  82:     {
  83:       StringBuffer s = new StringBuffer();
  84:       s.append(getClass().getName());
  85:       s.append('[');
  86:       s.append(getName());
  87:       s.append(' ');
  88:       s.append(getClassName());
  89:       s.append(']');
  90:       return s.toString();
  91:     }
  92:   }
  93: 
  94:   private static final long serialVersionUID = -5547433830339189365L;
  95: 
  96:   static LookAndFeelInfo [] installed = {
  97:     new LookAndFeelInfo ("Metal", "javax.swing.plaf.metal.MetalLookAndFeel")
  98:   };
  99: 
 100:   static LookAndFeel[] aux_installed;
 101:   
 102:   static LookAndFeel look_and_feel = new MetalLookAndFeel();
 103: 
 104:   static
 105:   {
 106:     String defaultlaf = System.getProperty("swing.defaultlaf");
 107:     try {
 108:       if (defaultlaf != null)
 109:         {
 110:           Class lafClass = Class.forName(defaultlaf);
 111:           LookAndFeel laf = (LookAndFeel) lafClass.newInstance();
 112:           setLookAndFeel(laf);
 113:         }
 114:     }
 115:     catch (Exception ex)
 116:       {
 117:         System.err.println("cannot initialize Look and Feel: " + defaultlaf);
 118:         System.err.println("errot: " + ex.getMessage());
 119:         System.err.println("falling back to Metal Look and Feel");
 120:       }
 121:   }
 122: 
 123:   public UIManager()
 124:   {
 125:     // Do nothing here.
 126:   }
 127: 
 128:   /**
 129:    * Add a <code>PropertyChangeListener</code> to the listener list.
 130:    *
 131:    * @param listener the listener to add
 132:    */
 133:   public static void addPropertyChangeListener(PropertyChangeListener listener)
 134:   {
 135:     // FIXME
 136:   }
 137: 
 138:   /**
 139:    * Remove a <code>PropertyChangeListener</code> from the listener list.
 140:    *
 141:    * @param listener the listener to remove
 142:    */
 143:   public static void removePropertyChangeListener(PropertyChangeListener listener)
 144:   {
 145:     // FIXME
 146:   }
 147: 
 148:   /**
 149:    * Returns an array of all added <code>PropertyChangeListener</code> objects.
 150:    *
 151:    * @return an array of listeners
 152:    *
 153:    * @since 1.4
 154:    */
 155:   public static PropertyChangeListener[] getPropertyChangeListeners()
 156:   {
 157:     // FIXME
 158:     throw new Error ("Not implemented");
 159:   }
 160: 
 161:   /**
 162:    * Add a LookAndFeel to the list of auxiliary look and feels.
 163:    */
 164:   public static void addAuxiliaryLookAndFeel (LookAndFeel l)
 165:   {
 166:     if (aux_installed == null)
 167:       {
 168:         aux_installed = new LookAndFeel[1];
 169:         aux_installed[0] = l;
 170:         return;
 171:       }
 172:     
 173:     LookAndFeel[] T = new LookAndFeel[ aux_installed.length+1 ];
 174:     System.arraycopy(aux_installed, 0, T, 0, aux_installed.length);             
 175:     aux_installed = T;
 176:     aux_installed[aux_installed.length-1] = l;
 177:   }
 178:     
 179:   public static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf)
 180:   {
 181:     if (aux_installed == null)
 182:       return false;
 183: 
 184:     for (int i=0;i<aux_installed.length;i++)
 185:       {
 186:         if (aux_installed[i] == laf)
 187:           {
 188:             aux_installed[ i ] = aux_installed[aux_installed.length-1];
 189:             LookAndFeel[] T = new LookAndFeel[ aux_installed.length-1 ];
 190:             System.arraycopy (aux_installed, 0, T, 0, aux_installed.length-1);
 191:             aux_installed = T;
 192:             return true;
 193:           }        
 194:       }
 195:     return false;
 196:   }
 197: 
 198:   public static  LookAndFeel[] getAuxiliaryLookAndFeels()
 199:   {
 200:     return aux_installed;
 201:   }
 202: 
 203:   public static  Object get(Object key)
 204:   {
 205:     return getLookAndFeel().getDefaults().get(key);
 206:   }
 207: 
 208:   public static  Object get(Object key, Locale locale)
 209:   {
 210:     return getLookAndFeel().getDefaults().get(key ,locale);
 211:   }
 212: 
 213:   /**
 214:    * Returns a boolean value from the defaults table,
 215:    * <code>false</code> if key is not present.
 216:    *
 217:    * @since 1.4
 218:    */
 219:   public static boolean getBoolean(Object key)
 220:   {
 221:     Boolean value = (Boolean) getLookAndFeel().getDefaults().get(key);
 222:     return value != null ? value.booleanValue() : false;
 223:   }
 224:   
 225:   /**
 226:    * Returns a boolean value from the defaults table,
 227:    * <code>false</code> if key is not present.
 228:    *
 229:    * @since 1.4
 230:    */
 231:   public static boolean getBoolean(Object key, Locale locale)
 232:   {
 233:     Boolean value = (Boolean) getLookAndFeel().getDefaults().get(key, locale);
 234:     return value != null ? value.booleanValue() : false;
 235:   }
 236:     
 237:   /**
 238:    * Returns a border from the defaults table. 
 239:    */
 240:   public static Border getBorder(Object key)
 241:   {
 242:     return (Border) getLookAndFeel().getDefaults().get(key);
 243:   }
 244:     
 245:   /**
 246:    * Returns a border from the defaults table.
 247:    *
 248:    * @since 1.4
 249:    */
 250:   public static Border getBorder(Object key, Locale locale)
 251:   {
 252:     return (Border) getLookAndFeel().getDefaults().get(key, locale);
 253:   }
 254:     
 255:   /**
 256:    * Returns a drawing color from the defaults table. 
 257:    */
 258:   public static  Color getColor(Object key)
 259:   {
 260:     return (Color) getLookAndFeel().getDefaults().get(key);
 261:   }
 262: 
 263:   /**
 264:    * Returns a drawing color from the defaults table. 
 265:    */
 266:   public static  Color getColor(Object key, Locale locale)
 267:   {
 268:     return (Color) getLookAndFeel().getDefaults().get(key);
 269:   }
 270: 
 271:   /**
 272:    * this string can be passed to Class.forName()
 273:    */
 274:   public static  String getCrossPlatformLookAndFeelClassName()
 275:   {    
 276:     return "javax.swing.plaf.metal.MetalLookAndFeel";
 277:   }
 278: 
 279:   /**
 280:    * Returns the default values for this look and feel. 
 281:    */
 282:   public static UIDefaults getDefaults()
 283:   {
 284:     return getLookAndFeel().getDefaults();
 285:   }
 286: 
 287:   /**
 288:    * Returns a dimension from the defaults table. 
 289:    */
 290:   public static Dimension getDimension(Object key)
 291:   {
 292:     return (Dimension) getLookAndFeel().getDefaults().get(key);
 293:   }
 294: 
 295:   /**
 296:    * Returns a dimension from the defaults table. 
 297:    */
 298:   public static Dimension getDimension(Object key, Locale locale)
 299:   {
 300:     return (Dimension) getLookAndFeel().getDefaults().get(key, locale);
 301:   }
 302: 
 303:   /**
 304:    * Retrieves a font from the defaults table of the current
 305:    * LookAndFeel.
 306:    *
 307:    * @param key an Object that specifies the font. Typically,
 308:    *        this is a String such as
 309:    *        <code>TitledBorder.font</code>.
 310:    */
 311:   public static Font getFont(Object key)
 312:   {
 313:     return (Font) getLookAndFeel().getDefaults().get(key);
 314:   }
 315: 
 316:   /**
 317:    * Retrieves a font from the defaults table of the current
 318:    * LookAndFeel.
 319:    *
 320:    * @param key an Object that specifies the font. Typically,
 321:    *        this is a String such as
 322:    *        <code>TitledBorder.font</code>.
 323:    */
 324:   public static Font getFont(Object key, Locale locale)
 325:   {
 326:     return (Font) getLookAndFeel().getDefaults().get(key ,locale);
 327:   }
 328: 
 329:   /**
 330:    * Returns an Icon from the defaults table.
 331:    */
 332:   public static Icon getIcon(Object key)
 333:   {
 334:     return (Icon) getLookAndFeel().getDefaults().get(key);
 335:   }
 336:   
 337:   /**
 338:    * Returns an Icon from the defaults table.
 339:    */
 340:   public static Icon getIcon(Object key, Locale locale)
 341:   {
 342:     return (Icon) getLookAndFeel().getDefaults().get(key, locale);
 343:   }
 344:   
 345:   /**
 346:    * Returns an Insets object from the defaults table.
 347:    */
 348:   public static Insets getInsets(Object key)
 349:   {
 350:     return (Insets) getLookAndFeel().getDefaults().getInsets(key);
 351:   }
 352: 
 353:   /**
 354:    * Returns an Insets object from the defaults table.
 355:    */
 356:   public static Insets getInsets(Object key, Locale locale)
 357:   {
 358:     return (Insets) getLookAndFeel().getDefaults().getInsets(key, locale);
 359:   }
 360: 
 361:   public static LookAndFeelInfo[] getInstalledLookAndFeels()
 362:   {
 363:     return installed;
 364:   }
 365: 
 366:   public static int getInt(Object key)
 367:   {
 368:     Integer x = (Integer) getLookAndFeel().getDefaults().get(key);
 369:     if (x == null)
 370:       return 0;
 371:     return x.intValue();
 372:   }
 373: 
 374:   public static int getInt(Object key, Locale locale)
 375:   {
 376:     Integer x = (Integer) getLookAndFeel().getDefaults().get(key, locale);
 377:     if (x == null)
 378:       return 0;
 379:     return x.intValue();
 380:   }
 381: 
 382:   public static LookAndFeel getLookAndFeel()
 383:   {
 384:     return look_and_feel;
 385:   }
 386: 
 387:   /**
 388:    * Returns the <code>UIDefaults</code> table of the currently active
 389:    * look and feel.
 390:    */
 391:   public static UIDefaults getLookAndFeelDefaults()
 392:   {
 393:     return getLookAndFeel().getDefaults();
 394:   }
 395: 
 396:   /**
 397:    * Returns a string from the defaults table.
 398:    */
 399:   public static String getString(Object key)
 400:   {
 401:     return (String) getLookAndFeel().getDefaults().get(key);
 402:   }
 403:   
 404:   /**
 405:    * Returns a string from the defaults table.
 406:    */
 407:   public static String getString(Object key, Locale locale)
 408:   {
 409:     return (String) getLookAndFeel().getDefaults().get(key, locale);
 410:   }
 411:   
 412:   /**
 413:    * Returns the name of the LookAndFeel class that implements the
 414:    * native systems look and feel if there is one, otherwise the name
 415:    * of the default cross platform LookAndFeel class.
 416:    */
 417:   public static String getSystemLookAndFeelClassName()
 418:   {
 419:     return getCrossPlatformLookAndFeelClassName();
 420:   }
 421: 
 422:   /**
 423:    * Returns the Look and Feel object that renders the target component.
 424:    */
 425:   public static ComponentUI getUI(JComponent target)
 426:   {
 427:     return getDefaults().getUI(target);
 428:   }
 429: 
 430:   /**
 431:    * Creates a new look and feel and adds it to the current array.
 432:    */
 433:   public static void installLookAndFeel(String name, String className)
 434:   {
 435:   }
 436: 
 437:   /**
 438:    * Adds the specified look and feel to the current array and then calls
 439:    * setInstalledLookAndFeels(javax.swing.UIManager.LookAndFeelInfo[]).
 440:    */
 441:   public static void installLookAndFeel(LookAndFeelInfo info)
 442:   {
 443:   }
 444: 
 445:   /**
 446:    * Stores an object in the defaults table.
 447:    */
 448:   public static Object put(Object key, Object value)
 449:   {
 450:     return getLookAndFeel().getDefaults().put(key,value);
 451:   }
 452: 
 453:   /**
 454:    * Replaces the current array of installed LookAndFeelInfos.
 455:    */
 456:   public static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)
 457:   {
 458:   }
 459:   
 460:   /**
 461:    * Set the current default look.
 462:    */
 463:   public static void setLookAndFeel(LookAndFeel newLookAndFeel)
 464:     throws UnsupportedLookAndFeelException
 465:   {
 466:     if (! newLookAndFeel.isSupportedLookAndFeel())
 467:       throw new UnsupportedLookAndFeelException(newLookAndFeel.getName());
 468:     
 469:     if (look_and_feel != null)
 470:       look_and_feel.uninitialize();
 471: 
 472:     // Set the current default look and feel using a LookAndFeel object. 
 473:     look_and_feel = newLookAndFeel;
 474:     look_and_feel.initialize();
 475:     
 476:     //revalidate();
 477:     //repaint();
 478:   }
 479: 
 480:   /**
 481:    * Set the current default look and feel using a class name.
 482:    */
 483:   public static void setLookAndFeel (String className)
 484:     throws ClassNotFoundException, InstantiationException, IllegalAccessException,
 485:     UnsupportedLookAndFeelException
 486:   {
 487:     Class c = Class.forName(className);
 488:     LookAndFeel a = (LookAndFeel) c.newInstance(); // throws class-cast-exception
 489:     setLookAndFeel(a);
 490:   }
 491: }