1 package fr.ifremer.quadrige2.ui.swing.common.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import com.google.common.collect.Lists;
28 import fr.ifremer.quadrige2.core.config.Quadrige2CoreConfiguration;
29 import fr.ifremer.quadrige2.ui.swing.common.ApplicationUI;
30 import fr.ifremer.quadrige2.ui.swing.common.ApplicationUIContext;
31 import fr.ifremer.quadrige2.ui.swing.common.AbstractUIHandler;
32 import fr.ifremer.quadrige2.ui.swing.common.model.ProgressionModel;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.apache.commons.lang3.StringUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.jdesktop.beans.AbstractBean;
38 import org.nuiton.jaxx.application.swing.action.AbstractApplicationAction;
39 import org.nuiton.jaxx.runtime.JaxxFileChooser;
40 import org.springframework.security.access.AccessDeniedException;
41 import org.springframework.security.core.AuthenticationException;
42
43 import java.io.File;
44 import java.util.List;
45
46 import static org.nuiton.i18n.I18n.t;
47
48
49
50
51
52
53
54
55
56
57 public abstract class AbstractAction<M extends AbstractBean, UI extends ApplicationUI<M, ?>, H extends AbstractUIHandler<M, UI>>
58 extends AbstractApplicationAction<M, UI, H> {
59
60 private static final Log LOG = LogFactory.getLog(AbstractAction.class);
61 private List<String> errorMessages;
62
63
64
65
66
67
68
69 public AbstractAction(H handler, boolean hideBody) {
70 super(handler, hideBody);
71 }
72
73
74 @Override
75 public ApplicationUIContext getContext() {
76 return getHandler().getContext();
77 }
78
79
80
81
82
83
84 public void setProgressionModel(ProgressionModel progressionModel) {
85 super.setProgressionModel(progressionModel);
86 }
87
88
89 @Override
90 protected ProgressionModel getProgressionModel() {
91 return (ProgressionModel) super.getProgressionModel();
92 }
93
94
95 @Override
96 protected Quadrige2CoreConfiguration getConfig() {
97 return getContext().getConfiguration();
98 }
99
100
101 @Override
102 protected void sendMessage(String message) {
103 getContext().showInformationMessage(message);
104 }
105
106
107
108
109
110
111 public void addErrorMessage(String errorMessage) {
112 if (errorMessages == null) {
113 errorMessages = Lists.newArrayList();
114 }
115 errorMessages.add(errorMessage);
116 }
117
118
119 @Override
120 public boolean prepareAction() throws Exception {
121 errorMessages = null;
122 return super.prepareAction();
123 }
124
125
126 @Override
127 public void postSuccessAction() {
128 super.postSuccessAction();
129
130 displayErrors();
131 }
132
133
134 @Override
135 public void postFailedAction(Throwable error) {
136
137
138 if (error instanceof AuthenticationException) {
139
140 if (LOG.isInfoEnabled()) {
141 LOG.info("authentication needed");
142 }
143
144
145
146
147
148 } else if (error instanceof AccessDeniedException) {
149
150 if (LOG.isInfoEnabled()) {
151 LOG.info("access denied");
152 }
153
154 getContext().getErrorHelper().showErrorDialog(t("quadrige2.error.authenticate.accessDenied"));
155 }
156 }
157
158
159 @Override
160 protected void createProgressionModelIfRequired(int total) {
161 ProgressionModel progressionModel = getProgressionModel();
162 if (progressionModel == null) {
163 progressionModel = new ProgressionModel();
164 progressionModel.setTotal(total);
165 progressionModel.setMessage("");
166 progressionModel.setCurrent(0);
167 setProgressionModel(progressionModel);
168 } else {
169 progressionModel.adaptTotal(total);
170 }
171 }
172
173
174 @Override
175 protected void displayInfoMessage(String title, String message) {
176 getContext().getDialogHelper().showMessageDialog(message, title);
177 }
178
179
180 @Override
181 protected void displayWarningMessage(String title, String message) {
182 getContext().getDialogHelper().showWarningDialog(message, title);
183 }
184
185
186 protected void displayErrorMessage(String title, String message) {
187 getContext().getDialogHelper().showErrorDialog(message, title);
188 }
189
190 private void displayErrors() {
191 if (CollectionUtils.isNotEmpty(errorMessages)) {
192 for (String errorMessage : errorMessages) {
193 getContext().getErrorHelper().showErrorDialog(errorMessage, null);
194 }
195 }
196 }
197
198
199 @Override
200 protected boolean askOverwriteFile(File file) {
201 return getHandler().askOverwriteFile(file);
202 }
203
204
205 @Override
206 protected boolean askBeforeDelete(String title, String message) {
207 return getHandler().askBeforeDelete(title, message);
208 }
209
210
211
212
213
214
215
216
217
218 protected boolean askBeforeDeleteMany(String title, String message, List<String> list) {
219 return getHandler().askBeforeDeleteMany(title, message, list);
220 }
221
222
223
224
225
226
227
228 protected int askSaveBeforeLeaving(String message) {
229 return getHandler().askSaveBeforeLeaving(message);
230 }
231
232
233
234
235
236
237
238 protected int askBeforeChangeTab(String message) {
239 return getHandler().askBeforeChangeTab(message);
240 }
241
242
243
244
245
246
247
248
249 protected boolean askBeforeReset(String title, String message) {
250 return getHandler().askBeforeReset(title, message);
251 }
252
253
254
255
256
257
258
259 protected boolean askCancelEditBeforeLeaving(String message) {
260 return getHandler().askCancelEditBeforeLeaving(message);
261 }
262
263
264
265
266
267
268
269 protected boolean askDeleteInvalidBeforeLeaving(String message) {
270 return getHandler().askDeleteInvalidBeforeLeaving(message);
271 }
272
273
274 @Override
275 public void setActionDescription(String actionDescription) {
276
277 if (StringUtils.isNotBlank(actionDescription)) {
278 super.setActionDescription(actionDescription);
279 }
280 }
281
282
283 @Override
284 protected File chooseFile(String title, String buttonLabel, String... filters) {
285 return super.chooseFile(title, buttonLabel, filters);
286 }
287
288
289
290
291
292
293
294
295 protected File chooseDirectory(String title, String buttonLabel) {
296 JaxxFileChooser.ToLoadDirectory toLoadDirectory = JaxxFileChooser.forLoadingDirectory();
297 toLoadDirectory.setParent(getContext().getMainUI());
298 toLoadDirectory.setTitle(title);
299 toLoadDirectory.setApprovalText(buttonLabel);
300 return toLoadDirectory.choose();
301 }
302
303 }