1 package fr.ifremer.tutti.ui.swing.content.protocol.rtp;
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
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
45
46
47
48 public class RtpEditorUIHandler extends AbstractTuttiUIHandler<RtpEditorUIModel, RtpEditorUI> {
49
50
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
71
72 ComponentMover cm = new ComponentMover();
73
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
126
127 result = quitInvalidRtpForm();
128 }
129 }
130
131 return result;
132 }
133
134 protected boolean quitUnsavedRtpForm() {
135
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
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 }