source: trunk/autoquest-core-usability/src/main/java/de/ugoe/cs/autoquest/usability/UsabilitySmellDescription.java @ 1918

Last change on this file since 1918 was 1918, checked in by pharms, 9 years ago
  • extension with further smell detections
  • may not fully work. But Hudson is more happy because compile errors should be gone
File size: 7.9 KB
Line 
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
15package de.ugoe.cs.autoquest.usability;
16
17import java.io.IOException;
18import java.io.InputStream;
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.List;
22import java.util.Map;
23
24import javax.xml.bind.JAXBContext;
25import javax.xml.bind.JAXBElement;
26import javax.xml.bind.Unmarshaller;
27
28/**
29 * TODO comment
30 *
31 * @version $Revision: $ $Date: 18.07.2012$
32 * @author 2012, last modified by $Author: pharms$
33 */
34public enum UsabilitySmellDescription {
35   
36    INEFFICIENT_ACTIONS,
37    TEXT_FIELD_INPUT_RATIO,
38    TEXT_FIELD_INPUT_REPETITIONS,
39    TEXT_FIELD_NO_LETTER_OR_DIGIT_RATIO,
40    COOCCURENCE_SUCCEED,
41    COOCCURENCE_PRECED,
42    HIGH_EVENT_COVERAGE,
43    HIGH_TARGET_DISTANCE,
44    MISSING_FEEDBACK,
45    UNUSED_GUI_ELEMENTS,
46    DATA_ENTRY_METHOD_CHANGE,
47    GOOD_DEFAULTS,
48    CHECK_BOX_SINGLE_SELECTION,
49    COMMON_TASK_RATE,
50    MISLEADING_CLICK_CUE,
51    MOST_OFTEN_DONE_FIRST;
52
53    /** */
54    private static final String DEFAULT_MESSAGES_FILE = "smellDescriptions_en.xml";
55
56    /** */
57    private static SmellDescriptions sSmellDescriptions;
58
59    /** */
60    private SmellDescription smellDescription;
61
62    /**
63     *
64     */
65    private UsabilitySmellDescription() {
66        init();
67    }
68
69    /**
70     *
71     */
72    @SuppressWarnings("unchecked")
73    private void init() {
74        synchronized (this.getClass()) {
75            if (sSmellDescriptions == null) {
76                InputStream inputStream =
77                    ClassLoader.getSystemResourceAsStream(DEFAULT_MESSAGES_FILE);
78
79                try {
80                    String packageName = SmellDescriptions.class.getPackage().getName();
81                    JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
82                    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
83
84                    sSmellDescriptions =
85                        ((JAXBElement<SmellDescriptions>) unmarshaller.unmarshal(inputStream))
86                            .getValue();
87                }
88                catch (Exception e) {
89                    throw new RuntimeException
90                        ("error while initializing usability smell descriptions", e);
91                }
92                finally {
93                    if (inputStream != null) {
94                        try {
95                            inputStream.close();
96                        }
97                        catch (IOException e) {
98                            // ignore
99                        }
100                    }
101                }
102            }
103        }
104
105        for (SmellDescription description : sSmellDescriptions.getSmellDescription()) {
106            if (this.name().equals(description.getSmellId())) {
107                smellDescription = description;
108                break;
109            }
110        }
111
112        if (smellDescription == null) {
113            throw new RuntimeException
114                ("error while initializing usability smell descriptions. No " +
115                 "description text available for description " + this.name());
116        }
117    }
118
119    /**
120     *
121     */
122    public String[] getDescriptionParameters() {
123        List<String> parameters = new ArrayList<String>();
124
125        for (Object fragment : smellDescription.getTextFragmentOrParameterFragment()) {
126            if (fragment instanceof ParameterFragment) {
127                parameters.add(((ParameterFragment) fragment).getParameterName());
128            }
129        }
130
131        return parameters.toArray(new String[parameters.size()]);
132    }
133
134    /**
135     *
136     */
137    public String toString(Map<String, Object> parameters) throws IllegalArgumentException {
138        StringBuffer result = new StringBuffer();
139
140        for (Object fragment : smellDescription.getTextFragmentOrParameterFragment()) {
141            if (result.length() > 0) {
142                result.append(" ");
143            }
144
145            if (fragment instanceof ParameterFragment) {
146                Object value = null;
147                if (parameters != null) {
148                    value = parameters.get(((ParameterFragment) fragment).getParameterName());
149                }
150
151                if (value != null) {
152                    if (value instanceof Collection<?>) {
153                        int counter = 1;
154                        for (Object elem : ((Collection<?>) value)) {
155                            result.append('\n');
156                            result.append(counter++);
157                            result.append(".: ");
158                            result.append(elem);
159                        }
160                    }
161                    else {
162                        result.append(value.toString());
163                    }
164                }
165                else {
166                    throw new IllegalArgumentException
167                        ("required parameter \"" +
168                         ((ParameterFragment) fragment).getParameterName() +
169                         "\" for usability smell description " + this.name() + " not provided");
170                }
171            }
172            else {
173                result.append(getFragmentString(fragment));
174            }
175        }
176
177        return result.toString();
178    }
179
180    /**
181     *
182     */
183    public List<Object> toFragmentList(Map<String, Object> parameters)
184        throws IllegalArgumentException
185    {
186        List<Object> result = new ArrayList<Object>();
187
188        for (Object fragment : smellDescription.getTextFragmentOrParameterFragment()) {
189            if (fragment instanceof ParameterFragment) {
190                Object value = null;
191                if (parameters != null) {
192                    value = parameters.get(((ParameterFragment) fragment).getParameterName());
193                }
194
195                if (value != null) {
196                    result.add(value);
197                }
198                else {
199                    throw new IllegalArgumentException
200                        ("required parameter \"" +
201                         ((ParameterFragment) fragment).getParameterName() +
202                         "\" for usability smell description " + this.name() + " not provided");
203                }
204            }
205            else {
206                result.add(getFragmentString(fragment));
207            }
208        }
209
210        return result;
211    }
212   
213    /**
214     *
215     */
216    public String getBriefDescription() {
217        return smellDescription.briefText;
218    }
219
220    /*
221     * (non-Javadoc)
222     *
223     * @see java.lang.Enum#toString()
224     */
225    @Override
226    public String toString() {
227        StringBuffer result = new StringBuffer();
228
229        int paramCount = 1;
230        for (Object fragment : smellDescription.getTextFragmentOrParameterFragment()) {
231            if (result.length() > 0) {
232                result.append(" ");
233            }
234
235            if (fragment instanceof ParameterFragment) {
236                result.append("<parameter");
237                result.append(paramCount++);
238                result.append(">");
239            }
240            else {
241                result.append(getFragmentString(fragment));
242            }
243        }
244
245        return result.toString();
246    }
247
248    /**
249     *
250     */
251    private String getFragmentString(Object fragment) {
252        String fragmentStr = fragment.toString().trim();
253
254        fragmentStr = fragmentStr.replaceAll("\n", " ");
255
256        while (fragmentStr.indexOf("  ") > -1) {
257            fragmentStr = fragmentStr.replaceAll("  ", " ");
258        }
259
260        return fragmentStr;
261    }
262
263}
Note: See TracBrowser for help on using the repository browser.