gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / ClearButtonUtils.java @ 2864
History | View | Annotate | Download (6.43 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.tools.swing.impl; |
7 |
|
8 |
import java.awt.BorderLayout; |
9 |
import java.awt.Component; |
10 |
import java.awt.Cursor; |
11 |
import java.awt.event.ActionEvent; |
12 |
import java.awt.event.ActionListener; |
13 |
import java.beans.PropertyChangeEvent; |
14 |
import javax.swing.BorderFactory; |
15 |
import javax.swing.ImageIcon; |
16 |
import javax.swing.JButton; |
17 |
import javax.swing.JComboBox; |
18 |
import javax.swing.JComponent; |
19 |
import javax.swing.JPanel; |
20 |
import javax.swing.JSpinner; |
21 |
import javax.swing.text.JTextComponent; |
22 |
import org.apache.commons.lang3.StringUtils; |
23 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
24 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
25 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
26 |
import org.gvsig.tools.swing.impl.pickercontroller.nullablespinner.NullableEditor; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author jjdelcerro
|
31 |
*/
|
32 |
public class ClearButtonUtils { |
33 |
private static final String CLEAR_BUTTON_NAME = "ClearButton"; |
34 |
|
35 |
private static ImageIcon getIcon(String name) { |
36 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
37 |
IconTheme theme = iconThemeManager.getCurrent(); |
38 |
ImageIcon icon = theme.get(name);
|
39 |
return icon;
|
40 |
} |
41 |
|
42 |
public static boolean hasClearButton(final JComponent component) { |
43 |
final JTextComponent text = getTextComponent(component); |
44 |
for (Component aComponent : text.getComponents()) { |
45 |
if( aComponent instanceof JButton && |
46 |
StringUtils.equalsIgnoreCase(CLEAR_BUTTON_NAME, aComponent.getName()) ) { |
47 |
return true; |
48 |
} |
49 |
} |
50 |
return false; |
51 |
} |
52 |
|
53 |
public static void addClearButton(final JComponent component) { |
54 |
addClearButton(component, null);
|
55 |
} |
56 |
|
57 |
private static class ClearButtonPanel extends JPanel { |
58 |
|
59 |
} |
60 |
|
61 |
public static void addClearButton(final JComponent component, final ActionListener action) { |
62 |
final JTextComponent text = getTextComponent(component); |
63 |
if( text == null ) { |
64 |
return;
|
65 |
} |
66 |
JPanel panel = null; |
67 |
JButton btnClear = null; |
68 |
for (Component aComponent : text.getComponents()) { |
69 |
if (aComponent instanceof ClearButtonPanel ) { |
70 |
panel = (JPanel) aComponent;
|
71 |
btnClear = (JButton) panel.getComponent(0); |
72 |
for (ActionListener l : btnClear.getActionListeners()) { |
73 |
btnClear.removeActionListener(l); |
74 |
} |
75 |
} |
76 |
} |
77 |
if( btnClear==null ) { |
78 |
panel = new ClearButtonPanel();
|
79 |
btnClear = new JButton(getIcon("picker-cleartext")); |
80 |
panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); |
81 |
btnClear.setFocusable(false);
|
82 |
btnClear.setBorderPainted(false);
|
83 |
btnClear.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 2)); |
84 |
btnClear.setBorderPainted(false);
|
85 |
btnClear.setFocusPainted(false);
|
86 |
btnClear.setContentAreaFilled(false);
|
87 |
btnClear.setOpaque(false);
|
88 |
btnClear.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
89 |
btnClear.setName(CLEAR_BUTTON_NAME); |
90 |
panel.setOpaque(false);
|
91 |
panel.setLayout(new BorderLayout()); |
92 |
panel.add(btnClear, BorderLayout.NORTH);
|
93 |
text.setLayout(new BorderLayout()); |
94 |
text.add(panel, BorderLayout.EAST);
|
95 |
// try {
|
96 |
// Insets originalMargin = text.getMargin();
|
97 |
// text.setMargin(
|
98 |
// new Insets(
|
99 |
// originalMargin.top,
|
100 |
// originalMargin.left,
|
101 |
// originalMargin.bottom,
|
102 |
// panel.getPreferredSize().width
|
103 |
// )
|
104 |
// );
|
105 |
// } catch(Exception ex) {
|
106 |
// ex.getCause(); // To allow set break points
|
107 |
// // Ignore exceptions
|
108 |
// }
|
109 |
JButton button = btnClear;
|
110 |
text.addPropertyChangeListener((PropertyChangeEvent evt) -> {
|
111 |
if (evt == null) { |
112 |
return;
|
113 |
} |
114 |
if (evt.getPropertyName().equalsIgnoreCase("enabled")) { |
115 |
boolean enabled = text.isEnabled();
|
116 |
button.setEnabled(enabled); |
117 |
} else if (evt.getPropertyName().equalsIgnoreCase("editable")) { |
118 |
boolean editable = text.isEditable();
|
119 |
button.setEnabled(editable); |
120 |
} |
121 |
}); |
122 |
} |
123 |
if (action == null) { |
124 |
btnClear.addActionListener((ActionEvent e) -> {
|
125 |
if(component instanceof JComboBox){ |
126 |
((JComboBox)component).setSelectedIndex(-1); |
127 |
} else {
|
128 |
text.setText("");
|
129 |
} |
130 |
}); |
131 |
} else {
|
132 |
btnClear.addActionListener(action); |
133 |
} |
134 |
} |
135 |
|
136 |
public static void removeClearButton(final JComponent component) { |
137 |
final JTextComponent text = getTextComponent(component); |
138 |
for (Component aComponent : text.getComponents()) { |
139 |
if (aComponent instanceof ClearButtonPanel ) { |
140 |
JPanel panel = (JPanel) aComponent; |
141 |
JButton btnClear = (JButton) panel.getComponent(0); |
142 |
for (ActionListener l : btnClear.getActionListeners()) { |
143 |
btnClear.removeActionListener(l); |
144 |
} |
145 |
text.remove(panel); |
146 |
return;
|
147 |
} |
148 |
} |
149 |
} |
150 |
|
151 |
public static JTextComponent getTextComponent(JComponent x) { |
152 |
if( x instanceof JTextComponent ) { |
153 |
return (JTextComponent) x; |
154 |
} |
155 |
if( x instanceof JComboBox ) { |
156 |
Component xx = ((JComboBox)x).getEditor().getEditorComponent(); |
157 |
if( xx instanceof JTextComponent ) { |
158 |
return (JTextComponent) xx; |
159 |
} |
160 |
return null; |
161 |
} |
162 |
if( x instanceof JSpinner ) { |
163 |
JComponent editor = ((JSpinner) x).getEditor(); |
164 |
if( editor instanceof NullableEditor) { |
165 |
return ((NullableEditor) editor).getTextField();
|
166 |
} |
167 |
if( editor instanceof JSpinner.DefaultEditor ) { |
168 |
return ((JSpinner.DefaultEditor) editor).getTextField(); |
169 |
} |
170 |
} |
171 |
return null; |
172 |
} |
173 |
} |