1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.telscenter.pas.common.ui.panel;
24
25 import java.awt.BorderLayout;
26 import java.awt.Color;
27 import java.awt.Cursor;
28 import java.awt.Dimension;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31
32 import javax.swing.BorderFactory;
33 import javax.swing.ImageIcon;
34 import javax.swing.JButton;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37 import javax.swing.JTextArea;
38 import javax.swing.ScrollPaneConstants;
39 import javax.swing.border.Border;
40 import javax.swing.text.JTextComponent;
41
42 import org.telscenter.pas.common.ui.CommonUI;
43 import org.telscenter.pas.ui.icons.PasCommonIconProvider;
44
45 /***
46 * @author aperritano
47 *
48 */
49 public class TextEditorPanel extends JPanel {
50
51 /***
52 *
53 */
54 public static final String TEXT_EDITOR_PANEL = "TextEditorPanel";
55
56 public static final String MODE = "MODE";
57
58 public static final String SAVE_MODE = "SAVE";
59
60 public static final long serialVersionUID = 1L;
61
62 public static final String EDIT_MODE = "EDIT";
63
64 public static final String CREATE_Label = "Create";
65
66 public static final String EDIT_LABEL = "Edit";
67
68 public static final String EDIT_BUTTON = "EDIT_BUTTON";
69
70 public static final String TEXT_COMP = "TEXT_COMP";
71
72 public static final String EDIT_PANEL = "EDIT_PANEL";
73
74 protected Dimension dim;
75
76 protected JTextComponent textComponent;
77
78 protected ImageIcon editIcon;
79
80 protected ImageIcon saveIcon;
81
82 protected Border whiteBorder;
83
84 protected Border blackBorder;
85
86 protected JButton editButton;
87
88 private String editButtonName = "Edit";
89
90 private String saveButtonName = "Save";
91 /***
92 *
93 */
94 public TextEditorPanel(JTextComponent textComponent, Dimension dim) {
95
96 this.textComponent = textComponent;
97 this.dim = dim;
98 init();
99 }
100
101 /***
102 *
103 */
104 protected void init() {
105
106 final JScrollPane sp = new JScrollPane(textComponent);
107 editIcon = new ImageIcon(PasCommonIconProvider.getImage("page_edit16.png"));
108 saveIcon = new ImageIcon(PasCommonIconProvider.getImage("page_save16.png"));
109
110 whiteBorder = BorderFactory.createLineBorder(Color.white,
111 1);
112 blackBorder = BorderFactory.createLineBorder(Color.black,
113 1);
114 this.setLayout(new BorderLayout(0, 0));
115
116 textComponent.setToolTipText("To modify this field click the EDIT button.");
117
118 if (textComponent instanceof JTextArea) {
119 textComponent.setBorder(whiteBorder);
120 ((JTextArea) textComponent).setWrapStyleWord(true);
121 ((JTextArea) textComponent).setLineWrap(true);
122 ((JTextArea) textComponent).setEditable(false);
123 sp.setName("AuthorNotesScrollPane");
124 sp
125 .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
126 sp
127 .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
128 if( dim != null)
129 sp.setPreferredSize(dim);
130 sp.setBorder(BorderFactory.createEmptyBorder());
131 textComponent.setBorder(BorderFactory.createEmptyBorder());
132 this.add(sp, BorderLayout.CENTER);
133 } else {
134 this.add(textComponent, BorderLayout.CENTER);
135 }
136
137
138 textComponent.setName(TEXT_COMP);
139 editButton = new JButton(getEditButtonName());
140 editButton.setName(EDIT_BUTTON);
141 editButton.setHorizontalTextPosition(JButton.LEFT);
142 editButton.setIcon(editIcon);
143
144
145
146 final Color orgColor = textComponent.getBackground();
147 final Border orgBorder = textComponent.getBorder();
148 textComponent.setBorder(whiteBorder);
149
150 CommonUI.makeButtonTransparent(editButton);
151
152 final Border editEmptyBorder = BorderFactory.createEmptyBorder(0, 4, 0, 4);
153 editButton.setBorder(editEmptyBorder);
154 editButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
155 editButton.putClientProperty(MODE, EDIT_MODE);
156 editButton.addActionListener(new ActionListener() {
157
158 public void actionPerformed(ActionEvent e) {
159 JButton source = (JButton) e.getSource();
160 String clientProperty = (String) source.getClientProperty(MODE);
161
162 if (clientProperty.equals(EDIT_MODE)) {
163
164 textComponent.setEditable(true);
165 textComponent.setBackground(Color.white);
166
167
168 textComponent.setBorder(blackBorder);
169 if (textComponent instanceof JTextArea) {
170 sp.setBorder(BorderFactory.createEtchedBorder());
171 }
172 sp.setBorder(BorderFactory.createEmptyBorder());
173 source.putClientProperty(MODE, SAVE_MODE);
174 source.setText(getSaveButtonName());
175 source.setIcon(saveIcon);
176 source.revalidate();
177 textComponent.setFocusable(true);
178 textComponent.requestFocusInWindow();
179
180 } else if (clientProperty.equals(SAVE_MODE)) {
181
182 textComponent.setEditable(false);
183 textComponent.setBackground(orgColor);
184
185 textComponent.setBorder(whiteBorder);
186 if (textComponent instanceof JTextArea) {
187 sp.setBorder(BorderFactory.createEmptyBorder());
188 }
189 textComponent.setFocusable(false);
190
191 textComponent.transferFocus();
192
193 source.putClientProperty(MODE, EDIT_MODE);
194
195 source.setText(getEditButtonName());
196 source.setIcon(editIcon);
197 CommonUI.makeButtonTransparent(source);
198 source.setBorder(editEmptyBorder);
199 source.revalidate();
200 }
201
202 }
203
204 });
205
206 this.setOpaque(false);
207
208 if (textComponent instanceof JTextArea) {
209 JPanel a = new JPanel(new BorderLayout(0, 0));
210 a.setOpaque(false);
211 a.add(editButton, BorderLayout.NORTH);
212 this.add(a, BorderLayout.EAST);
213 } else {
214 this.add(editButton, BorderLayout.EAST);
215 }
216 this.setName(EDIT_PANEL);
217 }
218
219 /***
220 * @return the editButton
221 */
222 public JButton getEditButton() {
223 return editButton;
224 }
225
226
227
228 /***
229 * @param editButton the editButton to set
230 */
231 public void setEditButton(JButton editButton) {
232 this.editButton = editButton;
233 }
234
235 public JTextComponent getTextComponent() {
236 return textComponent;
237 }
238
239 public void setTextComponent(JTextComponent textComponent) {
240 this.textComponent = textComponent;
241 }
242
243 /***
244 * @param editButtonName the editButtonName to set
245 */
246 public void setEditButtonName(String editButtonName) {
247 this.editButtonName = editButtonName;
248 editButton.setText(editButtonName);
249 }
250
251 /***
252 * @return the editButtonName
253 */
254 public String getEditButtonName() {
255 return editButtonName;
256 }
257
258 /***
259 * @param saveButtonName the saveButtonName to set
260 */
261 public void setSaveButtonName(String saveButtonName) {
262 this.saveButtonName = saveButtonName;
263 }
264
265 /***
266 * @return the saveButtonName
267 */
268 public String getSaveButtonName() {
269 return saveButtonName;
270 }
271 }