001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.admin.ui;
023:
024: import org.jboss.portal.common.value.Value;
025: import org.jboss.portal.portlet.info.PreferenceInfo;
026:
027: import java.util.ArrayList;
028: import java.util.Collections;
029: import java.util.List;
030:
031: /**
032: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
033: * @version $Revision: 8785 $
034: */
035: public class PreferencesBean {
036:
037: /** . */
038: List entries;
039:
040: /** . */
041: int selectedIndex;
042:
043: /** . */
044: private boolean mutable;
045:
046: public PreferencesBean(boolean mutable) {
047: this .entries = new ArrayList();
048: this .selectedIndex = -1;
049: this .mutable = mutable;
050: }
051:
052: public boolean isMutable() {
053: return mutable;
054: }
055:
056: public int getSelectedIndex() {
057: return selectedIndex;
058: }
059:
060: public void setSelectedIndex(int selectedIndex) {
061: if (selectedIndex < 0 || selectedIndex >= entries.size()) {
062: throw new IllegalArgumentException();
063: }
064: this .selectedIndex = selectedIndex;
065: }
066:
067: public void unselectEntry() {
068: selectedIndex = -1;
069: }
070:
071: public PreferenceBean getSelectedEntry() {
072: if (selectedIndex < 0 || selectedIndex >= entries.size()) {
073: return null;
074: }
075: return (PreferenceBean) entries.get(selectedIndex);
076: }
077:
078: public void addEntry(PreferenceInfo prefInfo) {
079: if (mutable) {
080: throw new IllegalStateException(
081: "Cannot add non mutable entry to a mutable preferences bean");
082: }
083:
084: //
085: addEntry(new PreferenceBean(prefInfo, null));
086: }
087:
088: public void addEntry(PreferenceInfo prefInfo, Value value) {
089: if (mutable == false) {
090: throw new IllegalStateException(
091: "Cannot add mutable entry to a non mutable preferences bean");
092: }
093:
094: //
095: addEntry(new PreferenceBean(prefInfo, value));
096: }
097:
098: private void addEntry(PreferenceBean pref) {
099: if (pref.container != null) {
100: throw new IllegalStateException(
101: "Already contained somewhere");
102: }
103: pref.container = this ;
104: entries.add(pref);
105: Collections.sort(entries);
106: }
107:
108: public List getEntries() {
109: return Collections.unmodifiableList(entries);
110: }
111:
112: public int getSize() {
113: return entries.size();
114: }
115: }
|