View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.synchro.log;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import fr.ifremer.quadrige2.core.dao.technical.os.ExternalEditors;
28  import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
29  import fr.ifremer.quadrige2.core.security.SecurityContextHelper;
30  import fr.ifremer.quadrige2.core.service.ClientServiceLocator;
31  import fr.ifremer.quadrige2.ui.swing.common.AbstractUIHandler;
32  import fr.ifremer.quadrige2.ui.swing.common.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  
43  /**
44   * <p>SynchroLogUIHandler class.</p>
45   *
46   * @author Lionel Touseau <lionel.touseau@e-is.pro>
47   */
48  public class SynchroLogUIHandler extends AbstractUIHandler<SynchroLogUIModel, SynchroLogUI> {
49  
50      /** {@inheritDoc} */
51      @Override
52      public void beforeInit(SynchroLogUI ui) {
53          super.beforeInit(ui);
54  
55          SynchroLogUIModel model = new SynchroLogUIModel();
56          ui.setContextValue(model);
57  
58          ui.setContextValue(SwingUtil.createActionIcon("synchro-log"));
59  
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public void afterInit(SynchroLogUI ui) {
65          initUI(ui);
66  
67          if (getContext().isAuthenticated()) {
68  
69              // Set model file
70              File logFile = ClientServiceLocator.instance().getSynchroHistoryService().getHistoryFileByUserId(SecurityContextHelper.getQuadrige2UserId());
71              getModel().setLogFile(logFile);
72  
73              // Set model content
74              String historyContent = null;
75              if (logFile.exists()) {
76                  try {
77                      historyContent = FileUtils.readFileToString(logFile);
78                  } catch (IOException e) {
79                      throw new Quadrige2TechnicalException(I18n.t("quadrige2.service.synchroHistory.readContent.failed"), e);
80                  }
81              }
82              if (StringUtils.isBlank(historyContent)) {
83                  historyContent = I18n.t("quadrige2.main.action.synchro.log.empty");
84              }
85              getModel().setMessage(historyContent);
86          }
87  
88          // Authentication required
89          else {
90              getModel().setMessage(I18n.t("quadrige2.login.infoMessage", I18n.t("quadrige2.main.menu.synchro.log")));
91          }
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      protected JComponent getComponentToFocus() {
97          return getUI().getSynchroLogPanel();
98      }
99      
100     /**
101      * <p>getNewWrapEditorKit.</p>
102      *
103      * @return a {@link EditorKit} object.
104      */
105     public EditorKit getNewWrapEditorKit() {
106         return new WrapEditorKit();
107     }
108 
109 
110     /**
111      * <p>openExternalEditor.</p>
112      */
113     public void openExternalEditor() {
114         File synchroLogFile = getModel().getLogFile();
115         if (synchroLogFile.exists()) {
116             ExternalEditors.openInExternalEditor(synchroLogFile);
117         }
118     }
119 }