View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.replace;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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.core.dto.referential.BaseReferentialDTO;
27  import fr.ifremer.reefdb.dto.ReefDbBeans;
28  import fr.ifremer.reefdb.service.referential.ReferentialService;
29  import fr.ifremer.reefdb.ui.swing.ReefDbUIContext;
30  import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbMainUIAction;
31  import fr.ifremer.reefdb.ui.swing.content.ReefDbMainUIHandler;
32  import jaxx.runtime.context.JAXXInitialContext;
33  import org.apache.commons.collections4.CollectionUtils;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import javax.swing.SwingUtilities;
38  import java.awt.Dimension;
39  import java.util.List;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * Created on 7/6/14.
45   *
46   * @param <E>  type of DTO implementing BaseReferentialDTO
47   * @param <M>
48   * @param <UI>
49   * @since 3.6
50   */
51  public abstract class AbstractOpenReplaceUIAction<
52          E extends BaseReferentialDTO,
53          M extends AbstractReplaceUIModel<E>,
54          UI extends AbstractReplaceUI<E, M>>
55          extends AbstractReefDbMainUIAction {
56  
57      /**
58       * Logger.
59       */
60      private static final Log log =
61              LogFactory.getLog(AbstractOpenReplaceUIAction.class);
62  
63      /**
64       * <p>getEntityLabel.</p>
65       *
66       * @return a {@link java.lang.String} object.
67       */
68      protected abstract String getEntityLabel();
69  
70      /**
71       * <p>createNewModel.</p>
72       *
73       * @return a M object.
74       */
75      protected abstract M createNewModel();
76  
77      /**
78       * <p>createUI.</p>
79       *
80       * @param ctx a {@link jaxx.runtime.context.JAXXInitialContext} object.
81       * @return a UI object.
82       */
83      protected abstract UI createUI(JAXXInitialContext ctx);
84  
85      /**
86       * <p>Getter for the field <code>referentialList</code>.</p>
87       *
88       * @param referentialService a {@link fr.ifremer.reefdb.service.referential.ReferentialService} object.
89       * @return a {@link java.util.List} object.
90       */
91      protected abstract List<E> getReferentialList(ReferentialService referentialService);
92  
93      /**
94       * <p>getSourceListFromReferentialList.</p>
95       *
96       * @param list a {@link java.util.List} object.
97       * @return a {@link java.util.List} object.
98       */
99      protected List<E> getSourceListFromReferentialList(List<E> list) {
100         // by default, filter out the local referential
101         return ReefDbBeans.filterLocalReferential(list);
102     }
103 
104     /**
105      * <p>getSelectedSource.</p>
106      *
107      * @return a E object.
108      */
109     protected abstract E getSelectedSource();
110 
111     /**
112      * <p>getSelectedTarget.</p>
113      *
114      * @return a E object.
115      */
116     protected abstract E getSelectedTarget();
117 
118     protected E source;
119 
120     protected E target;
121 
122     private boolean showDialog = true;
123 
124     private List<E> referentialList;
125 
126     private List<E> sourceList;
127 
128     /**
129      * <p>Constructor for AbstractOpenReplaceUIAction.</p>
130      *
131      * @param handler a {@link ReefDbMainUIHandler} object.
132      */
133     protected AbstractOpenReplaceUIAction(ReefDbMainUIHandler handler) {
134         super(handler, false);
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public boolean prepareAction() throws Exception {
140 
141         boolean doAction = super.prepareAction();
142 
143         if (doAction) {
144 
145             createProgressionUIModel(3);
146 
147         }
148         return doAction;
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public void releaseAction() {
154         source = target = null;
155         super.releaseAction();
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     public void doAction() {
161 
162         ReferentialService referentialService = getContext().getReferentialService();
163 
164         String entityLabel = getEntityLabel();
165 
166         getProgressionUIModel().increments(t("reefdb.replaceReferential.loading.target", entityLabel));
167 
168         // Get target list
169         referentialList = getReferentialList(referentialService);
170 
171         getProgressionUIModel().increments(t("reefdb.replaceReferential.loading.source", entityLabel));
172 
173         // Get source list
174         sourceList = getSourceListFromReferentialList(referentialList);
175 
176         if (log.isDebugEnabled()) {
177             log.debug("Loaded local referential: " + (source == null ? "null" : sourceList.size()));
178             log.debug("Loaded target referential: " + (referentialList == null ? "null" : referentialList.size()));
179         }
180 
181         if (CollectionUtils.isEmpty(referentialList)) {
182 
183             displayWarningMessage(t("reefdb.replaceReferential.noTarget.title", entityLabel),
184                     t("reefdb.replaceReferential.noTarget.message", entityLabel));
185 
186             showDialog = false;
187         }
188 
189         if (CollectionUtils.isEmpty(sourceList)) {
190 
191             displayWarningMessage(t("reefdb.replaceReferential.noSource.title", entityLabel),
192                     t("reefdb.replaceReferential.noSource.message", entityLabel));
193 
194             showDialog = false;
195         }
196 
197     }
198 
199     /** {@inheritDoc} */
200     @Override
201     public void postSuccessAction() {
202 
203         if (showDialog) {
204 
205             getProgressionUIModel().increments(t("reefdb.replaceReferential.open.dialog"));
206             // Load model
207             M model = createNewModel();
208 
209             model.setTargetList(referentialList);
210             model.setSourceList(sourceList);
211             model.setSelectedSource(getSelectedSource());
212             model.setSelectedTarget(getSelectedTarget());
213 
214             JAXXInitialContext ctx = new JAXXInitialContext();
215             ctx.add(getUI());
216             ctx.add(model);
217             ctx.add(ReefDbUIContext.PROPERTY_APPLICATION_CONTEXT, getContext());
218 
219             final UI dialog = createUI(ctx);
220             dialog.setResizable(false);
221             SwingUtilities.invokeLater(() -> getHandler().openDialog(dialog, new Dimension(800, 180)));
222         }
223 
224     }
225 }