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