1 package org.columba.core.gui.search; 2 3 import java.awt.Component ; 4 import java.awt.Cursor ; 5 import java.awt.Dimension ; 6 import java.awt.FontMetrics ; 7 import java.awt.Graphics ; 8 import java.awt.Insets ; 9 import java.awt.Rectangle ; 10 import java.awt.event.MouseEvent ; 11 12 import javax.swing.Icon ; 13 import javax.swing.JPopupMenu ; 14 import javax.swing.JTextField ; 15 import javax.swing.border.AbstractBorder ; 16 import javax.swing.border.Border ; 17 import javax.swing.event.MouseInputAdapter ; 18 import javax.swing.event.PopupMenuListener ; 19 20 public class IconTextField extends JTextField { 21 22 private Icon icon; 23 24 private Rectangle iconBounds; 25 26 private JPopupMenu popupMenu; 27 28 private IconifiedBorder border; 29 30 public IconTextField(Icon icon, int columns) { 31 super(columns); 33 34 this.icon = icon; 35 36 popupMenu = new JPopupMenu (); 37 40 iconBounds = new Rectangle (0, 0, icon.getIconWidth(), icon 43 .getIconHeight()); 44 45 border = new IconifiedBorder(getBorder(), icon, 1); 47 setBorder(border); 48 49 MouseHandler handler = new MouseHandler(); 51 addMouseListener(handler); 52 addMouseMotionListener(handler); 53 54 } 55 56 public void addPopupMenuListener(PopupMenuListener listener) { 57 popupMenu.addPopupMenuListener(listener); 58 } 59 60 public void showPopup() { 61 popupMenu.show(IconTextField.this, 0, getHeight()); 63 } 65 66 public void setPopupMenu(JPopupMenu menu) { 67 JPopupMenu oldMenu = popupMenu; 68 popupMenu = menu; 69 firePropertyChange("popup", oldMenu, popupMenu); 70 } 71 72 public void setIcon(Icon ico) { 73 Icon oldIcon = this.icon; 74 this.icon = ico; 75 iconBounds = new Rectangle (0, 0, ico.getIconWidth(), ico 76 .getIconHeight()); 77 border.setIcon(icon); 78 firePropertyChange("icon", oldIcon, icon); 79 } 80 81 public Icon getIcon() { 82 return icon; 83 } 84 85 public JPopupMenu getPopupMenu() { 86 return popupMenu; 87 } 88 89 public Dimension getPreferredSize() { 90 Dimension size = super.getPreferredSize(); 91 Insets insets = getInsets(); 92 Insets margin = getMargin(); 93 FontMetrics fm = getFontMetrics(getFont()); 94 size.height = Math.max(fm.getHeight(), icon.getIconHeight()) 95 + insets.top + insets.bottom; 96 return size; 97 } 98 99 private class MouseHandler extends MouseInputAdapter { 100 public void mouseMoved(MouseEvent e) { 101 if (iconBounds.contains(e.getPoint())) { 102 setCursor(Cursor.getDefaultCursor()); 103 } else { 104 setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); 105 } 106 } 107 108 public void mouseClicked(MouseEvent e) { 109 if (iconBounds.contains(e.getPoint())) { 110 if (popupMenu.isVisible()) { 111 popupMenu.setVisible(false); 112 } else { 113 popupMenu.show(IconTextField.this, 0, getHeight()); 114 } 115 } 116 } 117 } 118 119 private class IconifiedBorder extends AbstractBorder { 120 private Border innerBorder; 121 122 private Icon icon; 123 124 private Insets insets; 125 126 private int spacing; 127 128 public IconifiedBorder(Border innerBorder, Icon icon, int spacing) { 129 this.innerBorder = innerBorder; 130 this.icon = icon; 131 this.spacing = spacing; 132 } 133 134 public void setIcon(Icon ico) { 135 this.icon = ico; 136 insets = null; 137 } 138 139 public Border getInnerBorder() { 140 return innerBorder; 141 } 142 143 public void paintBorder(Component c, Graphics g, int x, int y, 144 int width, int height) { 145 innerBorder.paintBorder(c, g, x, y, width, height); 146 Insets innerInsets = innerBorder.getBorderInsets(c); 147 iconBounds.x = x + innerInsets.left + spacing; 148 iconBounds.y = y + innerInsets.top + spacing; 149 icon.paintIcon(c, g, iconBounds.x, iconBounds.y); 150 } 151 152 public Insets getBorderInsets(Component c) { 153 if (insets == null) { 154 insets = (Insets ) innerBorder.getBorderInsets(c).clone(); 155 insets.left += icon.getIconWidth() + spacing * 4; 156 insets.top += spacing; 157 insets.bottom += spacing; 158 } 159 return insets; 160 } 161 } 162 163 } | Popular Tags |