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