View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.attachment;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
28  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
29  import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
30  import java.awt.BorderLayout;
31  import java.awt.Container;
32  import java.awt.LayoutManager;
33  import java.util.ArrayList;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.TreeMap;
38  import javax.swing.JButton;
39  import javax.swing.JLabel;
40  import javax.swing.JPanel;
41  import javax.swing.JToolBar;
42  import jaxx.runtime.JAXXBinding;
43  import jaxx.runtime.JAXXContext;
44  import jaxx.runtime.JAXXObject;
45  import jaxx.runtime.JAXXObjectDescriptor;
46  import jaxx.runtime.JAXXUtil;
47  import jaxx.runtime.SwingUtil;
48  import jaxx.runtime.binding.DefaultJAXXBinding;
49  import jaxx.runtime.context.DefaultJAXXContext;
50  import org.apache.commons.logging.Log;
51  import org.apache.commons.logging.LogFactory;
52  import static jaxx.runtime.JAXXUtil.getStringValue;
53  import static org.nuiton.i18n.I18n.t;
54  
55  public class AttachmentItem extends JPanel implements TuttiUI<AttachmentItemModel, AttachmentItemHandler>, JAXXObject {
56  
57      /*-----------------------------------------------------------------------*/
58      /*------------------ Constants for all public bindings ------------------*/
59      /*-----------------------------------------------------------------------*/
60  
61      public static final String BINDING_ATTACHMENT_NAME_LABEL_TEXT = "attachmentNameLabel.text";
62      public static final String BINDING_ATTACHMENT_NAME_LABEL_TOOL_TIP_TEXT = "attachmentNameLabel.toolTipText";
63      public static final String BINDING_ATTACHMENT_PANEL_TOOL_TIP_TEXT = "attachmentPanel.toolTipText";
64  
65      /*-----------------------------------------------------------------------*/
66      /*------------------------- Other static fields -------------------------*/
67      /*-----------------------------------------------------------------------*/
68  
69      private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAK1VS28cRRAuL94160cetnESJUgJiChRlFk4IEUyjzgBC0frxIqNErEH0zvT6+1opqfpqU0mioL4CfwEuHNB4sYJceDMgQviLyDEgSuiqmd3x7MeezcOexit6vHV19VfVX/3J1QTC5ceiTT1bE+jiqR3Z+3hw3vtR9LHj2TiW2UwtpD9pipQacFcMLQnCG+2mpze6Kc3bseRibXU+7JXmzCb4NNQJl0pEeH1YoafJI3toXs1NT07QB2SKkP95u+/Kl8HX31bAUgNsZuho1wcl5WfZLoJFRUgLFKlx6IRCr1HNKzSe8R3gW23Q5Ekd0Ukv4AvYaYJNSMsgSG8MfmRHYbLTw3CSYEo/G5EsVtCyxDhRsd6qmNlJK2HPUTl9ZSXPCEWXg9V6OUJ3trw7wbKyBgHW0OoRnHAUO8fF2qT83O8pTyQD98UbUZf5I6kfbw7zsjx9WHaDMZx2BYWYbkQukPmW8IWg5djI3XO4hbRjTWVLmRmVs5YzBMT8ViOJrLvTDFuhfoQHx1p4XxBLaRDL9dhLpSpFlRtj8wIZ1sHpXufXJloz46IlgGd99+Vpd9+/OOH9YFS56n2a6Wh+waNFGQstcmi4tInM5nyTTY2hVltQT2RIU2pm8ILJcS2+24iR/VOc3omhE9E0iWI6szvP/288vmvr0BlHWbDWATrguM3oI5dS12IwyA1H950jOafvErfU8wN4cSuCIIHUu118VOtaCDOPHMa9PYk5uYrV5+n1JULJV0ZUmvXf/lnafv7m4POTBHTc4eG592pfgY1pUOlpZvi/oCWTu2cSWQviPNBLBtNMIbGcyFkWT9Q2GX+znfJfd8q60Kt646KUHn37bTHYVfdCfjftQxwru1U52Z9HNwcz8+OMjsyJczlvKO0XljArp2jVQ6uFNMf5MMqVRCm0ZU4nZfgdhH+sUmUrYwJiCy0YxtIuyWURkmruNoRYUKjV++QGlG0QzmwlRUdLJxxhaYR5tU7N/SmppWglY9wLVuPOemPA0Xy8kj+KtYe7yYvGkbPZuYNnzdUnZ3XO4qpFZt1eQJQVKyy3URFJpRrzoFw99nkizsDS7x7hfWZIXk+C7r0fkrX7f/eN17N+/q2uCuMCZUv2Pkyh90urPzCYUdup0aNjS2+4NU43nQ1pZ0re29evHPXj2aQPVaHaa5GMyjxgOCuTIRZKrmtY9zC/ZEHdazoDnmCxzWPN2sonsYUDCfeC2gPXGwrHRC1Dw5UmeLv7ASI08rPHv+0HGNlLAabnx+BcO6lEc4Twn9yXAgkkgsAAA==";
70      private static final Log log = LogFactory.getLog(AttachmentItem.class);
71      private static final long serialVersionUID = 1L;
72  
73      /*-----------------------------------------------------------------------*/
74      /*--------------------------- Internal states ---------------------------*/
75      /*-----------------------------------------------------------------------*/
76  
77      protected List<Object> $activeBindings = new ArrayList<Object>();
78      protected Map<String, Object> $bindingSources = new HashMap<String, Object>();
79      protected final Map<String, JAXXBinding> $bindings = new TreeMap<String, JAXXBinding>();
80      protected Map<String, Object> $objectMap = new HashMap<String, Object>();
81      protected Map<?, ?> $previousValues = new HashMap<Object, Object>();
82      private boolean allComponentsCreated;
83      protected final JAXXContext delegateContext = new DefaultJAXXContext();
84  
85      /*-----------------------------------------------------------------------*/
86      /*------------------------ Protected components  ------------------------*/
87      /*-----------------------------------------------------------------------*/
88  
89      protected JLabel attachmentNameLabel;
90      protected AttachmentItem attachmentPanel = this;
91      protected final AttachmentItemHandler handler = createHandler();
92      protected AttachmentItemModel model;
93      protected JButton openAttachmentButton;
94      protected JButton removeAttachmentButton;
95      protected JButton saveAttachmentButton;
96      protected JToolBar toolbar;
97  
98      /*-----------------------------------------------------------------------*/
99      /*---------------------------- Constructors  ----------------------------*/
100     /*-----------------------------------------------------------------------*/
101 
102     public AttachmentItem(AttachmentEditorUI parentUI) {
103         JAXXUtil.initContext(this, parentUI);
104             TuttiUIUtil.setApplicationContext(this, parentUI.getModel());
105             $initialize();
106     }
107 
108     public AttachmentItem(LayoutManager param0, boolean param1) {
109         super(param0 ,param1);
110         $initialize();
111     }
112 
113     public AttachmentItem(JAXXContext parentContext, LayoutManager param1, boolean param2) {
114         super(param1 ,param2);
115         JAXXUtil.initContext(this, parentContext);
116         $initialize();
117     }
118 
119     public AttachmentItem(LayoutManager param0) {
120         super(param0);
121         $initialize();
122     }
123 
124     public AttachmentItem(JAXXContext parentContext, LayoutManager param1) {
125         super(param1);
126         JAXXUtil.initContext(this, parentContext);
127         $initialize();
128     }
129 
130     public AttachmentItem() {
131         $initialize();
132     }
133 
134     public AttachmentItem(JAXXContext parentContext) {
135         JAXXUtil.initContext(this, parentContext);
136         $initialize();
137     }
138 
139     public AttachmentItem(boolean param0) {
140         super(param0);
141         $initialize();
142     }
143 
144     public AttachmentItem(JAXXContext parentContext, boolean param1) {
145         super(param1);
146         JAXXUtil.initContext(this, parentContext);
147         $initialize();
148     }
149 
150     /*-----------------------------------------------------------------------*/
151     /*--------------------------- Statics methods ---------------------------*/
152     /*-----------------------------------------------------------------------*/
153 
154     public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
155         return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
156     }
157 
158     /*-----------------------------------------------------------------------*/
159     /*---------------------- JAXXObject implementation ----------------------*/
160     /*-----------------------------------------------------------------------*/
161 
162     @Override
163     public void applyDataBinding(String $binding) {
164         if (allComponentsCreated && $bindings.containsKey($binding)) {
165             getDataBinding($binding).applyDataBinding();
166         }
167         processDataBinding($binding);
168     }
169 
170     @Override
171     public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
172         super.firePropertyChange(propertyName, oldValue, newValue);
173     }
174 
175     @Override
176     public Map<String, Object> get$objectMap() {
177         return $objectMap;
178     }
179 
180     @Override
181     public JAXXBinding getDataBinding(String bindingId) {
182         return $bindings.get(bindingId);
183     }
184 
185     @Override
186     public JAXXBinding[] getDataBindings() {
187         return $bindings.values().toArray(new JAXXBinding[$bindings.size()]);
188     }
189 
190     @Override
191     public Object getObjectById(String id) {
192         return $objectMap.get(id);
193     }
194 
195     @Override
196     public void processDataBinding(String $binding, boolean $force) {
197         if (!$force && $activeBindings.contains($binding)) { 
198             return;
199         }
200         $activeBindings.add($binding);
201         try {
202             if (allComponentsCreated && $bindings.containsKey($binding)) {
203                 getDataBinding($binding).processDataBinding();
204             }
205         } finally {
206             $activeBindings.remove($binding);
207         }
208     }
209 
210     @Override
211     public void processDataBinding(String $binding) {
212         processDataBinding($binding, false);
213     }
214 
215     @Override
216     public void registerDataBinding(JAXXBinding binding) {
217         $bindings.put(binding.getId(), binding);
218     }
219 
220     @Override
221     public void removeDataBinding(String $binding) {
222         if (allComponentsCreated && $bindings.containsKey($binding)) {
223             getDataBinding($binding).removeDataBinding();
224         }
225     }
226 
227     /*-----------------------------------------------------------------------*/
228     /*--------------------- JAXXContext implementation  ---------------------*/
229     /*-----------------------------------------------------------------------*/
230 
231     @Override
232     public <T> T getContextValue(Class<T> clazz) {
233         return delegateContext.getContextValue(clazz, null);
234     }
235 
236     @Override
237     public <T> T getContextValue(Class<T> clazz, String name) {
238         return delegateContext.getContextValue(clazz, name);
239     }
240 
241     @Override
242     public JAXXContext getDelegateContext() {
243         return delegateContext;
244     }
245 
246     @Override
247     public <O extends Container> O getParentContainer(Class<O> clazz) {
248         return SwingUtil.getParentContainer(this, clazz);
249     }
250 
251     @Override
252     public <O extends Container> O getParentContainer(Object source, Class<O> clazz) {
253         return SwingUtil.getParentContainer(source, clazz);
254     }
255 
256     @Override
257     public <T> void removeContextValue(Class<T> clazz) {
258         delegateContext.removeContextValue(clazz, null);
259     }
260 
261     @Override
262     public <T> void removeContextValue(Class<T> clazz, String name) {
263         delegateContext.removeContextValue(clazz, name);
264     }
265 
266     @Override
267     public <T> void setContextValue(T o) {
268         delegateContext.setContextValue(o, null);
269     }
270 
271     @Override
272     public <T> void setContextValue(T o, String name) {
273         delegateContext.setContextValue(o, name);
274     }
275 
276     /*-----------------------------------------------------------------------*/
277     /*----------------------- Public acessor methods  -----------------------*/
278     /*-----------------------------------------------------------------------*/
279 
280     public JLabel getAttachmentNameLabel() {
281         return attachmentNameLabel;
282     }
283 
284     public AttachmentItemHandler getHandler() {
285         return handler;
286     }
287 
288     public AttachmentItemModel getModel() {
289         return model;
290     }
291 
292     public JButton getOpenAttachmentButton() {
293         return openAttachmentButton;
294     }
295 
296     public JButton getRemoveAttachmentButton() {
297         return removeAttachmentButton;
298     }
299 
300     public JButton getSaveAttachmentButton() {
301         return saveAttachmentButton;
302     }
303 
304     public JToolBar getToolbar() {
305         return toolbar;
306     }
307 
308     /*-----------------------------------------------------------------------*/
309     /*--------------------- Components creation methods ---------------------*/
310     /*-----------------------------------------------------------------------*/
311 
312     protected void addChildrenToAttachmentPanel() {
313         if (!allComponentsCreated) {
314             return;
315         }
316         add(attachmentNameLabel, BorderLayout.CENTER);
317         add(toolbar, BorderLayout.EAST);
318     }
319 
320     protected void addChildrenToToolbar() {
321         if (!allComponentsCreated) {
322             return;
323         }
324         toolbar.add(openAttachmentButton);
325         toolbar.add(saveAttachmentButton);
326         toolbar.add(removeAttachmentButton);
327     }
328 
329     protected void createAttachmentNameLabel() {
330         $objectMap.put("attachmentNameLabel", attachmentNameLabel = new JLabel());
331         
332         attachmentNameLabel.setName("attachmentNameLabel");
333     }
334 
335     protected AttachmentItemHandler createHandler() {
336         return new AttachmentItemHandler();
337     }
338 
339     protected void createModel() {
340         $objectMap.put("model", model = getContextValue(AttachmentItemModel.class));
341     }
342 
343     protected void createOpenAttachmentButton() {
344         $objectMap.put("openAttachmentButton", openAttachmentButton = new JButton());
345         
346         openAttachmentButton.setName("openAttachmentButton");
347         openAttachmentButton.setToolTipText(t("tutti.attachmentEditor.action.open.tip"));
348         openAttachmentButton.putClientProperty("simpleAction", fr.ifremer.tutti.ui.swing.util.attachment.actions.OpenAttachmentAction.class);
349     }
350 
351     protected void createRemoveAttachmentButton() {
352         $objectMap.put("removeAttachmentButton", removeAttachmentButton = new JButton());
353         
354         removeAttachmentButton.setName("removeAttachmentButton");
355         removeAttachmentButton.setToolTipText(t("tutti.attachmentEditor.action.remove.tip"));
356         removeAttachmentButton.putClientProperty("simpleAction", fr.ifremer.tutti.ui.swing.util.attachment.actions.RemoveAttachmentAction.class);
357     }
358 
359     protected void createSaveAttachmentButton() {
360         $objectMap.put("saveAttachmentButton", saveAttachmentButton = new JButton());
361         
362         saveAttachmentButton.setName("saveAttachmentButton");
363         saveAttachmentButton.setToolTipText(t("tutti.attachmentEditor.action.save.tip"));
364         saveAttachmentButton.putClientProperty("applicationAction", fr.ifremer.tutti.ui.swing.util.attachment.actions.SaveAttachmentAction.class);
365     }
366 
367     protected void createToolbar() {
368         $objectMap.put("toolbar", toolbar = new JToolBar());
369         
370         toolbar.setName("toolbar");
371         toolbar.setBorderPainted(false);
372         toolbar.setFloatable(false);
373     }
374 
375     /*-----------------------------------------------------------------------*/
376     /*------------------------ Internal jaxx methods ------------------------*/
377     /*-----------------------------------------------------------------------*/
378 
379     private void $completeSetup() {
380         allComponentsCreated = true;
381         if (log.isDebugEnabled()) {
382             log.debug(this);
383         }
384         addChildrenToAttachmentPanel();
385         addChildrenToToolbar();
386         
387         // apply 3 data bindings
388         JAXXUtil.applyDataBinding(this, $bindings.keySet());
389         
390         // apply 7 property setters
391         openAttachmentButton.setMnemonic(SwingUtil.getFirstCharAt(t("tutti.attachmentEditor.action.open.mnemonic"),'Z'));
392         openAttachmentButton.setIcon(SwingUtil.createActionIcon("open-file"));
393         saveAttachmentButton.setMnemonic(SwingUtil.getFirstCharAt(t("tutti.attachmentEditor.action.save.mnemonic"),'Z'));
394         saveAttachmentButton.setIcon(SwingUtil.createActionIcon("import"));
395         removeAttachmentButton.setMnemonic(SwingUtil.getFirstCharAt(t("tutti.attachmentEditor.action.remove.mnemonic"),'Z'));
396         removeAttachmentButton.setIcon(SwingUtil.createActionIcon("delete"));
397     }
398 
399     private void $initialize() {
400         if (allComponentsCreated) {
401             return;
402         }
403         if (log.isDebugEnabled()) {
404             log.debug(this);
405         }
406         handler.beforeInit(this);
407         $objectMap.put("attachmentPanel", attachmentPanel);
408         createModel();
409         createAttachmentNameLabel();
410         createToolbar();
411         createOpenAttachmentButton();
412         createSaveAttachmentButton();
413         createRemoveAttachmentButton();
414         // inline creation of attachmentPanel
415         setName("attachmentPanel");
416         setLayout(new BorderLayout());
417         
418         // registers 3 data bindings
419         $registerDefaultBindings();
420         $completeSetup();
421         handler.afterInit(this);
422     }
423 
424     private void $registerDefaultBindings() {
425         // register 3 data bindings
426         registerDataBinding(new DefaultJAXXBinding(this, BINDING_ATTACHMENT_PANEL_TOOL_TIP_TEXT, true) {
427         
428             @Override
429             public void applyDataBinding() {
430                 if (model != null) {
431                     model.addPropertyChangeListener("comment", this);
432                 }
433             }
434         
435             @Override
436             public void processDataBinding() {
437                 if (model != null) {
438                     setToolTipText(t(model.getComment()));
439                 }
440             }
441         
442             @Override
443             public void removeDataBinding() {
444                 if (model != null) {
445                     model.removePropertyChangeListener("comment", this);
446                 }
447             }
448         });
449         registerDataBinding(new DefaultJAXXBinding(this, BINDING_ATTACHMENT_NAME_LABEL_TEXT, true) {
450         
451             @Override
452             public void applyDataBinding() {
453                 if (model != null) {
454                     model.addPropertyChangeListener("name", this);
455                 }
456             }
457         
458             @Override
459             public void processDataBinding() {
460                 if (model != null) {
461                     attachmentNameLabel.setText(t(model.getName()));
462                 }
463             }
464         
465             @Override
466             public void removeDataBinding() {
467                 if (model != null) {
468                     model.removePropertyChangeListener("name", this);
469                 }
470             }
471         });
472         registerDataBinding(new DefaultJAXXBinding(this, BINDING_ATTACHMENT_NAME_LABEL_TOOL_TIP_TEXT, true) {
473         
474             @Override
475             public void applyDataBinding() {
476                 if (model != null) {
477                     model.addPropertyChangeListener("comment", this);
478                 }
479             }
480         
481             @Override
482             public void processDataBinding() {
483                 if (model != null) {
484                     attachmentNameLabel.setToolTipText(t(model.getComment()));
485                 }
486             }
487         
488             @Override
489             public void removeDataBinding() {
490                 if (model != null) {
491                     model.removePropertyChangeListener("comment", this);
492                 }
493             }
494         });
495     }
496 
497 }