001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Roman I. Chernyatchik
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.awt.Component;
023: import java.awt.Dimension;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.awt.event.MouseAdapter;
027: import java.awt.event.MouseEvent;
028:
029: import javax.swing.AbstractButton;
030: import javax.swing.Icon;
031: import javax.swing.JToggleButton;
032: import javax.swing.text.AttributeSet;
033: import javax.swing.text.BadLocationException;
034: import javax.swing.text.ComponentView;
035: import javax.swing.text.Element;
036: import javax.swing.text.StyleConstants;
037: import javax.swing.text.View;
038: import javax.swing.text.html.FormViewComponentFactory.InputImageIcon;
039:
040: import org.apache.harmony.x.swing.Utilities;
041: import org.apache.harmony.x.swing.text.html.form.Form;
042: import org.apache.harmony.x.swing.text.html.form.FormAttributes;
043: import org.apache.harmony.x.swing.text.html.form.FormButtonModel;
044: import org.apache.harmony.x.swing.text.html.form.FormElement;
045: import org.apache.harmony.x.swing.text.html.form.FormSelectComboBoxModel;
046: import org.apache.harmony.x.swing.text.html.form.FormSelectListModel;
047: import org.apache.harmony.x.swing.text.html.form.FormTextModel;
048: import org.apache.harmony.x.swing.text.html.form.FormToggleButtonModel;
049: import org.apache.harmony.x.swing.internal.nls.Messages;
050:
051: import org.apache.harmony.x.swing.internal.nls.Messages;
052:
053: public class FormView extends ComponentView implements ActionListener {
054: private static final int EMPTY_SPAN = 0;
055:
056: protected class MouseEventListener extends MouseAdapter {
057: public void mouseReleased(final MouseEvent evt) {
058: throw new UnsupportedOperationException(Messages
059: .getString("swing.27")); //$NON-NLS-1$
060: }
061: }
062:
063: /**
064: * @deprecated
065: */
066: public static final String RESET = "Reset";
067:
068: /**
069: * @deprecated
070: */
071: public static final String SUBMIT = "Submit Query";
072:
073: private int inputTypeIndex = FormAttributes.INPUT_TYPE_INDEX_UNDEFINED;
074:
075: public FormView(final Element elem) {
076: super (elem);
077: }
078:
079: public float getMaximumSpan(final int axis) {
080: if (axis != View.X_AXIS && axis != View.Y_AXIS) {
081: throw new IllegalArgumentException(Messages.getString(
082: "swing.00", axis)); //$NON-NLS-1$
083: }
084: if (getComponent() == null || getParent() == null) {
085: return EMPTY_SPAN;
086: }
087:
088: Object tag = getElement().getAttributes().getAttribute(
089: StyleConstants.NameAttribute);
090: if (HTML.Tag.INPUT.equals(tag) || HTML.Tag.TEXTAREA.equals(tag)
091: || HTML.Tag.BUTTON.equals(tag)) {
092:
093: return getPreferredSpan(axis);
094: } else if (HTML.Tag.SELECT.equals(tag)) {
095: if (getAttributes().getAttribute(HTML.Attribute.MULTIPLE) == null) {
096: return getPreferredSpan(axis);
097: }
098: }
099: if (axis == View.X_AXIS) {
100: return getComponent().getMaximumSize().width + 2;
101: } else {
102: return getComponent().getMaximumSize().height;
103: }
104: }
105:
106: public void actionPerformed(final ActionEvent event) {
107: final Object source = event.getSource();
108: try {
109: switch (inputTypeIndex) {
110: case FormAttributes.INPUT_TYPE_PASSWORD_INDEX:
111: // Document doc = ((JTextComponent) source).getDocument();
112: /*
113: * If password is last element in form, form
114: * should be submitted.
115: *
116: * determineValidControls();
117: */
118: break;
119: case FormAttributes.INPUT_TYPE_TEXT_INDEX:
120: /*
121: * If text is last element in form, form
122: * should be submitted.
123: *
124: * determineValidControls();
125: */
126: break;
127: case FormAttributes.INPUT_TYPE_SUBMIT_INDEX:
128: final Form form = ((FormButtonModel) ((AbstractButton) source)
129: .getModel()).getForm();
130: determineValidControls(form);
131: /*
132: * TODO Submit form.
133: */
134: break;
135: case FormAttributes.INPUT_TYPE_RESET_INDEX:
136: resetForm(((FormButtonModel) ((AbstractButton) source)
137: .getModel()).getForm());
138: break;
139: default:
140: // Do nothing
141: break;
142: }
143: } catch (ClassCastException e) {
144: // Do nothing
145: }
146: }
147:
148: public void preferenceChanged(final View child,
149: final boolean width, final boolean height) {
150:
151: if (getParent() != null) {
152: if (inputTypeIndex == FormAttributes.INPUT_TYPE_IMAGE_INDEX) {
153: final AbstractButton image = (AbstractButton) getComponent();
154: if (image == null) {
155: return;
156: }
157: final Dimension size;
158: final Icon icon = image.getIcon();
159: if (!(icon instanceof InputImageIcon)
160: || ((InputImageIcon) icon).imageWasLoaded()) {
161: image.setBorderPainted(false);
162: size = new Dimension(icon.getIconWidth(), icon
163: .getIconHeight());
164: } else {
165: size = image.getPreferredSize();
166: }
167: image.setMinimumSize(size);
168: image.setPreferredSize(size);
169: image.setMaximumSize(size);
170: image.setContentAreaFilled(false);
171: image.setFocusPainted(false);
172: }
173: }
174: super .preferenceChanged(this , width, height);
175: }
176:
177: protected Component createComponent() {
178: try {
179: final AttributeSet attrs = getElement().getAttributes();
180: final FormElement model = (FormElement) attrs
181: .getAttribute(StyleConstants.ModelAttribute);
182:
183: // if (model == null) {
184: // return null;
185: // }
186: // inputTypeIndex = model.getElementType();
187: inputTypeIndex = FormAttributes.getElementTypeIndex(attrs);
188:
189: switch (inputTypeIndex) {
190:
191: case FormAttributes.INPUT_TYPE_BUTTON_INDEX:
192: return FormViewComponentFactory
193: .createInputButtonComponent(model, attrs);
194:
195: case FormAttributes.INPUT_TYPE_IMAGE_INDEX:
196: return FormViewComponentFactory
197: .createInputImageComponent(model, attrs, this );
198:
199: case FormAttributes.INPUT_TYPE_RESET_INDEX:
200: return FormViewComponentFactory
201: .createInputResetComponent(model, attrs, this );
202:
203: case FormAttributes.INPUT_TYPE_SUBMIT_INDEX:
204: return FormViewComponentFactory
205: .createInputSubmitComponent(model, attrs, this );
206:
207: case FormAttributes.INPUT_TYPE_CHECKBOX_INDEX:
208: return FormViewComponentFactory
209: .createInputCheckBoxComponent(model, attrs);
210:
211: case FormAttributes.INPUT_TYPE_RADIO_INDEX:
212: return FormViewComponentFactory
213: .createInputRadioComponent(model, attrs);
214:
215: case FormAttributes.INPUT_TYPE_FILE_INDEX:
216: return FormViewComponentFactory
217: .createInputFileComponent(model, attrs);
218:
219: case FormAttributes.INPUT_TYPE_PASSWORD_INDEX:
220: return FormViewComponentFactory
221: .createInputPasswordComponent(model, attrs,
222: this );
223:
224: case FormAttributes.INPUT_TYPE_TEXT_INDEX:
225: return FormViewComponentFactory
226: .createInputTextComponent(model, attrs, this );
227:
228: case FormAttributes.TEXTAREA_TYPE_INDEX:
229: return FormViewComponentFactory
230: .createTextAreaComponent(model, attrs, this );
231:
232: case FormAttributes.SELECT_LIST_TYPE_INDEX:
233: return FormViewComponentFactory
234: .createSelectMultipleComponent(model, attrs);
235:
236: case FormAttributes.SELECT_COMBOBOX_TYPE_INDEX:
237: return FormViewComponentFactory
238: .createSelectSimpleComponent(model, attrs);
239: /*
240: * TODO Uncomment this, when BUTTON model would be implemented.
241: *
242: * case FormAttributes.BUTTON_TYPE_INDEX:
243: * return FormViewComponentFactory
244: * .createButtonComponent(model, attrs, this);
245: */
246: default:
247: // Do nothing
248: break;
249: }
250: } catch (ClassCastException e) {
251: // Do nothing
252: }
253: return null;
254:
255: }
256:
257: protected void imageSubmit(final String imageData) {
258: // TODO implement imageSubmit
259: throw new UnsupportedOperationException(Messages
260: .getString("swing.27")); //$NON-NLS-1$
261: }
262:
263: protected void submitData(final String data) {
264: // TODO implement submitData
265: throw new UnsupportedOperationException(Messages
266: .getString("swing.27")); //$NON-NLS-1$
267: }
268:
269: private void determineValidControls(final Form form) {
270: FormElement formElement;
271:
272: for (int i = 0; i < form.getElementsCount(); i++) {
273: formElement = form.getElement(i);
274:
275: switch (formElement.getElementType()) {
276: case FormAttributes.INPUT_TYPE_BUTTON_INDEX:
277: case FormAttributes.INPUT_TYPE_IMAGE_INDEX:
278: case FormAttributes.INPUT_TYPE_RESET_INDEX:
279: case FormAttributes.INPUT_TYPE_SUBMIT_INDEX:
280: case FormAttributes.INPUT_TYPE_CHECKBOX_INDEX:
281: case FormAttributes.INPUT_TYPE_RADIO_INDEX:
282: case FormAttributes.INPUT_TYPE_FILE_INDEX:
283: case FormAttributes.INPUT_TYPE_PASSWORD_INDEX:
284: case FormAttributes.INPUT_TYPE_TEXT_INDEX:
285: case FormAttributes.TEXTAREA_TYPE_INDEX:
286: case FormAttributes.SELECT_LIST_TYPE_INDEX:
287: case FormAttributes.SELECT_COMBOBOX_TYPE_INDEX:
288: throw new UnsupportedOperationException(Messages
289: .getString("swing.27")); //$NON-NLS-1$
290: default:
291: // Do nothing
292: break;
293: }
294: }
295: }
296:
297: private void resetForm(final Form form) {
298: FormElement formElement;
299: AttributeSet attrs;
300:
301: for (int i = 0; i < form.getElementsCount(); i++) {
302: formElement = form.getElement(i);
303:
304: attrs = formElement.getAttributes();
305: switch (formElement.getElementType()) {
306: case FormAttributes.INPUT_TYPE_BUTTON_INDEX:
307: case FormAttributes.INPUT_TYPE_IMAGE_INDEX:
308: case FormAttributes.INPUT_TYPE_RESET_INDEX:
309: case FormAttributes.INPUT_TYPE_SUBMIT_INDEX:
310: //Do nothing
311: break;
312: case FormAttributes.INPUT_TYPE_CHECKBOX_INDEX:
313: case FormAttributes.INPUT_TYPE_RADIO_INDEX:
314: resetToogleButton((FormToggleButtonModel) formElement,
315: attrs);
316: break;
317: case FormAttributes.INPUT_TYPE_FILE_INDEX:
318: resetText((FormTextModel) formElement, attrs, false);
319: break;
320: case FormAttributes.INPUT_TYPE_PASSWORD_INDEX:
321: case FormAttributes.INPUT_TYPE_TEXT_INDEX:
322: case FormAttributes.TEXTAREA_TYPE_INDEX:
323: resetText((FormTextModel) formElement, attrs, true);
324: break;
325: case FormAttributes.SELECT_LIST_TYPE_INDEX:
326: FormViewUtils
327: .resetMultipleSelection((FormSelectListModel) formElement);
328: break;
329: case FormAttributes.SELECT_COMBOBOX_TYPE_INDEX:
330: FormViewUtils
331: .resetSimpleSelection((FormSelectComboBoxModel) formElement);
332: break;
333: default:
334: // Do nothing
335: break;
336: }
337: }
338: }
339:
340: private void resetText(final FormTextModel document,
341: final AttributeSet attrs, final boolean loadDefaultText) {
342: try {
343: document.remove(0, document.getLength());
344:
345: String initialContent = document.getInitialContent();
346: if (initialContent == null) {
347: initialContent = (String) attrs
348: .getAttribute(HTML.Attribute.VALUE);
349: }
350:
351: if (loadDefaultText
352: && !Utilities.isEmptyString(initialContent)) {
353: document.insertString(0, initialContent, null);
354: }
355: } catch (BadLocationException e) {
356: }
357: }
358:
359: private void resetToogleButton(
360: final JToggleButton.ToggleButtonModel model,
361: final AttributeSet attrs) {
362: //CHECKED
363: model
364: .setSelected(attrs.getAttribute(HTML.Attribute.CHECKED) != null);
365: }
366: }
|