View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.program;
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 com.google.common.collect.Lists;
26  import fr.ifremer.tutti.persistence.entities.data.Program;
27  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
28  import jaxx.runtime.JAXXUtil;
29  import jaxx.runtime.validator.swing.SwingValidator;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.nuiton.jaxx.application.swing.util.CloseableUI;
33  
34  import javax.swing.JComponent;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Handler of UI {@link EditProgramUI}.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 0.1
43   */
44  public class EditProgramUIHandler extends AbstractTuttiUIHandler<EditProgramUIModel, EditProgramUI> implements CloseableUI {
45  
46      /** Logger. */
47      private static final Log log =
48              LogFactory.getLog(EditProgramUIHandler.class);
49  
50      public static String getTitle(boolean exist) {
51  
52          String result;
53          if (exist) {
54              result = t("tutti.editProgram.title.edit.program");
55          } else {
56              result = t("tutti.editProgram.title.create.program");
57          }
58          return result;
59      }
60  
61      @Override
62      public void beforeInit(EditProgramUI ui) {
63  
64          super.beforeInit(ui);
65          getDataContext().resetValidationDataContext();
66  
67          EditProgramUIModel model = new EditProgramUIModel();
68  
69          String programId = getContext().getProgramId();
70          if (programId == null) {
71  
72              if (log.isInfoEnabled()) {
73                  log.info("Edit new program");
74              }
75          } else {
76  
77              if (log.isInfoEnabled()) {
78                  log.info("Edit existing program " + programId);
79              }
80              // load existing program
81              Program program = getPersistenceService().getProgram(programId);
82  
83              model.fromEntity(program);
84          }
85  
86          listModelIsModify(model);
87  
88          ui.setContextValue(model);
89      }
90  
91      @Override
92      public void afterInit(EditProgramUI ui) {
93  
94          initUI(ui);
95  
96          EditProgramUIModel model = getModel();
97          initBeanFilterableComboBox(ui.getZoneComboBox(),
98                                     Lists.newArrayList(getPersistenceService().getAllProgramZone()),
99                                     model.getZone());
100 
101         SwingValidator validator = ui.getValidator();
102         listenValidatorValid(validator, model);
103 
104         registerValidators(validator);
105 
106         // if new program can already cancel his creation
107         model.setModify(model.isCreate());
108 
109         //FIXME Binding does not work
110         JAXXUtil.applyDataBinding(ui, EditProgramUI.BINDING_RESET_BUTTON_ENABLED);
111     }
112 
113     @Override
114     protected JComponent getComponentToFocus() {
115         return getUI().getNameField();
116     }
117 
118     @Override
119     public void onCloseUI() {
120         if (log.isDebugEnabled()) {
121             log.debug("closing: " + ui);
122         }
123         clearValidators();
124     }
125 
126     @Override
127     public boolean quitUI() {
128         return quitScreen(
129                 getModel().isValid(),
130                 getModel().isModify(),
131                 t("tutti.editProgram.askCancelEditBeforeLeaving.cancelSaveProgram"),
132                 t("tutti.editProgram.askSaveBeforeLeaving.saveProgram"),
133                 ui.getSaveButton().getAction()
134         );
135     }
136 
137     @Override
138     public SwingValidator<EditProgramUIModel> getValidator() {
139         return ui.getValidator();
140     }
141 
142     public void reloadProgram(Program program) {
143 
144         EditProgramUIModel model = getModel();
145 
146         model.fromEntity(program);
147 
148         getModel().setModify(false);
149     }
150 
151 }