1 | iconBounds = new Rectangle(0, 0, icon.getIconWidth(), icon↵ | | 1 | iconBounds = new Rectangle(0, 0, icon.getIconWidth(), icon↵
|
2 | .getIconHeight());↵ | | 2 | .getIconHeight());↵
|
|
| | | 3 | ↵
|
| | | 4 | ↵
|
3 | // border↵ | | 5 | // border↵
|
4 | border = new IconifiedBorder(getBorder(), icon, 1);↵ | | 6 | border = new IconifiedBorder(getBorder(), icon, 1);↵
|
5 | setBorder(border);↵ | | 7 | setBorder(border);↵
|
|
| | | 8 | ↵
|
6 | // controller↵ | | 9 | // controller↵
|
7 | MouseHandler handler = new MouseHandler();↵ | | 10 | MouseHandler handler = new MouseHandler();↵
|
8 | addMouseListener(handler);↵ | | 11 | addMouseListener(handler);↵
|
9 | addMouseMotionListener(handler);↵ | | 12 | addMouseMotionListener(handler);↵
|
|
10 | }↵ | | |
|
|
11 | public void addPopupMenuListener(PopupMenuListener listener) {↵ | | |
|
12 | popupMenu.addPopupMenuListener(listener);↵ | | |
|
13 | }↵ | | |
|
|
14 | public void showPopup() {↵ | | |
|
15 | //popupMenu.getSelectionModel().clearSelection();↵ | | |
|
16 | popupMenu.show(IconTextField.this, 0, getHeight());↵ | | |
|
17 | //popupMenu.getSelectionModel().setSelectedIndex(0);↵ | | |
|
18 | }↵ | | |
|
|
| | | 13 | }↵
|
| | | 14 | ↵
|
19 | public void setPopupMenu(JPopupMenu menu) {↵ | | 15 | public void setPopupMenu(JPopupMenu menu) {↵
|
20 | JPopupMenu oldMenu = popupMenu;↵ | | 16 | JPopupMenu oldMenu = popupMenu;↵
|
21 | popupMenu = menu;↵ | | 17 | popupMenu = menu;↵
|
22 | firePropertyChange("popup", oldMenu, popupMenu);↵ | | 18 | firePropertyChange("popup", oldMenu, popupMenu);↵
|
23 | }↵ | | 19 | }↵
|
|
| | | 20 | ↵
|
24 | public void setIcon(Icon ico) {↵ | | 21 | public void setIcon(Icon ico) {↵
|
25 | Icon oldIcon = this.icon;↵ | | 22 | Icon oldIcon = this.icon;↵
|
26 | this.icon = ico;↵ | | 23 | this.icon = ico;↵
|
27 | iconBounds = new Rectangle(0, 0, ico.getIconWidth(), ico↵ | | 24 | iconBounds = new Rectangle(0, 0, ico.getIconWidth(), ico↵
|
28 | .getIconHeight());↵ | | 25 | .getIconHeight());↵
|
29 | border.setIcon(icon);↵ | | 26 | border.setIcon(icon);↵
|
30 | firePropertyChange("icon", oldIcon, icon);↵ | | 27 | firePropertyChange("icon", oldIcon, icon);↵
|
31 | }↵ | | 28 | }↵
|
|
| | | 29 | ↵
|
32 | public Icon getIcon() {↵ | | 30 | public Icon getIcon() {↵
|
33 | return icon;↵ | | 31 | return icon;↵
|
34 | }↵ | | 32 | }↵
|
| | | 33 | ↵
|
35 | public JPopupMenu getPopupMenu() {↵ | | 34 | public JPopupMenu getPopupMenu() {↵
|
36 | return popupMenu;↵ | | 35 | return popupMenu;↵
|
37 | }↵ | | 36 | }↵
|
|
| | | 37 | ↵
|
38 | public Dimension getPreferredSize() {↵ | | 38 | public Dimension getPreferredSize() {↵
|
39 | Dimension size = super.getPreferredSize();↵ | | 39 | Dimension size = super.getPreferredSize();↵
|
40 | Insets insets = getInsets();↵ | | 40 | Insets insets = getInsets();↵
|
41 | Insets margin = getMargin();↵ | | 41 | Insets margin = getMargin();↵
|
42 | FontMetrics fm = getFontMetrics(getFont());↵ | | 42 | FontMetrics fm = getFontMetrics(getFont());↵
|
43 | size.height = Math.max(fm.getHeight(), icon.getIconHeight())↵ | | 43 | size.height = Math.max(fm.getHeight(), icon.getIconHeight())↵
|
44 | + insets.top + insets.bottom;↵ | | 44 | + insets.top + insets.bottom;↵
|
45 | return size;↵ | | 45 | return size;↵
|
46 | }↵ | | 46 | ↵
|
|
| | | 47 | }↵
|
| | | 48 | ↵
|
47 | private class MouseHandler extends MouseInputAdapter {↵ | | 49 | private class MouseHandler extends MouseInputAdapter {↵
|
48 | public void mouseMoved(MouseEvent e) {↵ | | 50 | public void mouseMoved(MouseEvent e) {↵
|
49 | if (iconBounds.contains(e.getPoint())) {↵ | | 51 | if(iconBounds.contains(e.getPoint())) {↵
|
50 | setCursor(Cursor.getDefaultCursor());↵ | | 52 | setCursor(Cursor.getDefaultCursor());↵
|
51 | } else {↵ | | 53 | } else {↵
|
52 | setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));↵ | | 54 | setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));↵
|
53 | }↵ | | 55 | }↵
|
54 | }↵ | | 56 | }↵
|
|
55 | public void mouseClicked(MouseEvent e) {↵ | | 57 | public void mouseClicked(MouseEvent e) {↵
|
56 | if (iconBounds.contains(e.getPoint())) {↵ | | 58 | if(iconBounds.contains(e.getPoint())) {↵
|
57 | if (popupMenu.isVisible()) {↵ | | 59 | if(popupMenu.isVisible()) {↵
|
58 | popupMenu.setVisible(false);↵ | | 60 | popupMenu.setVisible(false);↵
|
59 | } else {↵ | | 61 | } else {↵
|
60 | popupMenu.show(IconTextField.this, 0, getHeight());↵ | | 62 | popupMenu.show(IconTextField.this, 0, getHeight());↵
|
61 | ↵ | | 63 | }↵
|
62 | }↵ | | 64 | }↵
|
63 | }↵ | | 65 | }↵
|
64 | }↵ | | 66 | }↵
|
65 | }↵ | | 67 | ↵
|
|
| | | 68 | ↵
|
66 | private class IconifiedBorder extends AbstractBorder {↵ | | 69 | private class IconifiedBorder extends AbstractBorder {↵
|
67 | private Border innerBorder;↵ | | 70 | private Border innerBorder;↵
|
|
68 | private Icon icon;↵ | | 71 | private Icon icon;↵
|
|
69 | private Insets insets;↵ | | 72 | private Insets insets;↵
|
|
70 | private int spacing;↵ | | 73 | private int spacing;↵
|
|
| | | 74 | ↵
|
71 | public IconifiedBorder(Border innerBorder, Icon icon, int spacing) {↵ | | 75 | public IconifiedBorder(Border innerBorder, Icon icon, int spacing) {↵
|
72 | this.innerBorder = innerBorder;↵ | | 76 | this.innerBorder = innerBorder;↵
|
73 | this.icon = icon;↵ | | 77 | this.icon = icon;↵
|
74 | this.spacing = spacing;↵ | | 78 | this.spacing = spacing;↵
|
75 | }↵ | | 79 | }↵
|
|
| | | 80 | ↵
|
76 | public void setIcon(Icon ico) {↵ | | 81 | public void setIcon(Icon ico) {↵
|
77 | this.icon = ico;↵ | | 82 | this.icon = ico;↵
|
78 | insets = null;↵ | | 83 | insets = null;↵
|
79 | }↵ | | 84 | }↵
|
|
| | | 85 | ↵
|
80 | public Border getInnerBorder() {↵ | | 86 | public Border getInnerBorder() {↵
|
81 | return innerBorder;↵ | | 87 | return innerBorder;↵
|
82 | }↵ | | 88 | }↵
|
|
83 | public void paintBorder(Component c, Graphics g, int x, int y,↵ | | 89 | public void paintBorder(Component c, Graphics g, int x, int y,↵
|
84 | int width, int height) {↵ | | 90 | int width, int height) {↵
|
85 | innerBorder.paintBorder(c, g, x, y, width, height);↵ | | 91 | innerBorder.paintBorder(c, g, x, y, width, height);↵
|
86 | Insets innerInsets = innerBorder.getBorderInsets(c);↵ | | 92 | Insets innerInsets = innerBorder.getBorderInsets(c);↵
|
87 | iconBounds.x = x + innerInsets.left + spacing;↵ | | 93 | iconBounds.x = x + innerInsets.left + spacing;↵
|
88 | iconBounds.y = y + innerInsets.top + spacing;↵ | | 94 | iconBounds.y = y + innerInsets.top + spacing;↵
|
89 | icon.paintIcon(c, g, iconBounds.x, iconBounds.y);↵ | | 95 | icon.paintIcon(c, g, iconBounds.x, iconBounds.y);↵
|
90 | | | 96 |
|