001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.model;
042:
043: import com.sun.rave.web.ui.util.MessageUtil;
044: import java.util.ArrayList;
045: import java.io.Serializable;
046:
047: /**
048: * Represents a list of selectable options, which can be used to
049: * initialize the <code>items</code> property of all selector-based components
050: * (<code>Listbox</code>, <code>Dropdown</code>, <code>JumpDropdown</code>,
051: * <code>CheckboxGroup</code>, <code>RadioButtonGroup</code>), and
052: * <code>AddRemove</code>.
053: *
054: * @author gjmurphy
055: */
056: public class OptionsList implements Serializable {
057:
058: private ArrayList options;
059: private ArrayList selectedValues;
060: private boolean isMultiple;
061:
062: public boolean isMultiple() {
063: return isMultiple;
064: }
065:
066: public void setMultiple(boolean isMultiple) {
067: this .isMultiple = isMultiple;
068: }
069:
070: public OptionsList() {
071: options = new ArrayList();
072: selectedValues = new ArrayList();
073: isMultiple = false;
074: }
075:
076: public void setOptions(Option[] options) {
077: this .options.clear();
078: if (options == null)
079: return;
080: for (int i = 0; i < options.length; i++)
081: this .options.add(options[i]);
082: }
083:
084: public Option[] getOptions() {
085: Option[] options = new Option[this .options.size()];
086: this .options.toArray(options);
087: return options;
088: }
089:
090: /**
091: * If this options list is in "multiple" mode, value specified may be
092: * an array of objects or a singleton. Otherwise, the value is treated as
093: * a singleton.
094: */
095: public void setSelectedValue(Object value) {
096: selectedValues.clear();
097: if (value == null)
098: return;
099: if (value instanceof Object[]) {
100: Object[] values = (Object[]) value;
101: for (int i = 0; i < values.length; i++)
102: selectedValues.add(values[i]);
103: } else {
104: selectedValues.add(value);
105: }
106: }
107:
108: /**
109: * If this options list is in "multiple" mode, returns an array of objects;
110: * otherwise, returns a singleton. By default, in "multiple" mode, returns
111: * an empty object array, otherwise, returns a null object.
112: */
113: public Object getSelectedValue() {
114: if (isMultiple) {
115: if (selectedValues.size() == 0)
116: return new Object[0];
117: return selectedValues.toArray();
118: } else {
119: if (selectedValues.size() == 0)
120: return null;
121: return selectedValues.get(0);
122: }
123: }
124:
125: }
|