1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.usability; |
---|
16 | |
---|
17 | import java.io.IOException; |
---|
18 | import java.io.InputStream; |
---|
19 | import java.io.Serializable; |
---|
20 | import java.util.ArrayList; |
---|
21 | import java.util.Collection; |
---|
22 | import java.util.List; |
---|
23 | import java.util.Map; |
---|
24 | |
---|
25 | import javax.xml.bind.JAXBContext; |
---|
26 | import javax.xml.bind.JAXBElement; |
---|
27 | import javax.xml.bind.Unmarshaller; |
---|
28 | |
---|
29 | /** |
---|
30 | * TODO comment |
---|
31 | * |
---|
32 | * @version $Revision: $ $Date: 18.07.2012$ |
---|
33 | * @author 2012, last modified by $Author: pharms$ |
---|
34 | */ |
---|
35 | public enum UsabilitySmellDescription implements Serializable { |
---|
36 | |
---|
37 | INEFFICIENT_ACTIONS, |
---|
38 | TEXT_FIELD_INPUT_RATIO, |
---|
39 | TEXT_FIELD_INPUT_REPETITIONS, |
---|
40 | TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO, |
---|
41 | COOCCURENCE_SUCCEED, |
---|
42 | COOCCURENCE_PRECED, |
---|
43 | HIGH_EVENT_COVERAGE, |
---|
44 | HIGH_TARGET_DISTANCE, |
---|
45 | MISSING_FEEDBACK, |
---|
46 | TASK_RETRIED, |
---|
47 | UNUSED_GUI_ELEMENTS, |
---|
48 | DATA_ENTRY_METHOD_CHANGE, |
---|
49 | GOOD_DEFAULTS, |
---|
50 | CHECK_BOX_SINGLE_SELECTION, |
---|
51 | COMMON_TASK_RATE, |
---|
52 | MISLEADING_CLICK_CUE, |
---|
53 | MOST_OFTEN_DONE_FIRST; |
---|
54 | |
---|
55 | /** */ |
---|
56 | private static final String DEFAULT_MESSAGES_FILE = "smellDescriptions_en.xml"; |
---|
57 | |
---|
58 | /** */ |
---|
59 | private static SmellDescriptions sSmellDescriptions; |
---|
60 | |
---|
61 | /** */ |
---|
62 | private SmellDescription smellDescription; |
---|
63 | |
---|
64 | /** |
---|
65 | * |
---|
66 | */ |
---|
67 | private UsabilitySmellDescription() { |
---|
68 | init(); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * |
---|
73 | */ |
---|
74 | @SuppressWarnings("unchecked") |
---|
75 | private void init() { |
---|
76 | synchronized (this.getClass()) { |
---|
77 | if (sSmellDescriptions == null) { |
---|
78 | InputStream inputStream = |
---|
79 | ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE); |
---|
80 | |
---|
81 | try { |
---|
82 | String packageName = SmellDescriptions.class.getPackage().getName(); |
---|
83 | JAXBContext jaxbContext = JAXBContext.newInstance(packageName); |
---|
84 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
---|
85 | |
---|
86 | sSmellDescriptions = |
---|
87 | ((JAXBElement<SmellDescriptions>) unmarshaller.unmarshal(inputStream)) |
---|
88 | .getValue(); |
---|
89 | } |
---|
90 | catch (Exception e) { |
---|
91 | throw new RuntimeException |
---|
92 | ("error while initializing usability smell descriptions", e); |
---|
93 | } |
---|
94 | finally { |
---|
95 | if (inputStream != null) { |
---|
96 | try { |
---|
97 | inputStream.close(); |
---|
98 | } |
---|
99 | catch (IOException e) { |
---|
100 | // ignore |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | for (SmellDescription description : sSmellDescriptions.getSmellDescription()) { |
---|
108 | if (this.name().equals(description.getSmellId())) { |
---|
109 | smellDescription = description; |
---|
110 | break; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | if (smellDescription == null) { |
---|
115 | throw new RuntimeException |
---|
116 | ("error while initializing usability smell descriptions. No " + |
---|
117 | "description text available for description " + this.name()); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * |
---|
123 | */ |
---|
124 | public String[] getDescriptionParameters() { |
---|
125 | List<String> parameters = new ArrayList<String>(); |
---|
126 | |
---|
127 | for (Object fragment : smellDescription.getTextFragmentOrParameterFragment()) { |
---|
128 | if (fragment instanceof ParameterFragment) { |
---|
129 | parameters.add(((ParameterFragment) fragment).getParameterName()); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | return parameters.toArray(new String[parameters.size()]); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * |
---|
138 | */ |
---|
139 | public String toString(Map<String, Object> parameters) throws IllegalArgumentException { |
---|
140 | StringBuffer result = new StringBuffer(); |
---|
141 | |
---|
142 | for (Object fragment : smellDescription.getTextFragmentOrParameterFragment()) { |
---|
143 | if (result.length() > 0) { |
---|
144 | result.append(" "); |
---|
145 | } |
---|
146 | |
---|
147 | if (fragment instanceof ParameterFragment) { |
---|
148 | Object value = null; |
---|
149 | if (parameters != null) { |
---|
150 | value = parameters.get(((ParameterFragment) fragment).getParameterName()); |
---|
151 | } |
---|
152 | |
---|
153 | if (value != null) { |
---|
154 | if (value instanceof Collection<?>) { |
---|
155 | int counter = 1; |
---|
156 | for (Object elem : ((Collection<?>) value)) { |
---|
157 | result.append('\n'); |
---|
158 | result.append(counter++); |
---|
159 | result.append(".: "); |
---|
160 | result.append(elem); |
---|
161 | } |
---|
162 | } |
---|
163 | else { |
---|
164 | result.append(value.toString()); |
---|
165 | } |
---|
166 | } |
---|
167 | else { |
---|
168 | throw new IllegalArgumentException |
---|
169 | ("required parameter \"" + |
---|
170 | ((ParameterFragment) fragment).getParameterName() + |
---|
171 | "\" for usability smell description " + this.name() + " not provided"); |
---|
172 | } |
---|
173 | } |
---|
174 | else { |
---|
175 | result.append(getFragmentString(fragment)); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | return result.toString(); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * |
---|
184 | */ |
---|
185 | public List<Object> toFragmentList(Map<String, Object> parameters) |
---|
186 | throws IllegalArgumentException |
---|
187 | { |
---|
188 | List<Object> result = new ArrayList<Object>(); |
---|
189 | |
---|
190 | for (Object fragment : smellDescription.getTextFragmentOrParameterFragment()) { |
---|
191 | if (fragment instanceof ParameterFragment) { |
---|
192 | Object value = null; |
---|
193 | if (parameters != null) { |
---|
194 | value = parameters.get(((ParameterFragment) fragment).getParameterName()); |
---|
195 | } |
---|
196 | |
---|
197 | if (value != null) { |
---|
198 | result.add(value); |
---|
199 | } |
---|
200 | else { |
---|
201 | throw new IllegalArgumentException |
---|
202 | ("required parameter \"" + |
---|
203 | ((ParameterFragment) fragment).getParameterName() + |
---|
204 | "\" for usability smell description " + this.name() + " not provided"); |
---|
205 | } |
---|
206 | } |
---|
207 | else { |
---|
208 | result.add(getFragmentString(fragment)); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | return result; |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * |
---|
217 | */ |
---|
218 | public String getBriefDescription() { |
---|
219 | return smellDescription.briefText; |
---|
220 | } |
---|
221 | |
---|
222 | /* |
---|
223 | * (non-Javadoc) |
---|
224 | * |
---|
225 | * @see java.lang.Enum#toString() |
---|
226 | */ |
---|
227 | @Override |
---|
228 | public String toString() { |
---|
229 | StringBuffer result = new StringBuffer(); |
---|
230 | |
---|
231 | int paramCount = 1; |
---|
232 | for (Object fragment : smellDescription.getTextFragmentOrParameterFragment()) { |
---|
233 | if (result.length() > 0) { |
---|
234 | result.append(" "); |
---|
235 | } |
---|
236 | |
---|
237 | if (fragment instanceof ParameterFragment) { |
---|
238 | result.append("<parameter"); |
---|
239 | result.append(paramCount++); |
---|
240 | result.append(">"); |
---|
241 | } |
---|
242 | else { |
---|
243 | result.append(getFragmentString(fragment)); |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | return result.toString(); |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * |
---|
252 | */ |
---|
253 | private String getFragmentString(Object fragment) { |
---|
254 | String fragmentStr = fragment.toString().trim(); |
---|
255 | |
---|
256 | fragmentStr = fragmentStr.replaceAll("\n", " "); |
---|
257 | |
---|
258 | while (fragmentStr.indexOf(" ") > -1) { |
---|
259 | fragmentStr = fragmentStr.replaceAll(" ", " "); |
---|
260 | } |
---|
261 | |
---|
262 | return fragmentStr; |
---|
263 | } |
---|
264 | |
---|
265 | } |
---|