View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  
25  package com.mindtree.techworks.insight.preferences.model;
26  
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  
32  /**
33   * This is a specialization of PreferenceAttribute, where all the possible
34   * values are added as a list.
35   * 
36   * @see PreferenceAttribute PreferenceAttribute
37   * @author Bindul Bhowmik
38   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
39   */
40  public class ListPreferenceAttribute extends PreferenceAttribute {
41  
42  	/**
43  	 * Used for object serialization
44  	 */
45  	private static final long serialVersionUID = 2935005894479171827L;
46  	
47  	/**
48  	 * The possible options for the value
49  	 */
50  	private ArrayList options;
51  
52  	/**
53  	 * @param id The id of the preference attribute
54  	 * @param value The value associated with the preference attribute
55  	 * @param isPersistant Whether the value will be saved to the store
56  	 * @param isUserModifiable Is the preference modifiable
57  	 * @param parent The parent Preference of this attribute
58  	 * @see PreferenceAttribute#PreferenceAttribute(PreferenceAttributeType,
59  	 *      String, String, boolean, boolean, Preference) PreferenceAttribtue
60  	 */
61  	public ListPreferenceAttribute (String id, String value, boolean isEncrypted,
62  			boolean isPersistant, boolean isUserModifiable, Preference parent) {
63  
64  		super(PreferenceAttributeType.LIST, id, value, isEncrypted, isPersistant,
65  				isUserModifiable, parent);
66  		options = new ArrayList();
67  	}
68  
69  	/**
70  	 * @param id The id of the preference attribute
71  	 * @param value The value associated with the preference attribute
72  	 * @param isPersistant Whether the value will be saved to the store
73  	 * @param isUserModifiable Is the preference modifiable
74  	 * @param parent The parent Preference of this attribute
75  	 * @param inOptions The list of options
76  	 */
77  	public ListPreferenceAttribute (String id, String value,boolean isEncrypted,
78  			boolean isPersistant, boolean isUserModifiable, Preference parent,
79  			List inOptions) {
80  
81  		this(id, value, isEncrypted, isPersistant, isUserModifiable, parent);
82  
83  		if (null != inOptions) {
84  			for (int i = 0; i < inOptions.size(); i++) {
85  				String optionValue = (String) inOptions.get(i);
86  				options.add(optionValue);
87  			}
88  		}
89  	}
90  
91  	/**
92  	 * Adds an option to the list.
93  	 * 
94  	 * @param option Option to add to the list.
95  	 */
96  	public void addOption (String option) {
97  
98  		options.add(option);
99  	}
100 
101 	/**
102 	 * Returns the number of options.
103 	 * 
104 	 * @return Number of options present
105 	 */
106 	public int getOptionsCount () {
107 
108 		return options.size();
109 	}
110 
111 	/**
112 	 * Returns the option from the list at the required position.
113 	 * 
114 	 * @param location Location
115 	 * @return The option at the location
116 	 * @throws ArrayIndexOutOfBoundsException if the location is not present in
117 	 *             the list
118 	 */
119 	public String getOption (int location) {
120 
121 		if (location < 0 || location > options.size()) {
122 			throw new ArrayIndexOutOfBoundsException(
123 					"Requested option not present.");
124 		}
125 		return (String) options.get(location);
126 	}
127 
128 	/**
129 	 * Returns an iterator for the options.
130 	 * 
131 	 * @return Iterator for the options.
132 	 */
133 	public Iterator iterateOptions () {
134 
135 		return options.iterator();
136 	}
137 	
138 	
139 
140 	/**
141 	 * Checks to see if the value is permitted before setting it.
142 	 * @see com.mindtree.techworks.insight.preferences.model.PreferenceAttribute#setValue(java.lang.String)
143 	 * @throws ArrayIndexOutOfBoundsException if the value is not in the options
144 	 */
145 	public void setValue (String value) {
146 
147 		if (options.contains(value)) {
148 			super.setValue(value);
149 		} else {
150 			throw new ArrayIndexOutOfBoundsException(
151 			"Requested option not present.");
152 		}
153 	}
154 }