1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: EdgeBorder.java 1966 2009-10-30 01:15:14Z dmsmith $
21   */
22  package org.crosswire.common.swing;
23  
24  import java.awt.Component;
25  import java.awt.Graphics;
26  import java.awt.Insets;
27  
28  import javax.swing.SwingConstants;
29  import javax.swing.border.Border;
30  
31  /**
32   * EdgeBorder.
33   * 
34   * @see gnu.lgpl.License for license details.<br>
35   *      The copyright to this program is held by it's authors.
36   * @author Joe Walker
37   * @author Claude Duguay Copyright (c) 1998
38   */
39  public class EdgeBorder implements Border, SwingConstants {
40      /**
41       * Create an EdgeBorder showing a northern border
42       */
43      public EdgeBorder() {
44          this(NORTH);
45      }
46  
47      /**
48       * Create an EdgeBorder with a selected edge
49       * 
50       * @param edge
51       *            The edge to display
52       */
53      public EdgeBorder(int edge) {
54          this.edge = edge;
55      }
56  
57      /**
58       * Get the insets for a given component
59       */
60      public Insets getBorderInsets(Component component) {
61          switch (edge) {
62          case SOUTH:
63              return new Insets(0, 0, 2, 0);
64          case EAST:
65              return new Insets(0, 2, 0, 0);
66          case WEST:
67              return new Insets(0, 0, 0, 2);
68          default:
69              return new Insets(2, 0, 0, 0);
70          }
71      }
72  
73      /**
74       * Is this border opaque
75       * 
76       * @return true/false if the border if opaque
77       */
78      public boolean isBorderOpaque() {
79          return true;
80      }
81  
82      /**
83       * Actually go and paint the border
84       */
85      public void paintBorder(Component component, Graphics g, int x, int y, int w, int h) {
86          if (lift == RAISED) {
87              g.setColor(component.getBackground().brighter());
88          } else {
89              g.setColor(component.getBackground().darker());
90          }
91  
92          switch (edge) {
93          case SOUTH:
94              g.drawLine(x, y + h - 2, w, y + h - 2);
95              break;
96          case EAST:
97              g.drawLine(x + w - 2, y, x + w - 2, y + h);
98              break;
99          case WEST:
100             g.drawLine(x + 1, y, x + 1, y + h);
101             break;
102         default:
103             g.drawLine(x, y, x + w, y);
104         }
105 
106         if (lift == RAISED) {
107             g.setColor(component.getBackground().darker());
108         } else {
109             g.setColor(component.getBackground().brighter());
110         }
111 
112         switch (edge) {
113         case SOUTH:
114             g.drawLine(x, y + h - 1, w, y + h - 1);
115             break;
116         case EAST:
117             g.drawLine(x + w - 1, y, x + w - 1, y + h);
118             break;
119         case WEST:
120             g.drawLine(x + 1, y, x + 1, y + h);
121             break;
122         default:
123             g.drawLine(x, y + 1, x + w, y + 1);
124         }
125     }
126 
127     /** A raised border */
128     public static final int RAISED = 1;
129 
130     /** A lowered border */
131     public static final int LOWERED = 2;
132 
133     /** The edge to draw */
134     protected int edge = NORTH;
135 
136     /** The raised/lowered state */
137     protected int lift = LOWERED;
138 }
139