gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.spi / src / main / java / org / gvsig / tools / dynform / spi / dynformfield / JCustomTextField.java @ 2872
History | View | Annotate | Download (2.87 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.dynform.spi.dynformfield; |
25 |
|
26 |
import java.awt.Color; |
27 |
|
28 |
import javax.swing.Action; |
29 |
import javax.swing.JPopupMenu; |
30 |
import javax.swing.JTextField; |
31 |
import javax.swing.UIManager; |
32 |
|
33 |
import org.gvsig.tools.dynform.spi.DynFormSPILocator; |
34 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
35 |
|
36 |
public class JCustomTextField extends JTextField implements SupportPopupMenu{ |
37 |
|
38 |
/**
|
39 |
*
|
40 |
*/
|
41 |
private static final long serialVersionUID = 1575097883238348147L; |
42 |
private boolean hasEditor = true; |
43 |
private String title = null; |
44 |
|
45 |
private static Color backgroundColor = null; |
46 |
|
47 |
public JCustomTextField(String title){ |
48 |
super();
|
49 |
if( backgroundColor == null ) { |
50 |
backgroundColor = UIManager.getLookAndFeel().getDefaults().getColor("TextField.background"); |
51 |
} |
52 |
this.title = title;
|
53 |
initContextMenu(); |
54 |
} |
55 |
|
56 |
public JCustomTextField(String title, boolean withEditor){ |
57 |
super();
|
58 |
this.title = title;
|
59 |
this.hasEditor = withEditor;
|
60 |
initContextMenu(); |
61 |
} |
62 |
|
63 |
private void setActionEnabled(String name, boolean enabled) { |
64 |
Action action = this.getActionMap().get(name); |
65 |
if( action != null ) { |
66 |
action.setEnabled(enabled); |
67 |
} |
68 |
} |
69 |
|
70 |
public void setEnabled(boolean enabled){ |
71 |
setActionEnabled("Cut", enabled);
|
72 |
setActionEnabled("Paste", enabled);
|
73 |
super.setEnabled(enabled);
|
74 |
} |
75 |
|
76 |
|
77 |
public void setEditable(boolean enabled){ |
78 |
setActionEnabled("Cut", enabled);
|
79 |
setActionEnabled("Paste", enabled);
|
80 |
super.setEditable(enabled);
|
81 |
this.setBackground(backgroundColor);
|
82 |
} |
83 |
|
84 |
private void initContextMenu() { |
85 |
ToolsSwingLocator.getToolsSwingManager().setDefaultPopupMenu(this, title, hasEditor);
|
86 |
} |
87 |
|
88 |
public JPopupMenu getJPopupMenu(){ |
89 |
return this.getComponentPopupMenu(); |
90 |
} |
91 |
|
92 |
public void addActionToPopupMenu(String name, Action action){ |
93 |
if(name != null && !name.isEmpty()){ |
94 |
action.putValue(Action.NAME, name);
|
95 |
} |
96 |
getJPopupMenu().add(action); |
97 |
} |
98 |
|
99 |
public void addSeparatorToPopupMenu(){ |
100 |
getJPopupMenu().addSeparator(); |
101 |
} |
102 |
|
103 |
} |