View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.rtp;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.persistence.entities.referential.Species;
28  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
29  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesTableModel;
30  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
31  import jaxx.runtime.swing.ComponentMover;
32  import jaxx.runtime.validator.swing.SwingValidator;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import javax.swing.JComponent;
37  import javax.swing.JOptionPane;
38  import javax.swing.RowSorter;
39  import java.awt.Component;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * Created on 14/01/16.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   */
48  public class RtpEditorUIHandler extends AbstractTuttiUIHandler<RtpEditorUIModel, RtpEditorUI> {
49  
50      /** Logger. */
51      private static final Log log = LogFactory.getLog(RtpEditorUIHandler.class);
52  
53      @Override
54      public void beforeInit(RtpEditorUI ui) {
55          super.beforeInit(ui);
56  
57          RtpEditorUIModel model = new RtpEditorUIModel();
58  
59          this.ui.setContextValue(model);
60  
61      }
62  
63      @Override
64      public void afterInit(RtpEditorUI ui) {
65  
66          super.initUI(ui);
67  
68          ui.pack();
69  
70  //        ComponentResizer cr = new ComponentResizer();
71  //        cr.registerComponent(ui);
72          ComponentMover cm = new ComponentMover();
73  //        cm.setDragInsets(cr.getDragInsets());
74          cm.registerComponent(ui);
75  
76          ui.getBodyPanel().setRightDecoration(ui.getHeaderToolBar());
77  
78          RtpEditorUIModel model = getModel();
79          listModelIsModify(model);
80          listenValidatorValid(ui.getValidator(), model);
81  
82          model.addPropertyChangeListener(RtpEditorUIModel.PROPERTY_ROW, evt -> {
83              EditProtocolSpeciesRowModel rowModel = getModel().getRowModel();
84  
85              Species species = rowModel.getSpecies();
86  
87              String decorate = decorate(species);
88  
89              if (log.isInfoEnabled()) {
90                  log.info("Edit RTP for species: " + decorate);
91              }
92  
93              getUI().getBodyPanel().setTitle(t("tutti.rtpEdit.title", decorate));
94          });
95      }
96  
97      @Override
98      protected JComponent getComponentToFocus() {
99          return getUI().getRtpMaleAField();
100     }
101 
102     @Override
103     public void onCloseUI() {
104 
105         boolean result = quitUI();
106 
107         if (result) {
108             getModel().reset();
109             ui.dispose();
110         }
111     }
112 
113     public boolean quitUI() {
114         boolean result = true;
115 
116         RtpEditorUIModel model = getModel();
117 
118         if (model.isModify()) {
119             if (model.isValid()) {
120 
121                 result = quitUnsavedRtpForm();
122 
123             } else {
124 
125                 // model is not valid, ask user to continue or not
126 
127                 result = quitInvalidRtpForm();
128             }
129         }
130 
131         return result;
132     }
133 
134     protected boolean quitUnsavedRtpForm() {
135         // ask if user want to save, do not save or cancel action
136 
137         boolean result;
138 
139         int answer = askSaveBeforeLeaving(t("tutti.rtpEdit.askSaveBeforeLeaving"));
140         switch (answer) {
141             case JOptionPane.YES_OPTION:
142                 getContext().getActionEngine().runAction(ui.getSaveButton());
143                 result = true;
144                 break;
145 
146             case JOptionPane.NO_OPTION:
147                 result = true;
148                 break;
149 
150             default:
151                 // other case, use cancel action
152                 result = false;
153         }
154         return result;
155     }
156 
157     protected boolean quitInvalidRtpForm() {
158         return askCancelEditBeforeLeaving(t("tutti.rtpEdit.askCancelEditBeforeLeaving"));
159     }
160 
161     @Override
162     public SwingValidator<RtpEditorUIModel> getValidator() {
163         return ui.getValidator();
164     }
165 
166     @Override
167     public Component getTopestUI() {
168         return getUI();
169     }
170 
171     public void setBean(EditProtocolSpeciesTableModel tableModel, RowSorter rowSorter, int row) {
172         getModel().setRowModel(tableModel, rowSorter, row);
173     }
174 
175     public void openEditor() {
176         ui.setVisible(true);
177     }
178 
179     public void closeEditor() {
180         onCloseUI();
181     }
182 }