1 package fr.ifremer.reefdb.ui.swing.content.manage.referential.replace;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.quadrige3.ui.core.dto.referential.BaseReferentialDTO;
27 import fr.ifremer.reefdb.dto.ReefDbBeans;
28 import fr.ifremer.reefdb.service.referential.ReferentialService;
29 import fr.ifremer.reefdb.ui.swing.ReefDbUIContext;
30 import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbMainUIAction;
31 import fr.ifremer.reefdb.ui.swing.content.ReefDbMainUIHandler;
32 import jaxx.runtime.context.JAXXInitialContext;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import javax.swing.SwingUtilities;
38 import java.awt.Dimension;
39 import java.util.List;
40
41 import static org.nuiton.i18n.I18n.t;
42
43
44
45
46
47
48
49
50
51 public abstract class AbstractOpenReplaceUIAction<
52 E extends BaseReferentialDTO,
53 M extends AbstractReplaceUIModel<E>,
54 UI extends AbstractReplaceUI<E, M>>
55 extends AbstractReefDbMainUIAction {
56
57
58
59
60 private static final Log log =
61 LogFactory.getLog(AbstractOpenReplaceUIAction.class);
62
63
64
65
66
67
68 protected abstract String getEntityLabel();
69
70
71
72
73
74
75 protected abstract M createNewModel();
76
77
78
79
80
81
82
83 protected abstract UI createUI(JAXXInitialContext ctx);
84
85
86
87
88
89
90
91 protected abstract List<E> getReferentialList(ReferentialService referentialService);
92
93
94
95
96
97
98
99 protected List<E> getSourceListFromReferentialList(List<E> list) {
100
101 return ReefDbBeans.filterLocalReferential(list);
102 }
103
104
105
106
107
108
109 protected abstract E getSelectedSource();
110
111
112
113
114
115
116 protected abstract E getSelectedTarget();
117
118 protected E source;
119
120 protected E target;
121
122 private boolean showDialog = true;
123
124 private List<E> referentialList;
125
126 private List<E> sourceList;
127
128
129
130
131
132
133 protected AbstractOpenReplaceUIAction(ReefDbMainUIHandler handler) {
134 super(handler, false);
135 }
136
137
138 @Override
139 public boolean prepareAction() throws Exception {
140
141 boolean doAction = super.prepareAction();
142
143 if (doAction) {
144
145 createProgressionUIModel(3);
146
147 }
148 return doAction;
149 }
150
151
152 @Override
153 public void releaseAction() {
154 source = target = null;
155 super.releaseAction();
156 }
157
158
159 @Override
160 public void doAction() {
161
162 ReferentialService referentialService = getContext().getReferentialService();
163
164 String entityLabel = getEntityLabel();
165
166 getProgressionUIModel().increments(t("reefdb.replaceReferential.loading.target", entityLabel));
167
168
169 referentialList = getReferentialList(referentialService);
170
171 getProgressionUIModel().increments(t("reefdb.replaceReferential.loading.source", entityLabel));
172
173
174 sourceList = getSourceListFromReferentialList(referentialList);
175
176 if (log.isDebugEnabled()) {
177 log.debug("Loaded local referential: " + (source == null ? "null" : sourceList.size()));
178 log.debug("Loaded target referential: " + (referentialList == null ? "null" : referentialList.size()));
179 }
180
181 if (CollectionUtils.isEmpty(referentialList)) {
182
183 displayWarningMessage(t("reefdb.replaceReferential.noTarget.title", entityLabel),
184 t("reefdb.replaceReferential.noTarget.message", entityLabel));
185
186 showDialog = false;
187 }
188
189 if (CollectionUtils.isEmpty(sourceList)) {
190
191 displayWarningMessage(t("reefdb.replaceReferential.noSource.title", entityLabel),
192 t("reefdb.replaceReferential.noSource.message", entityLabel));
193
194 showDialog = false;
195 }
196
197 }
198
199
200 @Override
201 public void postSuccessAction() {
202
203 if (showDialog) {
204
205 getProgressionUIModel().increments(t("reefdb.replaceReferential.open.dialog"));
206
207 M model = createNewModel();
208
209 model.setTargetList(referentialList);
210 model.setSourceList(sourceList);
211 model.setSelectedSource(getSelectedSource());
212 model.setSelectedTarget(getSelectedTarget());
213
214 JAXXInitialContext ctx = new JAXXInitialContext();
215 ctx.add(getUI());
216 ctx.add(model);
217 ctx.add(ReefDbUIContext.PROPERTY_APPLICATION_CONTEXT, getContext());
218
219 final UI dialog = createUI(ctx);
220 dialog.setResizable(false);
221 SwingUtilities.invokeLater(() -> getHandler().openDialog(dialog, new Dimension(800, 180)));
222 }
223
224 }
225 }