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 / DefaultToolsSwingManager.java @ 1867
History | View | Annotate | Download (18.6 KB)
1 | 1251 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.tools.swing.impl; |
||
3 | |||
4 | 1758 | jjdelcerro | import java.awt.BorderLayout; |
5 | 1628 | jjdelcerro | import java.awt.Color; |
6 | 1808 | jjdelcerro | import java.awt.Cursor; |
7 | 1530 | fdiaz | import java.awt.Dimension; |
8 | 1698 | jjdelcerro | import java.awt.event.ActionEvent; |
9 | 1758 | jjdelcerro | import java.awt.event.ActionListener; |
10 | 1530 | fdiaz | import java.awt.image.BufferedImage; |
11 | import java.awt.image.WritableRaster; |
||
12 | 1772 | jjdelcerro | import java.beans.PropertyChangeEvent; |
13 | import java.beans.PropertyChangeListener; |
||
14 | 1746 | jjdelcerro | import java.io.File; |
15 | import java.lang.reflect.InvocationTargetException; |
||
16 | import java.lang.reflect.Method; |
||
17 | 1698 | jjdelcerro | import javax.swing.AbstractAction; |
18 | 1640 | jjdelcerro | import javax.swing.AbstractButton; |
19 | 1698 | jjdelcerro | import javax.swing.Action; |
20 | 1424 | jjdelcerro | import javax.swing.ComboBoxModel; |
21 | 1758 | jjdelcerro | import javax.swing.ImageIcon; |
22 | 1610 | jjdelcerro | import javax.swing.JButton; |
23 | 1377 | jjdelcerro | import javax.swing.JComboBox; |
24 | 1746 | jjdelcerro | import javax.swing.JComponent; |
25 | 1640 | jjdelcerro | import javax.swing.JLabel; |
26 | 1377 | jjdelcerro | import javax.swing.JList; |
27 | 1772 | jjdelcerro | import javax.swing.JMenuItem; |
28 | 1698 | jjdelcerro | import javax.swing.JPopupMenu; |
29 | 1750 | jjdelcerro | import javax.swing.JScrollPane; |
30 | 1610 | jjdelcerro | import javax.swing.JSlider; |
31 | 1683 | jjdelcerro | import javax.swing.JTabbedPane; |
32 | 1796 | jjdelcerro | import javax.swing.JTextArea; |
33 | 1610 | jjdelcerro | import javax.swing.JTextField; |
34 | 1750 | jjdelcerro | import javax.swing.JViewport; |
35 | 1698 | jjdelcerro | import javax.swing.text.DefaultEditorKit; |
36 | import javax.swing.text.JTextComponent; |
||
37 | 1377 | jjdelcerro | import javax.swing.tree.TreeModel; |
38 | 1644 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
39 | 1640 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
40 | import org.gvsig.tools.i18n.I18nManager; |
||
41 | 1251 | jjdelcerro | import org.gvsig.tools.swing.api.ActionListenerSupport; |
42 | 1746 | jjdelcerro | import org.gvsig.tools.swing.api.ChangeListenerHelper; |
43 | 1610 | jjdelcerro | import org.gvsig.tools.swing.api.ColorChooserController; |
44 | 1377 | jjdelcerro | import org.gvsig.tools.swing.api.JListWithCheckbox; |
45 | 1840 | jjdelcerro | import org.gvsig.tools.swing.api.SimpleImage; |
46 | 1758 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
47 | 1251 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingManager; |
48 | 1746 | jjdelcerro | import org.gvsig.tools.swing.api.pickercontroller.ColorPickerController; |
49 | import org.gvsig.tools.swing.api.pickercontroller.DatePickerController; |
||
50 | import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
||
51 | import org.gvsig.tools.swing.api.pickercontroller.FolderPickerController; |
||
52 | 1758 | jjdelcerro | import org.gvsig.tools.swing.icontheme.IconTheme; |
53 | import org.gvsig.tools.swing.icontheme.IconThemeManager; |
||
54 | 1538 | fdiaz | import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper; |
55 | import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper.VirtualDataBuffer; |
||
56 | 1746 | jjdelcerro | import org.gvsig.tools.swing.impl.pickercontroller.ColorPickerControllerImpl; |
57 | import org.gvsig.tools.swing.impl.pickercontroller.DatePickerControllerImpl; |
||
58 | import org.gvsig.tools.swing.impl.pickercontroller.FilePickerControllerImpl; |
||
59 | import org.gvsig.tools.swing.impl.pickercontroller.FolderPickerControllerImpl; |
||
60 | 1251 | jjdelcerro | |
61 | |||
62 | public class DefaultToolsSwingManager implements ToolsSwingManager { |
||
63 | |||
64 | 1530 | fdiaz | private Dimension maxPhysicalSizeOfBufferedImage; |
65 | |||
66 | 1377 | jjdelcerro | @Override
|
67 | 1251 | jjdelcerro | public ActionListenerSupport createActionListenerSupport() {
|
68 | return new DefaultActionListenerSupport(); |
||
69 | } |
||
70 | 1377 | jjdelcerro | |
71 | @Override
|
||
72 | public JListWithCheckbox createJListWithCheckbox(JList wrappedList) { |
||
73 | return new DefaultJListWithCheckbox(wrappedList); |
||
74 | } |
||
75 | |||
76 | @Override
|
||
77 | public void setTreeModel(JComboBox comboBox, TreeModel aTreeModel) { |
||
78 | TreeComboUtils.setTreeModel(comboBox, aTreeModel); |
||
79 | } |
||
80 | 1530 | fdiaz | |
81 | 1538 | fdiaz | @Override
|
82 | 1424 | jjdelcerro | public ComboBoxModel createComboBoxModel(TreeModel treeModel) { |
83 | return TreeComboUtils.createComboBoxModel(treeModel);
|
||
84 | 1530 | fdiaz | } |
85 | |||
86 | |||
87 | 1538 | fdiaz | @Override
|
88 | 1530 | fdiaz | public BufferedImage createBufferedImage(int w, int h, int type) { |
89 | if(getMaxPhysicalSizeOfBufferedImage().getWidth() < w || getMaxPhysicalSizeOfBufferedImage().getHeight() < h){
|
||
90 | return createVirtualBufferedImage(w,h,type);
|
||
91 | } |
||
92 | return new BufferedImage(w,h,type); |
||
93 | } |
||
94 | |||
95 | 1538 | fdiaz | @Override
|
96 | 1530 | fdiaz | public BufferedImage createVirtualBufferedImage(int w, int h, int type) { |
97 | 1538 | fdiaz | return VirtualBufferedImageHelper.createVirtualBufferedImage(w, h, type);
|
98 | 1530 | fdiaz | } |
99 | |||
100 | 1538 | fdiaz | @Override
|
101 | 1530 | fdiaz | public BufferedImage copyBufferedImage(BufferedImage img) { |
102 | WritableRaster sourceRaster = img.getRaster();
|
||
103 | |||
104 | BufferedImage newImage;
|
||
105 | 1538 | fdiaz | if(sourceRaster.getDataBuffer() instanceof VirtualDataBuffer ){ |
106 | newImage = VirtualBufferedImageHelper.createVirtualBufferedImage( |
||
107 | 1692 | fdiaz | img.getWidth(), |
108 | img.getHeight(), |
||
109 | img.getSampleModel(), |
||
110 | 1538 | fdiaz | img.getColorModel() |
111 | ); |
||
112 | 1530 | fdiaz | } else {
|
113 | newImage = createBufferedImage(img.getWidth(), img.getHeight(), img.getType()); |
||
114 | } |
||
115 | |||
116 | WritableRaster raster = newImage.getRaster();
|
||
117 | img.copyData(raster); |
||
118 | return newImage;
|
||
119 | } |
||
120 | |||
121 | @Override
|
||
122 | public void setMaxPhysicalSizeOfBufferedImage(Dimension dimension) { |
||
123 | this.maxPhysicalSizeOfBufferedImage = dimension;
|
||
124 | } |
||
125 | |||
126 | @Override
|
||
127 | public Dimension getMaxPhysicalSizeOfBufferedImage() { |
||
128 | if(this.maxPhysicalSizeOfBufferedImage == null){ |
||
129 | this.maxPhysicalSizeOfBufferedImage = new Dimension(2000, 2000); |
||
130 | } |
||
131 | return this.maxPhysicalSizeOfBufferedImage; |
||
132 | } |
||
133 | |||
134 | 1610 | jjdelcerro | @Override
|
135 | public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
||
136 | return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha, allowNull); |
||
137 | } |
||
138 | 1530 | fdiaz | |
139 | 1610 | jjdelcerro | @Override
|
140 | public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog) { |
||
141 | return new DefaultColorChooserController(txtLabel, btnShowDialog); |
||
142 | } |
||
143 | |||
144 | @Override
|
||
145 | public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
||
146 | return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha); |
||
147 | } |
||
148 | |||
149 | 1628 | jjdelcerro | @Override
|
150 | public Color alphaBlendingWithOpaqueBackground(Color bgColor, Color fgColor) { |
||
151 | 1632 | jjdelcerro | // https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
|
152 | 1628 | jjdelcerro | Color src = fgColor;
|
153 | Color dst = bgColor;
|
||
154 | |||
155 | // double outa = 1;
|
||
156 | double srca = src.getAlpha() / 255.0; |
||
157 | double srca_1 = (1 - srca); |
||
158 | |||
159 | Color color = new Color( |
||
160 | (int)(src.getRed() * srca + dst.getRed() * srca_1) & 0xff, |
||
161 | (int)(src.getGreen()* srca + dst.getGreen()* srca_1) & 0xff, |
||
162 | (int)(src.getBlue() * srca + dst.getBlue() * srca_1) & 0xff |
||
163 | ); |
||
164 | 1692 | fdiaz | return color;
|
165 | 1628 | jjdelcerro | } |
166 | 1692 | fdiaz | |
167 | 1632 | jjdelcerro | @Override
|
168 | public Color opaqueColor(Color src) { |
||
169 | // https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
|
||
170 | double srca = src.getAlpha() / 255.0; |
||
171 | double srca_1 = (1 - srca); |
||
172 | |||
173 | Color color = new Color( |
||
174 | (int)(src.getRed() * srca + 255 * srca_1) & 0xff, |
||
175 | (int)(src.getGreen()* srca + 255 * srca_1) & 0xff, |
||
176 | (int)(src.getBlue() * srca + 255 * srca_1) & 0xff |
||
177 | ); |
||
178 | return color;
|
||
179 | } |
||
180 | 1640 | jjdelcerro | |
181 | 1746 | jjdelcerro | private Object call(Object obj, String name, Object... params) { |
182 | Class[] types = null; |
||
183 | if( params != null ) { |
||
184 | types = new Class[params.length]; |
||
185 | for (int i = 0; i < params.length; i++) { |
||
186 | types[i] = params[i].getClass(); |
||
187 | } |
||
188 | } |
||
189 | Method method;
|
||
190 | try {
|
||
191 | method = obj.getClass().getMethod(name, types); |
||
192 | Object r = method.invoke(obj, params);
|
||
193 | return r;
|
||
194 | } catch (NoSuchMethodException | |
||
195 | SecurityException |
|
||
196 | IllegalAccessException |
|
||
197 | IllegalArgumentException |
|
||
198 | InvocationTargetException ex) {
|
||
199 | } |
||
200 | return null; |
||
201 | } |
||
202 | |||
203 | 1640 | jjdelcerro | @Override
|
204 | 1746 | jjdelcerro | public void translate(JComponent component) { |
205 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
206 | String s = (String) call(component, "getText", (Object[]) null); |
||
207 | if( !StringUtils.isEmpty(s) ) {
|
||
208 | call(component,"setText",i18n.getTranslation(s));
|
||
209 | } |
||
210 | s = (String) call(component, "getToolTipText", (Object[]) null); |
||
211 | if( !StringUtils.isEmpty(s) ) {
|
||
212 | call(component,"setToolTipText",i18n.getTranslation(s));
|
||
213 | } |
||
214 | } |
||
215 | |||
216 | @Override
|
||
217 | 1640 | jjdelcerro | public void translate(AbstractButton component) { |
218 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
219 | 1644 | jjdelcerro | String s = component.getText();
|
220 | if( !StringUtils.isEmpty(s) ) {
|
||
221 | component.setText(i18n.getTranslation(s)); |
||
222 | } |
||
223 | s = component.getToolTipText(); |
||
224 | if( !StringUtils.isEmpty(s) ) {
|
||
225 | 1683 | jjdelcerro | component.setToolTipText(i18n.getTranslation(s)); |
226 | 1644 | jjdelcerro | } |
227 | 1640 | jjdelcerro | } |
228 | |||
229 | @Override
|
||
230 | public void translate(JLabel component) { |
||
231 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
232 | 1644 | jjdelcerro | String s = component.getText();
|
233 | if( !StringUtils.isEmpty(s) ) {
|
||
234 | component.setText(i18n.getTranslation(s)); |
||
235 | } |
||
236 | s = component.getToolTipText(); |
||
237 | if( !StringUtils.isEmpty(s) ) {
|
||
238 | 1683 | jjdelcerro | component.setToolTipText(i18n.getTranslation(s)); |
239 | 1644 | jjdelcerro | } |
240 | 1640 | jjdelcerro | } |
241 | 1692 | fdiaz | |
242 | 1746 | jjdelcerro | @Override
|
243 | 1683 | jjdelcerro | public void translate(JTabbedPane component) { |
244 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
245 | 1692 | fdiaz | |
246 | 1683 | jjdelcerro | for( int i=0; i<component.getTabCount(); i++) { |
247 | String text = component.getTitleAt(i);
|
||
248 | if( !StringUtils.isEmpty(text) ) {
|
||
249 | component.setTitleAt(i, i18n.getTranslation(text)); |
||
250 | } |
||
251 | text = component.getToolTipTextAt(i); |
||
252 | if( !StringUtils.isEmpty(text) ) {
|
||
253 | component.setToolTipTextAt(i, i18n.getTranslation(text)); |
||
254 | } |
||
255 | } |
||
256 | 1692 | fdiaz | } |
257 | 1698 | jjdelcerro | |
258 | @Override
|
||
259 | public void setDefaultPopupMenu(final JTextComponent component) { |
||
260 | this.setDefaultPopupMenu(component, null); |
||
261 | } |
||
262 | |||
263 | @Override
|
||
264 | public void setDefaultPopupMenu(final JTextComponent component, String title) { |
||
265 | JPopupMenu popupMenu = new JPopupMenu(); |
||
266 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
267 | |||
268 | Action copyAction = component.getActionMap().get(DefaultEditorKit.copyAction); |
||
269 | Action cutAction = component.getActionMap().get(DefaultEditorKit.cutAction); |
||
270 | Action pasteAction = component.getActionMap().get(DefaultEditorKit.pasteAction); |
||
271 | Action selectAllAction = component.getActionMap().get(DefaultEditorKit.selectAllAction); |
||
272 | |||
273 | if (copyAction == null |
||
274 | && cutAction == null
|
||
275 | && pasteAction == null
|
||
276 | && selectAllAction == null) {
|
||
277 | copyAction = component.getActionMap().get(i18nManager.getTranslation("copy"));
|
||
278 | cutAction = component.getActionMap().get(i18nManager.getTranslation("cut"));
|
||
279 | pasteAction = component.getActionMap().get(i18nManager.getTranslation("paste"));
|
||
280 | selectAllAction = component.getActionMap().get(i18nManager.getTranslation("SelectAll"));
|
||
281 | } else {
|
||
282 | copyAction.putValue(Action.NAME, i18nManager.getTranslation("copy")); |
||
283 | cutAction.putValue(Action.NAME, i18nManager.getTranslation("cut")); |
||
284 | pasteAction.putValue(Action.NAME, i18nManager.getTranslation("paste")); |
||
285 | selectAllAction.putValue(Action.NAME, i18nManager.getTranslation("SelectAll")); |
||
286 | } |
||
287 | |||
288 | final String title2; |
||
289 | if( title == null ) { |
||
290 | title2 = i18nManager.getTranslation("text_editor");
|
||
291 | } else {
|
||
292 | title2 = title; |
||
293 | } |
||
294 | Action textEditorAction = new AbstractAction(i18nManager.getTranslation("text_editor")) { |
||
295 | @Override
|
||
296 | public void actionPerformed(ActionEvent e) { |
||
297 | 1867 | jjdelcerro | DefaultZoomDialog dialog = new DefaultZoomDialog(component, title2, component.getText());
|
298 | 1698 | jjdelcerro | dialog.setEditable(component.isEditable()); |
299 | dialog.setAlwaysOnTop(true);
|
||
300 | dialog.setVisible(true);
|
||
301 | if (component.isEditable() && component.isEnabled()) {
|
||
302 | component.setText(dialog.getText()); |
||
303 | } |
||
304 | } |
||
305 | }; |
||
306 | 1772 | jjdelcerro | final JMenuItem menuCut = new JMenuItem(cutAction); |
307 | final JMenuItem menuPaste = new JMenuItem(pasteAction); |
||
308 | |||
309 | 1698 | jjdelcerro | popupMenu.add(textEditorAction); |
310 | popupMenu.addSeparator(); |
||
311 | |||
312 | 1772 | jjdelcerro | popupMenu.add(menuCut); |
313 | 1698 | jjdelcerro | popupMenu.add(copyAction); |
314 | 1772 | jjdelcerro | popupMenu.add(menuPaste); |
315 | 1698 | jjdelcerro | popupMenu.add(selectAllAction); |
316 | 1772 | jjdelcerro | |
317 | if( component.isEnabled() && component.isEditable() ) {
|
||
318 | menuCut.setEnabled(true);
|
||
319 | menuPaste.setEnabled(true);
|
||
320 | } else {
|
||
321 | menuCut.setEnabled(false);
|
||
322 | menuPaste.setEnabled(false);
|
||
323 | } |
||
324 | component.addPropertyChangeListener(new PropertyChangeListener() { |
||
325 | @Override
|
||
326 | public void propertyChange(PropertyChangeEvent evt) { |
||
327 | if( evt==null ) { |
||
328 | return;
|
||
329 | } |
||
330 | if( evt.getPropertyName().equalsIgnoreCase("enabled") ) { |
||
331 | boolean enabled = component.isEnabled();
|
||
332 | menuCut.setEnabled(enabled); |
||
333 | menuPaste.setEnabled(enabled); |
||
334 | } else if( evt.getPropertyName().equalsIgnoreCase("editable") ) { |
||
335 | boolean editable = component.isEditable();
|
||
336 | menuCut.setEnabled(editable); |
||
337 | menuPaste.setEnabled(editable); |
||
338 | } |
||
339 | } |
||
340 | }); |
||
341 | 1698 | jjdelcerro | component.setComponentPopupMenu(popupMenu); |
342 | } |
||
343 | |||
344 | @Override
|
||
345 | public void setDefaultPopupMenu(JComboBox component) { |
||
346 | this.setDefaultPopupMenu((JTextComponent) component.getEditor().getEditorComponent(), null); |
||
347 | } |
||
348 | |||
349 | |||
350 | @Override
|
||
351 | public void setDefaultPopupMenu(JComboBox component, String title) { |
||
352 | this.setDefaultPopupMenu((JTextComponent) component.getEditor().getEditorComponent(), title); |
||
353 | } |
||
354 | 1746 | jjdelcerro | |
355 | @Override
|
||
356 | public ChangeListenerHelper createChangeListenerHelper() {
|
||
357 | DefaultChangeListenerHelper x = new DefaultChangeListenerHelper();
|
||
358 | return x;
|
||
359 | } |
||
360 | |||
361 | @Override
|
||
362 | public ColorPickerController createColorPickerController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
||
363 | return new ColorPickerControllerImpl(txtLabel, btnShowDialog, sldAlpha, allowNull); |
||
364 | } |
||
365 | |||
366 | @Override
|
||
367 | public ColorPickerController createColorPickerController(JTextField txtLabel, JButton btnShowDialog) { |
||
368 | return new ColorPickerControllerImpl(txtLabel, btnShowDialog); |
||
369 | } |
||
370 | |||
371 | @Override
|
||
372 | public ColorPickerController createColorPickerController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
||
373 | return new ColorPickerControllerImpl(txtLabel, btnShowDialog, sldAlpha); |
||
374 | } |
||
375 | |||
376 | @Override
|
||
377 | public DatePickerController createDatePickerController(JTextField txtDate, JButton btnDate) { |
||
378 | return new DatePickerControllerImpl(txtDate, btnDate); |
||
379 | } |
||
380 | |||
381 | @Override
|
||
382 | public FilePickerController createFilePickerController(JTextField txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) { |
||
383 | return new FilePickerControllerImpl(txtFile, btnFile, dialogTitle, fileChooserID, initialPath, seticon); |
||
384 | } |
||
385 | |||
386 | @Override
|
||
387 | public FilePickerController createFilePickerController(JTextField txtFile, JButton btnFile) { |
||
388 | return new FilePickerControllerImpl(txtFile, btnFile); |
||
389 | } |
||
390 | |||
391 | @Override
|
||
392 | public FilePickerController createFilePickerController(JTextField txtFile, JButton btnFile, String dialogTitle) { |
||
393 | return new FilePickerControllerImpl(txtFile, btnFile, dialogTitle); |
||
394 | } |
||
395 | |||
396 | @Override
|
||
397 | public FolderPickerController createFolderPickerController(JTextField txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) { |
||
398 | return new FolderPickerControllerImpl(txtFile, btnFile, dialogTitle, fileChooserID, initialPath, seticon); |
||
399 | } |
||
400 | |||
401 | @Override
|
||
402 | public FolderPickerController createFolderPickerController(JTextField txtFile, JButton btnFile) { |
||
403 | return new FolderPickerControllerImpl(txtFile, btnFile); |
||
404 | } |
||
405 | |||
406 | @Override
|
||
407 | public FolderPickerController createFolderPickerController(JTextField txtFile, JButton btnFile, String dialogTitle) { |
||
408 | return new FolderPickerControllerImpl(txtFile, btnFile,dialogTitle); |
||
409 | } |
||
410 | 1750 | jjdelcerro | |
411 | @Override
|
||
412 | public void removeBorder(JComponent component) { |
||
413 | try {
|
||
414 | component.setBorder(null);
|
||
415 | JComponent p1 = (JComponent) component.getParent(); |
||
416 | if( p1 instanceof JViewport ) { |
||
417 | p1 = (JComponent) component.getParent();
|
||
418 | } |
||
419 | if( p1 instanceof JScrollPane ) { |
||
420 | JScrollPane scrollPanel = (JScrollPane) component.getParent(); |
||
421 | scrollPanel.setBorder(null);
|
||
422 | } |
||
423 | } catch(Exception ex) { |
||
424 | |||
425 | } |
||
426 | } |
||
427 | 1758 | jjdelcerro | |
428 | protected ImageIcon getIcon(String name) { |
||
429 | IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
||
430 | IconTheme theme = iconThemeManager.getCurrent(); |
||
431 | ImageIcon icon = theme.get(name);
|
||
432 | return icon;
|
||
433 | } |
||
434 | |||
435 | @Override
|
||
436 | 1796 | jjdelcerro | public void addClearButton(JTextComponent text) { |
437 | 1758 | jjdelcerro | this.addClearButton(text, null); |
438 | } |
||
439 | |||
440 | @Override
|
||
441 | 1862 | jjdelcerro | public void addClearButton(JComboBox combo) { |
442 | try {
|
||
443 | JTextComponent text = (JTextComponent) combo.getEditor().getEditorComponent(); |
||
444 | this.addClearButton(text, null); |
||
445 | } catch(Throwable th) { |
||
446 | // Si no puede a?adir el boton no hace nada.
|
||
447 | } |
||
448 | } |
||
449 | |||
450 | @Override
|
||
451 | 1796 | jjdelcerro | public void addClearButton(final JTextComponent text, final ActionListener action) { |
452 | 1772 | jjdelcerro | final JButton btnClear = new JButton(getIcon("picker-cleartext")); |
453 | 1758 | jjdelcerro | btnClear.setBorderPainted( false );
|
454 | btnClear.setFocusPainted( false );
|
||
455 | btnClear.setContentAreaFilled( false );
|
||
456 | btnClear.setOpaque(false);
|
||
457 | 1808 | jjdelcerro | btnClear.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
458 | 1758 | jjdelcerro | if( action == null ) { |
459 | btnClear.addActionListener(new ActionListener() { |
||
460 | @Override
|
||
461 | public void actionPerformed(ActionEvent e) { |
||
462 | text.setText("");
|
||
463 | } |
||
464 | }); |
||
465 | } else {
|
||
466 | btnClear.addActionListener(action); |
||
467 | } |
||
468 | text.setLayout(new BorderLayout()); |
||
469 | text.add(btnClear, BorderLayout.EAST);
|
||
470 | 1772 | jjdelcerro | text.addPropertyChangeListener(new PropertyChangeListener() { |
471 | @Override
|
||
472 | public void propertyChange(PropertyChangeEvent evt) { |
||
473 | 1862 | jjdelcerro | if( evt==null ) { |
474 | return;
|
||
475 | } |
||
476 | if( evt.getPropertyName().equalsIgnoreCase("enabled") ) { |
||
477 | 1772 | jjdelcerro | boolean enabled = text.isEnabled();
|
478 | btnClear.setEnabled(enabled); |
||
479 | 1862 | jjdelcerro | } else if( evt.getPropertyName().equalsIgnoreCase("editable") ) { |
480 | boolean editable = text.isEditable();
|
||
481 | btnClear.setEnabled(editable); |
||
482 | 1772 | jjdelcerro | } |
483 | } |
||
484 | }); |
||
485 | 1758 | jjdelcerro | } |
486 | 1840 | jjdelcerro | |
487 | @Override
|
||
488 | public SimpleImage createSimpleImage() {
|
||
489 | SimpleImage x = new DefaultSimpleImage();
|
||
490 | return x;
|
||
491 | } |
||
492 | |||
493 | @Override
|
||
494 | public SimpleImage createSimpleImage(Object source) { |
||
495 | SimpleImage x = new DefaultSimpleImage(source);
|
||
496 | return x;
|
||
497 | } |
||
498 | |||
499 | 1251 | jjdelcerro | } |