View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.referential.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.persistence.ProgressionModel;
28  import fr.ifremer.tutti.persistence.entities.referential.TuttiReferentialEntity;
29  import fr.ifremer.tutti.service.PersistenceService;
30  import fr.ifremer.tutti.ui.swing.content.MainUI;
31  import fr.ifremer.tutti.ui.swing.content.referential.ManageTemporaryReferentialUI;
32  import fr.ifremer.tutti.ui.swing.content.referential.ManageTemporaryReferentialUIHandler;
33  import fr.ifremer.tutti.ui.swing.content.referential.ManageTemporaryReferentialUIModel;
34  import fr.ifremer.tutti.ui.swing.content.referential.replace.AbstractReplaceTemporaryUI;
35  import fr.ifremer.tutti.ui.swing.content.referential.replace.AbstractReplaceTemporaryUIModel;
36  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
37  import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
38  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
39  import jaxx.runtime.SwingUtil;
40  import jaxx.runtime.context.JAXXInitialContext;
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import javax.swing.JButton;
45  import java.util.List;
46  
47  import static org.nuiton.i18n.I18n.t;
48  
49  /**
50   * Created on 7/6/14.
51   *
52   * @author Tony Chemit - chemit@codelutin.com
53   * @since 3.6
54   */
55  public abstract class AbstractOpenReplaceTemporaryUIAction<E extends TuttiReferentialEntity, M extends AbstractReplaceTemporaryUIModel<E>, UI extends AbstractReplaceTemporaryUI<E, M>> extends LongActionSupport<ManageTemporaryReferentialUIModel, ManageTemporaryReferentialUI, ManageTemporaryReferentialUIHandler> {
56  
57      /** Logger. */
58      private static final Log log =
59              LogFactory.getLog(AbstractOpenReplaceTemporaryUIAction.class);
60  
61      protected abstract String getEntityLabel();
62  
63      protected abstract M createNewModel();
64  
65      protected abstract UI createUI(JAXXInitialContext ctx);
66  
67      protected abstract List<E> getTargetList(PersistenceService persistenceService);
68  
69      protected abstract List<E> retainTemporaryList(PersistenceService persistenceService, List<E> targetList);
70  
71      protected E source;
72  
73      protected E target;
74  
75      protected List<E> targetList;
76  
77      protected List<E> sourceList;
78  
79      protected AbstractOpenReplaceTemporaryUIAction(ManageTemporaryReferentialUIHandler handler) {
80          super(handler, true);
81      }
82  
83      protected abstract JButton getButton();
84  
85      @Override
86      public boolean prepareAction() throws Exception {
87  
88          boolean doAction = super.prepareAction() && getButton().isEnabled();
89  
90          if (doAction) {
91  
92              setProgressionModel(new ProgressionModel());
93              getProgressionModel().setTotal(3);
94  
95          }
96          return doAction;
97      }
98  
99      @Override
100     public void releaseAction() {
101         source = target = null;
102         targetList = sourceList = null;
103         super.releaseAction();
104     }
105 
106     @Override
107     public void doAction() throws Exception {
108 
109         PersistenceService persistenceService = getContext().getPersistenceService();
110 
111         String entityLabel = getEntityLabel();
112 
113         // Get target list
114         getProgressionModel().increments(t("tutti.openReplaceTemporaryUI.loading.target", entityLabel));
115         targetList = getTargetList(persistenceService);
116         log.info("Loaded official referentials: " + targetList.size());
117 
118         // Get source list
119         getProgressionModel().increments(t("tutti.openReplaceTemporaryUI.loading.source", entityLabel));
120         sourceList = retainTemporaryList(persistenceService, targetList);
121         log.info("Loaded temporary referentials: " + sourceList.size());
122 
123     }
124 
125     @Override
126     public void postSuccessAction() {
127 
128         boolean showDialog = true;
129 
130         String entityLabel = getEntityLabel();
131 
132         M model = createNewModel();
133 
134         if (targetList.isEmpty()) {
135 
136             showDialog = false;
137             displayWarningMessage(t("tutti.title.openReplaceTemporaryUI.noTarget", entityLabel),
138                                   t("tutti.message.openReplaceTemporaryUI.noTarget", entityLabel));
139 
140         } else {
141 
142             model.setTargetList(targetList);
143 
144         }
145 
146         if (sourceList.isEmpty()) {
147 
148             showDialog = false;
149 
150             displayWarningMessage(t("tutti.title.openReplaceTemporaryUI.noSource", entityLabel),
151                                   t("tutti.message.openReplaceTemporaryUI.noSource", entityLabel));
152 
153         } else {
154 
155             model.setSourceList(sourceList);
156 
157         }
158 
159         if (showDialog) {
160 
161             getProgressionModel().increments(t("tutti.openReplaceTemporaryUI.open.dialog"));
162 
163             JAXXInitialContext ctx = new JAXXInitialContext();
164             ctx.add(getUI());
165             ctx.add(model);
166 
167             UI dialog = createUI(ctx);
168             MainUI mainUI = TuttiUIUtil.getApplicationContext(dialog).getMainUI();
169             SwingUtil.setComponentHeight(dialog, 400);
170             SwingUtil.setComponentWidth(dialog, mainUI.getWidth() - 100);
171             SwingUtil.center(mainUI, dialog);
172 
173             dialog.setContextValue(getUI(), "owner");
174             dialog.setVisible(true);
175             ((TuttiUI) dialog).getHandler().onCloseUI();
176 
177         }
178 
179     }
180 
181 }