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.util.Stack;
023:
024: import javax.swing.ComboBoxModel;
025: import javax.swing.ListSelectionModel;
026: import javax.swing.text.AttributeSet;
027:
028: import org.apache.harmony.x.swing.text.html.form.FormOption;
029: import org.apache.harmony.x.swing.text.html.form.FormSelectListModel;
030:
031: final class FormViewUtils {
032:
033: private FormViewUtils() {
034: }
035:
036: public static boolean selectOption(final FormOption option,
037: final boolean isMultiple, final boolean notSelected) {
038:
039: if (isMultiple || notSelected) {
040: AttributeSet attrs = option.getAttributes();
041:
042: // SELECTED
043: Object attribute = attrs
044: .getAttribute(HTML.Attribute.SELECTED);
045: if (attribute != null) {
046: option.setSelection(true);
047: return true;
048: }
049: }
050: return false;
051: }
052:
053: public static void resetSimpleSelection(final ComboBoxModel model) {
054: Object element;
055: FormOption option;
056: Stack options = new Stack();
057:
058: boolean notSelected = true;
059: for (int i = model.getSize() - 1; i >= 0; i--) {
060: element = model.getElementAt(i);
061: if (element instanceof FormOption) {
062: option = (FormOption) element;
063: notSelected &= !selectElement(option, false,
064: notSelected);
065: if (option.isSelected()) {
066: model.setSelectedItem(element);
067: }
068: options.push(option);
069: }
070: }
071: if (notSelected) {
072: while (!options.isEmpty()) {
073: option = (FormOption) options.pop();
074: if (option.isEnabled()) {
075: model.setSelectedItem(option);
076: break;
077: }
078: }
079: }
080: }
081:
082: public static void resetMultipleSelection(
083: final FormSelectListModel model) {
084: Object item;
085: FormOption option;
086:
087: ListSelectionModel selectionModel = model.getSelectionModel();
088: selectionModel.clearSelection();
089:
090: for (int i = 0; i < model.getSize(); i++) {
091: item = model.getElementAt(i);
092:
093: if (item instanceof FormOption) {
094: option = (FormOption) item;
095: selectElement(option, true, true);
096: if (option.isSelected()) {
097: selectionModel.addSelectionInterval(i, i);
098: }
099: }
100: }
101: }
102:
103: private static boolean selectElement(final FormOption option,
104: final boolean multiple, final boolean notSelected) {
105:
106: option.setSelection(false);
107: if (option.getDepth() == 0) {
108: return selectOption(option, multiple, notSelected);
109: }
110: return false;
111: }
112:
113: }
|