View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.context.menu;
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 com.google.common.collect.ImmutableList;
27  import fr.ifremer.dali.dto.configuration.context.ContextDTO;
28  import fr.ifremer.dali.ui.swing.action.AbstractCheckModelAction;
29  import fr.ifremer.dali.ui.swing.action.AbstractDaliSaveAction;
30  import fr.ifremer.dali.ui.swing.content.manage.context.ManageContextsUI;
31  import fr.ifremer.dali.ui.swing.content.manage.context.ManageContextsUIModel;
32  import fr.ifremer.dali.ui.swing.content.manage.context.SaveAction;
33  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
34  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
35  
36  import java.util.List;
37  
38  /**
39   * <p>SearchAction class.</p>
40   *
41   */
42  public class SearchAction extends AbstractCheckModelAction<ManageContextsListMenuUIModel, ManageContextsListMenuUI, ManageContextsListMenuUIHandler> {
43  
44      private List<ContextDTO> results;
45  
46      /**
47       * Constructor.
48       *
49       * @param handler Le controller
50       */
51      public SearchAction(final ManageContextsListMenuUIHandler handler) {
52          super(handler, false);
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public void doAction() throws Exception {
58  
59          Integer idLocalContext = null;
60          if (getUI().getContextsLabelsCombo().getSelectedItem() instanceof ContextDTO) {
61              final ContextDTO localContext = (ContextDTO) getUI().getContextsLabelsCombo().getSelectedItem();
62              idLocalContext = localContext.getId();
63          }
64  
65          if (idLocalContext == null) {
66              results = getContext().getContextService().getAllContexts();
67          } else {
68              results = ImmutableList.of(getContext().getContextService().getContext(idLocalContext));
69          }
70  
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public void postSuccessAction() {
76  
77          getModel().setResults(results);
78  
79          super.postSuccessAction();
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      protected Class<? extends AbstractDaliSaveAction> getSaveActionClass() {
85          return SaveAction.class;
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      protected boolean isModelModify() {
91          return getParentModel() != null && getParentModel().isModify();
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      protected void setModelModify(boolean modelModify) {
97          if (getParentModel() != null) {
98              getParentModel().setModify(modelModify);
99          }
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     protected boolean isModelValid() {
105         return getParentModel() == null || getParentModel().isValid();
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     protected AbstractApplicationUIHandler<?, ?> getSaveHandler() {
111         return getParentUI().getHandler();
112     }
113 
114     @Override
115     protected List<AbstractBeanUIModel> getOtherModelsToModify() {
116         if (getParentModel() != null) {
117             return ImmutableList.of(getParentModel().getContextListModel());
118         }
119         return super.getOtherModelsToModify();
120     }
121 
122     private ManageContextsUI getParentUI() {
123         return getUI().getParentContainer(ManageContextsUI.class);
124     }
125 
126     private ManageContextsUIModel getParentModel() {
127         final ManageContextsUI ui = getParentUI();
128         if (ui != null) {
129             return ui.getModel();
130         }
131         return null;
132     }
133 
134 }