1 package org.columba.core.gui.base; 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 19 public class IconTextField extends JTextField { 20 private Icon icon; 21 private Rectangle iconBounds; 22 private JPopupMenu popupMenu; 23 private IconifiedBorder border; 24 25 26 public IconTextField(Icon icon, int columns) { 27 super(columns); 29 30 popupMenu = new JPopupMenu (); 31 32 this.icon = icon; 34 iconBounds = new Rectangle (0, 0, icon.getIconWidth(), icon.getIconHeight()); 35 36 37 border = new IconifiedBorder(getBorder(), icon, 1); 39 setBorder(border); 40 41 MouseHandler handler = new MouseHandler(); 43 addMouseListener(handler); 44 addMouseMotionListener(handler); 45 } 46 47 public void setPopupMenu(JPopupMenu menu) { 48 JPopupMenu oldMenu = popupMenu; 49 popupMenu = menu; 50 firePropertyChange("popup", oldMenu, popupMenu); 51 } 52 53 public void setIcon(Icon ico) { 54 Icon oldIcon = this.icon; 55 this.icon = ico; 56 iconBounds = new Rectangle (0, 0, ico.getIconWidth(), ico.getIconHeight()); 57 border.setIcon(icon); 58 firePropertyChange("icon", oldIcon, icon); 59 } 60 61 public Icon getIcon() { 62 return icon; 63 } 64 65 public JPopupMenu getPopupMenu() { 66 return popupMenu; 67 } 68 69 public Dimension getPreferredSize() { 70 Dimension size = super.getPreferredSize(); 71 Insets insets = getInsets(); 72 Insets margin = getMargin(); 73 FontMetrics fm = getFontMetrics(getFont()); 74 size.height = Math.max(fm.getHeight(), icon.getIconHeight()) 75 + insets.top + insets.bottom; 76 return size; 77 } 78 79 private class MouseHandler extends MouseInputAdapter { 80 public void mouseMoved(MouseEvent e) { 81 if(iconBounds.contains(e.getPoint())) { 82 setCursor(Cursor.getDefaultCursor()); 83 } else { 84 setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); 85 } 86 } 87 88 public void mouseClicked(MouseEvent e) { 89 if(iconBounds.contains(e.getPoint())) { 90 if(popupMenu.isVisible()) { 91 popupMenu.setVisible(false); 92 } else { 93 popupMenu.show(IconTextField.this, 0, getHeight()); 94 } 95 } 96 } 97 } 98 99 private class IconifiedBorder extends AbstractBorder { 100 private Border innerBorder; 101 private Icon icon; 102 private Insets insets; 103 private int spacing; 104 105 public IconifiedBorder(Border innerBorder, Icon icon, int spacing) { 106 this.innerBorder = innerBorder; 107 this.icon = icon; 108 this.spacing = spacing; 109 } 110 111 public void setIcon(Icon ico) { 112 this.icon = ico; 113 insets = null; 114 } 115 116 public Border getInnerBorder() { 117 return innerBorder; 118 } 119 120 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 121 innerBorder.paintBorder(c, g, x, y, width, height); 122 Insets innerInsets = innerBorder.getBorderInsets(c); 123 iconBounds.x = x + innerInsets.left + spacing; 124 iconBounds.y = y + innerInsets.top + spacing; 125 icon.paintIcon(c, g, iconBounds.x, iconBounds.y); 126 } 127 128 public Insets getBorderInsets(Component c) { 129 if(insets == null) { 130 insets = (Insets )innerBorder.getBorderInsets(c).clone(); 131 insets.left += icon.getIconWidth() + spacing * 4; 132 } 135 return insets; 136 } 137 } 138 } | Popular Tags |