001 package sears.gui.search; 002 003 import java.awt.Color; 004 import java.awt.Component; 005 import java.awt.Graphics; 006 import java.awt.Graphics2D; 007 import java.awt.event.KeyListener; 008 009 import javax.swing.Box; 010 import javax.swing.BoxLayout; 011 import javax.swing.Icon; 012 import javax.swing.JComponent; 013 import javax.swing.JTextField; 014 import javax.swing.event.CaretListener; 015 016 import sears.gui.resources.SearsResources; 017 018 public class JSearchField extends JComponent { 019 020 /** 021 * 022 */ 023 private static final long serialVersionUID = -8471752029617130242L; 024 private static final Icon FIND_ICON = SearsResources.getIcon("TinyFindIcon"); 025 private static final int LEFT_BORDER = 6; 026 027 private JTextField _text; 028 029 public JSearchField() { 030 super(); 031 initComponents(); 032 } 033 034 private void initComponents() { 035 // Border bd = BorderFactory. 036 037 038 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 039 //this.setBackground(Color.WHITE); 040 041 //this.setOpaque(true); 042 // this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 043 _text = new JTextField(); 044 //_text.setBorder(BorderFactory.createEmptyBorder()); 045 046 Component bx = Box.createHorizontalStrut(FIND_ICON.getIconWidth() + LEFT_BORDER*2); 047 bx.setBackground(Color.WHITE); 048 this.add(bx); 049 this.add(_text); 050 this.add(Box.createHorizontalStrut(12)); 051 } 052 053 public void paint(Graphics gr) { 054 //super.paint(gr); 055 Graphics2D g = (Graphics2D) gr; 056 /*Color oc = g.getColor(); 057 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); 058 g.setColor(Color.WHITE); 059 g.fillRoundRect(0, 1, _text.getWidth()+_text.getX()+10, _text.getHeight()-2, this.getHeight(), this.getHeight()); 060 */ 061 super.paint(g); 062 /* 063 g.setColor(Color.GRAY); 064 g.drawRoundRect(0, 1, _text.getWidth()+_text.getX()+10, _text.getHeight()-2, this.getHeight(), this.getHeight()); 065 g.setColor(oc); 066 //System.out.println(this.getWidth());*/ 067 068 // centered coordonates: 069 int cy = this.getY() + (_text.getHeight()-FIND_ICON.getIconHeight())/2; 070 int cx = this.getX()+LEFT_BORDER; 071 FIND_ICON.paintIcon(this, g,cx , cy); 072 //_text.setBounds(20, 0, 073 //g.translate(cx+FIND_ICON.getIconWidth()+LEFT_BORDER, 0); 074 //_text.setSize(this.getWidth() - 30, this.getHeight()); 075 // _text.paint(g); 076 //_text.setLocation(this.getX()+30, 0); 077 } 078 079 public void addCaretListener(CaretListener listener) { 080 _text.addCaretListener(listener); 081 } 082 083 084 public void addKeyListener(KeyListener listener) { 085 _text.addKeyListener(listener); 086 } 087 088 public String getText() { 089 return _text.getText(); 090 } 091 092 public void setEnabled(boolean enabled) { 093 _text.setEnabled(enabled); 094 } 095 096 }