View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.synchro.log;
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.swing.ApplicationUI;
26  import fr.ifremer.quadrige3.ui.swing.ApplicationUIContext;
27  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
28  import java.awt.BorderLayout;
29  import java.awt.Color;
30  import java.awt.Component;
31  import java.awt.Container;
32  import java.awt.GridLayout;
33  import java.awt.LayoutManager;
34  import java.awt.event.ActionEvent;
35  import java.awt.event.ActionListener;
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.TreeMap;
41  import javax.swing.BorderFactory;
42  import javax.swing.BoxLayout;
43  import javax.swing.JButton;
44  import javax.swing.JLabel;
45  import javax.swing.JPanel;
46  import javax.swing.JScrollPane;
47  import javax.swing.JTextPane;
48  import jaxx.runtime.JAXXBinding;
49  import jaxx.runtime.JAXXContext;
50  import jaxx.runtime.JAXXObject;
51  import jaxx.runtime.JAXXObjectDescriptor;
52  import jaxx.runtime.JAXXUtil;
53  import jaxx.runtime.SwingUtil;
54  import jaxx.runtime.binding.DefaultJAXXBinding;
55  import jaxx.runtime.context.DefaultJAXXContext;
56  import org.apache.commons.logging.Log;
57  import org.apache.commons.logging.LogFactory;
58  import static org.nuiton.i18n.I18n.t;
59  
60  public class SynchroLogUI extends JPanel implements ApplicationUI<SynchroLogUIModel, SynchroLogUIHandler>, JAXXObject {
61  
62      /*-----------------------------------------------------------------------*/
63      /*------------------ Constants for all public bindings ------------------*/
64      /*-----------------------------------------------------------------------*/
65  
66      public static final String BINDING_LOG_TEXT_PANE_TEXT = "logTextPane.text";
67      public static final String BINDING_OPEN_BUTTON_ENABLED = "openButton.enabled";
68  
69      /*-----------------------------------------------------------------------*/
70      /*------------------------- Other static fields -------------------------*/
71      /*-----------------------------------------------------------------------*/
72  
73      private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAKVWT28bRRSfGNuJ7bRpGyUtaqAOrVpHbdeNQAiRAk1NQpI6KcKNiOoDjHcn9rSzM5uZWbLGKuIj8BHgzgWJGyfEgTMHLoivgBAHrog3u/auHbux1UbWeD3vvd/7vb+b7/9CGSXR8hMcBJb0uaYusXbWDw4eNp4QW39IlC2pp4VE0d9UCqXqqODE90qjq/WqMS93zcsV4XqCE95nvVZFeaXbjKgWIVqj1wYtbKXKtVi8Fni+7KHGpEahfvvP36lvnK+/SyEUeMAuB6EUx1klkaSrKEUdjS6Apy9wmWHeBBqS8ibwPWPuKgwrtYddcoS+QtNVlPWwBDCN3pg85BAjtA88jWau7XyMOWF3NHr7UFr0UBKXSOvIx46kTfKm5VNLHQMFS7W53ZLCYqJp1aLnqmjub3teiJbVKOMKhzCN1l4MaNdYJ2hzKpaFDCEvJgVBFyWibXRzsckMQD6impGTulXciHTnYt0z13ZqthSMGRyI/uKAQSIatCoYDyTQRqLRwoBN796oLiScugleNb/PD6LlhUf4fV9rwTWaHwCLbo3e5Vj9LDivMKFIIrzS1ZDo8kCfQQdbSQcnLTZVRxnpw7VGl+rDTf8JiKJ2v3Si3Q1gKP1vcf73n/78cbPX4wXwvTBStW9Eofc8CcFKTY3ruajBfU1ZeRd7a3WYFMJgvsP5XRpBrNYVAznwd96YW8bc2sKqBRCZ6T9+/mXx899eQalNlGcCO5vY6G+jnG5JyIJgTuB9cC9kNHs8A+c5w02jbENIB4ihbIf7jD0LIPilEcHHDBq5X/+dr/1wr5eAKSD06nPVkyRkHqMs5YxyEo55d4JHjnXBU8R3RDKpo2YXwfQW+hq1Ozk3wvPmUKjZvlBvd+6HT1GO2pYtCdZkw/V0OxKU3rpV7H5WnoGh8PCRD32UOcRMwfcMcajGDdZ3lW9g+2lTCp/DBrvY4eS4WBFMyNKdW8Xux0ClNUyJRu921j2PURtrKvj+9r4pZZPoLe2yaOOV4Fe4EEorRrBLlMJNUloxGDnjXcgHFICudFqYOww2DWjtkeNPJfY2euLSCpTTpKMc1sk8raLBIfbGpC2j0SxdfYfvcuIKTm3wmKw0W7hwadlmJi031rjwGU6CW7fNCUuxc/pWxKGe9ZF4JLaESyIzgIaCQ8j5SLxtG6jZ0OFth2IIBLbQsLsHpK3R3TEe+yoA+pB28ZQoq7K+V9moJpVaHB3uqLye2E/jUpseSu1q4qv/JWG25EagieSYRbXtS/ZAZtKmM2LqpUnhNJom3LSzY+oUvsVMO8GbZ5MyaLri8ntFsxyK168Xh6UWCajS6jnN1rfjxyXEbCOG2wKU0dm7Dta42KDcgVq9P4Q8Zc78WERzbZvDGY1w7qURliaIKo8ZbXIX/h85OAXp9Ym4HJ2CsDwBlzS1o1fnS7H4cgyL/wGxXFodyAoAAA==";
74      private static final Log log = LogFactory.getLog(SynchroLogUI.class);
75      private static final long serialVersionUID = 1L;
76  
77      /*-----------------------------------------------------------------------*/
78      /*--------------------------- Internal states ---------------------------*/
79      /*-----------------------------------------------------------------------*/
80  
81      protected List<Object> $activeBindings = new ArrayList<Object>();
82      protected Map<String, Object> $bindingSources = new HashMap<String, Object>();
83      protected final Map<String, JAXXBinding> $bindings = new TreeMap<String, JAXXBinding>();
84      protected Map<String, Object> $objectMap = new HashMap<String, Object>();
85      protected Map<?, ?> $previousValues = new HashMap<Object, Object>();
86      private boolean allComponentsCreated;
87      protected final JAXXContext delegateContext = new DefaultJAXXContext();
88  
89      /*-----------------------------------------------------------------------*/
90      /*------------------------ Protected components  ------------------------*/
91      /*-----------------------------------------------------------------------*/
92  
93      protected final SynchroLogUIHandler handler = createHandler();
94      protected JButton logCloseButton;
95      protected JTextPane logTextPane;
96      protected JLabel logTitle;
97      protected SynchroLogUIModel model;
98      protected JButton openButton;
99      protected JPanel synchroLogPanel;
100 
101     /*-----------------------------------------------------------------------*/
102     /*------------------------- Private components  -------------------------*/
103     /*-----------------------------------------------------------------------*/
104 
105     private SynchroLogUI $JPanel0 = this;
106     private JPanel $JPanel1;
107     private JScrollPane $JScrollPane0;
108 
109     /*-----------------------------------------------------------------------*/
110     /*---------------------------- Constructors  ----------------------------*/
111     /*-----------------------------------------------------------------------*/
112 
113     public SynchroLogUI(ApplicationUI parentUI) {
114         ApplicationUIUtil.setParentUI(this, parentUI);
115             $initialize();
116     }
117 
118     public SynchroLogUI() {
119         $initialize();
120     }
121 
122     public SynchroLogUI(JAXXContext parentContext) {
123         JAXXUtil.initContext(this, parentContext);
124         $initialize();
125     }
126 
127     public SynchroLogUI(boolean param0) {
128         super(param0);
129         $initialize();
130     }
131 
132     public SynchroLogUI(JAXXContext parentContext, boolean param1) {
133         super(param1);
134         JAXXUtil.initContext(this, parentContext);
135         $initialize();
136     }
137 
138     public SynchroLogUI(LayoutManager param0) {
139         super(param0);
140         $initialize();
141     }
142 
143     public SynchroLogUI(JAXXContext parentContext, LayoutManager param1) {
144         super(param1);
145         JAXXUtil.initContext(this, parentContext);
146         $initialize();
147     }
148 
149     public SynchroLogUI(LayoutManager param0, boolean param1) {
150         super(param0 ,param1);
151         $initialize();
152     }
153 
154     public SynchroLogUI(JAXXContext parentContext, LayoutManager param1, boolean param2) {
155         super(param1 ,param2);
156         JAXXUtil.initContext(this, parentContext);
157         $initialize();
158     }
159 
160     /*-----------------------------------------------------------------------*/
161     /*--------------------------- Statics methods ---------------------------*/
162     /*-----------------------------------------------------------------------*/
163 
164     public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
165         return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
166     }
167 
168     /*-----------------------------------------------------------------------*/
169     /*---------------------- JAXXObject implementation ----------------------*/
170     /*-----------------------------------------------------------------------*/
171 
172     @Override
173     public void applyDataBinding(String $binding) {
174         if (allComponentsCreated && $bindings.containsKey($binding)) {
175             getDataBinding($binding).applyDataBinding();
176         }
177         processDataBinding($binding);
178     }
179 
180     @Override
181     public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
182         super.firePropertyChange(propertyName, oldValue, newValue);
183     }
184 
185     @Override
186     public Map<String, Object> get$objectMap() {
187         return $objectMap;
188     }
189 
190     @Override
191     public JAXXBinding getDataBinding(String bindingId) {
192         return $bindings.get(bindingId);
193     }
194 
195     @Override
196     public JAXXBinding[] getDataBindings() {
197         return $bindings.values().toArray(new JAXXBinding[$bindings.size()]);
198     }
199 
200     @Override
201     public Object getObjectById(String id) {
202         return $objectMap.get(id);
203     }
204 
205     @Override
206     public void processDataBinding(String $binding, boolean $force) {
207         if (!$force && $activeBindings.contains($binding)) { 
208             return;
209         }
210         $activeBindings.add($binding);
211         try {
212             if (allComponentsCreated && $bindings.containsKey($binding)) {
213                 getDataBinding($binding).processDataBinding();
214             }
215         } finally {
216             $activeBindings.remove($binding);
217         }
218     }
219 
220     @Override
221     public void processDataBinding(String $binding) {
222         processDataBinding($binding, false);
223     }
224 
225     @Override
226     public void registerDataBinding(JAXXBinding binding) {
227         $bindings.put(binding.getId(), binding);
228     }
229 
230     @Override
231     public void removeDataBinding(String $binding) {
232         if (allComponentsCreated && $bindings.containsKey($binding)) {
233             getDataBinding($binding).removeDataBinding();
234         }
235     }
236 
237     /*-----------------------------------------------------------------------*/
238     /*--------------------- JAXXContext implementation  ---------------------*/
239     /*-----------------------------------------------------------------------*/
240 
241     @Override
242     public <T> T getContextValue(Class<T> clazz) {
243         return delegateContext.getContextValue(clazz, null);
244     }
245 
246     @Override
247     public <T> T getContextValue(Class<T> clazz, String name) {
248         return delegateContext.getContextValue(clazz, name);
249     }
250 
251     @Override
252     public JAXXContext getDelegateContext() {
253         return delegateContext;
254     }
255 
256     @Override
257     public <O extends Container> O getParentContainer(Class<O> clazz) {
258         return SwingUtil.getParentContainer(this, clazz);
259     }
260 
261     @Override
262     public <O extends Container> O getParentContainer(Object source, Class<O> clazz) {
263         return SwingUtil.getParentContainer(source, clazz);
264     }
265 
266     @Override
267     public <T> void removeContextValue(Class<T> clazz) {
268         delegateContext.removeContextValue(clazz, null);
269     }
270 
271     @Override
272     public <T> void removeContextValue(Class<T> clazz, String name) {
273         delegateContext.removeContextValue(clazz, name);
274     }
275 
276     @Override
277     public <T> void setContextValue(T o) {
278         delegateContext.setContextValue(o, null);
279     }
280 
281     @Override
282     public <T> void setContextValue(T o, String name) {
283         delegateContext.setContextValue(o, name);
284     }
285 
286     /*-----------------------------------------------------------------------*/
287     /*---------------------------- Event methods ----------------------------*/
288     /*-----------------------------------------------------------------------*/
289 
290     public void doActionPerformed__on__openButton(ActionEvent event) {
291         if (log.isDebugEnabled()) {
292             log.debug(event);
293         }
294         handler.openExternalEditor();
295     }
296 
297     /*-----------------------------------------------------------------------*/
298     /*----------------------- Public acessor methods  -----------------------*/
299     /*-----------------------------------------------------------------------*/
300 
301     public SynchroLogUIHandler getHandler() {
302         return handler;
303     }
304 
305     public JButton getLogCloseButton() {
306         return logCloseButton;
307     }
308 
309     public JTextPane getLogTextPane() {
310         return logTextPane;
311     }
312 
313     public JLabel getLogTitle() {
314         return logTitle;
315     }
316 
317     public SynchroLogUIModel getModel() {
318         return model;
319     }
320 
321     public JButton getOpenButton() {
322         return openButton;
323     }
324 
325     public JPanel getSynchroLogPanel() {
326         return synchroLogPanel;
327     }
328 
329     /*-----------------------------------------------------------------------*/
330     /*--------------------- Protected acessors methods  ---------------------*/
331     /*-----------------------------------------------------------------------*/
332 
333     protected JPanel get$JPanel1() {
334         return $JPanel1;
335     }
336 
337     protected JScrollPane get$JScrollPane0() {
338         return $JScrollPane0;
339     }
340 
341     /*-----------------------------------------------------------------------*/
342     /*--------------------- Components creation methods ---------------------*/
343     /*-----------------------------------------------------------------------*/
344 
345     protected void addChildrenToSynchroLogPanel() {
346         if (!allComponentsCreated) {
347             return;
348         }
349         synchroLogPanel.add(logTitle, BorderLayout.NORTH);
350         synchroLogPanel.add($JScrollPane0, BorderLayout.CENTER);
351         synchroLogPanel.add($JPanel1, BorderLayout.SOUTH);
352     }
353 
354     protected SynchroLogUIHandler createHandler() {
355         return new SynchroLogUIHandler();
356     }
357 
358     protected void createLogCloseButton() {
359         $objectMap.put("logCloseButton", logCloseButton = new JButton());
360         
361         logCloseButton.setName("logCloseButton");
362         logCloseButton.setText(t("quadrige3.common.close"));
363         logCloseButton.putClientProperty("applicationActionKey", fr.ifremer.quadrige3.ui.swing.ApplicationKeyStrokes.CANCEL);
364         logCloseButton.putClientProperty("applicationAction", fr.ifremer.quadrige3.ui.swing.action.GoToHomeAction.class);
365     }
366 
367     protected void createLogTextPane() {
368         $objectMap.put("logTextPane", logTextPane = new JTextPane());
369         
370         logTextPane.setName("logTextPane");
371         logTextPane.setOpaque(false);
372         logTextPane.setEditable(false);
373     }
374 
375     protected void createLogTitle() {
376         $objectMap.put("logTitle", logTitle = new JLabel());
377         
378         logTitle.setName("logTitle");
379     }
380 
381     protected void createModel() {
382         $objectMap.put("model", model = getContextValue(SynchroLogUIModel.class));
383     }
384 
385     protected void createOpenButton() {
386         $objectMap.put("openButton", openButton = new JButton());
387         
388         openButton.setName("openButton");
389         openButton.setText(t("quadrige3.synchro.log.openExternalEditor"));
390         openButton.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__openButton"));
391     }
392 
393     protected void createSynchroLogPanel() {
394         $objectMap.put("synchroLogPanel", synchroLogPanel = new JPanel());
395         
396         synchroLogPanel.setName("synchroLogPanel");
397         synchroLogPanel.setLayout(new BorderLayout());
398     }
399 
400     /*-----------------------------------------------------------------------*/
401     /*------------------------ Internal jaxx methods ------------------------*/
402     /*-----------------------------------------------------------------------*/
403 
404     private void $completeSetup() {
405         allComponentsCreated = true;
406         if (log.isDebugEnabled()) {
407             log.debug(this);
408         }
409         // inline complete setup of $JPanel0
410         add(synchroLogPanel);
411         addChildrenToSynchroLogPanel();
412         // inline complete setup of $JScrollPane0
413         $JScrollPane0.getViewport().add(logTextPane);
414         // inline complete setup of $JPanel1
415         $JPanel1.add(openButton);
416         $JPanel1.add(logCloseButton);
417         
418         // apply 2 data bindings
419         JAXXUtil.applyDataBinding(this, $bindings.keySet());
420         
421         // apply 13 property setters
422         openButton.setAlignmentX(Component.CENTER_ALIGNMENT);
423         logCloseButton.setAlignmentX(Component.CENTER_ALIGNMENT);
424         $JScrollPane0.setBorder(null);
425         logTextPane.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
426         logTextPane.setBackground(new Color(0, 0, 0, 0));
427         logTextPane.setEditorKit(handler.getNewWrapEditorKit());
428         openButton.setMnemonic(SwingUtil.getFirstCharAt(t("quadrige3.synchro.log.openExternalEditor.mnemonic"),'Z'));
429         openButton.setIcon(SwingUtil.createActionIcon("edit"));
430         logCloseButton.setMnemonic(SwingUtil.getFirstCharAt(t("quadrige3.common.close.mnemonic"),'Z'));
431         logCloseButton.setIcon(SwingUtil.createActionIcon("close-dialog"));
432     }
433 
434     private void $initialize() {
435         if (allComponentsCreated) {
436             return;
437         }
438         if (log.isDebugEnabled()) {
439             log.debug(this);
440         }
441         handler.beforeInit(this);
442         $objectMap.put("$JPanel0", $JPanel0);
443         createModel();
444         createSynchroLogPanel();
445         createLogTitle();
446         // inline creation of $JScrollPane0
447         $objectMap.put("$JScrollPane0", $JScrollPane0 = new JScrollPane());
448         
449         $JScrollPane0.setName("$JScrollPane0");
450         createLogTextPane();
451         // inline creation of $JPanel1
452         $objectMap.put("$JPanel1", $JPanel1 = new JPanel());
453         
454         $JPanel1.setName("$JPanel1");
455         $JPanel1.setLayout(new GridLayout(1,0));
456         createOpenButton();
457         createLogCloseButton();
458         // inline creation of $JPanel0
459         setName("$JPanel0");
460         setLayout(new BorderLayout());
461         
462         // registers 2 data bindings
463         $registerDefaultBindings();
464         $completeSetup();
465         handler.afterInit(this);
466     }
467 
468     private void $registerDefaultBindings() {
469         // register 2 data bindings
470         registerDataBinding(new DefaultJAXXBinding(this, BINDING_LOG_TEXT_PANE_TEXT, true) {
471         
472             @Override
473             public void applyDataBinding() {
474                 if (getModel() != null) {
475                     getModel().addPropertyChangeListener("message", this);
476                 }
477             }
478         
479             @Override
480             public void processDataBinding() {
481                 if (getModel() != null) {
482                     SwingUtil.setText(logTextPane, ApplicationUIUtil.getHtmlString(getModel().getMessage()));
483                 }
484             }
485         
486             @Override
487             public void removeDataBinding() {
488                 if (getModel() != null) {
489                     getModel().removePropertyChangeListener("message", this);
490                 }
491             }
492         });
493         registerDataBinding(new DefaultJAXXBinding(this, BINDING_OPEN_BUTTON_ENABLED, true) {
494         
495             @Override
496             public void applyDataBinding() {
497                 if (model != null) {
498                     model.addPropertyChangeListener("logFile", this);
499                 }
500             }
501         
502             @Override
503             public void processDataBinding() {
504                 if (model != null && model.getLogFile() != null) {
505                     openButton.setEnabled(model.getLogFile() != null && model.getLogFile().exists());
506                 }
507             }
508         
509             @Override
510             public void removeDataBinding() {
511                 if (model != null) {
512                     model.removePropertyChangeListener("logFile", this);
513                 }
514             }
515         });
516     }
517 
518 }