Source for javax.swing.plaf.metal.DefaultMetalTheme

   1: /* DefaultMetalTheme.java --
   2:    Copyright (C) 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.plaf.metal;
  40: 
  41: import gnu.classpath.SystemProperties;
  42: 
  43: import java.awt.Font;
  44: 
  45: import javax.swing.UIManager;
  46: import javax.swing.plaf.ColorUIResource;
  47: import javax.swing.plaf.FontUIResource;
  48: 
  49: /**
  50:  * The default theme for the {@link MetalLookAndFeel}.
  51:  * 
  52:  * @see MetalLookAndFeel#setCurrentTheme(MetalTheme)
  53:  */
  54: public class DefaultMetalTheme extends MetalTheme
  55: {
  56:   private static final ColorUIResource PRIMARY1 =
  57:     new ColorUIResource(102, 102, 153);
  58:   private static final ColorUIResource PRIMARY2 =
  59:     new ColorUIResource(153, 153, 204);
  60:   private static final ColorUIResource PRIMARY3 = 
  61:     new ColorUIResource(204, 204, 255);
  62:   private static final ColorUIResource SECONDARY1 = 
  63:     new ColorUIResource(102, 102, 102);
  64:   private static final ColorUIResource SECONDARY2 = 
  65:     new ColorUIResource(153, 153, 153);
  66:   private static final ColorUIResource SECONDARY3 = 
  67:     new ColorUIResource(204, 204, 204);
  68:   
  69:   private static final FontUIResource SUB_TEXT_FONT =
  70:     new FontUIResource("Dialog", Font.PLAIN, 10);
  71:   private static final FontUIResource SYSTEM_TEXT_FONT =
  72:     new FontUIResource("Dialog", Font.PLAIN, 12);
  73:   private static final FontUIResource USER_TEXT_FONT =
  74:     new FontUIResource("Dialog", Font.PLAIN, 12);
  75:   private static final FontUIResource WINDOW_TITLE_FONT =
  76:     new FontUIResource("Dialog", Font.BOLD, 12);
  77:   
  78:   /**
  79:    * The control text font for swing.boldMetal=false.
  80:    */
  81:   private static final FontUIResource PLAIN_CONTROL_TEXT_FONT =
  82:     new FontUIResource("Dialog", Font.PLAIN, 12);
  83: 
  84:   /**
  85:    * The standard control text font.
  86:    */
  87:   private static final FontUIResource BOLD_CONTROL_TEXT_FONT =
  88:     new FontUIResource("Dialog", Font.BOLD, 12);
  89: 
  90:   /**
  91:    * The menu text font for swing.boldMetal=false.
  92:    */
  93:   private static final FontUIResource PLAIN_MENU_TEXT_FONT =
  94:     new FontUIResource("Dialog", Font.PLAIN, 12);
  95: 
  96:   /**
  97:    * The menu control text font.
  98:    */
  99:   private static final FontUIResource BOLD_MENU_TEXT_FONT =
 100:     new FontUIResource("Dialog", Font.BOLD, 12);
 101: 
 102:   /**
 103:    * Indicates the control text font.
 104:    */
 105:   static final int CONTROL_TEXT_FONT = 1;
 106: 
 107:   /**
 108:    * Indicates the menu text font.
 109:    */
 110:   static final int MENU_TEXT_FONT = 2;
 111:   
 112:   /**
 113:    * Creates a new instance of this theme.
 114:    */
 115:   public DefaultMetalTheme()
 116:   {
 117:     // Do nothing here.
 118:   }
 119: 
 120:   /**
 121:    * Returns the name of the theme.
 122:    * 
 123:    * @return <code>"Steel"</code>.
 124:    */
 125:   public String getName()
 126:   {
 127:     return "Steel";
 128:   }
 129: 
 130:   /**
 131:    * Returns the first primary color for this theme.
 132:    * 
 133:    * @return The first primary color.
 134:    */
 135:   protected ColorUIResource getPrimary1()
 136:   {
 137:     return PRIMARY1;
 138:   }
 139: 
 140:   /**
 141:    * Returns the second primary color for this theme.
 142:    * 
 143:    * @return The second primary color.
 144:    */
 145:   protected ColorUIResource getPrimary2()
 146:   {
 147:     return PRIMARY2;
 148:   }
 149: 
 150:   /**
 151:    * Returns the third primary color for this theme.
 152:    * 
 153:    * @return The third primary color.
 154:    */
 155:   protected ColorUIResource getPrimary3()
 156:   {
 157:     return PRIMARY3;
 158:   }
 159: 
 160:   /**
 161:    * Returns the first secondary color for this theme.
 162:    * 
 163:    * @return The first secondary color.
 164:    */
 165:   protected ColorUIResource getSecondary1()
 166:   {
 167:     return SECONDARY1;
 168:   }
 169: 
 170:   /**
 171:    * Returns the second secondary color for this theme.
 172:    * 
 173:    * @return The second secondary color.
 174:    */
 175:   protected ColorUIResource getSecondary2()
 176:   {
 177:     return SECONDARY2;
 178:   }
 179: 
 180:   /**
 181:    * Returns the third secondary color for this theme.
 182:    * 
 183:    * @return The third secondary color.
 184:    */
 185:   protected ColorUIResource getSecondary3()
 186:   {
 187:     return SECONDARY3;
 188:   }
 189: 
 190:   /**
 191:    * Returns the font used for text on controls.  In this case, the font is
 192:    * <code>FontUIResource("Dialog", Font.BOLD, 12)</code>, unless the
 193:    * <code>swing.boldMetal</code> UI default is set to {@link Boolean#FALSE} 
 194:    * in which case it is <code>FontUIResource("Dialog", Font.PLAIN, 12)</code>.
 195:    * 
 196:    * @return The font.
 197:    */
 198:   public FontUIResource getControlTextFont()
 199:   {
 200:     return getFont(CONTROL_TEXT_FONT);
 201:   }
 202:   
 203:   /**
 204:    * Returns the font used for text in menus.  In this case, the font is
 205:    * <code>FontUIResource("Dialog", Font.BOLD, 12)</code>, unless the
 206:    * <code>swing.boldMetal</code> UI default is set to {@link Boolean#FALSE} 
 207:    * in which case it is <code>FontUIResource("Dialog", Font.PLAIN, 12)</code>.
 208:    * 
 209:    * @return The font used for text in menus.
 210:    */
 211:   public FontUIResource getMenuTextFont()
 212:   {
 213:     return getFont(MENU_TEXT_FONT);
 214:   }
 215:   
 216:   /**
 217:    * Returns the font used for sub text.  In this case, the font is
 218:    * <code>FontUIResource("Dialog", Font.PLAIN, 10)</code>.
 219:    * 
 220:    * @return The font used for sub text.
 221:    */
 222:   public FontUIResource getSubTextFont()
 223:   {
 224:     return SUB_TEXT_FONT;
 225:   }
 226:   
 227:   /**
 228:    * Returns the font used for system text.  In this case, the font is
 229:    * <code>FontUIResource("Dialog", Font.PLAIN, 12)</code>.
 230:    * 
 231:    * @return The font used for system text.
 232:    */
 233:   public FontUIResource getSystemTextFont()
 234:   {
 235:     return SYSTEM_TEXT_FONT;
 236:   }
 237:   
 238:   /**
 239:    * Returns the font used for user text.  In this case, the font is
 240:    * <code>FontUIResource("Dialog", Font.PLAIN, 12)</code>.
 241:    * 
 242:    * @return The font used for user text. 
 243:    */
 244:   public FontUIResource getUserTextFont()
 245:   {
 246:     return USER_TEXT_FONT;
 247:   }
 248:   
 249:   /**
 250:    * Returns the font used for window titles.  In this case, the font is 
 251:    * <code>FontUIResource("Dialog", Font.BOLD, 12)</code>.
 252:    * 
 253:    * @return The font used for window titles.
 254:    */
 255:   public FontUIResource getWindowTitleFont()
 256:   {
 257:     return WINDOW_TITLE_FONT;
 258:   }
 259: 
 260:   /**
 261:    * Returns the appropriate font. The font type to return is identified
 262:    * by the specified id.
 263:    *
 264:    * @param id the font type to return
 265:    *
 266:    * @return the correct font
 267:    */
 268:   private FontUIResource getFont(int id)
 269:   {
 270:     FontUIResource font = null;
 271:     switch (id)
 272:       {
 273:       case CONTROL_TEXT_FONT:
 274:         if (isBoldMetal())
 275:           font = BOLD_CONTROL_TEXT_FONT;
 276:         else
 277:           font = PLAIN_CONTROL_TEXT_FONT;
 278:         break;
 279:       case MENU_TEXT_FONT:
 280:         if (isBoldMetal())
 281:           font = BOLD_MENU_TEXT_FONT;
 282:         else
 283:           font = PLAIN_MENU_TEXT_FONT;
 284:       break;
 285:       // TODO: Add other font types and their mapping here.
 286:       }
 287:     return font;
 288:   }
 289: 
 290:   /**
 291:    * Determines if the theme should be bold or not. The theme is bold by
 292:    * default, this can be turned off by setting the system property
 293:    * swing.boldMetal to true, or by putting the property with the same name
 294:    * into the current UIManager's defaults.
 295:    *
 296:    * @return <code>true</code>, when the theme is bold, <code>false</code>
 297:    *         otherwise
 298:    */
 299:   private boolean isBoldMetal()
 300:   {
 301:     Object boldMetal = UIManager.get("swing.boldMetal");
 302:     return (boldMetal == null || ! Boolean.FALSE.equals(boldMetal))
 303:         && ! ("false".equals(SystemProperties.getProperty("swing.boldMetal")));
 304:   }
 305: }