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 @ 2491
History | View | Annotate | Download (5.17 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 |
public static void addClearButton(final JComponent component, final ActionListener action) { |
58 |
final JTextComponent text = getTextComponent(component); |
59 |
JPanel panel = new JPanel(); |
60 |
final JButton btnClear = new JButton(getIcon("picker-cleartext")); |
61 |
panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); |
62 |
btnClear.setBorderPainted(false);
|
63 |
btnClear.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 2)); |
64 |
btnClear.setBorderPainted(false);
|
65 |
btnClear.setFocusPainted(false);
|
66 |
btnClear.setContentAreaFilled(false);
|
67 |
btnClear.setOpaque(false);
|
68 |
btnClear.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
69 |
btnClear.setName(CLEAR_BUTTON_NAME); |
70 |
if (action == null) { |
71 |
btnClear.addActionListener((ActionEvent e) -> {
|
72 |
text.setText("");
|
73 |
}); |
74 |
} else {
|
75 |
btnClear.addActionListener(action); |
76 |
} |
77 |
panel.setOpaque(false);
|
78 |
panel.setLayout(new BorderLayout()); |
79 |
panel.add(btnClear, BorderLayout.NORTH);
|
80 |
text.setLayout(new BorderLayout()); |
81 |
text.add(panel, BorderLayout.EAST);
|
82 |
// try {
|
83 |
// Insets originalMargin = text.getMargin();
|
84 |
// text.setMargin(
|
85 |
// new Insets(
|
86 |
// originalMargin.top,
|
87 |
// originalMargin.left,
|
88 |
// originalMargin.bottom,
|
89 |
// panel.getPreferredSize().width
|
90 |
// )
|
91 |
// );
|
92 |
// } catch(Exception ex) {
|
93 |
// ex.getCause(); // To allow set break points
|
94 |
// // Ignore exceptions
|
95 |
// }
|
96 |
text.addPropertyChangeListener((PropertyChangeEvent evt) -> {
|
97 |
if (evt == null) { |
98 |
return;
|
99 |
} |
100 |
if (evt.getPropertyName().equalsIgnoreCase("enabled")) { |
101 |
boolean enabled = text.isEnabled();
|
102 |
btnClear.setEnabled(enabled); |
103 |
} else if (evt.getPropertyName().equalsIgnoreCase("editable")) { |
104 |
boolean editable = text.isEditable();
|
105 |
btnClear.setEnabled(editable); |
106 |
} |
107 |
}); |
108 |
} |
109 |
|
110 |
public static void removeClearButton(final JComponent component) { |
111 |
final JTextComponent text = getTextComponent(component); |
112 |
for (Component aComponent : text.getComponents()) { |
113 |
if (aComponent instanceof JButton |
114 |
&& StringUtils.equalsIgnoreCase(CLEAR_BUTTON_NAME, aComponent.getName())) { |
115 |
text.remove(aComponent); |
116 |
return;
|
117 |
} |
118 |
} |
119 |
} |
120 |
|
121 |
public static JTextComponent getTextComponent(JComponent x) { |
122 |
if( x instanceof JTextComponent ) { |
123 |
return (JTextComponent) x; |
124 |
} |
125 |
if( x instanceof JComboBox ) { |
126 |
JTextComponent text = (JTextComponent) ((JComboBox)x).getEditor().getEditorComponent(); |
127 |
return text;
|
128 |
} |
129 |
if( x instanceof JSpinner ) { |
130 |
JComponent editor = ((JSpinner) x).getEditor(); |
131 |
if( editor instanceof NullableEditor) { |
132 |
return ((NullableEditor) editor).getTextField();
|
133 |
} |
134 |
if( editor instanceof JSpinner.DefaultEditor ) { |
135 |
return ((JSpinner.DefaultEditor) editor).getTextField(); |
136 |
} |
137 |
} |
138 |
return null; |
139 |
} |
140 |
} |