View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.home.actions;
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.base.Preconditions;
26  import com.google.common.collect.Sets;
27  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
28  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
29  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocols;
30  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
31  import fr.ifremer.tutti.ui.swing.TuttiScreen;
32  import fr.ifremer.tutti.ui.swing.content.actions.AbstractChangeScreenAction;
33  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
34  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
35  import jaxx.runtime.context.JAXXContextEntryDef;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  import javax.swing.JOptionPane;
40  import javax.swing.UIManager;
41  import java.util.Set;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Opens the protocol edition screen.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 1.0
50   */
51  public class EditProtocolAction extends AbstractChangeScreenAction {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(EditProtocolAction.class);
55  
56      public static final JAXXContextEntryDef<TuttiProtocol> CLEAN_PROTOCOL_ENTRY = new JAXXContextEntryDef<>("cleanProtocol", TuttiProtocol.class);
57  
58      public EditProtocolAction(MainUIHandler handler) {
59          super(handler, true, TuttiScreen.EDIT_PROTOCOL);
60      }
61  
62      @Override
63      public boolean prepareAction() throws Exception {
64  
65          CLEAN_PROTOCOL_ENTRY.removeContextValue(getContext().getMainUI());
66  
67          boolean doAction = super.prepareAction();
68          if (doAction) {
69  
70              // check that protocol is compatible with sample category model
71              SampleCategoryModel sampleCategoryModel =
72                      getDataContext().getSampleCategoryModel();
73  
74              TuttiProtocol protocol = getDataContext().getProtocol();
75  
76              Set<Integer> badCategories = Sets.newHashSet();
77  
78              TuttiProtocols.checkSampleCategories(sampleCategoryModel,
79                                                   protocol,
80                                                   badCategories);
81  
82              if (!badCategories.isEmpty()) {
83  
84                  // detect some bad categories
85                  if (log.isWarnEnabled()) {
86                      log.warn("There is some bad categories: " + badCategories);
87                  }
88  
89                  String message = TuttiProtocols.getBadCategoriesMessage(
90                          badCategories,
91                          getDecorator(Caracteristic.class, null),
92                          getContext().getPersistenceService());
93  
94                  String htmlMessage = String.format(
95                          AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
96                          message,
97                          t("tutti.common.askBeforeEditProtocol.help"));
98                  int response = JOptionPane.showOptionDialog(
99                          getContext().getActionUI(),
100                         htmlMessage,
101                         t("tutti.common.askBeforeEditProtocol.title"),
102                         JOptionPane.YES_NO_OPTION,
103                         JOptionPane.WARNING_MESSAGE,
104                         UIManager.getIcon("warning"),
105                         new Object[]{t("tutti.option.cleanAndEdit"), t("tutti.option.edit"), t("tutti.option.cancel")},
106                         t("tutti.option.cancel")
107                 );
108 
109                 switch (response) {
110                     case 0:
111                         // edit and clean
112                         if (log.isInfoEnabled()) {
113                             log.info("Clean and edit");
114                         }
115 
116                         protocol = getContext().getPersistenceService().getProtocol(getContext().getProtocolId());
117 
118                         CLEAN_PROTOCOL_ENTRY.setContextValue(getContext().getMainUI(), protocol);
119 
120                         TuttiProtocols.removeBadCategories(sampleCategoryModel,
121                                                            protocol);
122 
123                         break;
124 
125                     case 1:
126                         // edit with no modification
127                         if (log.isInfoEnabled()) {
128                             log.info("Edit with no cleaning");
129                         }
130                         break;
131 
132                     default:
133 
134                         // cancel
135                         doAction = false;
136                 }
137 
138             }
139         }
140         return doAction;
141     }
142 
143     @Override
144     public void doAction() throws Exception {
145         Preconditions.checkState(getContext().isProtocolFilled());
146         if (log.isInfoEnabled()) {
147             log.info("Edit protocol: " + getContext().getProtocolId());
148         }
149         createProgressionModelIfRequired(4);
150         super.doAction();
151     }
152 
153 }