View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.synchro;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2013 - 2014 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  import fr.ifremer.quadrige3.ui.swing.AbstractUIHandler;
27  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
28  import fr.ifremer.quadrige3.ui.swing.component.wrapeditor.WrapEditorKit;
29  import fr.ifremer.quadrige3.ui.swing.model.ProgressionUIModel;
30  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroDirectAction;
31  
32  import javax.swing.SwingUtilities;
33  import javax.swing.text.EditorKit;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * <p>SynchroUIHandler class.</p>
39   *
40   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
41   * @since 1.0
42   */
43  public class SynchroUIHandler extends AbstractUIHandler<SynchroUIContext, SynchroUI> {
44  
45      /** Constant <code>REPORT_CARD="reportCard"</code> */
46      public static final String REPORT_CARD = "reportCard";
47      /** Constant <code>DATE_CHOICE_CARD="dateChoiceCard"</code> */
48      public static final String DATE_CHOICE_CARD = "dateChoiceCard";
49      /** Constant <code>PROGRESS_CARD="progressCard"</code> */
50      public static final String PROGRESS_CARD = "progressCard";
51      /** Constant <code>VALID_APPLY_CARD="validApplyCard"</code> */
52      public static final String VALID_APPLY_CARD = "validApplyCard";
53      /** Constant <code>VALID_START_CARD="validStartCard"</code> */
54      public static final String VALID_START_CARD = "validStartCard";
55  
56      /** {@inheritDoc} */
57      @Override
58      public void beforeInit(SynchroUI ui) {
59          super.beforeInit(ui);
60  
61          SynchroUIContext model = new SynchroUIContext(getContext());
62          ui.setContextValue(model);
63  
64          model.getProgressionModel().addPropertyChangeListener(evt -> {
65  
66              String propertyName = evt.getPropertyName();
67              if (null != propertyName) {
68                  switch (propertyName) {
69                      case ProgressionUIModel.PROPERTY_MESSAGE:
70                          // change server side label
71                          if (getModel().isServerSide()) {
72                              ui.getStatusSideLabel().setText(t("quadrige3.synchro.status.serverSide.label"));
73                          }
74                          else {
75                              ui.getStatusSideLabel().setText(t("quadrige3.synchro.status.label"));
76                          }
77                          // change message
78                          ui.getStatusLabel().setText(ApplicationUIUtil.getHtmlString((String) evt.getNewValue()));
79                          break;
80                      case ProgressionUIModel.PROPERTY_TOTAL:
81                          // change total progressbar max
82                          ui.getProgressBar().setMaximum((Integer) evt.getNewValue());
83                          break;
84                      case ProgressionUIModel.PROPERTY_CURRENT:
85                          // change value of progress bar
86                          ui.getProgressBar().setValue((Integer) evt.getNewValue());
87                          break;
88                      case ProgressionUIModel.PROPERTY_INDETERMINATE:
89                          // change aspect of progress bar
90                          ui.getProgressBar().setIndeterminate((boolean) evt.getNewValue());
91                          break;
92                  }
93              }
94          });
95  
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public void afterInit(SynchroUI ui) {
101         initUI(ui);
102         
103         // override check Action if not using a synchro server
104         if (!getConfig().isSynchronizationUsingServer()) {
105             // launch import synchronization instead
106             ui.getValidChoiceButton().setAction(getContext().getActionFactory().createUIAction(this, ui.getValidChoiceButton(), ImportSynchroDirectAction.class));
107         }
108 
109         report(t("quadrige3.synchro.report.idle"));
110 
111     }
112 
113     /**
114      * <p>showDateChoicePopup.</p>
115      */
116     public void showDateChoicePopup() {
117         if (!DATE_CHOICE_CARD.equals(getUI().getSynchroPanelLayout().getSelected())) {
118             getUI().getSynchroPanelLayout().setSelected(DATE_CHOICE_CARD);
119             pack();
120         }
121         showPopup();
122     }
123 
124     /**
125      * <p>showProgressCard.</p>
126      */
127     public void showProgressCard() {
128         if (!PROGRESS_CARD.equals(getUI().getSynchroPanelLayout().getSelected())) {
129             getUI().getSynchroPanelLayout().setSelected(PROGRESS_CARD);
130             pack();
131         }
132     }
133 
134     /**
135      * <p>showValidApplyPopup.</p>
136      */
137     public void showValidApplyPopup() {
138         if (!VALID_APPLY_CARD.equals(getUI().getSynchroPanelLayout().getSelected())) {
139             getUI().getSynchroPanelLayout().setSelected(VALID_APPLY_CARD);
140             
141             // update the apply label
142             if (getModel().isImportData()) {
143                 getUI().getApplyLabel().setText(t("quadrige3.synchro.valid.apply.data.label"));
144             } else {
145                 getUI().getApplyLabel().setText(t("quadrige3.synchro.valid.apply.label"));
146             }
147             
148             pack();
149         }
150         showPopup();
151     }
152 
153     /**
154      * <p>showValidStartPopup.</p>
155      */
156     public void showValidStartPopup() {
157         // with default message
158         showValidStartPopup(t("quadrige3.synchro.valid.start.label"));
159     }
160 
161     public void showValidStartPopup(String message) {
162         if (!VALID_START_CARD.equals(getUI().getSynchroPanelLayout().getSelected())) {
163             getUI().getSynchroPanelLayout().setSelected(VALID_START_CARD);
164             getUI().getStartLabel().setText(message);
165             pack();
166         }
167         showPopup();
168     }
169     // quadrige3.synchro.valid.start.label
170 
171     /**
172      * <p>showReportCard.</p>
173      */
174     public void showReportCard() {
175         if (!REPORT_CARD.equals(getUI().getSynchroPanelLayout().getSelected())) {
176             getUI().getSynchroPanelLayout().setSelected(REPORT_CARD);
177             pack();
178         }
179     }
180 
181     /**
182      * <p>clearDataDates.</p>
183      */
184     public void clearDataDates() {
185         getModel().setImportDataStartDate(null);
186         getModel().setImportDataEndDate(null);
187     }
188 
189     /**
190      * <p>report.</p>
191      *
192      * @param report a {@link String} object.
193      * @param showPopup a boolean.
194      */
195     public void report(String report, boolean showPopup) {
196 
197         getUI().getReportTextPane().setText(ApplicationUIUtil.getHtmlString(report));
198 
199         showReportCard();
200         if (showPopup) {
201             showPopup();
202         }
203     }
204 
205     /**
206      * <p>report.</p>
207      *
208      * @param report a {@link String} object.
209      */
210     public void report(String report) {
211 
212         report(report, true);
213     }
214 
215     /**
216      * <p>cancelSynchro.</p>
217      */
218     public void cancelSynchro() {
219 
220         hidePopup();
221 
222         if (getModel().getDirection() == SynchroDirection.IMPORT) {
223             getModel().resetImportContext();
224             // save context with data date range
225             getModel().saveImportContext(true, false);
226         }
227 
228         else if (getModel().getDirection() == SynchroDirection.EXPORT) {
229             getModel().resetExportContext();
230             getModel().saveExportContext();
231         }
232 
233         // Direction could be null, if a reboot occur, or exception already displayed
234         else {
235             getModel().resetExportContext();
236             getModel().resetImportContext();
237         }
238 
239     }
240 
241     /**
242      * <p>showPopup.</p>
243      */
244     public void showPopup() {
245         if (getPopup() != null) {
246             SwingUtilities.invokeLater(() -> getPopup().showPopup());
247         }
248     }
249 
250     /**
251      * <p>hidePopup.</p>
252      */
253     public void hidePopup() {
254         if (getPopup() != null) {
255             SwingUtilities.invokeLater(() -> getPopup().hidePopup());
256         }
257     }
258 
259     private SynchroWidget getPopup() {
260         if (getContext() != null && getContext().getMainUI() != null) {
261             return getContext().getMainUI().getSynchroWidget();
262         }
263         return null;
264     }
265 
266     private void pack() {
267         if (getPopup() != null) {
268             getPopup().pack();
269         }
270     }
271 
272     /**
273      * <p>getNewWrapEditorKit.</p>
274      *
275      * @return a {@link EditorKit} object.
276      */
277     public EditorKit getNewWrapEditorKit() {
278         return new WrapEditorKit();
279     }
280     
281 }