View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.TuttiConfiguration;
26  import fr.ifremer.tutti.persistence.ProgressionModel;
27  import fr.ifremer.tutti.persistence.entities.referential.Species;
28  import fr.ifremer.tutti.service.TuttiDataContext;
29  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
30  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
31  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
32  import fr.ifremer.tutti.ui.swing.util.species.SelectSpeciesUI;
33  import fr.ifremer.tutti.ui.swing.util.species.SelectSpeciesUIModel;
34  import org.apache.commons.collections4.CollectionUtils;
35  import org.apache.commons.lang3.StringUtils;
36  import org.jdesktop.beans.AbstractBean;
37  import org.nuiton.jaxx.application.swing.action.AbstractApplicationAction;
38  import org.nuiton.util.StringUtil;
39  
40  import javax.swing.JOptionPane;
41  import java.awt.Component;
42  import java.awt.Dimension;
43  import java.util.List;
44  
45  /**
46   * Tutti base action.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 1.0
50   */
51  public abstract class LongActionSupport<M extends AbstractBean, UI extends TuttiUI<M, ?>, H extends AbstractTuttiUIHandler<M, UI>>
52          extends AbstractApplicationAction<M, UI, H> {
53  
54      public abstract void doAction() throws Exception;
55  
56      protected LongActionSupport(H handler, boolean hideBody) {
57          super(handler, hideBody);
58      }
59  
60      @Override
61      public TuttiUIContext getContext() {
62          return handler.getContext();
63      }
64  
65      public void setProgressionModel(ProgressionModel progressionModel) {
66          super.setProgressionModel(progressionModel);
67      }
68  
69      @Override
70      protected ProgressionModel getProgressionModel() {
71          return (ProgressionModel) getContext().getActionUI().getModel().getProgressionModel();
72      }
73  
74      public TuttiDataContext getDataContext() {
75          return getContext().getDataContext();
76      }
77  
78      @Override
79      protected TuttiConfiguration getConfig() {
80          return getContext().getConfig();
81      }
82  
83      @Override
84      protected void sendMessage(String message) {
85          getContext().showInformationMessage(message);
86      }
87  
88      @Override
89      protected void createProgressionModelIfRequired(int total) {
90          ProgressionModel progressionModel = getProgressionModel();
91          if (progressionModel == null) {
92              progressionModel = new ProgressionModel();
93              progressionModel.setMessage("");
94              progressionModel.setCurrent(0);
95              setProgressionModel(progressionModel);
96              progressionModel.setTotal(total);
97  
98          } else {
99              progressionModel.adaptTotal(total);
100         }
101     }
102 
103     protected Species openAddSpeciesDialog(String title, List<Species> species) {
104         return openAddSpeciesDialog(title, species, null);
105     }
106 
107     protected Species openAddSpeciesDialog(String title, List<Species> species, List<Species> filteredSpecies) {
108         SelectSpeciesUI dialogContent = new SelectSpeciesUI(true, getUI());
109         SelectSpeciesUIModel model = dialogContent.getModel();
110         model.setSelectedSpecies(null);
111         model.setSpecies(species);
112         model.setFilteredSpecies(filteredSpecies);
113         model.setShowAllSpecies(CollectionUtils.isEmpty(filteredSpecies));
114 
115         getHandler().openDialog(dialogContent, title, new Dimension(400, 130));
116 
117         return model.getSelectedSpecies();
118     }
119 
120 
121     protected boolean askAdminPassword(String askMessage,
122                                        String askMessageTitle,
123                                        String errorMessage,
124                                        String errorMessageTitle) {
125 
126         Component container = getContext().getMainUI();
127 
128         String answer;
129         boolean result;
130         do {
131             answer = JOptionPane.showInputDialog(container,
132                                                  askMessage,
133                                                  askMessageTitle,
134                                                  JOptionPane.WARNING_MESSAGE);
135             if (answer != null) {
136                 String cryptedAnswer = StringUtil.encodeMD5(answer);
137                 String correctAnswer = getConfig().getAdminPassword();
138                 result = StringUtils.equals(cryptedAnswer, correctAnswer);
139 
140                 if (!result) {
141                     JOptionPane.showMessageDialog(container,
142                                                   errorMessage,
143                                                   errorMessageTitle,
144                                                   JOptionPane.ERROR_MESSAGE);
145                 }
146 
147             } else {
148                 result = false;
149             }
150 
151         } while (!result && answer != null);
152         return result;
153     }
154 }