1 package fr.ifremer.dali.ui.swing.action;
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 fr.ifremer.dali.ui.swing.util.DaliUI;
27 import fr.ifremer.quadrige3.ui.swing.AbstractUIHandler;
28 import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
29 import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
30
31 import javax.swing.JOptionPane;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import static org.nuiton.i18n.I18n.t;
37
38 /**
39 * Constructor.
40 *
41 * @param <M> Model
42 * @param <UI> UI
43 * @param <H> Handler
44 */
45 public abstract class AbstractCheckModelAction<M extends AbstractBeanUIModel, UI extends DaliUI<M, ?>, H extends AbstractUIHandler<M, UI>>
46 extends AbstractDaliAction<M, UI, H> {
47
48 /**
49 * Constructor.
50 *
51 * @param handler Handler
52 * @param hideBody HideBody
53 */
54 protected AbstractCheckModelAction(final H handler, final boolean hideBody) {
55 super(handler, hideBody);
56 }
57
58 /**
59 * Save action class.
60 *
61 * @return Save action
62 */
63 protected abstract Class<? extends AbstractDaliSaveAction> getSaveActionClass();
64
65 /**
66 * Model modify.
67 *
68 * @return True if model is modify, False otherwise
69 */
70 protected abstract boolean isModelModify();
71
72 /**
73 * Model modify
74 *
75 * @param modelModify a boolean.
76 */
77 protected abstract void setModelModify(boolean modelModify);
78
79 /**
80 * Model valid
81 *
82 * @return True if model is valid, false otherwise
83 */
84 protected abstract boolean isModelValid();
85
86 /**
87 * Return the handler used for the SaveAction
88 *
89 * @return a {@link org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler} object.
90 */
91 protected abstract AbstractApplicationUIHandler<?, ?> getSaveHandler();
92
93 /** {@inheritDoc} */
94 @Override
95 public boolean prepareAction() throws Exception {
96 boolean canContinue = super.prepareAction();
97 // Model is modify
98 if (canContinue && isModelModify()) {
99
100 // Ask save modifications or not
101 final int result = askSaveBeforeLeaving(t("dali.action.common.askSaveBeforeLeaving.message"));
102 switch (result) {
103 case JOptionPane.YES_OPTION:
104 // Model is valid
105 if (isModelValid()) {
106
107 // Save action
108 AbstractDaliSaveAction saveAction = getActionFactory().createLogicAction(getSaveHandler(), getSaveActionClass());
109 if (saveAction.prepareAction()) {
110 if (this instanceof AbstractCheckBeforeChangeScreenAction) {
111 saveAction.setFromChangeScreenAction(true);
112 }
113 saveAction.setFromCheckModelAction(true);
114 getActionEngine().runInternalAction(saveAction);
115
116 // Continue
117 canContinue = saveAction.isAllowChangeScreen();
118 } else {
119 canContinue = false;
120 }
121 } else {
122
123 // Ask user for remove error
124 displayErrorMessage(t("dali.action.save.errors.title"), t("dali.action.save.errors.remove"));
125
126 // Stop saving
127 canContinue = false;
128 }
129 break;
130
131 case JOptionPane.NO_OPTION:
132 // Continue
133 canContinue = true;
134 break;
135
136 default:
137 // Stop closing
138 canContinue = false;
139 break;
140 }
141 }
142 return canContinue;
143 }
144
145 // Override this method to gather other models to set as not modified at the end of action
146 protected List<AbstractBeanUIModel> getOtherModelsToModify() {
147 return new ArrayList<>();
148 }
149
150 /** {@inheritDoc} */
151 @Override
152 public void postSuccessAction() {
153 super.postSuccessAction();
154
155 // Set each model as not modified
156 getOtherModelsToModify().forEach(model -> model.setModify(false));
157
158 //if action is done successfully, either model has been saved or changes have been canceled
159 //so we set the model modify to false
160 setModelModify(false);
161 }
162 }