1 package fr.ifremer.quadrige2.ui.swing.common.content;
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 import com.google.common.base.Preconditions;
27 import fr.ifremer.quadrige2.core.security.AuthenticationInfo;
28 import fr.ifremer.quadrige2.ui.swing.common.*;
29 import fr.ifremer.quadrige2.ui.swing.common.content.db.DbManagerUIHandler;
30 import fr.ifremer.quadrige2.ui.swing.common.content.login.LoginUI;
31 import jaxx.runtime.SwingUtil;
32 import jaxx.runtime.validator.swing.SwingValidator;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.nuiton.jaxx.application.bean.RemoveablePropertyChangeListener;
37 import org.nuiton.jaxx.application.swing.util.CloseableUI;
38
39 import javax.swing.*;
40 import java.awt.Cursor;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.beans.PropertyChangeEvent;
44 import java.util.Locale;
45
46 import static org.nuiton.i18n.I18n.t;
47
48
49
50
51
52
53 public abstract class AbstractMainUIHandler<M extends ApplicationUIContext, UI extends MainUI<? extends AbstractMainUIHandler>> extends AbstractUIHandler<ApplicationUIContext, UI> {
54
55
56
57
58 private static final Log LOG = LogFactory.getLog(AbstractMainUIHandler.class);
59
60 private JComponent currentBody;
61
62 private Timer messageTimer;
63
64
65
66
67 public void reloadDbManagerText() {
68
69 ApplicationUI<?, ?> body = (ApplicationUI<?, ?>) currentBody;
70
71 if (body != null && body.getHandler() instanceof DbManagerUIHandler) {
72 DbManagerUIHandler dbManagerUIHandler = (DbManagerUIHandler) body.getHandler();
73 dbManagerUIHandler.updateMessage();
74 }
75 }
76
77
78
79
80
81
82
83
84 @Override
85 public void beforeInit(UI ui) {
86 super.beforeInit(ui);
87 ApplicationUIContext context = ApplicationUIContext.getInstance();
88 ui.setContextValue(context);
89
90 context.installActionUI(ui);
91 context.addPropertyChangeListener(new RemoveablePropertyChangeListener() {
92 @Override
93 public void propertyChange(final PropertyChangeEvent evt) {
94 String propertyName = evt.getPropertyName();
95 if (ApplicationUIContext.PROPERTIES_TO_SAVE.contains(propertyName)) {
96
97
98 changeTitle();
99
100 } else if (propertyName.equals(ApplicationUIContext.PROPERTY_SCREEN)) {
101
102
103 setScreen((Screen) evt.getNewValue());
104 }
105 }
106 });
107 ui.setContextValue(ui, MainUI.class.getName());
108
109
110 context.addPropertyChangeListener(ApplicationUIContext.PROPERTY_BUSY, new RemoveablePropertyChangeListener() {
111
112 @Override
113 public void propertyChange(PropertyChangeEvent evt) {
114 Boolean newvalue = (Boolean) evt.getNewValue();
115 updateBusyState(newvalue != null && newvalue);
116 }
117 });
118
119
120 context.addPropertyChangeListener(ApplicationUIContext.PROPERTY_HIDE_BODY, new RemoveablePropertyChangeListener() {
121
122 @Override
123 public void propertyChange(PropertyChangeEvent evt) {
124 Boolean newvalue = (Boolean) evt.getNewValue();
125 if (getUI() != null && getUI().getBody() != null) {
126 getUI().getBody().setVisible(newvalue != null && newvalue);
127 }
128 }
129 });
130
131 }
132
133
134
135
136
137
138 protected void updateBusyState(boolean busy) {
139
140 if (LOG.isDebugEnabled()) {
141 LOG.debug(busy ? "block ui in busy mode" : "unblock ui in none busy mode");
142 }
143
144
145 getUI().setCursor(busy ? Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR) : Cursor.getDefaultCursor());
146
147 if (getUI().getMenu() != null) {
148 if (busy) getUI().getMenu().removeNotify();
149 else getUI().getMenu().addNotify();
150 }
151 }
152
153
154
155
156 @Override
157 public void afterInit(UI ui) {
158
159 initUI(ui);
160
161
162
163
164
165
166
167
168
169
170 SwingUtil.setLayerUI(getUI().getBody(), getUI().getBusyBlockLayerUI());
171
172
173
174
175
176
177 getContext().initSwingSession(getUI());
178
179 changeTitle();
180
181 JToolBar bar = getUI().getBottomBar();
182 getUI().getStatus().addWidget(bar, 0);
183
184 messageTimer = new Timer(3000, new ActionListener() {
185
186 @Override
187 public void actionPerformed(ActionEvent e) {
188 getUI().getStatus().clearStatus();
189 }
190 });
191 messageTimer.setRepeats(false);
192
193 }
194
195
196
197
198 @Override
199 protected JComponent getComponentToFocus() {
200 return currentBody;
201 }
202
203
204
205
206
207
208 public JComponent getCurrentBody() {
209 return currentBody;
210 }
211
212
213
214
215 public void closeUI() {
216
217
218 onCloseUI();
219
220 getContext().saveSwingSession(null);
221
222
223 getContext().close();
224
225 }
226
227
228
229
230 @Override
231 public void onCloseUI() {
232
233 if (getUI() != null) {
234
235
236 ApplicationUIUtil.removeBindings(getUI());
237 getUI().setVisible(false);
238 }
239 }
240
241
242
243
244
245
246
247
248 public void reloadUI() {
249
250
251 onCloseUI();
252
253
254
255 showNotImplementedFunctionnality();
256
257 }
258
259
260
261
262
263
264 public void changeLocale(Locale locale) {
265
266
267 getModel().setLocale(locale);
268
269
270 reloadUI();
271 }
272
273
274
275
276 @Override
277 public final void showInformationMessage(String message) {
278 getUI().getStatus().setStatus(ApplicationUIUtil.getHtmlString(message));
279
280 messageTimer.restart();
281 }
282
283
284
285
286
287
288 public void registerValidator(SwingValidator<?> validator) {
289 getUI().getValidatorMessageWidget().registerValidator(validator);
290 }
291
292
293
294
295 @Override
296 public void clearValidators() {
297 getUI().getValidatorMessageWidget().clearValidators();
298 }
299
300
301
302
303
304
305 public boolean quitCurrentScreen() {
306
307 boolean canClose;
308 if (getContext().getScreen() == null || currentBody == null) {
309
310
311 canClose = true;
312 if (LOG.isWarnEnabled()) {
313 LOG.warn("==================================================");
314 LOG.warn("No screen, Should then skipCheckCurrent in action.");
315 LOG.warn("==================================================");
316 }
317 } else {
318 ApplicationUI<?, ?> body = (ApplicationUI<?, ?>) currentBody;
319 Preconditions.checkNotNull(currentBody);
320 AbstractUIHandler<?, ?> handler = body.getHandler();
321 if (handler instanceof CloseableUI) {
322
323
324 canClose = ((CloseableUI) handler).quitUI();
325 } else {
326
327
328 canClose = true;
329 }
330 }
331 return canClose;
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345 protected void setScreen(Screen screen) {
346
347 ApplicationUIContext context = getContext();
348
349
350 if (currentBody != null) {
351 ApplicationUI<?, ?> body = (ApplicationUI<?, ?>) currentBody;
352 body.getHandler().onCloseUI();
353
354 getContext().saveSwingSession(currentBody);
355
356 getUI().getBody().remove(currentBody);
357
358 currentBody = null;
359 }
360
361 if (screen != null) {
362
363
364 ApplicationUI<?, ?> screenUI = context.getApplicationUI(screen);
365
366 if (screenUI != null) {
367
368
369 Icon icon = screenUI.getContextValue(Icon.class);
370
371 final JButton showHelp = getUI().getShowHelp();
372 final JToolBar rightDecoration = new JToolBar();
373 rightDecoration.setFloatable(false);
374 rightDecoration.setOpaque(false);
375 rightDecoration.setBorderPainted(false);
376 rightDecoration.add(showHelp, 0);
377 this.currentBody = (JComponent) screenUI;
378 context.getSwingSession().add(currentBody, true);
379 getUI().getBody().setTitle(screenUI.getHandler().getTitle());
380 getUI().getBody().setContentContainer(currentBody);
381 getUI().getBody().setLeftDecoration(new JLabel(icon));
382 getUI().getBody().setRightDecoration(rightDecoration);
383 getUI().getBody().getRightDecoration().setVisible(true);
384
385 } else {
386
387
388 context.getDialogHelper().showErrorDialog(t("quadrige2.main.screen.error", screen.getName()));
389 context.setFallBackScreen();
390 }
391 }
392 }
393
394
395
396
397 public void changeTitle() {
398
399 String title = getContext().getSelectedScreenTitle();
400
401 getUI().setTitle(t("quadrige2.main.title.application",
402 getConfig().getApplicationName(),
403 getConfig().getVersion(),
404 title));
405
406 }
407
408
409
410
411
412
413
414 public AuthenticationInfo askAuthenticationInfo(String url) {
415
416 AuthenticationInfo authentication;
417
418
419 if (getContext().getAuthenticationInfo() == null
420 && StringUtils.isNotBlank(getConfig().getAuthenticationDefaultUsername())
421 && StringUtils.isNotBlank(getConfig().getAuthenticationDefaultPassword())) {
422 authentication = new AuthenticationInfo(
423 getConfig().getAuthenticationDefaultUsername(),
424 getConfig().getAuthenticationDefaultPassword()
425 );
426 }
427
428
429 else {
430 LoginUI loginUI = new LoginUI((ApplicationUI) getUI());
431 loginUI.getModel().setUrl(url);
432 loginUI.getModel().setAuthenticationInfo(getContext().getAuthenticationInfo());
433 openDialogForceOnTop(loginUI);
434 authentication = loginUI.getModel().getAuthenticationInfo();
435 }
436
437 if (authentication != null) {
438
439
440 getContext().setAuthenticationInfo(authentication);
441 }
442
443 return authentication;
444 }
445
446 }