View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.synchro.log;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 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.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   * <p>SynchroLogUIHandler class.</p>
46   *
47   * @author Lionel Touseau <lionel.touseau@e-is.pro>
48   */
49  public class SynchroLogUIHandler extends AbstractUIHandler<SynchroLogUIModel, SynchroLogUI> {
50  
51      /** {@inheritDoc} */
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      /** {@inheritDoc} */
64      @Override
65      public void afterInit(SynchroLogUI ui) {
66          initUI(ui);
67  
68          if (getContext().isAuthenticated()) {
69  
70              // Set model file
71              File logFile = ClientServiceLocator.instance().getSynchroHistoryService().getHistoryFileByUserId(SecurityContextHelper.getQuadrigeUserId());
72              getModel().setLogFile(logFile);
73  
74              // Set model content
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          // Authentication required
90          else {
91              getModel().setMessage(I18n.t("quadrige3.login.infoMessage", I18n.t("quadrige3.main.menu.synchro.log")));
92          }
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      protected JComponent getComponentToFocus() {
98          return getUI().getSynchroLogPanel();
99      }
100     
101     /**
102      * <p>getNewWrapEditorKit.</p>
103      *
104      * @return a {@link EditorKit} object.
105      */
106     public EditorKit getNewWrapEditorKit() {
107         return new WrapEditorKit();
108     }
109 
110 
111     /**
112      * <p>openExternalEditor.</p>
113      */
114     public void openExternalEditor() {
115         File synchroLogFile = getModel().getLogFile();
116         if (synchroLogFile.exists()) {
117             ExternalEditors.openInExternalEditor(synchroLogFile);
118         }
119     }
120 }