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 / JCustomTextArea.java @ 969
History | View | Annotate | Download (3.32 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 |
import java.awt.event.ActionEvent; |
28 |
|
29 |
import javax.swing.AbstractAction; |
30 |
import javax.swing.Action; |
31 |
import javax.swing.JPopupMenu; |
32 |
import javax.swing.JTextArea; |
33 |
import javax.swing.UIManager; |
34 |
|
35 |
import org.gvsig.tools.dynform.spi.DynFormSPILocator; |
36 |
|
37 |
public class JCustomTextArea extends JTextArea implements SupportPopupMenu { |
38 |
|
39 |
/**
|
40 |
*
|
41 |
*/
|
42 |
private static final long serialVersionUID = -6180702611660178166L; |
43 |
private JPopupMenu popupMenu = null; |
44 |
private boolean hasEditor = true; |
45 |
private static Color backgroundColor = null; |
46 |
private String title = null; |
47 |
|
48 |
public JCustomTextArea(String title){ |
49 |
this(title, true); |
50 |
} |
51 |
|
52 |
public JCustomTextArea(String title, boolean withEditor){ |
53 |
super();
|
54 |
this.title = title;
|
55 |
this.hasEditor = withEditor;
|
56 |
if( backgroundColor == null ) { |
57 |
backgroundColor = UIManager.getLookAndFeel().getDefaults().getColor("TextField.background"); |
58 |
} |
59 |
initComponent(); |
60 |
} |
61 |
|
62 |
private void initComponent() { |
63 |
this.add(getJPopupMenu());
|
64 |
this.setComponentPopupMenu(getJPopupMenu());
|
65 |
this.addSeparatorToPopupMenu();
|
66 |
ObservableAction sact = new DefaultObservableAction(new AbstractAction() { |
67 |
public void actionPerformed(ActionEvent arg0) { |
68 |
toggleWrapMode(); |
69 |
} |
70 |
}); |
71 |
this.addActionToPopupMenu("Toggle wrap mode", sact); |
72 |
} |
73 |
|
74 |
private void toggleWrapMode() { |
75 |
this.setLineWrap(!this.getLineWrap() ); |
76 |
} |
77 |
|
78 |
private void setActionEnabled(String name, boolean enabled) { |
79 |
Action action = this.getActionMap().get(name); |
80 |
if( action != null ) { |
81 |
action.setEnabled(enabled); |
82 |
} |
83 |
} |
84 |
|
85 |
public void setEnabled(boolean enabled){ |
86 |
setActionEnabled("Cut", enabled);
|
87 |
setActionEnabled("Paste", enabled);
|
88 |
super.setEnabled(enabled);
|
89 |
} |
90 |
|
91 |
|
92 |
public void setEditable(boolean enabled){ |
93 |
setActionEnabled("Cut", enabled);
|
94 |
setActionEnabled("Paste", enabled);
|
95 |
super.setEditable(enabled);
|
96 |
this.setBackground(backgroundColor);
|
97 |
} |
98 |
|
99 |
public JPopupMenu getJPopupMenu(){ |
100 |
if(this.popupMenu == null){ |
101 |
this.popupMenu = DynFormSPILocator.getDynFormSPIManager().createTextFieldPopupMenu(title, this, hasEditor); |
102 |
} |
103 |
return this.popupMenu; |
104 |
} |
105 |
|
106 |
public void addActionToPopupMenu(String name, ObservableAction action){ |
107 |
if(name != null && !name.isEmpty()){ |
108 |
action.getAction().putValue(Action.NAME, name);
|
109 |
} |
110 |
getJPopupMenu().add(action); |
111 |
} |
112 |
|
113 |
public void addSeparatorToPopupMenu(){ |
114 |
getJPopupMenu().addSeparator(); |
115 |
} |
116 |
|
117 |
} |