001 /*
002 * $Id: CapsulePainter.java 3288 2009-03-10 14:36:28Z kschaefe $
003 *
004 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020 */
021
022
023 package org.jdesktop.swingx.painter;
024
025 import java.awt.*;
026 import java.awt.geom.RoundRectangle2D;
027
028 /**
029 * Draws a capsule. This is a rectangle capped by two semi circles. You can draw only a
030 * portion of a capsule using the portion property.
031 * @author joshy
032 */
033 public class CapsulePainter extends AbstractAreaPainter<Object> {
034 public enum Portion { Top, Full, Bottom, Left, Right }
035 private Portion portion;
036
037
038 /**
039 * Create a new CapsulePainter that draws a full capsule.
040 */
041 public CapsulePainter() {
042 this.setPortion(Portion.Full);
043 }
044 /**
045 * Create a new CapsulePainter that only draws the portion specified.
046 * @param portion the portion to draw
047 */
048 public CapsulePainter(Portion portion) {
049 this.setPortion(portion);
050 }
051
052
053
054 /**
055 * Returns the current portion property. This property determines
056 * which part of the capsule will be drawn.
057 * @return the current portion
058 */
059 public Portion getPortion() {
060 return portion;
061 }
062
063 /**
064 * Sets the current portion property. This property determines
065 * which part of the capsule will be drawn.
066 * @param portion the new portion
067 */
068 public void setPortion(Portion portion) {
069 Portion old = this.portion;
070 this.portion = portion;
071 setDirty(true);
072 firePropertyChange("portion",old,getPortion());
073 }
074
075
076 /**
077 * {@inheritDoc}
078 */
079 @Override
080 protected void doPaint(Graphics2D g, Object component, int width, int height) {
081 Shape rect = provideShape(g,component,width,height);
082 if(getStyle() == Style.BOTH || getStyle() == Style.FILLED) {
083 g.setPaint(getFillPaint());
084 g.fill(rect);
085 }
086 if(getStyle() == Style.BOTH || getStyle() == Style.OUTLINE) {
087 g.setPaint(getBorderPaint());
088 g.draw(rect);
089 }
090 }
091
092 /**
093 * {@inheritDoc}
094 */
095 @Override
096 protected Shape provideShape(Graphics2D g, Object comp, int width, int height) {
097 int round = 10;
098 int rheight = height;
099 int ry = 0;
100 if(getPortion() == Portion.Top) {
101 round = height*2;
102 rheight = height*2;
103 }
104 if(getPortion() == Portion.Bottom) {
105 round = height*2;
106 rheight = height*2;
107 ry = -height;
108 }
109 // need to support left and right!
110
111 return new RoundRectangle2D.Double(0, ry, width, rheight, round, round);
112 }
113
114 }