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