1   package org.wcb.gui.component.border;
2   
3   /**
4    * <small>
5    * <p/>
6    * Copyright (c)  2006  wbogaardt.
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20   * <p/>
21   * $File:  $ <br>
22   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 15, 2006 8:30:34 AM $ <br>
23   * </small>
24   *
25   * @author wbogaardt
26   *         Simple Search field border
27   */
28  import org.wcb.gui.util.UIHelper;
29  
30  import java.awt.BasicStroke;
31  import java.awt.Color;
32  import java.awt.Component;
33  import java.awt.GradientPaint;
34  import java.awt.Graphics;
35  import java.awt.Graphics2D;
36  import java.awt.Insets;
37  import java.awt.Paint;
38  import java.awt.RenderingHints;
39  import java.awt.Shape;
40  import java.awt.Stroke;
41  import java.awt.geom.Arc2D;
42  import java.awt.geom.Line2D;
43  import java.awt.image.BufferedImage;
44  
45  import javax.swing.border.Border;
46  
47  
48  public class SearchFieldBorder implements Border {
49      private static final Color BOTTOM_LINE_COLOR = new Color(216, 216, 216);
50      private static final Color TOP_LINE_1_COLOR = new Color(119, 119, 119);
51      private static final Color TOP_LINE_2_COLOR = new Color(199, 199, 199);
52      private static final Color TOP_LINE_3_COLOR = new Color(241, 241, 241);
53  
54      private BufferedImage lens;
55  
56      /**
57       * Paints a pretty icon and border for search fields.
58       */
59      public SearchFieldBorder() {
60          lens = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/lens.png");
61      }
62  
63      public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
64          int x1 = x + 8;
65          int y1 = y + height - 1;
66          int x2 = x1 + width - 14 - 8;
67          int y2 = y1;
68  
69          Graphics2D g2 = (Graphics2D) g;
70          g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
71          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
72          g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
73  
74          g2.drawImage(lens, null, 5, 5);
75  
76          Shape s;
77  
78          g.setColor(TOP_LINE_1_COLOR);
79          s = new Line2D.Double(x1, y, x2 + 1, y);
80          g2.draw(s);
81  
82          Stroke oldStroke = g2.getStroke();
83          g2.setStroke(new BasicStroke(2));
84  
85          g2.setColor(BOTTOM_LINE_COLOR);
86          s = new Line2D.Double(x1 - 2, y2, x2 + 4, y2);
87          g2.draw(s);
88  
89          Paint oldPaint = g2.getPaint();
90          Paint p = new GradientPaint(0, 0, TOP_LINE_1_COLOR, 0, height - 6, BOTTOM_LINE_COLOR);
91          g2.setPaint(p);
92  
93          //TODO: This causes some sort of rendering problem on OS X and actually crashes the VM
94          if (!System.getProperty("os.name").startsWith("Mac OS")) {
95              s = new Arc2D.Double(0, 0, height - 2, height, 90, 180, Arc2D.OPEN);
96              g2.draw(s); //This line causes a rendering problem in OS X for mac computers.
97          }
98  
99          p = new GradientPaint(width - height - 2, 0, TOP_LINE_1_COLOR, width - 2, height - 6, BOTTOM_LINE_COLOR);
100         g2.setPaint(p);
101 
102         s = new Arc2D.Double(width - height - 2, 0, height, height, 270, 180, Arc2D.OPEN);
103         g2.draw(s);
104 
105         g2.setPaint(oldPaint);
106         g2.setStroke(oldStroke);
107 
108         g.setColor(TOP_LINE_2_COLOR);
109         s = new Line2D.Double(x1 - 1, y + 1, x2 + 2, y + 1);
110         g2.draw(s);
111 
112         g.setColor(TOP_LINE_3_COLOR);
113         s = new Line2D.Double(x1 - 2, y + 2, x2 + 4, y + 2);
114         g2.draw(s);
115 
116     }
117 
118     public Insets getBorderInsets(Component c) {
119         return new Insets(3, 24, 2, 14);
120     }
121 
122     public boolean isBorderOpaque() {
123         return false;
124     }
125 }