View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.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.persistence.ProgressionModel;
26  import fr.ifremer.tutti.ui.swing.TuttiScreen;
27  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
28  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
29  import jaxx.runtime.SwingUtil;
30  import jaxx.runtime.context.JAXXContextEntryDef;
31  
32  import java.beans.PropertyVetoException;
33  
34  import static org.nuiton.i18n.I18n.t;
35  
36  /**
37   * Action to change the screen.
38   *
39   * Will just check that the current screen can be quit via
40   * the {@link MainUIHandler#quitCurrentScreen()}.
41   *
42   * @author Kevin Morin - kmorin@codelutin.com
43   * @since 1.0
44   */
45  public abstract class AbstractChangeScreenAction extends AbstractMainUITuttiAction {
46  
47      /**
48       * Context entry to keep previous screen.
49       *
50       * @since 1.1
51       */
52      protected static final JAXXContextEntryDef<TuttiScreen> PREVIOUS_SCREEN =
53              SwingUtil.newContextEntryDef("previousScreen", TuttiScreen.class);
54  
55      /**
56       * Screen where to go.
57       *
58       * @since 1.0
59       */
60      protected TuttiScreen screen;
61  
62      /**
63       * Flag to skip the check of current screen.
64       *
65       * @since 1.1
66       */
67      protected boolean skipCheckCurrentScreen;
68  
69      protected AbstractChangeScreenAction(MainUIHandler handler, boolean hideBody, TuttiScreen screen) {
70          super(handler, hideBody);
71          this.screen = screen;
72      }
73  
74      public void setSkipCheckCurrentScreen(boolean skipCheckCurrentScreen) {
75          this.skipCheckCurrentScreen = skipCheckCurrentScreen;
76      }
77  
78      protected void setScreen(TuttiScreen screen) {
79          this.screen = screen;
80      }
81  
82      @Override
83      public boolean prepareAction() throws Exception {
84          boolean result = super.prepareAction();
85          result &= skipCheckCurrentScreen || getHandler().quitCurrentScreen();
86          return result;
87      }
88  
89      @Override
90      public void doAction() throws Exception {
91  
92          TuttiUIContext context = getContext();
93  
94          TuttiScreen previousScreen = context.getScreen();
95          if (getUI() != null) {
96              if (previousScreen == null) {
97                  PREVIOUS_SCREEN.removeContextValue(getUI());
98              } else {
99                  PREVIOUS_SCREEN.setContextValue(getUI(), previousScreen);
100             }
101         }
102 
103         // clean current screen
104         context.setScreen(null);
105 
106         // change screen
107         context.setScreen(screen);
108     }
109 
110     @Override
111     public void postFailedAction(Throwable error) {
112         if (error != null && !(error instanceof PropertyVetoException)) {
113 
114             getContext().setFallBackScreen();
115         }
116     }
117 
118     protected void loadReferantials(boolean createProgressionModel) {
119 
120 
121         ProgressionModel progressionModel;
122 
123         if (createProgressionModel) {
124 
125             progressionModel = new ProgressionModel();
126             progressionModel.setTotal(5);
127             setProgressionModel(progressionModel);
128 
129         } else {
130 
131             progressionModel = getProgressionModel();
132 
133         }
134 
135         progressionModel.increments(t("tutti.openScreen.step.loading.allGear"));
136         getContext().getPersistenceService().getAllGear();
137 
138         progressionModel.increments(t("tutti.openScreen.step.loading.allPerson"));
139         getContext().getPersistenceService().getAllPerson();
140 
141         progressionModel.increments(t("tutti.openScreen.step.loading.allSpecies"));
142         getContext().getPersistenceService().getAllSpecies();
143 
144         progressionModel.increments(t("tutti.openScreen.step.loading.allVessel"));
145         getContext().getPersistenceService().getAllVessel();
146 
147         progressionModel.increments(t("tutti.openScreen.step.loading.ui"));
148 
149     }
150 
151     protected void loadReferantialsWithObsoletes(boolean createProgressionModel) {
152 
153         ProgressionModel progressionModel;
154 
155         if (createProgressionModel) {
156 
157             progressionModel = new ProgressionModel();
158             progressionModel.setTotal(9);
159             setProgressionModel(progressionModel);
160 
161         } else {
162 
163             progressionModel = getProgressionModel();
164 
165         }
166 
167         progressionModel.increments(t("tutti.openScreen.step.loading.allGear"));
168         getContext().getPersistenceService().getAllGear();
169 
170         progressionModel.increments(t("tutti.openScreen.step.loading.allGearWithObsoletes"));
171         getContext().getPersistenceService().getAllGearWithObsoletes();
172 
173         progressionModel.increments(t("tutti.openScreen.step.loading.allPerson"));
174         getContext().getPersistenceService().getAllPerson();
175 
176         progressionModel.increments(t("tutti.openScreen.step.loading.allPersonWithObsoletes"));
177         getContext().getPersistenceService().getAllPersonWithObsoletes();
178 
179         progressionModel.increments(t("tutti.openScreen.step.loading.allSpecies"));
180         getContext().getPersistenceService().getAllSpecies();
181 
182         progressionModel.increments(t("tutti.openScreen.step.loading.allSpeciesWithObsoletes"));
183         getContext().getPersistenceService().getAllReferentSpeciesWithObsoletes();
184 
185         progressionModel.increments(t("tutti.openScreen.step.loading.allVessel"));
186         getContext().getPersistenceService().getAllVessel();
187 
188         progressionModel.increments(t("tutti.openScreen.step.loading.allVesselWithObsoletes"));
189         getContext().getPersistenceService().getAllVesselWithObsoletes();
190 
191         progressionModel.increments(t("tutti.openScreen.step.loading.ui"));
192 
193     }
194 
195 }