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.i18n.LocalizedString;
025: import org.jboss.portal.common.value.StringValue;
026: import org.jboss.portal.common.value.Value;
027: import org.jboss.portal.faces.el.PropertyValue;
028: import org.jboss.portal.faces.el.dynamic.DynamicBean;
029: import org.jboss.portal.portlet.info.PreferenceInfo;
030:
031: import javax.faces.event.ActionEvent;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: /**
036: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
037: * @version $Revision: 8785 $
038: */
039: public class PreferenceBean implements Comparable, DynamicBean {
040:
041: /** . */
042: private String name;
043:
044: /** . */
045: private boolean readOnly;
046:
047: /** . */
048: private LocalizedString displayName;
049:
050: /** . */
051: private LocalizedString description;
052:
053: /** . */
054: private Value value;
055:
056: /** . */
057: private String line;
058:
059: /** . */
060: PreferencesBean container;
061:
062: /** . */
063: private boolean stale;
064:
065: public PreferenceBean(PreferenceInfo prefInfo, Value value) {
066: this .name = prefInfo.getKey();
067: this .readOnly = Boolean.TRUE.equals(prefInfo.isReadOnly());
068: this .displayName = prefInfo.getDisplayName();
069: this .description = prefInfo.getDescription();
070: this .value = value;
071: }
072:
073: public String getName() {
074: return name;
075: }
076:
077: public boolean isReadOnly() {
078: return readOnly;
079: }
080:
081: public void setReadOnly(boolean readOnly) {
082: this .readOnly = readOnly;
083: }
084:
085: public LocalizedString getDisplayName() {
086: return displayName;
087: }
088:
089: public void setDisplayName(LocalizedString displayName) {
090: this .displayName = displayName;
091: }
092:
093: public LocalizedString getDescription() {
094: return description;
095: }
096:
097: public void setDescription(LocalizedString description) {
098: this .description = description;
099: }
100:
101: public Value getValue() {
102: return value;
103: }
104:
105: public void setValue(Value value) {
106: this .value = value;
107: }
108:
109: public boolean isStale() {
110: return stale;
111: }
112:
113: public List getIndices() {
114: List list = new ArrayList(value.size());
115: for (int i = 0; i < value.size(); i++) {
116: list.add("index_" + i);
117: }
118: return list;
119: }
120:
121: public String getLine() {
122: return line;
123: }
124:
125: public void setLine(String line) {
126: this .line = line;
127: }
128:
129: public void select() {
130: if (container == null) {
131: throw new IllegalStateException(
132: "Not attached to a container");
133: }
134: container.selectedIndex = container.entries.indexOf(this );
135: }
136:
137: public void deleteLine(ActionEvent event) {
138: String s = event.getComponent().getId();
139: int index = Integer.parseInt(s.substring(4));
140: List list = new ArrayList(value.asStringList());
141: list.remove(index);
142: String[] strings = (String[]) list.toArray(new String[list
143: .size()]);
144: value = new StringValue(strings);
145: stale = true;
146: }
147:
148: public void appendLine() {
149: List list = new ArrayList(value.asStringList());
150: list.add(line);
151: String[] strings = (String[]) list.toArray(new String[list
152: .size()]);
153: value = new StringValue(strings);
154: stale = true;
155: line = null;
156: }
157:
158: public Class getType(Object propertyName)
159: throws IllegalArgumentException {
160: if (propertyName instanceof String) {
161: String s = (String) propertyName;
162: if (s.startsWith("index_")) {
163: return String.class;
164: }
165: }
166: return null;
167: }
168:
169: public PropertyValue getValue(Object propertyName)
170: throws IllegalArgumentException {
171: if (propertyName instanceof String) {
172: String s = (String) propertyName;
173: if (s.startsWith("index_")) {
174: int index = Integer.parseInt(s.substring(6));
175: return new PropertyValue(value.asStringList()
176: .get(index));
177: }
178: }
179: return null;
180: }
181:
182: public boolean setValue(Object propertyName, Object value)
183: throws IllegalArgumentException {
184: if (propertyName instanceof String) {
185: String s = (String) propertyName;
186: if (s.startsWith("index_")) {
187: String[] tmp = this .value.asStringArray();
188: int index = Integer.parseInt(s.substring(6));
189: tmp[index] = (String) value;
190: this .value = new StringValue(tmp);
191: stale = true;
192: return true;
193: }
194: }
195: return false;
196: }
197:
198: public int compareTo(Object o) {
199: PreferenceBean that = (PreferenceBean) o;
200: return name.compareTo(that.name);
201: }
202: }
|