View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.filter;
2   
3   /*-
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2022 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  
27  import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
28  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
29  import fr.ifremer.reefdb.ui.swing.ReefDbHelpBroker;
30  import fr.ifremer.reefdb.ui.swing.ReefDbUIContext;
31  import fr.ifremer.reefdb.ui.swing.content.ReefDbMainUI;
32  import fr.ifremer.reefdb.ui.swing.content.manage.filter.element.FilterElementUI;
33  import fr.ifremer.reefdb.ui.swing.content.manage.filter.list.FilterListUI;
34  import fr.ifremer.reefdb.ui.swing.util.ReefDbUI;
35  import java.awt.BorderLayout;
36  import java.awt.Component;
37  import java.awt.Container;
38  import java.awt.FlowLayout;
39  import java.awt.GridLayout;
40  import java.awt.LayoutManager;
41  import java.util.ArrayList;
42  import java.util.HashMap;
43  import java.util.List;
44  import java.util.Map;
45  import java.util.TreeMap;
46  import javax.swing.Box;
47  import javax.swing.BoxLayout;
48  import javax.swing.JButton;
49  import javax.swing.JPanel;
50  import javax.swing.JScrollPane;
51  import jaxx.runtime.JAXXBinding;
52  import jaxx.runtime.JAXXContext;
53  import jaxx.runtime.JAXXObject;
54  import jaxx.runtime.JAXXObjectDescriptor;
55  import jaxx.runtime.JAXXUtil;
56  import jaxx.runtime.JAXXValidator;
57  import jaxx.runtime.SwingUtil;
58  import jaxx.runtime.binding.DefaultJAXXBinding;
59  import jaxx.runtime.context.DefaultJAXXContext;
60  import jaxx.runtime.swing.help.JAXXHelpUI;
61  import jaxx.runtime.validator.swing.SwingValidator;
62  import jaxx.runtime.validator.swing.SwingValidatorUtil;
63  import jaxx.runtime.validator.swing.meta.Validator;
64  import jaxx.runtime.validator.swing.meta.ValidatorField;
65  import jaxx.runtime.validator.swing.ui.ImageValidationUI;
66  import org.apache.commons.logging.Log;
67  import org.apache.commons.logging.LogFactory;
68  import static org.nuiton.i18n.I18n.t;
69  
70  public abstract class FilterUI extends JPanel implements JAXXHelpUI<ReefDbHelpBroker>, ReefDbUI<FilterUIModel, FilterUIHandler>, JAXXObject, JAXXValidator {
71  
72      /*-----------------------------------------------------------------------*/
73      /*------------------ Constants for all public bindings ------------------*/
74      /*-----------------------------------------------------------------------*/
75  
76      public static final String BINDING_SAVE_BUTTON_ENABLED = "saveButton.enabled";
77  
78      /*-----------------------------------------------------------------------*/
79      /*------------------------- Other static fields -------------------------*/
80      /*-----------------------------------------------------------------------*/
81  
82      private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAKVVz28bRRSeGNuJnZRAopYECoS0KSlI6x6Q+BFUmriJ4uL+UB2iCB/C7O7YmXa8s8zOpmtZqbhx5U+AOxckbpwQB84cuCD+BYQ4cEW82VnveuON7bQ+jLwz733ve+/N++b7v1DBE+itRzgIDOE7knaIcWfz4OC++YhY8jbxLEFdyQXSv6kcyjXRrB3vexJdadaVeyVyr1R5x+UOcQa8N+qo7MkuI94RIVKi19MeludVGvHxRuD6oo8ak8pC/fafv3Pf2F99l0MocIHdHKSyMs4rySRfRzlqS7QAkY5xhWGnDTQEddrA94LaqzLsefdwh3yJnqLpOiq6WACYRKuTpxxihP6BK9FMizJJxGc1iT5sCYO2BOkQYQhCWrZp+NTwnkB8w+KOBDijgx3cJoZ2MnYiX9cNAYsSFTrcJkyim8+MdVcBJIBFU/DHREj07gjEh/B929wFv63QOnEvHWNGbQz5A0KqE/FBhNFQ635/M0GY0/z2ui6pQW9eVm0wVGuMGqTRHgw2c/XOA+wQdgNaqMyCCFrvKqPSadg69aSq/da568XAMyqaBlHASzF+GfrOGVORJXolxaYRn6Rd5jXyNgMWTshq99ysiHaOiMVQKsIbSe6mLyV3vLgsr6ZrM2sx7pGt0EiixRR5vasMrw7kio9JcrAenQr0WqrjMNVGMtXJ2E01UUH4sC3RUnNYCB7CkZaApVMSoADD0/8uLf7+058/7vTn/gLEvphpOiBbMI+u4C7cbapCz+uh9yVllbvY3WiikgfVtLSmXc4g1oiOgRzE0xdTuRu72DsCiML0Hz//cumL315AuR1UZhzbO1jZ11BJHgmoAmd24H5yK2Q092QG1pcUN7gvh9xh3X1FzcJM3xdsMihaXgqfBFCLyxm1iAmZpV//XWz8cKtfjyngt3ymeVKTwueoSB1GHRIqYSRymco363rEt3kiZlnyhlJz4EaTeiNc3xtKPA9je4hdl0HSknJn01IrXMBeVd1H/WlYCv8EgHH4XVNzgIoWdiylexeHAT4lXdDW3ohRCpumNQyMQfFBwzyjunmvul0/UTUnAUj8YuRm8U4n5AGcAl+l8kFYYvXvI5SennEpF89IeaHXgJEalXFezdzZ+b5//nwbm/vbSbYL6Wx1tFnJOduj7l5oshyZaFqhSSRDEk0TR11YEOy1XvgkGdSDl4W2uuvXV65dW+nvhYK/fv0kq5ADsjKujjAz84fxi1LHproLV3pyfTXiGMmjnmhiwwMCJqvZcU/r8DMFXxsOnrw4hqSSkTPCp16mCWIXGe5yqBJ68WMggFdM6tjQ5ZtD2FNqLY9FVNuP1cKyEZYn4WRyYRMxAuXN5+axNgGPMnSl7ag2HoxAensiLk9HILwzAZc8tfT7+Fwsvh7D4n8GEAeNwQsAAA==";
83      private static final Log log = LogFactory.getLog(FilterUI.class);
84      private static final long serialVersionUID = 1L;
85  
86      /*-----------------------------------------------------------------------*/
87      /*--------------------------- Internal states ---------------------------*/
88      /*-----------------------------------------------------------------------*/
89  
90      protected List<Object> $activeBindings = new ArrayList<Object>();
91      protected Map<String, Object> $bindingSources = new HashMap<String, Object>();
92      protected final Map<String, JAXXBinding> $bindings = new TreeMap<String, JAXXBinding>();
93      protected Map<String, Object> $objectMap = new HashMap<String, Object>();
94      protected Map<?, ?> $previousValues = new HashMap<Object, Object>();
95      private boolean allComponentsCreated;
96      protected final JAXXContext delegateContext = new DefaultJAXXContext();
97  
98      /*-----------------------------------------------------------------------*/
99      /*------------------------ Protected components  ------------------------*/
100     /*-----------------------------------------------------------------------*/
101 
102     protected ReefDbHelpBroker broker;
103     protected JPanel buttonsPanel;
104     protected JButton closeButton;
105     @ValidatorField( validatorId = "validator",  propertyName = "filterElementUIModel",  editorName = "filterElementUI")
106     protected FilterElementUI filterElementUI;
107     @ValidatorField( validatorId = "validator",  propertyName = "filterListUIModel",  editorName = "filterListUI")
108     protected FilterListUI filterListUI;
109     protected Integer filterTypeId;
110     protected FilterUI filterUI = this;
111     protected final FilterUIHandler handler = createHandler();
112     protected FilterUIModel model;
113     protected JButton saveButton;
114     protected JScrollPane scrollPane;
115     @Validator( validatorId = "validator")
116     protected SwingValidator<FilterUIModel> validator;
117     protected List<String> validatorIds = new ArrayList<String>();
118 
119     /*-----------------------------------------------------------------------*/
120     /*------------------------- Private components  -------------------------*/
121     /*-----------------------------------------------------------------------*/
122 
123     private JPanel $JPanel0;
124 
125     /*-----------------------------------------------------------------------*/
126     /*---------------------------- Constructors  ----------------------------*/
127     /*-----------------------------------------------------------------------*/
128 
129     public FilterUI(ReefDbMainUI parentUI) {
130         ApplicationUIUtil.setParentUI(this, parentUI);
131             $initialize();
132     }
133 
134     public FilterUI(LayoutManager param0, boolean param1) {
135         super(param0 ,param1);
136         $initialize();
137     }
138 
139     public FilterUI(JAXXContext parentContext, LayoutManager param1, boolean param2) {
140         super(param1 ,param2);
141         JAXXUtil.initContext(this, parentContext);
142         $initialize();
143     }
144 
145     public FilterUI(boolean param0) {
146         super(param0);
147         $initialize();
148     }
149 
150     public FilterUI(JAXXContext parentContext, boolean param1) {
151         super(param1);
152         JAXXUtil.initContext(this, parentContext);
153         $initialize();
154     }
155 
156     public FilterUI() {
157         $initialize();
158     }
159 
160     public FilterUI(JAXXContext parentContext) {
161         JAXXUtil.initContext(this, parentContext);
162         $initialize();
163     }
164 
165     public FilterUI(LayoutManager param0) {
166         super(param0);
167         $initialize();
168     }
169 
170     public FilterUI(JAXXContext parentContext, LayoutManager param1) {
171         super(param1);
172         JAXXUtil.initContext(this, parentContext);
173         $initialize();
174     }
175 
176     /*-----------------------------------------------------------------------*/
177     /*--------------------------- Statics methods ---------------------------*/
178     /*-----------------------------------------------------------------------*/
179 
180     public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
181         return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
182     }
183 
184     /*-----------------------------------------------------------------------*/
185     /*---------------------- JAXXObject implementation ----------------------*/
186     /*-----------------------------------------------------------------------*/
187 
188     @Override
189     public void applyDataBinding(String $binding) {
190         if (allComponentsCreated && $bindings.containsKey($binding)) {
191             getDataBinding($binding).applyDataBinding();
192         }
193         processDataBinding($binding);
194     }
195 
196     @Override
197     public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
198         super.firePropertyChange(propertyName, oldValue, newValue);
199     }
200 
201     @Override
202     public Map<String, Object> get$objectMap() {
203         return $objectMap;
204     }
205 
206     @Override
207     public JAXXBinding getDataBinding(String bindingId) {
208         return $bindings.get(bindingId);
209     }
210 
211     @Override
212     public JAXXBinding[] getDataBindings() {
213         return $bindings.values().toArray(new JAXXBinding[$bindings.size()]);
214     }
215 
216     @Override
217     public Object getObjectById(String id) {
218         return $objectMap.get(id);
219     }
220 
221     @Override
222     public void processDataBinding(String $binding, boolean $force) {
223         if (!$force && $activeBindings.contains($binding)) { 
224             return;
225         }
226         $activeBindings.add($binding);
227         try {
228             if (allComponentsCreated && $bindings.containsKey($binding)) {
229                 getDataBinding($binding).processDataBinding();
230             }
231         } finally {
232             $activeBindings.remove($binding);
233         }
234     }
235 
236     @Override
237     public void processDataBinding(String $binding) {
238         processDataBinding($binding, false);
239     }
240 
241     @Override
242     public void registerDataBinding(JAXXBinding binding) {
243         $bindings.put(binding.getId(), binding);
244     }
245 
246     @Override
247     public void removeDataBinding(String $binding) {
248         if (allComponentsCreated && $bindings.containsKey($binding)) {
249             getDataBinding($binding).removeDataBinding();
250         }
251     }
252 
253     /*-----------------------------------------------------------------------*/
254     /*--------------------- JAXXContext implementation  ---------------------*/
255     /*-----------------------------------------------------------------------*/
256 
257     @Override
258     public <T> T getContextValue(Class<T> clazz) {
259         return delegateContext.getContextValue(clazz, null);
260     }
261 
262     @Override
263     public <T> T getContextValue(Class<T> clazz, String name) {
264         return delegateContext.getContextValue(clazz, name);
265     }
266 
267     @Override
268     public JAXXContext getDelegateContext() {
269         return delegateContext;
270     }
271 
272     @Override
273     public <O extends Container> O getParentContainer(Class<O> clazz) {
274         return SwingUtil.getParentContainer(this, clazz);
275     }
276 
277     @Override
278     public <O extends Container> O getParentContainer(Object source, Class<O> clazz) {
279         return SwingUtil.getParentContainer(source, clazz);
280     }
281 
282     @Override
283     public <T> void removeContextValue(Class<T> clazz) {
284         delegateContext.removeContextValue(clazz, null);
285     }
286 
287     @Override
288     public <T> void removeContextValue(Class<T> clazz, String name) {
289         delegateContext.removeContextValue(clazz, name);
290     }
291 
292     @Override
293     public <T> void setContextValue(T o) {
294         delegateContext.setContextValue(o, null);
295     }
296 
297     @Override
298     public <T> void setContextValue(T o, String name) {
299         delegateContext.setContextValue(o, name);
300     }
301 
302     /*-----------------------------------------------------------------------*/
303     /*-------------------- JAXXValidator implementation  --------------------*/
304     /*-----------------------------------------------------------------------*/
305 
306     @Override
307     public SwingValidator<?> getValidator(String validatorId) {
308         return (SwingValidator<?>) (validatorIds.contains(validatorId) ? getObjectById(validatorId) : null);
309     }
310 
311     @Override
312     public List<String> getValidatorIds() {
313         return validatorIds;
314     }
315 
316     @Override
317     public void registerValidatorFields() {
318         SwingValidatorUtil.detectValidatorFields(this);
319     }
320 
321     /*-----------------------------------------------------------------------*/
322     /*----------------------- Public acessor methods  -----------------------*/
323     /*-----------------------------------------------------------------------*/
324 
325     public ReefDbHelpBroker getBroker() {
326         return broker;
327     }
328 
329     public JPanel getButtonsPanel() {
330         return buttonsPanel;
331     }
332 
333     public JButton getCloseButton() {
334         return closeButton;
335     }
336 
337     public FilterElementUI getFilterElementUI() {
338         return filterElementUI;
339     }
340 
341     public FilterListUI getFilterListUI() {
342         return filterListUI;
343     }
344 
345     public Integer getFilterTypeId() {
346         return filterTypeId;
347     }
348 
349     public FilterUIHandler getHandler() {
350         return handler;
351     }
352 
353     public FilterUIModel getModel() {
354         return model;
355     }
356 
357     public JButton getSaveButton() {
358         return saveButton;
359     }
360 
361     public JScrollPane getScrollPane() {
362         return scrollPane;
363     }
364 
365     public SwingValidator<FilterUIModel> getValidator() {
366         return validator;
367     }
368 
369     /*-----------------------------------------------------------------------*/
370     /*---------------------------- Other methods ----------------------------*/
371     /*-----------------------------------------------------------------------*/
372 
373     @Override
374     public void registerHelpId(ReefDbHelpBroker broker, Component component, String helpId) {
375         broker.installUI(component, helpId);
376     }
377 
378     @Override
379     public void showHelp(String helpId) {
380         getBroker().showHelp(this, helpId);
381     }
382 
383     /*-----------------------------------------------------------------------*/
384     /*--------------------- Protected acessors methods  ---------------------*/
385     /*-----------------------------------------------------------------------*/
386 
387     protected JPanel get$JPanel0() {
388         return $JPanel0;
389     }
390 
391     /*-----------------------------------------------------------------------*/
392     /*--------------------- Components creation methods ---------------------*/
393     /*-----------------------------------------------------------------------*/
394 
395     protected void addChildrenToButtonsPanel() {
396         if (!allComponentsCreated) {
397             return;
398         }
399         buttonsPanel.add(closeButton);
400         buttonsPanel.add(saveButton);
401     }
402 
403     protected void addChildrenToFilterUI() {
404         if (!allComponentsCreated) {
405             return;
406         }
407         add($JPanel0);
408         add(buttonsPanel, BorderLayout.PAGE_END);
409     }
410 
411     protected void addChildrenToScrollPane() {
412         if (!allComponentsCreated) {
413             return;
414         }
415         scrollPane.getViewport().add(SwingUtil.boxComponentWithJxLayer(filterElementUI));
416     }
417 
418     protected void addChildrenToValidator() {
419         if (!allComponentsCreated) {
420             return;
421         }
422         validator.setUiClass(ImageValidationUI.class);
423         validator.setBean(model);
424     }
425 
426     protected void createBroker() {
427         $objectMap.put("broker", broker = new ReefDbHelpBroker("reefdb.home.help"));
428     }
429 
430     protected void createButtonsPanel() {
431         $objectMap.put("buttonsPanel", buttonsPanel = new JPanel());
432         
433         buttonsPanel.setName("buttonsPanel");
434         buttonsPanel.setLayout(new GridLayout(1,0));
435     }
436 
437     protected void createCloseButton() {
438         $objectMap.put("closeButton", closeButton = new JButton());
439         
440         closeButton.setName("closeButton");
441         closeButton.setText(t("reefdb.common.close"));
442         closeButton.putClientProperty("applicationActionKey", fr.ifremer.reefdb.ui.swing.util.ReefDbKeyStrokes.CANCEL);
443         closeButton.putClientProperty("applicationAction", CloseAction.class);
444     }
445 
446     protected void createFilterElementUI() {
447         $objectMap.put("filterElementUI", filterElementUI = null);
448         
449         filterElementUI.setName("filterElementUI");
450         filterElementUI.putClientProperty("validatorLabel", t("reefdb.filter.selected.label"));
451     }
452 
453     protected void createFilterListUI() {
454         $objectMap.put("filterListUI", filterListUI = new FilterListUI(this));
455         
456         filterListUI.setName("filterListUI");
457         filterListUI.putClientProperty("validatorLabel", t("reefdb.filter.filterList.title"));
458     }
459 
460     protected void createFilterTypeId() {
461         $objectMap.put("filterTypeId", filterTypeId = null);
462     }
463 
464     protected FilterUIHandler createHandler() {
465         return new FilterUIHandler();
466     }
467 
468     protected void createModel() {
469         $objectMap.put("model", model = getContextValue(FilterUIModel.class));
470     }
471 
472     protected void createSaveButton() {
473         $objectMap.put("saveButton", saveButton = new JButton());
474         
475         saveButton.setName("saveButton");
476         saveButton.setText(t("reefdb.common.save"));
477         saveButton.setToolTipText(t("reefdb.action.save.filter"));
478         saveButton.putClientProperty("applicationActionKey", fr.ifremer.reefdb.ui.swing.util.ReefDbKeyStrokes.SAVE);
479         saveButton.putClientProperty("applicationAction", SaveAction.class);
480     }
481 
482     protected void createScrollPane() {
483         $objectMap.put("scrollPane", scrollPane = new JScrollPane());
484         
485         scrollPane.setName("scrollPane");
486         scrollPane.putClientProperty("onlyVerticalScrollable", true);
487     }
488 
489     protected void createValidator() {
490         $objectMap.put("validator", validator = SwingValidator.newValidator(FilterUIModel.class, null));
491     }
492 
493     /*-----------------------------------------------------------------------*/
494     /*------------------------ Internal jaxx methods ------------------------*/
495     /*-----------------------------------------------------------------------*/
496 
497     private void $completeSetup() {
498         allComponentsCreated = true;
499         if (log.isDebugEnabled()) {
500             log.debug(this);
501         }
502         addChildrenToFilterUI();
503         addChildrenToValidator();
504         // inline complete setup of $JPanel0
505         $JPanel0.add(SwingUtil.boxComponentWithJxLayer(filterListUI), BorderLayout.PAGE_START);
506         $JPanel0.add(scrollPane);
507         addChildrenToScrollPane();
508         addChildrenToButtonsPanel();
509         
510         // apply 1 data bindings
511         JAXXUtil.applyDataBinding(this, $bindings.keySet());
512         
513         // apply 8 property setters
514         scrollPane.setBorder(null);
515         closeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
516         saveButton.setAlignmentX(Component.CENTER_ALIGNMENT);
517         closeButton.setIcon(SwingUtil.createActionIcon("cancel"));
518         saveButton.setIcon(SwingUtil.createActionIcon("save"));
519         // late initializer
520         // register 1 validator(s)
521         validatorIds = SwingValidatorUtil.detectValidators(this);
522         SwingValidatorUtil.installUI(this);
523     }
524 
525     private void $initialize() {
526         if (allComponentsCreated) {
527             return;
528         }
529         if (log.isDebugEnabled()) {
530             log.debug(this);
531         }
532         handler.beforeInit(this);
533         $objectMap.put("filterUI", filterUI);
534         createModel();
535         createBroker();
536         createValidator();
537         createFilterTypeId();
538         // inline creation of $JPanel0
539         $objectMap.put("$JPanel0", $JPanel0 = new JPanel());
540         
541         $JPanel0.setName("$JPanel0");
542         $JPanel0.setLayout(new BorderLayout());
543         createFilterListUI();
544         createScrollPane();
545         createFilterElementUI();
546         createButtonsPanel();
547         createCloseButton();
548         createSaveButton();
549         // inline creation of filterUI
550         setName("filterUI");
551         setLayout(new BorderLayout());
552         
553         // registers 1 data bindings
554         $registerDefaultBindings();
555         $completeSetup();
556         handler.afterInit(this);
557     }
558 
559     private void $registerDefaultBindings() {
560         // register 1 data bindings
561         registerDataBinding(new DefaultJAXXBinding(this, BINDING_SAVE_BUTTON_ENABLED, true, true) {
562         
563             @Override
564             public void applyDataBinding() {
565                 if (model != null) {
566                     model.addPropertyChangeListener("modify", this);
567                 }
568                 if (model != null) {
569                     model.addPropertyChangeListener("valid", this);
570                 }
571             }
572         
573             @Override
574             public void processDataBinding() {
575                 if (model != null) {
576                     saveButton.setEnabled(model.isModify() && model.isValid());
577                 }
578             }
579         
580             @Override
581             public void removeDataBinding() {
582                 if (model != null) {
583                     model.removePropertyChangeListener("modify", this);
584                 }
585                 if (model != null) {
586                     model.removePropertyChangeListener("valid", this);
587                 }
588             }
589         });
590     }
591 
592 }