View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.extensions;
17  
18  import java.awt.Color;
19  import java.awt.Component;
20  import java.awt.Font;
21  import java.awt.FontMetrics;
22  import java.awt.Graphics;
23  import java.awt.Image;
24  import java.awt.Insets;
25  import java.awt.Rectangle;
26  
27  import javax.swing.ImageIcon;
28  import javax.swing.UIManager;
29  import javax.swing.border.AbstractBorder;
30  
31  /**
32   * A border wich supports an icon as well as text.
33   *
34   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
35   * @version $Id: IconTitledBorder.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
36   */
37  public class IconTitledBorder extends AbstractBorder {
38  
39    public static final int EDGE_SPACING = 2;
40    public static final int TITLE_SPACING = 5;
41    public static final int TEXTICON_SPACING = 5;
42    
43    protected int thickness = 1;
44    
45    protected String title;
46    protected ImageIcon image;
47  
48    /**
49     * 
50     */
51    public IconTitledBorder() {
52      super();
53    }
54  
55    /**
56     * @see javax.swing.border.Border#paintBorder(java.awt.Component, java.awt.Graphics, int, int, int, int)
57     */
58    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
59  //    System.out.println("Border : " + c);
60      int lc = (getLineCorrection(c)/2);
61      Color tmpColor = g.getColor();
62      g.setColor(Color.gray);
63      for (int i = 0; i < getThickness(); i++) {
64        g.drawRect(x+EDGE_SPACING+i, y+EDGE_SPACING+i+lc, width-EDGE_SPACING-i-i-1, height-EDGE_SPACING-i-i-1-lc);
65      }
66      g.setColor(tmpColor);
67      clearLineForTitle(c, g, x, y, width, height);
68      paintImage(c, g, x, y, width, height);
69      paintTitle(c, g, x, y, width, height);
70      //super.paintBorder(c, g, x+EDGE_SPACING, y, width, height-EDGE_SPACING);
71    }
72    
73    protected void clearLineForTitle(Component c, Graphics g, int x, int y, int width, int height) {
74      Rectangle rect = getTitleRectangle(c, x, y, width);
75      Color tmpColor = g.getColor();
76      g.setColor(c.getBackground());
77      if (getImage() != null) {
78        rect.width+=EDGE_SPACING;
79        rect.width+=getImage().getIconWidth();
80      }
81      g.fillRect(rect.x, rect.y, rect.width, rect.height);
82      g.setColor(tmpColor);
83    }
84  
85    /**
86     * @param c the component
87     * @param x the x position
88     * @param y the y position
89     * @param width the width of the paint area
90     * @return a rectangle containing the dimensions of the title including image.
91     */  
92    protected Rectangle getTitleRectangle(Component c, int x, int y, int width) {
93        Rectangle rectangle = new Rectangle();
94        rectangle.x = x+EDGE_SPACING+TITLE_SPACING;
95        rectangle.y = y+EDGE_SPACING;
96        rectangle.width = getTitleWidth(c);
97        rectangle.height = getMaxHeight(c);
98        return rectangle;
99    }
100 
101   protected void paintTitle(Component c, Graphics g, int x, int y, int width, int height) {
102     if (getTitle() == null) {
103       return;
104     }
105     int imageCorrection = 0;
106     if (getImage() != null) {
107       imageCorrection = EDGE_SPACING+getImage().getIconWidth();
108     }
109     Font tmpFont = g.getFont();
110     Color tmpColor = g.getColor();
111     g.setFont(UIManager.getFont("TitledBorder.font"));
112     g.setColor(UIManager.getColor("TitledBorder.titleColor"));
113     g.drawString(title, x+EDGE_SPACING+TITLE_SPACING+imageCorrection, y+EDGE_SPACING+getLineCorrection(c));
114     g.setFont(tmpFont);
115     g.setColor(tmpColor);
116   }
117 
118 
119   public int getTitleWidth(Component c) {
120     int titleWidth = 0;
121     if (getImage() != null) {
122       titleWidth+=getImage().getIconWidth();
123     }
124     if (getTitle() != null && getImage() != null) {
125       titleWidth+=TEXTICON_SPACING;
126     }
127     if (getTitle() != null) {
128       titleWidth = c.getFontMetrics(UIManager.getFont("TitledBorder.font")).stringWidth(getTitle());
129     }
130     return titleWidth;
131   }
132 
133   protected void paintImage(Component c, Graphics g, int x, int y, int width, int height) {
134     if (getImage() != null) {                 
135       getImage().paintIcon(c, g, x+EDGE_SPACING+TITLE_SPACING, y);
136     }
137   }
138   
139   /**
140    * @param c the component to get the metric from
141    * @return the correction that needs to take place based on the icon and title for drawing
142    * a line. It will return the y correction so the line can be drawn in the middle of those components
143    */
144   protected int getLineCorrection(Component c) {
145     return getMaxHeight(c) / 2;
146   }
147   
148   public int getMaxHeight(Component c) {
149     int textHeight = 0;
150     int imageHeight = 0;
151     if (getTitle() != null) {
152         FontMetrics metrics = c.getFontMetrics(c.getFont());
153         textHeight = metrics.getHeight();
154     }
155     if (getImage() != null) {
156         imageHeight = getImage().getIconHeight();
157     }
158     return Math.max(textHeight, imageHeight);
159   }
160 
161   /**
162    * @return the image
163    */
164   public ImageIcon getImage() {
165     return image;
166   }
167 
168   /**
169    * @return the thickness
170    */
171   public int getThickness() {
172     return thickness;
173   }
174 
175   /**
176    * @return the title of the border
177    */
178   public String getTitle() {
179     return title;
180   }
181 
182   /**
183    * Set the image of the border.
184    *
185    * @param image the image to use
186    */
187   public void setImage(Image image) {
188     this.image = new ImageIcon(image);
189   }
190 
191   /**
192    * @param thickness
193    */
194   public void setThickness(int thickness) {
195     this.thickness = thickness;
196   }
197 
198   /**
199    * @param string
200    */
201   public void setTitle(String string) {
202     title = string;
203   }
204 
205   /**
206    * @see javax.swing.border.Border#getBorderInsets(java.awt.Component)
207    */
208   public Insets getBorderInsets(Component c) {
209     return new Insets(EDGE_SPACING+getMaxHeight(c), EDGE_SPACING+thickness, EDGE_SPACING+thickness, EDGE_SPACING+thickness);
210   }
211   
212   /**
213    * @see javax.swing.border.AbstractBorder#getBorderInsets(java.awt.Component, java.awt.Insets)
214    */
215   public Insets getBorderInsets(Component c, Insets insets) {
216     return super.getBorderInsets(c, insets);
217   }
218 
219 }