View Javadoc
1   package fr.ifremer.dali.ui.swing.action;
2   
3   /*
4    * #%L
5    * Dali :: 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.dali.ui.swing.content.DaliMainUIHandler;
27  import fr.ifremer.quadrige3.ui.swing.action.AbstractMainUIAction;
28  import fr.ifremer.quadrige3.ui.swing.action.GoToHomeAction;
29  import fr.ifremer.quadrige3.ui.swing.action.GoToManageDbAction;
30  import fr.ifremer.quadrige3.ui.swing.content.db.OpenDbAction;
31  
32  import static org.nuiton.i18n.I18n.t;
33  
34  /**
35   * Start action.
36   * <p/>
37   * starts ui action (open db if exists, or go to manage db screen).
38   *
39   * @since 2.4
40   */
41  public class StartAction extends AbstractDaliMainUIAction {
42  
43      private AbstractMainUIAction delegateAction;
44  
45      /**
46       * <p>Constructor for StartAction.</p>
47       *
48       * @param handler a {@link DaliMainUIHandler} object.
49       */
50      public StartAction(DaliMainUIHandler handler) {
51          super(handler, true);
52          setActionDescription(t("dali.main.action.startDali"));
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public boolean prepareAction() throws Exception {
58          super.prepareAction();
59  
60          if (getContext().isPersistenceLoaded()) {
61              // db already opened (happens when reloading ui)
62              // just go to home screen
63              GoToHomeAction action = getContext().getActionFactory().createLogicAction(handler, GoToHomeAction.class);
64              action.setSkipCheckCurrentScreen(true);
65              delegateAction = action;
66  
67          } else {
68  
69              if (getContext().isDbExist()) {
70                  // open db (using a fake button to have simple api)
71                  OpenDbAction action = getContext().getActionFactory().createLogicAction(handler, OpenDbAction.class);
72                  action.setSkipCheckCurrentScreen(true);
73                  delegateAction = action;
74  
75              } else {
76  
77                  // clean db context
78                  getContext().clearDbContext();
79  
80                  // go to manage db screen (to install db)
81                  GoToManageDbAction action = getContext().getActionFactory().createLogicAction(handler, GoToManageDbAction.class);
82                  action.setSkipCheckCurrentScreen(true);
83                  delegateAction = action;
84              }
85          }
86  
87          setActionDescription(delegateAction.getActionDescription());
88          return delegateAction.prepareAction();
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public void doAction() throws Exception {
94          getActionEngine().runInternalAction(delegateAction);
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      protected void releaseAction() {
100         delegateAction = null;
101         super.releaseAction();
102     }
103 
104 }