Java Source Code: org.makagiga.commons.CoolHeader


   1: // Copyright 2006 Konrad Twardowski
   2: //
   3: // Licensed under the Apache License, Version 2.0 (the "License");
   4: // you may not use this file except in compliance with the License.
   5: // You may obtain a copy of the License at
   6: //
   7: //   http://www.apache.org/licenses/LICENSE-2.0
   8: //
   9: // Unless required by applicable law or agreed to in writing, software
  10: // distributed under the License is distributed on an "AS IS" BASIS,
  11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12: // See the License for the specific language governing permissions and
  13: // limitations under the License.
  14: 
  15: package org.makagiga.commons;
  16: 
  17: import static java.awt.Color.*;
  18: 
  19: import static org.makagiga.commons.UI._;
  20: 
  21: import java.awt.Color;
  22: import java.awt.Font;
  23: import java.awt.GradientPaint;
  24: import java.awt.Graphics;
  25: import java.awt.Graphics2D;
  26: import java.awt.RenderingHints;
  27: import java.awt.Shape;
  28: import java.awt.event.FocusEvent;
  29: import java.awt.event.FocusListener;
  30: import java.awt.geom.RoundRectangle2D;
  31: import javax.swing.Icon;
  32: 
  33:	  public class CoolHeader extends MLabel {
  34:
  35:    // public
  36:    
  37:    public enum Direction { NONE, TOP, LEFT, BOTTOM, RIGHT };
  38:
  39:    // private
  40:
  41:    private boolean textAntialiasing;
  42:    private Direction direction = Direction.NONE;
  43:    private FocusListener focusListener;
  44:    private int brightness = DEFAULT_BRIGHTNESS;
  45:    private static final int DEFAULT_BRIGHTNESS = 80;
  46:    
  47:    // public
  48:    
  49:	      public CoolHeader() {
  50:        this(null, null);
  51:    }
  52:
  53:	      public CoolHeader(final Icon icon) {
  54:        this(null, icon);
  55:    }
  56:
  57:	      public CoolHeader(final String text) {
  58:        this(text, null);
  59:    }
  60:    
  61:	      public CoolHeader(final String text, final Icon icon) {
  62:        super(text, icon);
  63:        setIconTextGap(5);
  64:        setMargin(5);
  65:        UI.setFontStyle(this, Font.BOLD);
  66:    }
  67:
  68:	      public Color getColor() {
  69:        return getBackground();
  70:    }
  71:    
  72:    /**
  73:     * @deprecated As of 1.2, replaced by setColor(final Color, final Color)
  74:     */
  75:    @Deprecated
  76:	      public void setColor(final Color value) {
  77:        setColor(value, getForeground(), false);
  78:    }
  79:
  80:    /**
  81:     * @deprecated As of 1.2, replaced by setColor(final Color, final Color, final boolean)
  82:     */
  83:    @Deprecated
  84:	      public void setColor(final Color value, final boolean blend) {
  85:        setColor(value, getForeground(), blend);
  86:    }
  87:
  88:    /**
  89:     * @since 1.2
  90:     */
  91:	      public void setColor(final Color background, final Color foreground) {
  92:        setColor(background, foreground, false);
  93:    }
  94:
  95:    /**
  96:     * @since 1.2
  97:     */
  98:	      public void setColor(final Color background, final Color foreground, final boolean blend) {
  99:        if (blend && UI.specialEffects.get())
 100:            Blend.background(this, null, (background == null) ? UI.getColor("Label.background") : background);
 101:        else
 102:            setBackground(background);
 103:        setForeground(foreground);
 104:    }
 105:
 106:    public int getBrightness() { return brightness; }
 107:    
 108:	      public void setBrightness(final int value) {
 109:        brightness = TK.range(value, DEFAULT_BRIGHTNESS, 0, 100);
 110:    }
 111:    
 112:    public Direction getDirection() { return direction; }
 113:    
 114:	      public void setDirection(final Direction value) {
 115:	          if (value != direction) {
 116:            direction = value;
 117:            if (direction == Direction.NONE)
 118:                clearIcon();
 119:            else
 120:                setIcon(FS.getDefaultIcon("ui/" + direction.name().toLowerCase() + "arrow"));
 121:        }
 122:    }
 123:
 124:    public boolean isTextAntialiasing() { return textAntialiasing; }
 125:    
 126:    public void setTextAntialiasing(final boolean value) { textAntialiasing = value; }
 127:
 128:    @Override
 129:	      public void paintComponent(final Graphics graphics) {
 130:	          if (UI.specialEffects.get()) {
 131:            setOpaque(false);
 132:        }
 133:	          else {
 134:            setOpaque(true);
 135:            super.paintComponent(graphics);
 136:            
 137:            return;
 138:        }
 139:        
 140:        final int w = getWidth();
 141:        final int h = getHeight();
 142:        
 143:        Graphics2D g = (Graphics2D)graphics;
 144:
 145:        int midHeight = h / 2;
 146:        
 147:        Color baseColor = getBackground();
 148:        if (baseColor == null)
 149:            baseColor = WHITE;
 150:        
 151:        // save AA
 152:        Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
 153:        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 154:        final int ARC = 8;
 155:        
 156:        // bottom gradient
 157:        GradientPaint bottomGradient = new GradientPaint(
 158:            0.0f, midHeight, UI.getDarker(baseColor, 0.98f),
 159:            0.0f, h, baseColor
 160:        );
 161:        g.setPaint(bottomGradient);
 162:        Shape shape = new RoundRectangle2D.Float(0, midHeight - ARC / 2, w, midHeight + ARC / 2 + 1, ARC, ARC);
 163:        g.fill(shape);
 164:
 165:        // top gradient
 166:        int y2 = midHeight + ARC / 2;
 167:        GradientPaint topGradient = new GradientPaint(
 168:            0.0f, 0.0f, UI.getBrighter(baseColor, brightness),
 169:            0.0f, y2, baseColor
 170:        );
 171:        g.setPaint(topGradient);
 172:        shape = new RoundRectangle2D.Float(0, 0, w, y2, ARC, ARC);
 173:        g.fill(shape);
 174:
 175:        g.setPaint(null);
 176:        
 177:        // restore AA
 178:        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
 179:        
 180:        if (textAntialiasing)
 181:            UI.setTextAntialiasing(g, true);
 182:        super.paintComponent(graphics);
 183:        
 184:        // paint focus
 185:	          if (isFocusOwner()) {
 186:            g.setColor(UI.getDarker(baseColor));
 187:            g.drawRect(0, 0, w - 1, h - 1);
 188:            g.drawRect(1, 1, w - 3, h - 3);
 189:        }
 190:    }
 191:    
 192:	      public void setError(final String text) {
 193:        setMessage(text, "ui/error", MHighlighter.ERROR_COLOR);
 194:    }
 195:
 196:    @Override
 197:	      public void setFocusable(final boolean value) {
 198:        super.setFocusable(value);
 199:	          if (focusListener != null) {
 200:            removeFocusListener(focusListener);
 201:            focusListener = null;
 202:        }
 203:	          if (value) {
 204:	              focusListener = new FocusListener() {
 205:	                  public void focusGained(final FocusEvent e) {
 206:                    CoolHeader.this.repaint();
 207:                }
 208:	                  public void focusLost(final FocusEvent e) {
 209:                    CoolHeader.this.repaint();
 210:                }
 211:            };
 212:            addFocusListener(focusListener);
 213:        }
 214:    }
 215:
 216:	      public void setInfo(final String text) {
 217:        setMessage(text, "ui/info", MHighlighter.OK_COLOR);
 218:    }
 219:
 220:	      public void setMessage(final String text, final Icon icon, final Color color) {
 221:        setColor(color, true);
 222:        setIcon(icon);
 223:        setText(text);
 224:    }
 225:    
 226:	      public void setMessage(final String text, final String iconName, final Color color) {
 227:        setMessage(text, FS.getIcon(iconName), color);
 228:    }
 229:    
 230:	      public void setOK(final String text) {
 231:        setMessage(text, "ui/ok", MHighlighter.OK_COLOR);
 232:    }
 233:
 234:	      public void setWarning(final String text) {
 235:        setMessage(text, "ui/warning", MHighlighter.DEFAULT_COLOR);
 236:    }
 237:
 238:}