View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.table.comment;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: UI Swing Common
6    * %%
7    * Copyright (C) 2017 - 2024 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  
25  import fr.ifremer.quadrige3.ui.core.dto.CommentAware;
26  import java.awt.BorderLayout;
27  import java.awt.Container;
28  import java.awt.Dialog;
29  import java.awt.Dialog.ModalityType;
30  import java.awt.Frame;
31  import java.awt.GraphicsConfiguration;
32  import java.awt.Window;
33  import java.awt.event.KeyEvent;
34  import java.awt.event.KeyListener;
35  import java.util.ArrayList;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.TreeMap;
40  import javax.swing.JComponent;
41  import javax.swing.JDialog;
42  import javax.swing.JScrollPane;
43  import javax.swing.JTextArea;
44  import jaxx.runtime.JAXXBinding;
45  import jaxx.runtime.JAXXContext;
46  import jaxx.runtime.JAXXObject;
47  import jaxx.runtime.JAXXObjectDescriptor;
48  import jaxx.runtime.JAXXUtil;
49  import jaxx.runtime.SwingUtil;
50  import jaxx.runtime.binding.SimpleJAXXObjectBinding;
51  import jaxx.runtime.context.DefaultJAXXContext;
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  import org.jdesktop.swingx.JXTitledPanel;
55  import static org.nuiton.i18n.I18n.t;
56  
57  public class CommentEditorUI extends JDialog implements JAXXObject {
58  
59      /*-----------------------------------------------------------------------*/
60      /*---------------- Constants for all javaBean properties ----------------*/
61      /*-----------------------------------------------------------------------*/
62  
63      public static final String PROPERTY_BEAN = "bean";
64      public static final String PROPERTY_PROPERTY = "property";
65      public static final String PROPERTY_TITLE_I18N = "titleI18n";
66  
67      /*-----------------------------------------------------------------------*/
68      /*------------------ Constants for all public bindings ------------------*/
69      /*-----------------------------------------------------------------------*/
70  
71      public static final String BINDING_COMMENT_EDITOR_TOP_PANEL_TITLE = "commentEditorTopPanel.title";
72  
73      /*-----------------------------------------------------------------------*/
74      /*------------------------- Other static fields -------------------------*/
75      /*-----------------------------------------------------------------------*/
76  
77      private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAI1TTWtTQRS9SZu0aWxtrf0QK7QVREEniBshoi3VYku0YiIUs3GSN01fnbyZzrvPvCCKP8GfoHs3gjtX4sK1CzfiXxBx4Va88/KS148UG8gkzL3n3I85591PyPgGFnZ4GDITeOg2BVtf3tzcqO2IOt4Wft24GpWBzieVhnQV8k7v3kc4Xy1ZeCGGF1ZUUytPeHvQxRKM+NiWwt8WAhHO7UfUfb9Q7oWLoQ5Ml7XXVD/WN79/pV87r96mAUJN3Q3RKPP/QyWTDJYg7ToIp6jSM16Q3GtQG8b1GtTvqL1bkdz37/Om2IWXMFSCrOaGyBAWjz9yxBHhQ40wWlfNpk10uVQNhOKWYe6WEU1h2G7AHeM2xDUWuMxvUR8MeU0KFmPsNPb3juNSgUdrWke8WYTBmuAewpWjyOrKCOag6jIst2iOBD6sjdLCYBth3I7N7CpYZxVJVg5dlGLt6nXPXkwkgan63r4qSj/gnpAIC8o02A5p5Skq3RkoZOubFUvjRDmWINfjGUMRYrlulJQ2ijBjmwnjVawnEZt/ugfLW9iK8jB6mal9mAqFlo3gNnVGkzzO7pMHCY8lwkuUkapCxgR0jTBbPazVhxTqqHT2gEotYRT9Oz357eOPD6tdaQ5Q7am+qXucRZKJX8K1pU92dBmgKwv3uC5WIecLSbaMbDfXp7FyHKbmqN5E9JYWzu5yf5soMkPfP32efvJ1ANKrMCIVd1a5zV+jx902tAUlnVDfWoo6OtEapnOcvmlacuA5gmTEUZBjBtEEtK88ly3e9jc8evP4MqSlzPVZSq+zWu7Ln8ny+6XuYlLU6Jkj05PlZB5D1vWk64nItbEh+7o0r30ROCoxXj8rwkEv6ljQi9F54dASUgiZyAFkkucNgZWuGy5eehEGFnM5Gsf+Y3CUK45RJSt5WwUk5bEbDkc+X3M9h8R881CRlD1HiPEfVLcrRMYFAAA=";
78      private static final Log log = LogFactory.getLog(CommentEditorUI.class);
79      private static final long serialVersionUID = 1L;
80  
81      /*-----------------------------------------------------------------------*/
82      /*--------------------------- Internal states ---------------------------*/
83      /*-----------------------------------------------------------------------*/
84  
85      protected List<Object> $activeBindings = new ArrayList<Object>();
86      protected Map<String, Object> $bindingSources = new HashMap<String, Object>();
87      protected final Map<String, JAXXBinding> $bindings = new TreeMap<String, JAXXBinding>();
88      protected Map<String, Object> $objectMap = new HashMap<String, Object>();
89      protected Map<?, ?> $previousValues = new HashMap<Object, Object>();
90      private boolean allComponentsCreated;
91      protected final JAXXContext delegateContext = new DefaultJAXXContext();
92  
93      /*-----------------------------------------------------------------------*/
94      /*------------------------ Protected components  ------------------------*/
95      /*-----------------------------------------------------------------------*/
96  
97      protected CommentAware bean;
98      protected CommentEditorUI commentDialog = this;
99      protected JXTitledPanel commentEditorTopPanel;
100     protected final CommentEditorUIHandler handler = createHandler();
101     protected String property;
102     protected JTextArea textContent;
103     protected JScrollPane textScrollPane;
104     protected String titleI18n;
105 
106     /*-----------------------------------------------------------------------*/
107     /*---------------------- Raw body code from script ----------------------*/
108     /*-----------------------------------------------------------------------*/
109 
110     public void openEditor(JComponent component) {
111        handler.openEditor(component);
112     }
113     
114     public void closeEditor() {
115        handler.closeEditor();
116     }
117 
118     /*-----------------------------------------------------------------------*/
119     /*---------------------------- Constructors  ----------------------------*/
120     /*-----------------------------------------------------------------------*/
121 
122     public CommentEditorUI(Window param0) {
123         super(param0);
124         $initialize();
125     }
126 
127     public CommentEditorUI(JAXXContext parentContext, Window param1) {
128         super(param1);
129         JAXXUtil.initContext(this, parentContext);
130         $initialize();
131     }
132 
133     public CommentEditorUI(Dialog param0, String param1, boolean param2, GraphicsConfiguration param3) {
134         super(param0 ,param1 ,param2 ,param3);
135         $initialize();
136     }
137 
138     public CommentEditorUI(JAXXContext parentContext, Dialog param1, String param2, boolean param3, GraphicsConfiguration param4) {
139         super(param1 ,param2 ,param3 ,param4);
140         JAXXUtil.initContext(this, parentContext);
141         $initialize();
142     }
143 
144     public CommentEditorUI(Dialog param0, String param1, boolean param2) {
145         super(param0 ,param1 ,param2);
146         $initialize();
147     }
148 
149     public CommentEditorUI(JAXXContext parentContext, Dialog param1, String param2, boolean param3) {
150         super(param1 ,param2 ,param3);
151         JAXXUtil.initContext(this, parentContext);
152         $initialize();
153     }
154 
155     public CommentEditorUI(Dialog param0, String param1) {
156         super(param0 ,param1);
157         $initialize();
158     }
159 
160     public CommentEditorUI(JAXXContext parentContext, Dialog param1, String param2) {
161         super(param1 ,param2);
162         JAXXUtil.initContext(this, parentContext);
163         $initialize();
164     }
165 
166     public CommentEditorUI(Window param0, String param1, ModalityType param2, GraphicsConfiguration param3) {
167         super(param0 ,param1 ,param2 ,param3);
168         $initialize();
169     }
170 
171     public CommentEditorUI(JAXXContext parentContext, Window param1, String param2, ModalityType param3, GraphicsConfiguration param4) {
172         super(param1 ,param2 ,param3 ,param4);
173         JAXXUtil.initContext(this, parentContext);
174         $initialize();
175     }
176 
177     public CommentEditorUI(Window param0, String param1, ModalityType param2) {
178         super(param0 ,param1 ,param2);
179         $initialize();
180     }
181 
182     public CommentEditorUI(JAXXContext parentContext, Window param1, String param2, ModalityType param3) {
183         super(param1 ,param2 ,param3);
184         JAXXUtil.initContext(this, parentContext);
185         $initialize();
186     }
187 
188     public CommentEditorUI(Window param0, String param1) {
189         super(param0 ,param1);
190         $initialize();
191     }
192 
193     public CommentEditorUI(JAXXContext parentContext, Window param1, String param2) {
194         super(param1 ,param2);
195         JAXXUtil.initContext(this, parentContext);
196         $initialize();
197     }
198 
199     public CommentEditorUI(Window param0, ModalityType param1) {
200         super(param0 ,param1);
201         $initialize();
202     }
203 
204     public CommentEditorUI(JAXXContext parentContext, Window param1, ModalityType param2) {
205         super(param1 ,param2);
206         JAXXUtil.initContext(this, parentContext);
207         $initialize();
208     }
209 
210     public CommentEditorUI(Frame param0, String param1) {
211         super(param0 ,param1);
212         $initialize();
213     }
214 
215     public CommentEditorUI(JAXXContext parentContext, Frame param1, String param2) {
216         super(param1 ,param2);
217         JAXXUtil.initContext(this, parentContext);
218         $initialize();
219     }
220 
221     public CommentEditorUI(Frame param0, boolean param1) {
222         super(param0 ,param1);
223         $initialize();
224     }
225 
226     public CommentEditorUI(JAXXContext parentContext, Frame param1, boolean param2) {
227         super(param1 ,param2);
228         JAXXUtil.initContext(this, parentContext);
229         $initialize();
230     }
231 
232     public CommentEditorUI(Frame param0) {
233         super(param0);
234         $initialize();
235     }
236 
237     public CommentEditorUI(JAXXContext parentContext, Frame param1) {
238         super(param1);
239         JAXXUtil.initContext(this, parentContext);
240         $initialize();
241     }
242 
243     public CommentEditorUI() {
244         $initialize();
245     }
246 
247     public CommentEditorUI(JAXXContext parentContext) {
248         JAXXUtil.initContext(this, parentContext);
249         $initialize();
250     }
251 
252     public CommentEditorUI(Dialog param0, boolean param1) {
253         super(param0 ,param1);
254         $initialize();
255     }
256 
257     public CommentEditorUI(JAXXContext parentContext, Dialog param1, boolean param2) {
258         super(param1 ,param2);
259         JAXXUtil.initContext(this, parentContext);
260         $initialize();
261     }
262 
263     public CommentEditorUI(Dialog param0) {
264         super(param0);
265         $initialize();
266     }
267 
268     public CommentEditorUI(JAXXContext parentContext, Dialog param1) {
269         super(param1);
270         JAXXUtil.initContext(this, parentContext);
271         $initialize();
272     }
273 
274     public CommentEditorUI(Frame param0, String param1, boolean param2, GraphicsConfiguration param3) {
275         super(param0 ,param1 ,param2 ,param3);
276         $initialize();
277     }
278 
279     public CommentEditorUI(JAXXContext parentContext, Frame param1, String param2, boolean param3, GraphicsConfiguration param4) {
280         super(param1 ,param2 ,param3 ,param4);
281         JAXXUtil.initContext(this, parentContext);
282         $initialize();
283     }
284 
285     public CommentEditorUI(Frame param0, String param1, boolean param2) {
286         super(param0 ,param1 ,param2);
287         $initialize();
288     }
289 
290     public CommentEditorUI(JAXXContext parentContext, Frame param1, String param2, boolean param3) {
291         super(param1 ,param2 ,param3);
292         JAXXUtil.initContext(this, parentContext);
293         $initialize();
294     }
295 
296     /*-----------------------------------------------------------------------*/
297     /*--------------------------- Statics methods ---------------------------*/
298     /*-----------------------------------------------------------------------*/
299 
300     public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
301         return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
302     }
303 
304     /*-----------------------------------------------------------------------*/
305     /*---------------------- JAXXObject implementation ----------------------*/
306     /*-----------------------------------------------------------------------*/
307 
308     @Override
309     public void applyDataBinding(String $binding) {
310         if (allComponentsCreated && $bindings.containsKey($binding)) {
311             getDataBinding($binding).applyDataBinding();
312         }
313         processDataBinding($binding);
314     }
315 
316     @Override
317     public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
318         super.firePropertyChange(propertyName, oldValue, newValue);
319     }
320 
321     @Override
322     public Map<String, Object> get$objectMap() {
323         return $objectMap;
324     }
325 
326     @Override
327     public JAXXBinding getDataBinding(String bindingId) {
328         return $bindings.get(bindingId);
329     }
330 
331     @Override
332     public JAXXBinding[] getDataBindings() {
333         return $bindings.values().toArray(new JAXXBinding[$bindings.size()]);
334     }
335 
336     @Override
337     public Object getObjectById(String id) {
338         return $objectMap.get(id);
339     }
340 
341     @Override
342     public void processDataBinding(String $binding, boolean $force) {
343         if (!$force && $activeBindings.contains($binding)) { 
344             return;
345         }
346         $activeBindings.add($binding);
347         try {
348             if (allComponentsCreated && $bindings.containsKey($binding)) {
349                 getDataBinding($binding).processDataBinding();
350             }
351         } finally {
352             $activeBindings.remove($binding);
353         }
354     }
355 
356     @Override
357     public void processDataBinding(String $binding) {
358         processDataBinding($binding, false);
359     }
360 
361     @Override
362     public void registerDataBinding(JAXXBinding binding) {
363         $bindings.put(binding.getId(), binding);
364     }
365 
366     @Override
367     public void removeDataBinding(String $binding) {
368         if (allComponentsCreated && $bindings.containsKey($binding)) {
369             getDataBinding($binding).removeDataBinding();
370         }
371     }
372 
373     /*-----------------------------------------------------------------------*/
374     /*--------------------- JAXXContext implementation  ---------------------*/
375     /*-----------------------------------------------------------------------*/
376 
377     @Override
378     public <T> T getContextValue(Class<T> clazz) {
379         return delegateContext.getContextValue(clazz, null);
380     }
381 
382     @Override
383     public <T> T getContextValue(Class<T> clazz, String name) {
384         return delegateContext.getContextValue(clazz, name);
385     }
386 
387     @Override
388     public JAXXContext getDelegateContext() {
389         return delegateContext;
390     }
391 
392     @Override
393     public <O extends Container> O getParentContainer(Class<O> clazz) {
394         return SwingUtil.getParentContainer(this, clazz);
395     }
396 
397     @Override
398     public <O extends Container> O getParentContainer(Object source, Class<O> clazz) {
399         return SwingUtil.getParentContainer(source, clazz);
400     }
401 
402     @Override
403     public <T> void removeContextValue(Class<T> clazz) {
404         delegateContext.removeContextValue(clazz, null);
405     }
406 
407     @Override
408     public <T> void removeContextValue(Class<T> clazz, String name) {
409         delegateContext.removeContextValue(clazz, name);
410     }
411 
412     @Override
413     public <T> void setContextValue(T o) {
414         delegateContext.setContextValue(o, null);
415     }
416 
417     @Override
418     public <T> void setContextValue(T o, String name) {
419         delegateContext.setContextValue(o, name);
420     }
421 
422     /*-----------------------------------------------------------------------*/
423     /*---------------------------- Event methods ----------------------------*/
424     /*-----------------------------------------------------------------------*/
425 
426     public void doKeyReleased__on__textContent(KeyEvent event) {
427         if (log.isDebugEnabled()) {
428             log.debug(event);
429         }
430         handler.setText(((JTextArea)event.getSource()).getText());
431     }
432 
433     /*-----------------------------------------------------------------------*/
434     /*----------------------- Public acessor methods  -----------------------*/
435     /*-----------------------------------------------------------------------*/
436 
437     public CommentAware getBean() {
438         return bean;
439     }
440 
441     public JXTitledPanel getCommentEditorTopPanel() {
442         return commentEditorTopPanel;
443     }
444 
445     public CommentEditorUIHandler getHandler() {
446         return handler;
447     }
448 
449     public String getProperty() {
450         return property;
451     }
452 
453     public JTextArea getTextContent() {
454         return textContent;
455     }
456 
457     public JScrollPane getTextScrollPane() {
458         return textScrollPane;
459     }
460 
461     public String getTitleI18n() {
462         return titleI18n;
463     }
464 
465     /*-----------------------------------------------------------------------*/
466     /*----------------------- Public mutator methods  -----------------------*/
467     /*-----------------------------------------------------------------------*/
468 
469     public void setBean(CommentAware bean) {
470         CommentAware oldValue = this.bean;
471         this.bean = bean;
472         firePropertyChange(PROPERTY_BEAN, oldValue, bean);
473     }
474 
475     public void setProperty(String property) {
476         String oldValue = this.property;
477         this.property = property;
478         firePropertyChange(PROPERTY_PROPERTY, oldValue, property);
479     }
480 
481     public void setTitleI18n(String titleI18n) {
482         String oldValue = this.titleI18n;
483         this.titleI18n = titleI18n;
484         firePropertyChange(PROPERTY_TITLE_I18N, oldValue, titleI18n);
485     }
486 
487     /*-----------------------------------------------------------------------*/
488     /*--------------------- Components creation methods ---------------------*/
489     /*-----------------------------------------------------------------------*/
490 
491     protected void addChildrenToCommentDialog() {
492         if (!allComponentsCreated) {
493             return;
494         }
495         add(commentEditorTopPanel, BorderLayout.CENTER);
496     }
497 
498     protected void addChildrenToCommentEditorTopPanel() {
499         if (!allComponentsCreated) {
500             return;
501         }
502         commentEditorTopPanel.add(textScrollPane);
503     }
504 
505     protected void addChildrenToTextScrollPane() {
506         if (!allComponentsCreated) {
507             return;
508         }
509         textScrollPane.getViewport().add(textContent);
510     }
511 
512     protected void createBean() {
513         $objectMap.put("bean", bean = null);
514     }
515 
516     protected void createCommentEditorTopPanel() {
517         $objectMap.put("commentEditorTopPanel", commentEditorTopPanel = new JXTitledPanel());
518         
519         commentEditorTopPanel.setName("commentEditorTopPanel");
520     }
521 
522     protected CommentEditorUIHandler createHandler() {
523         return new CommentEditorUIHandler();
524     }
525 
526     protected void createProperty() {
527         $objectMap.put("property", property = null);
528     }
529 
530     protected void createTextContent() {
531         $objectMap.put("textContent", textContent = new JTextArea());
532         
533         textContent.setName("textContent");
534         textContent.setColumns(15);
535         textContent.setLineWrap(true);
536         textContent.setWrapStyleWord(true);
537         textContent.addKeyListener(JAXXUtil.getEventListener(KeyListener.class, "keyReleased", this, "doKeyReleased__on__textContent"));
538     }
539 
540     protected void createTextScrollPane() {
541         $objectMap.put("textScrollPane", textScrollPane = new JScrollPane());
542         
543         textScrollPane.setName("textScrollPane");
544     }
545 
546     protected void createTitleI18n() {
547         $objectMap.put("titleI18n", titleI18n = null);
548     }
549 
550     /*-----------------------------------------------------------------------*/
551     /*------------------------ Internal jaxx methods ------------------------*/
552     /*-----------------------------------------------------------------------*/
553 
554     private void $completeSetup() {
555         allComponentsCreated = true;
556         if (log.isDebugEnabled()) {
557             log.debug(this);
558         }
559         addChildrenToCommentDialog();
560         addChildrenToCommentEditorTopPanel();
561         addChildrenToTextScrollPane();
562         
563         // apply 1 data bindings
564         JAXXUtil.applyDataBinding(this, $bindings.keySet());
565         
566         // late initializer
567         commentDialog.pack();
568     }
569 
570     private void $initialize() {
571         if (allComponentsCreated) {
572             return;
573         }
574         if (log.isDebugEnabled()) {
575             log.debug(this);
576         }
577         handler.beforeInit(this);
578         $objectMap.put("commentDialog", commentDialog);
579         createBean();
580         createProperty();
581         createTitleI18n();
582         createCommentEditorTopPanel();
583         createTextScrollPane();
584         createTextContent();
585         // inline creation of commentDialog
586         setName("commentDialog");
587         commentDialog.getContentPane().setLayout(new BorderLayout());
588         setUndecorated(true);
589         setAlwaysOnTop(true);
590         
591         // registers 1 data bindings
592         $registerDefaultBindings();
593         $completeSetup();
594         handler.afterInit(this);
595     }
596 
597     private void $registerDefaultBindings() {
598         // register 1 data bindings
599         registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_COMMENT_EDITOR_TOP_PANEL_TITLE, true ,"titleI18n") {
600         
601             @Override
602             public void processDataBinding() {
603                 commentEditorTopPanel.setTitle(t(getTitleI18n()));
604             }
605         });
606     }
607 
608 }