View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.action;
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 com.google.common.collect.Lists;
28  import fr.ifremer.quadrige2.core.config.Quadrige2CoreConfiguration;
29  import fr.ifremer.quadrige2.ui.swing.common.ApplicationUI;
30  import fr.ifremer.quadrige2.ui.swing.common.ApplicationUIContext;
31  import fr.ifremer.quadrige2.ui.swing.common.AbstractUIHandler;
32  import fr.ifremer.quadrige2.ui.swing.common.model.ProgressionModel;
33  import org.apache.commons.collections4.CollectionUtils;
34  import org.apache.commons.lang3.StringUtils;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.jdesktop.beans.AbstractBean;
38  import org.nuiton.jaxx.application.swing.action.AbstractApplicationAction;
39  import org.nuiton.jaxx.runtime.JaxxFileChooser;
40  import org.springframework.security.access.AccessDeniedException;
41  import org.springframework.security.core.AuthenticationException;
42  
43  import java.io.File;
44  import java.util.List;
45  
46  import static org.nuiton.i18n.I18n.t;
47  
48  /**
49   * Application base action.
50   *
51   * @author Lionel Touseau <lionel.touseau@e-is.pro>
52   * @param <M>
53   * @param <UI>
54   * @param <H>
55   * @since 1.0
56   */
57  public abstract class AbstractAction<M extends AbstractBean, UI extends ApplicationUI<M, ?>, H extends AbstractUIHandler<M, UI>>
58      extends AbstractApplicationAction<M, UI, H> {
59  
60      private static final Log LOG = LogFactory.getLog(AbstractAction.class);
61      private List<String> errorMessages;
62  
63      /**
64       * <p>Constructor for AbstractAction.</p>
65       *
66       * @param handler a H object.
67       * @param hideBody a boolean.
68       */
69      public AbstractAction(H handler, boolean hideBody) {
70          super(handler, hideBody);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public ApplicationUIContext getContext() {
76          return getHandler().getContext();
77      }
78  
79      /**
80       * <p>setProgressionModel.</p>
81       *
82       * @param progressionModel a {@link ProgressionModel} object.
83       */
84      public void setProgressionModel(ProgressionModel progressionModel) {
85          super.setProgressionModel(progressionModel);
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      protected ProgressionModel getProgressionModel() {
91          return (ProgressionModel) super.getProgressionModel();
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      protected Quadrige2CoreConfiguration getConfig() {
97          return getContext().getConfiguration();
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     protected void sendMessage(String message) {
103         getContext().showInformationMessage(message);
104     }
105 
106     /**
107      * <p>addErrorMessage.</p>
108      *
109      * @param errorMessage a {@link java.lang.String} object.
110      */
111     public void addErrorMessage(String errorMessage) {
112         if (errorMessages == null) {
113             errorMessages = Lists.newArrayList();
114         }
115         errorMessages.add(errorMessage);
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     public boolean prepareAction() throws Exception {
121         errorMessages = null;
122         return super.prepareAction();
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public void postSuccessAction() {
128         super.postSuccessAction();
129 
130         displayErrors();
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public void postFailedAction(Throwable error) {
136 
137         // manage security exception 
138         if (error instanceof AuthenticationException) {
139 
140             if (LOG.isInfoEnabled()) {
141                 LOG.info("authentication needed");
142             }
143 
144             // TODO how to tell the application to authenticate !!!!!!!!!!!!!
145 //            AuthenticationAction action = new AuthenticationAction(getContext().getMainUI().getHandler(), this);
146 //            getActionEngine().runAction(action);
147 
148         } else if (error instanceof AccessDeniedException) {
149 
150             if (LOG.isInfoEnabled()) {
151                 LOG.info("access denied");
152             }
153 
154             getContext().getErrorHelper().showErrorDialog(t("quadrige2.error.authenticate.accessDenied"));
155         }
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     protected void createProgressionModelIfRequired(int total) {
161         ProgressionModel progressionModel = getProgressionModel();
162         if (progressionModel == null) {
163             progressionModel = new ProgressionModel();
164             progressionModel.setTotal(total);
165             progressionModel.setMessage("");
166             progressionModel.setCurrent(0);
167             setProgressionModel(progressionModel);
168         } else {
169             progressionModel.adaptTotal(total);
170         }
171     }
172 
173     /** {@inheritDoc} */
174     @Override
175     protected void displayInfoMessage(String title, String message) {
176         getContext().getDialogHelper().showMessageDialog(message, title);
177     }
178 
179     /** {@inheritDoc} */
180     @Override
181     protected void displayWarningMessage(String title, String message) {
182         getContext().getDialogHelper().showWarningDialog(message, title);
183     }
184 
185     /** {@inheritDoc} */
186     protected void displayErrorMessage(String title, String message) {
187         getContext().getDialogHelper().showErrorDialog(message, title);
188     }
189 
190     private void displayErrors() {
191         if (CollectionUtils.isNotEmpty(errorMessages)) {
192             for (String errorMessage : errorMessages) {
193                 getContext().getErrorHelper().showErrorDialog(errorMessage, null);
194             }
195         }
196     }
197 
198     /** {@inheritDoc} */
199     @Override
200     protected boolean askOverwriteFile(File file) {
201         return getHandler().askOverwriteFile(file);
202     }
203 
204     /** {@inheritDoc} */
205     @Override
206     protected boolean askBeforeDelete(String title, String message) {
207         return getHandler().askBeforeDelete(title, message);
208     }
209 
210     /**
211      * <p>askBeforeDeleteMany.</p>
212      *
213      * @param title a {@link java.lang.String} object.
214      * @param message a {@link java.lang.String} object.
215      * @param list a {@link java.util.List} object.
216      * @return a boolean.
217      */
218     protected boolean askBeforeDeleteMany(String title, String message, List<String> list) {
219         return getHandler().askBeforeDeleteMany(title, message, list);
220     }
221 
222     /**
223      * <p>askSaveBeforeLeaving.</p>
224      *
225      * @param message a {@link java.lang.String} object.
226      * @return a int.
227      */
228     protected int askSaveBeforeLeaving(String message) {
229         return getHandler().askSaveBeforeLeaving(message);
230     }
231 
232     /**
233      * <p>askBeforeChangeTab.</p>
234      *
235      * @param message a {@link java.lang.String} object.
236      * @return a int.
237      */
238     protected int askBeforeChangeTab(String message) {
239         return getHandler().askBeforeChangeTab(message);
240     }
241 
242     /**
243      * <p>askBeforeReset.</p>
244      *
245      * @param title a {@link java.lang.String} object.
246      * @param message a {@link java.lang.String} object.
247      * @return a boolean.
248      */
249     protected boolean askBeforeReset(String title, String message) {
250         return getHandler().askBeforeReset(title, message);
251     }
252 
253     /**
254      * <p>askCancelEditBeforeLeaving.</p>
255      *
256      * @param message a {@link java.lang.String} object.
257      * @return a boolean.
258      */
259     protected boolean askCancelEditBeforeLeaving(String message) {
260         return getHandler().askCancelEditBeforeLeaving(message);
261     }
262 
263     /**
264      * <p>askDeleteInvalidBeforeLeaving.</p>
265      *
266      * @param message a {@link java.lang.String} object.
267      * @return a boolean.
268      */
269     protected boolean askDeleteInvalidBeforeLeaving(String message) {
270         return getHandler().askDeleteInvalidBeforeLeaving(message);
271     }
272 
273     /** {@inheritDoc} */
274     @Override
275     public void setActionDescription(String actionDescription) {
276         // FIXME-in nuiton 
277         if (StringUtils.isNotBlank(actionDescription)) {
278             super.setActionDescription(actionDescription);
279         }
280     }
281 
282     /** {@inheritDoc} */
283     @Override
284     protected File chooseFile(String title, String buttonLabel, String... filters) {
285         return super.chooseFile(title, buttonLabel, filters);
286     }
287     
288     /**
289      * <p>chooseDirectory.</p>
290      *
291      * @param title a {@link java.lang.String} object.
292      * @param buttonLabel a {@link java.lang.String} object.
293      * @return a {@link java.io.File} object.
294      */
295     protected File chooseDirectory(String title, String buttonLabel) {
296         JaxxFileChooser.ToLoadDirectory toLoadDirectory = JaxxFileChooser.forLoadingDirectory();
297         toLoadDirectory.setParent(getContext().getMainUI());
298         toLoadDirectory.setTitle(title);
299         toLoadDirectory.setApprovalText(buttonLabel);
300         return toLoadDirectory.choose();
301     }
302     
303 }