1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.metis.ui;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
21 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
22 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
23 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerEntry;
24 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerErrorList;
25 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerExceptionWrapper;
26 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerManager;
27 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerStandardEntry;
28 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
29 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButton;
30 import io.github.tonywasher.joceanus.tethys.api.control.TethysUILabel;
31 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
32 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
33
34
35
36
37 public class MetisErrorPanel
38 implements OceanusEventProvider<MetisUIEvent>, TethysUIComponent {
39
40
41
42 private static final String NLS_CLEAR = MetisUIResource.ERROR_BUTTON_CLEAR.getValue();
43
44
45
46
47 private static final String NLS_TITLE = MetisUIResource.ERROR_TITLE.getValue();
48
49
50
51
52 private final OceanusEventManager<MetisUIEvent> theEventManager;
53
54
55
56
57 private final TethysUIBoxPaneManager thePanel;
58
59
60
61
62 private final TethysUILabel theErrorField;
63
64
65
66
67 private final TethysUIButton theClearButton;
68
69
70
71
72 private final MetisViewerEntry theViewerError;
73
74
75
76
77 private final MetisViewerErrorList theErrors;
78
79
80
81
82
83
84
85
86 public MetisErrorPanel(final TethysUIFactory<?> pFactory,
87 final MetisViewerManager pViewerMgr,
88 final MetisViewerEntry pParent) {
89
90 theViewerError = pViewerMgr.newEntry(pParent, MetisViewerStandardEntry.ERROR.toString());
91 theViewerError.setVisible(false);
92
93
94 theEventManager = new OceanusEventManager<>();
95
96
97 theErrors = new MetisViewerErrorList();
98 theViewerError.setObject(theErrors);
99
100
101 theErrorField = pFactory.controlFactory().newLabel();
102 theErrorField.setErrorText();
103
104
105 theClearButton = pFactory.buttonFactory().newButton();
106 theClearButton.setTextOnly();
107 theClearButton.setText(NLS_CLEAR);
108
109
110 theClearButton.getEventRegistrar().addEventListener(e -> clearErrors());
111
112
113 thePanel = pFactory.paneFactory().newHBoxPane();
114 thePanel.setBorderTitle(NLS_TITLE);
115
116
117 thePanel.addNode(theClearButton);
118 thePanel.addNode(theErrorField);
119
120
121 thePanel.setVisible(false);
122 }
123
124 @Override
125 public OceanusEventRegistrar<MetisUIEvent> getEventRegistrar() {
126 return theEventManager.getEventRegistrar();
127 }
128
129 @Override
130 public TethysUIComponent getUnderlying() {
131 return thePanel;
132 }
133
134 @Override
135 public void setEnabled(final boolean bEnabled) {
136
137 theClearButton.setEnabled(bEnabled);
138 }
139
140
141
142
143
144
145 public boolean hasError() {
146 return !theErrors.isEmpty();
147 }
148
149
150
151
152
153
154 public void addError(final OceanusException pException) {
155
156 if (!hasError()) {
157
158 theViewerError.setVisible(true);
159 }
160
161
162 theErrors.add(new MetisViewerExceptionWrapper(pException));
163
164
165 setErrorText(pException.getMessage());
166
167
168 theEventManager.fireEvent(MetisUIEvent.VISIBILITY);
169 }
170
171
172
173
174
175
176 public void showValidateError(final String pError) {
177 if (pError != null) {
178 setErrorText(pError);
179 } else {
180 thePanel.setVisible(false);
181 }
182 }
183
184
185
186
187
188
189 private void setErrorText(final String pText) {
190
191 theErrorField.setText(pText);
192
193
194 thePanel.setVisible(true);
195 }
196
197
198
199
200
201
202 public void setErrors(final MetisViewerErrorList pExceptions) {
203
204 if (hasError()) {
205
206 theErrors.clear();
207 theViewerError.setVisible(false);
208 }
209
210
211 if (!pExceptions.isEmpty()) {
212
213 theViewerError.setVisible(true);
214
215
216 theErrors.addList(pExceptions);
217
218
219 setErrorText(pExceptions.getUnderlyingList().get(0).getMessage());
220 }
221
222
223 theEventManager.fireEvent(MetisUIEvent.VISIBILITY);
224 }
225
226
227
228
229 private void clearErrors() {
230
231 if (hasError()) {
232
233 theErrors.clear();
234 theViewerError.setVisible(false);
235 }
236
237
238 setVisible(false);
239
240
241 theEventManager.fireEvent(MetisUIEvent.VISIBILITY);
242 }
243 }