View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui;
21  
22  import java.awt.Component;
23  import java.awt.Graphics;
24  import java.awt.Insets;
25  import javax.swing.border.AbstractBorder;
26  
27  /***
28   * Provides a simple 2 line bevel border. If the Border is raised it will look
29   * like a tiny wall around the component it is used with. If it is lowered it
30   * will look like a thin trench.
31   */
32  public class ThinEtchedBorder extends AbstractBorder {
33  
34      public static final int RAISED  = 0;
35  
36      public static final int LOWERED = 1;
37  
38      protected int etchType;
39  
40      /***
41       * Constructs a border with the given <code>etchType</code>.
42       * 
43  	 * @see javax.swing.border.BevelBorder#BevelBorder(int)
44  	 * @param etchType either <code>BevelBoder.RAISED</code> or <code>BevelBorder.LOWERED</code> 
45  	 */
46      public ThinEtchedBorder(int etchType)
47      {
48          this.etchType = etchType;
49      }
50  
51      /***
52       * Constructs a lowered border.
53       *  
54  	 * @see javax.swing.border.BevelBorder#BevelBorder(int)
55       */
56      public ThinEtchedBorder()
57      {
58          this(LOWERED);
59      }
60  
61      public void paintBorder(Component c, Graphics g, int x, int y, int w,
62  							int h) 
63  	{
64  		g.translate(x, y);
65  	
66  		g.setColor((etchType == LOWERED) 
67  				   ? c.getBackground().darker() 
68  				   : c.getBackground().brighter());
69  		g.drawLine(0, h - 2, w - 1, h - 2);
70  
71  		g.setColor((etchType == LOWERED)
72  				   ? c.getBackground().brighter() 
73  				   : c.getBackground().darker());
74  		g.drawLine(0, h - 1, w - 1, h - 1);
75  	
76  		g.translate(-x, -y);
77      }
78  
79      public Insets getBorderInsets(Component c)       
80  	{
81          return new Insets(2, 2, 2, 2);
82      }
83  
84      public Insets getBorderInsets(Component c, Insets insets) 
85  	{
86          insets.left = insets.top = insets.right = insets.bottom = 2;
87          return insets;
88      }
89  
90      /***
91       * Returns true.
92       */
93      public boolean isBorderOpaque() 
94  	{ 
95  		return true; 
96  	}
97  
98      /***
99       * Returns the etch type.
100      */
101     public int getEtchType() 
102 	{
103         return etchType;
104     }
105 
106 }