author | serb |
Thu, 30 Mar 2017 16:40:45 +0300 | |
changeset 44655 | 06871a50a4b5 |
parent 32865 | f9cb6e427f9e |
child 45648 | 87c997b74bb8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44655
06871a50a4b5
8177461: Wrong references are used in the javadoc in the java.desktop module
serb
parents:
32865
diff
changeset
|
2 |
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing.text.html; |
|
26 |
||
27 |
import java.net.*; |
|
28 |
import java.io.*; |
|
29 |
import java.awt.*; |
|
30 |
import java.awt.event.*; |
|
31 |
import java.util.*; |
|
32 |
import javax.swing.*; |
|
33 |
import javax.swing.event.*; |
|
34 |
import javax.swing.text.*; |
|
35 |
||
36 |
/** |
|
37 |
* Component decorator that implements the view interface |
|
38 |
* for form elements, <input>, <textarea>, |
|
39 |
* and <select>. The model for the component is stored |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
40 |
* as an attribute of the element (using StyleConstants.ModelAttribute), |
2 | 41 |
* and is used to build the component of the view. The type |
42 |
* of the model is assumed to of the type that would be set by |
|
43 |
* <code>HTMLDocument.HTMLReader.FormAction</code>. If there are |
|
44 |
* multiple views mapped over the document, they will share the |
|
45 |
* embedded component models. |
|
46 |
* <p> |
|
47 |
* The following table shows what components get built |
|
48 |
* by this view. |
|
49 |
* <table summary="shows what components get built by this view"> |
|
50 |
* <tr> |
|
51 |
* <th>Element Type</th> |
|
52 |
* <th>Component built</th> |
|
53 |
* </tr> |
|
54 |
* <tr> |
|
55 |
* <td>input, type button</td> |
|
56 |
* <td>JButton</td> |
|
57 |
* </tr> |
|
58 |
* <tr> |
|
59 |
* <td>input, type checkbox</td> |
|
60 |
* <td>JCheckBox</td> |
|
61 |
* </tr> |
|
62 |
* <tr> |
|
63 |
* <td>input, type image</td> |
|
64 |
* <td>JButton</td> |
|
65 |
* </tr> |
|
66 |
* <tr> |
|
67 |
* <td>input, type password</td> |
|
68 |
* <td>JPasswordField</td> |
|
69 |
* </tr> |
|
70 |
* <tr> |
|
71 |
* <td>input, type radio</td> |
|
72 |
* <td>JRadioButton</td> |
|
73 |
* </tr> |
|
74 |
* <tr> |
|
75 |
* <td>input, type reset</td> |
|
76 |
* <td>JButton</td> |
|
77 |
* </tr> |
|
78 |
* <tr> |
|
79 |
* <td>input, type submit</td> |
|
80 |
* <td>JButton</td> |
|
81 |
* </tr> |
|
82 |
* <tr> |
|
83 |
* <td>input, type text</td> |
|
84 |
* <td>JTextField</td> |
|
85 |
* </tr> |
|
86 |
* <tr> |
|
87 |
* <td>select, size > 1 or multiple attribute defined</td> |
|
88 |
* <td>JList in a JScrollPane</td> |
|
89 |
* </tr> |
|
90 |
* <tr> |
|
91 |
* <td>select, size unspecified or 1</td> |
|
92 |
* <td>JComboBox</td> |
|
93 |
* </tr> |
|
94 |
* <tr> |
|
95 |
* <td>textarea</td> |
|
96 |
* <td>JTextArea in a JScrollPane</td> |
|
97 |
* </tr> |
|
98 |
* <tr> |
|
99 |
* <td>input, type file</td> |
|
100 |
* <td>JTextField</td> |
|
101 |
* </tr> |
|
102 |
* </table> |
|
103 |
* |
|
104 |
* @author Timothy Prinzing |
|
105 |
* @author Sunita Mani |
|
106 |
*/ |
|
107 |
public class FormView extends ComponentView implements ActionListener { |
|
108 |
||
109 |
/** |
|
110 |
* If a value attribute is not specified for a FORM input element |
|
111 |
* of type "submit", then this default string is used. |
|
112 |
* |
|
113 |
* @deprecated As of 1.3, value now comes from UIManager property |
|
114 |
* FormView.submitButtonText |
|
115 |
*/ |
|
116 |
@Deprecated |
|
117 |
public static final String SUBMIT = new String("Submit Query"); |
|
118 |
/** |
|
119 |
* If a value attribute is not specified for a FORM input element |
|
120 |
* of type "reset", then this default string is used. |
|
121 |
* |
|
122 |
* @deprecated As of 1.3, value comes from UIManager UIManager property |
|
123 |
* FormView.resetButtonText |
|
124 |
*/ |
|
125 |
@Deprecated |
|
126 |
public static final String RESET = new String("Reset"); |
|
127 |
||
128 |
/** |
|
129 |
* Document attribute name for storing POST data. JEditorPane.getPostData() |
|
130 |
* uses the same name, should be kept in sync. |
|
131 |
*/ |
|
32865
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
28236
diff
changeset
|
132 |
static final String PostDataProperty = "javax.swing.JEditorPane.postdata"; |
2 | 133 |
|
134 |
/** |
|
135 |
* Used to indicate if the maximum span should be the same as the |
|
136 |
* preferred span. This is used so that the Component's size doesn't |
|
137 |
* change if there is extra room on a line. The first bit is used for |
|
138 |
* the X direction, and the second for the y direction. |
|
139 |
*/ |
|
140 |
private short maxIsPreferred; |
|
141 |
||
142 |
/** |
|
143 |
* Creates a new FormView object. |
|
144 |
* |
|
145 |
* @param elem the element to decorate |
|
146 |
*/ |
|
147 |
public FormView(Element elem) { |
|
148 |
super(elem); |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Create the component. This is basically a |
|
153 |
* big switch statement based upon the tag type |
|
154 |
* and html attributes of the associated element. |
|
155 |
*/ |
|
156 |
protected Component createComponent() { |
|
157 |
AttributeSet attr = getElement().getAttributes(); |
|
158 |
HTML.Tag t = (HTML.Tag) |
|
159 |
attr.getAttribute(StyleConstants.NameAttribute); |
|
160 |
JComponent c = null; |
|
161 |
Object model = attr.getAttribute(StyleConstants.ModelAttribute); |
|
15977
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
162 |
|
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
163 |
// Remove listeners previously registered in shared model |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
164 |
// when a new UI component is replaced. See bug 7189299. |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
165 |
removeStaleListenerForModel(model); |
2 | 166 |
if (t == HTML.Tag.INPUT) { |
167 |
c = createInputComponent(attr, model); |
|
168 |
} else if (t == HTML.Tag.SELECT) { |
|
169 |
||
170 |
if (model instanceof OptionListModel) { |
|
25193
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
171 |
@SuppressWarnings("unchecked") |
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
172 |
JList<?> list = new JList<>((ListModel) model); |
2 | 173 |
int size = HTML.getIntegerAttributeValue(attr, |
174 |
HTML.Attribute.SIZE, |
|
175 |
1); |
|
176 |
list.setVisibleRowCount(size); |
|
177 |
list.setSelectionModel((ListSelectionModel)model); |
|
178 |
c = new JScrollPane(list); |
|
179 |
} else { |
|
25193
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
180 |
@SuppressWarnings("unchecked") |
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
181 |
JComboBox<?> tmp = new JComboBox<>((ComboBoxModel) model); |
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
182 |
c = tmp; |
2 | 183 |
maxIsPreferred = 3; |
184 |
} |
|
185 |
} else if (t == HTML.Tag.TEXTAREA) { |
|
186 |
JTextArea area = new JTextArea((Document) model); |
|
187 |
int rows = HTML.getIntegerAttributeValue(attr, |
|
188 |
HTML.Attribute.ROWS, |
|
189 |
1); |
|
190 |
area.setRows(rows); |
|
191 |
int cols = HTML.getIntegerAttributeValue(attr, |
|
192 |
HTML.Attribute.COLS, |
|
193 |
20); |
|
194 |
maxIsPreferred = 3; |
|
195 |
area.setColumns(cols); |
|
196 |
c = new JScrollPane(area, |
|
197 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
|
198 |
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); |
|
199 |
} |
|
200 |
||
201 |
if (c != null) { |
|
202 |
c.setAlignmentY(1.0f); |
|
203 |
} |
|
204 |
return c; |
|
205 |
} |
|
206 |
||
207 |
||
208 |
/** |
|
209 |
* Creates a component for an <INPUT> element based on the |
|
210 |
* value of the "type" attribute. |
|
211 |
* |
|
44655
06871a50a4b5
8177461: Wrong references are used in the javadoc in the java.desktop module
serb
parents:
32865
diff
changeset
|
212 |
* @param attr set of attributes associated with the <INPUT> element. |
2 | 213 |
* @param model the value of the StyleConstants.ModelAttribute |
214 |
* @return the component. |
|
215 |
*/ |
|
216 |
private JComponent createInputComponent(AttributeSet attr, Object model) { |
|
217 |
JComponent c = null; |
|
218 |
String type = (String) attr.getAttribute(HTML.Attribute.TYPE); |
|
219 |
||
220 |
if (type.equals("submit") || type.equals("reset")) { |
|
221 |
String value = (String) |
|
222 |
attr.getAttribute(HTML.Attribute.VALUE); |
|
223 |
if (value == null) { |
|
224 |
if (type.equals("submit")) { |
|
225 |
value = UIManager.getString("FormView.submitButtonText"); |
|
226 |
} else { |
|
227 |
value = UIManager.getString("FormView.resetButtonText"); |
|
228 |
} |
|
229 |
} |
|
230 |
JButton button = new JButton(value); |
|
231 |
if (model != null) { |
|
232 |
button.setModel((ButtonModel)model); |
|
233 |
button.addActionListener(this); |
|
234 |
} |
|
235 |
c = button; |
|
236 |
maxIsPreferred = 3; |
|
237 |
} else if (type.equals("image")) { |
|
238 |
String srcAtt = (String) attr.getAttribute(HTML.Attribute.SRC); |
|
239 |
JButton button; |
|
240 |
try { |
|
241 |
URL base = ((HTMLDocument)getElement().getDocument()).getBase(); |
|
242 |
URL srcURL = new URL(base, srcAtt); |
|
243 |
Icon icon = new ImageIcon(srcURL); |
|
244 |
button = new JButton(icon); |
|
245 |
} catch (MalformedURLException e) { |
|
246 |
button = new JButton(srcAtt); |
|
247 |
} |
|
248 |
if (model != null) { |
|
249 |
button.setModel((ButtonModel)model); |
|
250 |
button.addMouseListener(new MouseEventListener()); |
|
251 |
} |
|
252 |
c = button; |
|
253 |
maxIsPreferred = 3; |
|
254 |
} else if (type.equals("checkbox")) { |
|
255 |
c = new JCheckBox(); |
|
256 |
if (model != null) { |
|
257 |
((JCheckBox)c).setModel((JToggleButton.ToggleButtonModel) model); |
|
258 |
} |
|
259 |
maxIsPreferred = 3; |
|
260 |
} else if (type.equals("radio")) { |
|
261 |
c = new JRadioButton(); |
|
262 |
if (model != null) { |
|
263 |
((JRadioButton)c).setModel((JToggleButton.ToggleButtonModel)model); |
|
264 |
} |
|
265 |
maxIsPreferred = 3; |
|
266 |
} else if (type.equals("text")) { |
|
267 |
int size = HTML.getIntegerAttributeValue(attr, |
|
268 |
HTML.Attribute.SIZE, |
|
269 |
-1); |
|
270 |
JTextField field; |
|
271 |
if (size > 0) { |
|
272 |
field = new JTextField(); |
|
273 |
field.setColumns(size); |
|
274 |
} |
|
275 |
else { |
|
276 |
field = new JTextField(); |
|
277 |
field.setColumns(20); |
|
278 |
} |
|
279 |
c = field; |
|
280 |
if (model != null) { |
|
281 |
field.setDocument((Document) model); |
|
282 |
} |
|
283 |
field.addActionListener(this); |
|
284 |
maxIsPreferred = 3; |
|
285 |
} else if (type.equals("password")) { |
|
286 |
JPasswordField field = new JPasswordField(); |
|
287 |
c = field; |
|
288 |
if (model != null) { |
|
289 |
field.setDocument((Document) model); |
|
290 |
} |
|
291 |
int size = HTML.getIntegerAttributeValue(attr, |
|
292 |
HTML.Attribute.SIZE, |
|
293 |
-1); |
|
294 |
field.setColumns((size > 0) ? size : 20); |
|
295 |
field.addActionListener(this); |
|
296 |
maxIsPreferred = 3; |
|
297 |
} else if (type.equals("file")) { |
|
298 |
JTextField field = new JTextField(); |
|
299 |
if (model != null) { |
|
300 |
field.setDocument((Document)model); |
|
301 |
} |
|
302 |
int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE, |
|
303 |
-1); |
|
304 |
field.setColumns((size > 0) ? size : 20); |
|
305 |
JButton browseButton = new JButton(UIManager.getString |
|
306 |
("FormView.browseFileButtonText")); |
|
307 |
Box box = Box.createHorizontalBox(); |
|
308 |
box.add(field); |
|
309 |
box.add(Box.createHorizontalStrut(5)); |
|
310 |
box.add(browseButton); |
|
311 |
browseButton.addActionListener(new BrowseFileAction( |
|
312 |
attr, (Document)model)); |
|
313 |
c = box; |
|
314 |
maxIsPreferred = 3; |
|
315 |
} |
|
316 |
return c; |
|
317 |
} |
|
318 |
||
15977
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
319 |
private void removeStaleListenerForModel(Object model) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
320 |
if (model instanceof DefaultButtonModel) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
321 |
// case of JButton whose model is DefaultButtonModel |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
322 |
// Need to remove stale ActionListener, ChangeListener and |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
323 |
// ItemListener that are instance of AbstractButton$Handler. |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
324 |
DefaultButtonModel buttonModel = (DefaultButtonModel) model; |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
325 |
String listenerClass = "javax.swing.AbstractButton$Handler"; |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
326 |
for (ActionListener listener : buttonModel.getActionListeners()) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
327 |
if (listenerClass.equals(listener.getClass().getName())) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
328 |
buttonModel.removeActionListener(listener); |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
329 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
330 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
331 |
for (ChangeListener listener : buttonModel.getChangeListeners()) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
332 |
if (listenerClass.equals(listener.getClass().getName())) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
333 |
buttonModel.removeChangeListener(listener); |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
334 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
335 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
336 |
for (ItemListener listener : buttonModel.getItemListeners()) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
337 |
if (listenerClass.equals(listener.getClass().getName())) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
338 |
buttonModel.removeItemListener(listener); |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
339 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
340 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
341 |
} else if (model instanceof AbstractListModel) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
342 |
// case of JComboBox and JList |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
343 |
// For JList, the stale ListDataListener is instance |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
344 |
// BasicListUI$Handler. |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
345 |
// For JComboBox, there are 2 stale ListDataListeners, which are |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
346 |
// BasicListUI$Handler and BasicComboBoxUI$Handler. |
25193
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
347 |
@SuppressWarnings("unchecked") |
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
348 |
AbstractListModel<?> listModel = (AbstractListModel) model; |
15977
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
349 |
String listenerClass1 = |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
350 |
"javax.swing.plaf.basic.BasicListUI$Handler"; |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
351 |
String listenerClass2 = |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
352 |
"javax.swing.plaf.basic.BasicComboBoxUI$Handler"; |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
353 |
for (ListDataListener listener : listModel.getListDataListeners()) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
354 |
if (listenerClass1.equals(listener.getClass().getName()) |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
355 |
|| listenerClass2.equals(listener.getClass().getName())) |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
356 |
{ |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
357 |
listModel.removeListDataListener(listener); |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
358 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
359 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
360 |
} else if (model instanceof AbstractDocument) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
361 |
// case of JPasswordField, JTextField and JTextArea |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
362 |
// All have 2 stale DocumentListeners. |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
363 |
String listenerClass1 = |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
364 |
"javax.swing.plaf.basic.BasicTextUI$UpdateHandler"; |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
365 |
String listenerClass2 = |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
366 |
"javax.swing.text.DefaultCaret$Handler"; |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
367 |
AbstractDocument docModel = (AbstractDocument) model; |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
368 |
for (DocumentListener listener : docModel.getDocumentListeners()) { |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
369 |
if (listenerClass1.equals(listener.getClass().getName()) |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
370 |
|| listenerClass2.equals(listener.getClass().getName())) |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
371 |
{ |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
372 |
docModel.removeDocumentListener(listener); |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
373 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
374 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
375 |
} |
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
376 |
} |
2 | 377 |
|
378 |
/** |
|
379 |
* Determines the maximum span for this view along an |
|
380 |
* axis. For certain components, the maximum and preferred span are the |
|
381 |
* same. For others this will return the value |
|
382 |
* returned by Component.getMaximumSize along the |
|
383 |
* axis of interest. |
|
384 |
* |
|
385 |
* @param axis may be either View.X_AXIS or View.Y_AXIS |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
15977
diff
changeset
|
386 |
* @return the span the view would like to be rendered into >= 0. |
2 | 387 |
* Typically the view is told to render into the span |
388 |
* that is returned, although there is no guarantee. |
|
389 |
* The parent may choose to resize or break the view. |
|
390 |
* @exception IllegalArgumentException for an invalid axis |
|
391 |
*/ |
|
392 |
public float getMaximumSpan(int axis) { |
|
393 |
switch (axis) { |
|
394 |
case View.X_AXIS: |
|
395 |
if ((maxIsPreferred & 1) == 1) { |
|
396 |
super.getMaximumSpan(axis); |
|
397 |
return getPreferredSpan(axis); |
|
398 |
} |
|
399 |
return super.getMaximumSpan(axis); |
|
400 |
case View.Y_AXIS: |
|
401 |
if ((maxIsPreferred & 2) == 2) { |
|
402 |
super.getMaximumSpan(axis); |
|
403 |
return getPreferredSpan(axis); |
|
404 |
} |
|
405 |
return super.getMaximumSpan(axis); |
|
406 |
default: |
|
407 |
break; |
|
408 |
} |
|
409 |
return super.getMaximumSpan(axis); |
|
410 |
} |
|
411 |
||
412 |
||
413 |
/** |
|
15977
225648de9c4d
8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents:
10100
diff
changeset
|
414 |
* Responsible for processing the ActionEvent. |
2 | 415 |
* If the element associated with the FormView, |
416 |
* has a type of "submit", "reset", "text" or "password" |
|
417 |
* then the action is processed. In the case of a "submit" |
|
418 |
* the form is submitted. In the case of a "reset" |
|
419 |
* the form is reset to its original state. |
|
420 |
* In the case of "text" or "password", if the |
|
421 |
* element is the last one of type "text" or "password", |
|
422 |
* the form is submitted. Otherwise, focus is transferred |
|
423 |
* to the next component in the form. |
|
424 |
* |
|
425 |
* @param evt the ActionEvent. |
|
426 |
*/ |
|
427 |
public void actionPerformed(ActionEvent evt) { |
|
428 |
Element element = getElement(); |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
429 |
StringBuilder dataBuffer = new StringBuilder(); |
2 | 430 |
HTMLDocument doc = (HTMLDocument)getDocument(); |
431 |
AttributeSet attr = element.getAttributes(); |
|
432 |
||
433 |
String type = (String) attr.getAttribute(HTML.Attribute.TYPE); |
|
434 |
||
435 |
if (type.equals("submit")) { |
|
436 |
getFormData(dataBuffer); |
|
437 |
submitData(dataBuffer.toString()); |
|
438 |
} else if (type.equals("reset")) { |
|
439 |
resetForm(); |
|
440 |
} else if (type.equals("text") || type.equals("password")) { |
|
441 |
if (isLastTextOrPasswordField()) { |
|
442 |
getFormData(dataBuffer); |
|
443 |
submitData(dataBuffer.toString()); |
|
444 |
} else { |
|
445 |
getComponent().transferFocus(); |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
||
451 |
/** |
|
452 |
* This method is responsible for submitting the form data. |
|
453 |
* A thread is forked to undertake the submission. |
|
24269
325ea76ea0e9
8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents:
23010
diff
changeset
|
454 |
* |
325ea76ea0e9
8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents:
23010
diff
changeset
|
455 |
* @param data data to submit |
2 | 456 |
*/ |
457 |
protected void submitData(String data) { |
|
458 |
Element form = getFormElement(); |
|
459 |
AttributeSet attrs = form.getAttributes(); |
|
460 |
HTMLDocument doc = (HTMLDocument) form.getDocument(); |
|
461 |
URL base = doc.getBase(); |
|
462 |
||
463 |
String target = (String) attrs.getAttribute(HTML.Attribute.TARGET); |
|
464 |
if (target == null) { |
|
465 |
target = "_self"; |
|
466 |
} |
|
467 |
||
468 |
String method = (String) attrs.getAttribute(HTML.Attribute.METHOD); |
|
469 |
if (method == null) { |
|
470 |
method = "GET"; |
|
471 |
} |
|
472 |
method = method.toLowerCase(); |
|
473 |
boolean isPostMethod = method.equals("post"); |
|
474 |
if (isPostMethod) { |
|
475 |
storePostData(doc, target, data); |
|
476 |
} |
|
477 |
||
478 |
String action = (String) attrs.getAttribute(HTML.Attribute.ACTION); |
|
479 |
URL actionURL; |
|
480 |
try { |
|
481 |
actionURL = (action == null) |
|
482 |
? new URL(base.getProtocol(), base.getHost(), |
|
483 |
base.getPort(), base.getFile()) |
|
484 |
: new URL(base, action); |
|
485 |
if (!isPostMethod) { |
|
486 |
String query = data.toString(); |
|
487 |
actionURL = new URL(actionURL + "?" + query); |
|
488 |
} |
|
489 |
} catch (MalformedURLException e) { |
|
490 |
actionURL = null; |
|
491 |
} |
|
492 |
final JEditorPane c = (JEditorPane) getContainer(); |
|
493 |
HTMLEditorKit kit = (HTMLEditorKit) c.getEditorKit(); |
|
494 |
||
495 |
FormSubmitEvent formEvent = null; |
|
496 |
if (!kit.isAutoFormSubmission() || doc.isFrameDocument()) { |
|
497 |
FormSubmitEvent.MethodType methodType = isPostMethod |
|
498 |
? FormSubmitEvent.MethodType.POST |
|
499 |
: FormSubmitEvent.MethodType.GET; |
|
500 |
formEvent = new FormSubmitEvent( |
|
501 |
FormView.this, HyperlinkEvent.EventType.ACTIVATED, |
|
502 |
actionURL, form, target, methodType, data); |
|
503 |
||
504 |
} |
|
505 |
// setPage() may take significant time so schedule it to run later. |
|
506 |
final FormSubmitEvent fse = formEvent; |
|
507 |
final URL url = actionURL; |
|
508 |
SwingUtilities.invokeLater(new Runnable() { |
|
509 |
public void run() { |
|
510 |
if (fse != null) { |
|
511 |
c.fireHyperlinkUpdate(fse); |
|
512 |
} else { |
|
513 |
try { |
|
514 |
c.setPage(url); |
|
515 |
} catch (IOException e) { |
|
516 |
UIManager.getLookAndFeel().provideErrorFeedback(c); |
|
517 |
} |
|
518 |
} |
|
519 |
} |
|
520 |
}); |
|
521 |
} |
|
522 |
||
523 |
private void storePostData(HTMLDocument doc, String target, String data) { |
|
524 |
||
525 |
/* POST data is stored into the document property named by constant |
|
526 |
* PostDataProperty from where it is later retrieved by method |
|
527 |
* JEditorPane.getPostData(). If the current document is in a frame, |
|
528 |
* the data is initially put into the toplevel (frameset) document |
|
529 |
* property (named <PostDataProperty>.<Target frame name>). It is the |
|
530 |
* responsibility of FrameView which updates the target frame |
|
531 |
* to move data from the frameset document property into the frame |
|
532 |
* document property. |
|
533 |
*/ |
|
534 |
||
535 |
Document propDoc = doc; |
|
536 |
String propName = PostDataProperty; |
|
537 |
||
538 |
if (doc.isFrameDocument()) { |
|
539 |
// find the top-most JEditorPane holding the frameset view. |
|
540 |
FrameView.FrameEditorPane p = |
|
541 |
(FrameView.FrameEditorPane) getContainer(); |
|
542 |
FrameView v = p.getFrameView(); |
|
543 |
JEditorPane c = v.getOutermostJEditorPane(); |
|
544 |
if (c != null) { |
|
545 |
propDoc = c.getDocument(); |
|
546 |
propName += ("." + target); |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
propDoc.putProperty(propName, data); |
|
551 |
} |
|
552 |
||
553 |
/** |
|
554 |
* MouseEventListener class to handle form submissions when |
|
555 |
* an input with type equal to image is clicked on. |
|
556 |
* A MouseListener is necessary since along with the image |
|
557 |
* data the coordinates associated with the mouse click |
|
558 |
* need to be submitted. |
|
559 |
*/ |
|
560 |
protected class MouseEventListener extends MouseAdapter { |
|
561 |
||
562 |
public void mouseReleased(MouseEvent evt) { |
|
563 |
String imageData = getImageData(evt.getPoint()); |
|
564 |
imageSubmit(imageData); |
|
565 |
} |
|
566 |
} |
|
567 |
||
568 |
/** |
|
569 |
* This method is called to submit a form in response |
|
570 |
* to a click on an image -- an <INPUT> form |
|
571 |
* element of type "image". |
|
572 |
* |
|
573 |
* @param imageData the mouse click coordinates. |
|
574 |
*/ |
|
575 |
protected void imageSubmit(String imageData) { |
|
576 |
||
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
577 |
StringBuilder dataBuffer = new StringBuilder(); |
2 | 578 |
Element elem = getElement(); |
579 |
HTMLDocument hdoc = (HTMLDocument)elem.getDocument(); |
|
580 |
getFormData(dataBuffer); |
|
581 |
if (dataBuffer.length() > 0) { |
|
582 |
dataBuffer.append('&'); |
|
583 |
} |
|
584 |
dataBuffer.append(imageData); |
|
585 |
submitData(dataBuffer.toString()); |
|
586 |
return; |
|
587 |
} |
|
588 |
||
589 |
/** |
|
590 |
* Extracts the value of the name attribute |
|
591 |
* associated with the input element of type |
|
592 |
* image. If name is defined it is encoded using |
|
593 |
* the URLEncoder.encode() method and the |
|
594 |
* image data is returned in the following format: |
|
595 |
* name + ".x" +"="+ x +"&"+ name +".y"+"="+ y |
|
596 |
* otherwise, |
|
597 |
* "x="+ x +"&y="+ y |
|
598 |
* |
|
599 |
* @param point associated with the mouse click. |
|
600 |
* @return the image data. |
|
601 |
*/ |
|
28231
b608ffcaed74
8066621: Suppress deprecation warnings in java.desktop module
darcy
parents:
25859
diff
changeset
|
602 |
@SuppressWarnings("deprecation") |
2 | 603 |
private String getImageData(Point point) { |
604 |
||
605 |
String mouseCoords = point.x + ":" + point.y; |
|
606 |
int sep = mouseCoords.indexOf(':'); |
|
607 |
String x = mouseCoords.substring(0, sep); |
|
608 |
String y = mouseCoords.substring(++sep); |
|
609 |
String name = (String) getElement().getAttributes().getAttribute(HTML.Attribute.NAME); |
|
610 |
||
611 |
String data; |
|
612 |
if (name == null || name.equals("")) { |
|
613 |
data = "x="+ x +"&y="+ y; |
|
614 |
} else { |
|
615 |
name = URLEncoder.encode(name); |
|
616 |
data = name + ".x" +"="+ x +"&"+ name +".y"+"="+ y; |
|
617 |
} |
|
618 |
return data; |
|
619 |
} |
|
620 |
||
621 |
||
622 |
/** |
|
623 |
* The following methods provide functionality required to |
|
624 |
* iterate over a the elements of the form and in the case |
|
625 |
* of a form submission, extract the data from each model |
|
626 |
* that is associated with each form element, and in the |
|
627 |
* case of reset, reinitialize the each model to its |
|
628 |
* initial state. |
|
629 |
*/ |
|
630 |
||
631 |
||
632 |
/** |
|
633 |
* Returns the Element representing the <code>FORM</code>. |
|
634 |
*/ |
|
635 |
private Element getFormElement() { |
|
636 |
Element elem = getElement(); |
|
637 |
while (elem != null) { |
|
638 |
if (elem.getAttributes().getAttribute |
|
639 |
(StyleConstants.NameAttribute) == HTML.Tag.FORM) { |
|
640 |
return elem; |
|
641 |
} |
|
642 |
elem = elem.getParentElement(); |
|
643 |
} |
|
644 |
return null; |
|
645 |
} |
|
646 |
||
647 |
/** |
|
648 |
* Iterates over the |
|
649 |
* element hierarchy, extracting data from the |
|
650 |
* models associated with the relevant form elements. |
|
651 |
* "Relevant" means the form elements that are part |
|
652 |
* of the same form whose element triggered the submit |
|
653 |
* action. |
|
654 |
* |
|
655 |
* @param buffer the buffer that contains that data to submit |
|
656 |
*/ |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
657 |
private void getFormData(StringBuilder buffer) { |
2 | 658 |
Element formE = getFormElement(); |
659 |
if (formE != null) { |
|
660 |
ElementIterator it = new ElementIterator(formE); |
|
661 |
Element next; |
|
662 |
||
663 |
while ((next = it.next()) != null) { |
|
664 |
if (isControl(next)) { |
|
665 |
String type = (String)next.getAttributes().getAttribute |
|
666 |
(HTML.Attribute.TYPE); |
|
667 |
||
668 |
if (type != null && type.equals("submit") && |
|
669 |
next != getElement()) { |
|
21278 | 670 |
// do nothing - this submit is not the trigger |
2 | 671 |
} else if (type == null || !type.equals("image")) { |
672 |
// images only result in data if they triggered |
|
673 |
// the submit and they require that the mouse click |
|
674 |
// coords be appended to the data. Hence its |
|
675 |
// processing is handled by the view. |
|
676 |
loadElementDataIntoBuffer(next, buffer); |
|
677 |
} |
|
678 |
} |
|
679 |
} |
|
680 |
} |
|
681 |
} |
|
682 |
||
683 |
/** |
|
684 |
* Loads the data |
|
685 |
* associated with the element into the buffer. |
|
686 |
* The format in which data is appended depends |
|
687 |
* on the type of the form element. Essentially |
|
688 |
* data is loaded in name/value pairs. |
|
689 |
* |
|
690 |
*/ |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
691 |
private void loadElementDataIntoBuffer(Element elem, StringBuilder buffer) { |
2 | 692 |
|
693 |
AttributeSet attr = elem.getAttributes(); |
|
694 |
String name = (String)attr.getAttribute(HTML.Attribute.NAME); |
|
695 |
if (name == null) { |
|
696 |
return; |
|
697 |
} |
|
698 |
String value = null; |
|
699 |
HTML.Tag tag = (HTML.Tag)elem.getAttributes().getAttribute |
|
700 |
(StyleConstants.NameAttribute); |
|
701 |
||
702 |
if (tag == HTML.Tag.INPUT) { |
|
703 |
value = getInputElementData(attr); |
|
704 |
} else if (tag == HTML.Tag.TEXTAREA) { |
|
705 |
value = getTextAreaData(attr); |
|
706 |
} else if (tag == HTML.Tag.SELECT) { |
|
707 |
loadSelectData(attr, buffer); |
|
708 |
} |
|
709 |
||
710 |
if (name != null && value != null) { |
|
711 |
appendBuffer(buffer, name, value); |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
||
716 |
/** |
|
717 |
* Returns the data associated with an <INPUT> form |
|
718 |
* element. The value of "type" attributes is |
|
719 |
* used to determine the type of the model associated |
|
720 |
* with the element and then the relevant data is |
|
721 |
* extracted. |
|
722 |
*/ |
|
723 |
private String getInputElementData(AttributeSet attr) { |
|
724 |
||
725 |
Object model = attr.getAttribute(StyleConstants.ModelAttribute); |
|
726 |
String type = (String) attr.getAttribute(HTML.Attribute.TYPE); |
|
727 |
String value = null; |
|
728 |
||
729 |
if (type.equals("text") || type.equals("password")) { |
|
730 |
Document doc = (Document)model; |
|
731 |
try { |
|
732 |
value = doc.getText(0, doc.getLength()); |
|
733 |
} catch (BadLocationException e) { |
|
734 |
value = null; |
|
735 |
} |
|
736 |
} else if (type.equals("submit") || type.equals("hidden")) { |
|
737 |
value = (String) attr.getAttribute(HTML.Attribute.VALUE); |
|
738 |
if (value == null) { |
|
739 |
value = ""; |
|
740 |
} |
|
741 |
} else if (type.equals("radio") || type.equals("checkbox")) { |
|
742 |
ButtonModel m = (ButtonModel)model; |
|
743 |
if (m.isSelected()) { |
|
744 |
value = (String) attr.getAttribute(HTML.Attribute.VALUE); |
|
745 |
if (value == null) { |
|
746 |
value = "on"; |
|
747 |
} |
|
748 |
} |
|
749 |
} else if (type.equals("file")) { |
|
750 |
Document doc = (Document)model; |
|
751 |
String path; |
|
752 |
||
753 |
try { |
|
754 |
path = doc.getText(0, doc.getLength()); |
|
755 |
} catch (BadLocationException e) { |
|
756 |
path = null; |
|
757 |
} |
|
758 |
if (path != null && path.length() > 0) { |
|
759 |
value = path; |
|
760 |
} |
|
761 |
} |
|
762 |
return value; |
|
763 |
} |
|
764 |
||
765 |
/** |
|
766 |
* Returns the data associated with the <TEXTAREA> form |
|
767 |
* element. This is done by getting the text stored in the |
|
768 |
* Document model. |
|
769 |
*/ |
|
770 |
private String getTextAreaData(AttributeSet attr) { |
|
771 |
Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute); |
|
772 |
try { |
|
773 |
return doc.getText(0, doc.getLength()); |
|
774 |
} catch (BadLocationException e) { |
|
775 |
return null; |
|
776 |
} |
|
777 |
} |
|
778 |
||
779 |
||
780 |
/** |
|
781 |
* Loads the buffer with the data associated with the Select |
|
782 |
* form element. Basically, only items that are selected |
|
783 |
* and have their name attribute set are added to the buffer. |
|
784 |
*/ |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
785 |
private void loadSelectData(AttributeSet attr, StringBuilder buffer) { |
2 | 786 |
|
787 |
String name = (String)attr.getAttribute(HTML.Attribute.NAME); |
|
788 |
if (name == null) { |
|
789 |
return; |
|
790 |
} |
|
791 |
Object m = attr.getAttribute(StyleConstants.ModelAttribute); |
|
792 |
if (m instanceof OptionListModel) { |
|
25193
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
793 |
@SuppressWarnings("unchecked") |
10100
c525c5fbb86c
7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents:
7668
diff
changeset
|
794 |
OptionListModel<Option> model = (OptionListModel<Option>) m; |
2 | 795 |
|
796 |
for (int i = 0; i < model.getSize(); i++) { |
|
797 |
if (model.isSelectedIndex(i)) { |
|
10100
c525c5fbb86c
7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents:
7668
diff
changeset
|
798 |
Option option = model.getElementAt(i); |
2 | 799 |
appendBuffer(buffer, name, option.getValue()); |
800 |
} |
|
801 |
} |
|
802 |
} else if (m instanceof ComboBoxModel) { |
|
25193
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
803 |
@SuppressWarnings("unchecked") |
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
804 |
ComboBoxModel<?> model = (ComboBoxModel)m; |
2 | 805 |
Option option = (Option)model.getSelectedItem(); |
806 |
if (option != null) { |
|
807 |
appendBuffer(buffer, name, option.getValue()); |
|
808 |
} |
|
809 |
} |
|
810 |
} |
|
811 |
||
812 |
/** |
|
813 |
* Appends name / value pairs into the |
|
814 |
* buffer. Both names and values are encoded using the |
|
815 |
* URLEncoder.encode() method before being added to the |
|
816 |
* buffer. |
|
817 |
*/ |
|
28231
b608ffcaed74
8066621: Suppress deprecation warnings in java.desktop module
darcy
parents:
25859
diff
changeset
|
818 |
@SuppressWarnings("deprecation") |
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
819 |
private void appendBuffer(StringBuilder buffer, String name, String value) { |
2 | 820 |
if (buffer.length() > 0) { |
821 |
buffer.append('&'); |
|
822 |
} |
|
823 |
String encodedName = URLEncoder.encode(name); |
|
824 |
buffer.append(encodedName); |
|
825 |
buffer.append('='); |
|
826 |
String encodedValue = URLEncoder.encode(value); |
|
827 |
buffer.append(encodedValue); |
|
828 |
} |
|
829 |
||
830 |
/** |
|
831 |
* Returns true if the Element <code>elem</code> represents a control. |
|
832 |
*/ |
|
833 |
private boolean isControl(Element elem) { |
|
834 |
return elem.isLeaf(); |
|
835 |
} |
|
836 |
||
837 |
/** |
|
838 |
* Iterates over the element hierarchy to determine if |
|
839 |
* the element parameter, which is assumed to be an |
|
840 |
* <INPUT> element of type password or text, is the last |
|
841 |
* one of either kind, in the form to which it belongs. |
|
842 |
*/ |
|
843 |
boolean isLastTextOrPasswordField() { |
|
844 |
Element parent = getFormElement(); |
|
845 |
Element elem = getElement(); |
|
846 |
||
847 |
if (parent != null) { |
|
848 |
ElementIterator it = new ElementIterator(parent); |
|
849 |
Element next; |
|
850 |
boolean found = false; |
|
851 |
||
852 |
while ((next = it.next()) != null) { |
|
853 |
if (next == elem) { |
|
854 |
found = true; |
|
855 |
} |
|
856 |
else if (found && isControl(next)) { |
|
857 |
AttributeSet elemAttr = next.getAttributes(); |
|
858 |
||
859 |
if (HTMLDocument.matchNameAttribute |
|
860 |
(elemAttr, HTML.Tag.INPUT)) { |
|
861 |
String type = (String)elemAttr.getAttribute |
|
862 |
(HTML.Attribute.TYPE); |
|
863 |
||
864 |
if ("text".equals(type) || "password".equals(type)) { |
|
865 |
return false; |
|
866 |
} |
|
867 |
} |
|
868 |
} |
|
869 |
} |
|
870 |
} |
|
871 |
return true; |
|
872 |
} |
|
873 |
||
874 |
/** |
|
875 |
* Resets the form |
|
876 |
* to its initial state by reinitializing the models |
|
877 |
* associated with each form element to their initial |
|
878 |
* values. |
|
879 |
* |
|
880 |
* param elem the element that triggered the reset |
|
881 |
*/ |
|
882 |
void resetForm() { |
|
883 |
Element parent = getFormElement(); |
|
884 |
||
885 |
if (parent != null) { |
|
886 |
ElementIterator it = new ElementIterator(parent); |
|
887 |
Element next; |
|
888 |
||
889 |
while((next = it.next()) != null) { |
|
890 |
if (isControl(next)) { |
|
891 |
AttributeSet elemAttr = next.getAttributes(); |
|
892 |
Object m = elemAttr.getAttribute(StyleConstants. |
|
893 |
ModelAttribute); |
|
894 |
if (m instanceof TextAreaDocument) { |
|
895 |
TextAreaDocument doc = (TextAreaDocument)m; |
|
896 |
doc.reset(); |
|
897 |
} else if (m instanceof PlainDocument) { |
|
898 |
try { |
|
899 |
PlainDocument doc = (PlainDocument)m; |
|
900 |
doc.remove(0, doc.getLength()); |
|
901 |
if (HTMLDocument.matchNameAttribute |
|
902 |
(elemAttr, HTML.Tag.INPUT)) { |
|
903 |
String value = (String)elemAttr. |
|
904 |
getAttribute(HTML.Attribute.VALUE); |
|
905 |
if (value != null) { |
|
906 |
doc.insertString(0, value, null); |
|
907 |
} |
|
908 |
} |
|
909 |
} catch (BadLocationException e) { |
|
910 |
} |
|
911 |
} else if (m instanceof OptionListModel) { |
|
25193
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
912 |
@SuppressWarnings("unchecked") |
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
913 |
OptionListModel<?> model = (OptionListModel) m; |
2 | 914 |
int size = model.getSize(); |
915 |
for (int i = 0; i < size; i++) { |
|
916 |
model.removeIndexInterval(i, i); |
|
917 |
} |
|
918 |
BitSet selectionRange = model.getInitialSelection(); |
|
919 |
for (int i = 0; i < selectionRange.size(); i++) { |
|
920 |
if (selectionRange.get(i)) { |
|
921 |
model.addSelectionInterval(i, i); |
|
922 |
} |
|
923 |
} |
|
924 |
} else if (m instanceof OptionComboBoxModel) { |
|
25193
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
925 |
@SuppressWarnings("unchecked") |
187a455af8f8
8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents:
24269
diff
changeset
|
926 |
OptionComboBoxModel<?> model = (OptionComboBoxModel) m; |
2 | 927 |
Option option = model.getInitialSelection(); |
928 |
if (option != null) { |
|
929 |
model.setSelectedItem(option); |
|
930 |
} |
|
931 |
} else if (m instanceof JToggleButton.ToggleButtonModel) { |
|
932 |
boolean checked = ((String)elemAttr.getAttribute |
|
933 |
(HTML.Attribute.CHECKED) != null); |
|
934 |
JToggleButton.ToggleButtonModel model = |
|
935 |
(JToggleButton.ToggleButtonModel)m; |
|
936 |
model.setSelected(checked); |
|
937 |
} |
|
938 |
} |
|
939 |
} |
|
940 |
} |
|
941 |
} |
|
942 |
||
943 |
||
944 |
/** |
|
945 |
* BrowseFileAction is used for input type == file. When the user |
|
946 |
* clicks the button a JFileChooser is brought up allowing the user |
|
947 |
* to select a file in the file system. The resulting path to the selected |
|
948 |
* file is set in the text field (actually an instance of Document). |
|
949 |
*/ |
|
950 |
private class BrowseFileAction implements ActionListener { |
|
951 |
private AttributeSet attrs; |
|
952 |
private Document model; |
|
953 |
||
954 |
BrowseFileAction(AttributeSet attrs, Document model) { |
|
955 |
this.attrs = attrs; |
|
956 |
this.model = model; |
|
957 |
} |
|
958 |
||
959 |
public void actionPerformed(ActionEvent ae) { |
|
960 |
// PENDING: When mime support is added to JFileChooser use the |
|
961 |
// accept value of attrs. |
|
962 |
JFileChooser fc = new JFileChooser(); |
|
963 |
fc.setMultiSelectionEnabled(false); |
|
964 |
if (fc.showOpenDialog(getContainer()) == |
|
965 |
JFileChooser.APPROVE_OPTION) { |
|
966 |
File selected = fc.getSelectedFile(); |
|
967 |
||
968 |
if (selected != null) { |
|
969 |
try { |
|
970 |
if (model.getLength() > 0) { |
|
971 |
model.remove(0, model.getLength()); |
|
972 |
} |
|
973 |
model.insertString(0, selected.getPath(), null); |
|
974 |
} catch (BadLocationException ble) {} |
|
975 |
} |
|
976 |
} |
|
977 |
} |
|
978 |
} |
|
979 |
} |