View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.context.menu;
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 com.google.common.collect.ImmutableList;
27  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
28  import fr.ifremer.reefdb.dto.configuration.context.ContextDTO;
29  import fr.ifremer.reefdb.ui.swing.action.AbstractCheckModelAction;
30  import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbSaveAction;
31  import fr.ifremer.reefdb.ui.swing.content.manage.context.ManageContextsUI;
32  import fr.ifremer.reefdb.ui.swing.content.manage.context.ManageContextsUIModel;
33  import fr.ifremer.reefdb.ui.swing.content.manage.context.SaveAction;
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() {
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  //        getUI().getParentContainer(ManageContextsUI.class).getHandler().loadContexts(idLocalContext);
66  
67          if (idLocalContext == null) {
68              results = getContext().getContextService().getAllContexts();
69          } else {
70              results = ImmutableList.of(getContext().getContextService().getContext(idLocalContext));
71          }
72  
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public void postSuccessAction() {
78  
79          getModel().setResults(results);
80  
81          super.postSuccessAction();
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      protected Class<? extends AbstractReefDbSaveAction> getSaveActionClass() {
87          return SaveAction.class;
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      protected boolean isModelModify() {
93          return getParentModel() != null && getParentModel().isModify();
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      protected void setModelModify(boolean modelModify) {
99          if (getParentModel() != null) {
100             getParentModel().setModify(modelModify);
101         }
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     protected boolean isModelValid() {
107         return getParentModel() == null || getParentModel().isValid();
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     protected AbstractApplicationUIHandler<?, ?> getSaveHandler() {
113         return getParentUI().getHandler();
114     }
115 
116     @Override
117     protected List<AbstractBeanUIModel> getOtherModelsToModify() {
118         if (getParentModel() != null) {
119             return ImmutableList.of(getParentModel().getContextListModel());
120         }
121         return super.getOtherModelsToModify();
122     }
123 
124     private ManageContextsUI getParentUI() {
125         return getUI().getParentContainer(ManageContextsUI.class);
126     }
127 
128     private ManageContextsUIModel getParentModel() {
129         final ManageContextsUI ui = getParentUI();
130         if (ui != null) {
131             return ui.getModel();
132         }
133         return null;
134     }
135 
136 }