1 package fr.ifremer.quadrige3.ui.swing.synchro.log;
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 fr.ifremer.quadrige3.core.dao.technical.os.ExternalEditors;
28 import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
29 import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
30 import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
31 import fr.ifremer.quadrige3.ui.swing.AbstractUIHandler;
32 import fr.ifremer.quadrige3.ui.swing.component.wrapeditor.WrapEditorKit;
33 import jaxx.runtime.SwingUtil;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.lang3.StringUtils;
36 import org.nuiton.i18n.I18n;
37
38 import javax.swing.JComponent;
39 import javax.swing.text.EditorKit;
40 import java.io.File;
41 import java.io.IOException;
42 import java.nio.charset.Charset;
43
44
45
46
47
48
49 public class SynchroLogUIHandler extends AbstractUIHandler<SynchroLogUIModel, SynchroLogUI> {
50
51
52 @Override
53 public void beforeInit(SynchroLogUI ui) {
54 super.beforeInit(ui);
55
56 SynchroLogUIModel model = new SynchroLogUIModel();
57 ui.setContextValue(model);
58
59 ui.setContextValue(SwingUtil.createActionIcon("synchro-log"));
60
61 }
62
63
64 @Override
65 public void afterInit(SynchroLogUI ui) {
66 initUI(ui);
67
68 if (getContext().isAuthenticated()) {
69
70
71 File logFile = ClientServiceLocator.instance().getSynchroHistoryService().getHistoryFileByUserId(SecurityContextHelper.getQuadrigeUserId());
72 getModel().setLogFile(logFile);
73
74
75 String historyContent = null;
76 if (logFile.exists()) {
77 try {
78 historyContent = FileUtils.readFileToString(logFile, Charset.defaultCharset());
79 } catch (IOException e) {
80 throw new QuadrigeTechnicalException(I18n.t("quadrige3.service.synchroHistory.readContent.failed"), e);
81 }
82 }
83 if (StringUtils.isBlank(historyContent)) {
84 historyContent = I18n.t("quadrige3.main.action.synchro.log.empty");
85 }
86 getModel().setMessage(historyContent);
87 }
88
89
90 else {
91 getModel().setMessage(I18n.t("quadrige3.login.infoMessage", I18n.t("quadrige3.main.menu.synchro.log")));
92 }
93 }
94
95
96 @Override
97 protected JComponent getComponentToFocus() {
98 return getUI().getSynchroLogPanel();
99 }
100
101
102
103
104
105
106 public EditorKit getNewWrapEditorKit() {
107 return new WrapEditorKit();
108 }
109
110
111
112
113
114 public void openExternalEditor() {
115 File synchroLogFile = getModel().getLogFile();
116 if (synchroLogFile.exists()) {
117 ExternalEditors.openInExternalEditor(synchroLogFile);
118 }
119 }
120 }