Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LoadProblemRecorder |
|
| 1.8333333333333333;1.833 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.editor.policy.view; | |
11 | ||
12 | import java.awt.Component; | |
13 | import java.io.File; | |
14 | import java.util.ArrayList; | |
15 | import java.util.List; | |
16 | ||
17 | import org.jdesktop.application.ApplicationContext; | |
18 | import org.jdesktop.application.ResourceMap; | |
19 | import org.jdesktop.swingx.JXErrorPane; | |
20 | import org.jdesktop.swingx.error.ErrorInfo; | |
21 | import org.slf4j.Logger; | |
22 | import org.slf4j.LoggerFactory; | |
23 | ||
24 | import org.openpermis.policy.io.ProblemReporter; | |
25 | import org.openpermis.policy.io.ProblemType; | |
26 | ||
27 | ||
28 | /** | |
29 | * Records import problems and shows warning messages. | |
30 | * @since 0.3.0 | |
31 | */ | |
32 | public class LoadProblemRecorder | |
33 | implements ProblemReporter | |
34 | { | |
35 | ||
36 | //---- Static | |
37 | ||
38 | /** | |
39 | * Prefix for all problem keys. | |
40 | * @since 0.3.0 | |
41 | */ | |
42 | private static final String PROBLEM_KEY_PREFIX = "problem."; | |
43 | ||
44 | /** | |
45 | * The logger object of this class. | |
46 | * @since 0.3.0 | |
47 | */ | |
48 | 0 | private static final Logger LOGGER = |
49 | LoggerFactory.getLogger(LoadProblemRecorder.class); | |
50 | ||
51 | /** | |
52 | * Dialog format for warnings. | |
53 | * @since 0.3.0 | |
54 | */ | |
55 | private static final int FORMAT_LIST = 1; | |
56 | ||
57 | /** | |
58 | * Dialog format for warnings. | |
59 | * @since 0.3.0 | |
60 | */ | |
61 | private static final int FORMAT_TABLE = 2; | |
62 | ||
63 | //---- State | |
64 | ||
65 | /** | |
66 | * The translations for problem types handled by this reporter. | |
67 | * @since 0.3.0 | |
68 | */ | |
69 | private final ResourceMap resourceMap; | |
70 | ||
71 | /** | |
72 | * A list of warnings recorded. | |
73 | * @since 0.3.0 | |
74 | */ | |
75 | private final List<String> warnings; | |
76 | ||
77 | //---- Constructors | |
78 | ||
79 | /** | |
80 | * Creates a new problem reporter for import problems. | |
81 | * @param context the context used to retrieve translations. | |
82 | * @since 0.3.0 | |
83 | */ | |
84 | 0 | public LoadProblemRecorder (ApplicationContext context) { |
85 | 0 | this.resourceMap = context.getResourceMap(getClass()); |
86 | 0 | this.warnings = new ArrayList<String>(); |
87 | 0 | } |
88 | ||
89 | //---- Methods | |
90 | ||
91 | /** | |
92 | * Tests if there have been any warnings during the import. | |
93 | * @return {@code true} if a warning was recorded, {@code false} otherwise. | |
94 | * @since 0.3.0 | |
95 | */ | |
96 | public boolean hasWarnings () { | |
97 | 0 | return !this.warnings.isEmpty(); |
98 | } | |
99 | ||
100 | /** | |
101 | * Returns an HTML version of the collected warnings. | |
102 | * @param file the file for which to issue the warning message. | |
103 | * @param format {FORMAT_LIST, FORMAT_TABLE} | |
104 | * @return the warning details. | |
105 | * @since 0.3.0 | |
106 | */ | |
107 | private String getWarningDetails (File file, int format) { | |
108 | 0 | final StringBuilder sb = new StringBuilder(); |
109 | 0 | sb.append("<tt>").append(file.getName()).append(":</tt><br />"); |
110 | 0 | if (format == FORMAT_LIST) { |
111 | 0 | sb.append("<ol style=\"margin-left:24px\">"); |
112 | 0 | for (String warning : this.warnings) { |
113 | 0 | sb.append("<li style=\"margin-bottom:8px\">").append(warning).append("</li>"); |
114 | } | |
115 | 0 | } else if (format == FORMAT_TABLE) { |
116 | 0 | sb.append("</ol>"); |
117 | String part1; | |
118 | String part2; | |
119 | 0 | int row = 1; |
120 | 0 | sb.append("<table border=\"1\">"); |
121 | 0 | sb.append("<tr style=\"margin-bottom:8px\">"); |
122 | 0 | sb.append("<th>" + this.resourceMap.getString("numberTitle") + "</th>"); |
123 | 0 | sb.append("<th>" + this.resourceMap.getString("locationTitle") + "</th>"); |
124 | 0 | sb.append("<th>" + this.resourceMap.getString("warningTitle") + "</th>"); |
125 | 0 | sb.append("</tr>"); |
126 | 0 | for (String warning : this.warnings) { |
127 | 0 | part1 = warning.substring(0, warning.indexOf(": ")); |
128 | 0 | part2 = warning.substring(part1.length() + 2); |
129 | 0 | part1 = part1.replace("Location", ""); |
130 | 0 | part1.trim(); |
131 | 0 | sb.append("<tr style=\"margin-bottom:8px\">"); |
132 | 0 | sb.append("<td>"); |
133 | 0 | sb.append(row); |
134 | 0 | sb.append("</td>"); |
135 | 0 | sb.append("<td>"); |
136 | 0 | sb.append(part1); |
137 | 0 | sb.append("</td>"); |
138 | 0 | sb.append("<td>"); |
139 | 0 | sb.append(part2); |
140 | 0 | sb.append("</td>"); |
141 | 0 | sb.append("</tr>"); |
142 | 0 | row++; |
143 | } | |
144 | 0 | sb.append("</table>"); |
145 | } | |
146 | 0 | return sb.toString(); |
147 | } | |
148 | ||
149 | /** | |
150 | * Shows a warning dialog with detail messages for the specified file. | |
151 | * @param owner the owner of the dialog to display. | |
152 | * @param file the file for which to show the warnings. | |
153 | * @since 0.3.0 | |
154 | */ | |
155 | public void showDialog (Component owner, File file) { | |
156 | 0 | JXErrorPane.showDialog( |
157 | owner, | |
158 | new ErrorInfo( | |
159 | this.resourceMap.getString("dialog.title"), | |
160 | this.resourceMap.getString("dialog.message", file.getName()), | |
161 | getWarningDetails(file, FORMAT_TABLE), | |
162 | null, | |
163 | null, | |
164 | null, | |
165 | null | |
166 | ) | |
167 | ); | |
168 | 0 | } |
169 | ||
170 | /** | |
171 | * Returns the translation key for a problem type. | |
172 | * @param type the problem type for which to retrieve the key. | |
173 | * @return the translation key for the problem type. | |
174 | * @since 0.3.0 | |
175 | */ | |
176 | private String getKey (ProblemType type) { | |
177 | 0 | return PROBLEM_KEY_PREFIX + type.getKey(); |
178 | } | |
179 | ||
180 | //---- ProblemReporter | |
181 | ||
182 | /** | |
183 | * @since 0.3.0 | |
184 | */ | |
185 | public void reportProblem (ProblemType type, Object... parameters) { | |
186 | try { | |
187 | 0 | this.warnings.add(this.resourceMap.getString(getKey(type), parameters)); |
188 | 0 | } catch (Exception e) { |
189 | 0 | LOGGER.error("key [{}] missing in resourcemap", getKey(type)); |
190 | 0 | this.warnings.add(type.getMessage(parameters)); |
191 | 0 | } |
192 | 0 | } |
193 | ||
194 | } |