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