View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.attachment;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (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 General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.persistence.entities.data.Attachment;
26  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
27  import jaxx.runtime.SwingUtil;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.jdesktop.beans.AbstractSerializableBean;
31  
32  import javax.swing.JToggleButton;
33  import javax.swing.event.ChangeEvent;
34  import javax.swing.event.ChangeListener;
35  import java.awt.Point;
36  import java.awt.event.HierarchyBoundsAdapter;
37  import java.awt.event.HierarchyEvent;
38  import java.awt.event.WindowAdapter;
39  import java.awt.event.WindowEvent;
40  import java.beans.PropertyChangeListener;
41  import java.util.List;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Button to edit attachments.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 1.1
50   */
51  public class ButtonAttachment extends JToggleButton {
52  
53      private static final long serialVersionUID = 1L;
54  
55      /** Logger. */
56      private static final Log log = LogFactory.getLog(ButtonAttachment.class);
57  
58      protected final AttachmentEditorUI popup;
59  
60      private transient PropertyChangeListener listenAttachmentsChanged;
61  
62  //    protected Point popupPosition = null;
63  
64      protected boolean popupMoving;
65  
66      public ButtonAttachment(TuttiUIContext context, AttachmentModelAware model) {
67  
68          setIcon(SwingUtil.createActionIcon("edit-attachment"));
69          setText(t("tutti.attachmentEditor.simpleText"));
70          setToolTipText(t("tutti.attachmentEditor.action.tip"));
71  
72          popup = new AttachmentEditorUI(context);
73  
74          popup.addWindowListener(new WindowAdapter() {
75  
76              @Override
77              public void windowOpened(WindowEvent e) {
78                  setSelected(true);
79              }
80  
81              @Override
82              public void windowClosing(WindowEvent e) {
83                  setSelected(false);
84              }
85  
86              @Override
87              public void windowClosed(WindowEvent e) {
88                  setSelected(false);
89              }
90          });
91  
92          addChangeListener(new ChangeListener() {
93              @Override
94              public void stateChanged(ChangeEvent e) {
95                  if (ButtonAttachment.this.isSelected()) {
96                      if (!popup.isVisible()) {
97                          popup.openEditor(ButtonAttachment.this);
98                      }
99                  } else {
100                     popup.closeEditor();
101                 }
102             }
103         });
104 
105         addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
106 
107             @Override
108             public void ancestorMoved(HierarchyEvent e) {
109                 if (popup.isShowing()) {
110 
111                     // place dialog just under the button
112                     Point point = new Point(getLocationOnScreen());
113                     point.translate(-popup.getWidth() + getWidth(), getHeight());
114                     popupMoving = true;
115                     try {
116                         popup.setLocation(point);
117                     } finally {
118                         popupMoving = false;
119                     }
120                 }
121             }
122         });
123         if (log.isDebugEnabled()) {
124             log.debug("Attach bean: " + model);
125         }
126         setBean(model);
127     }
128 
129     public static String getButtonText(List<Attachment> attachment) {
130         return t("tutti.attachmentEditor.text", attachment.size());
131     }
132 
133     public void init() {
134         popup.getHandler().init();
135     }
136 
137     public void init(AttachmentModelAware model) {
138         setBean(model);
139         init();
140     }
141 
142     public void onCloseUI() {
143         setSelected(false);
144     }
145 
146     public AttachmentModelAware getBean() {
147         return popup.getBean();
148     }
149 
150     protected void setBean(AttachmentModelAware model) {
151         AttachmentModelAware bean = popup.getBean();
152         if (bean != null) {
153             ((AbstractSerializableBean) bean).removePropertyChangeListener(AttachmentModelAware.PROPERTY_ATTACHMENT, getListenAttachmentsChanged());
154         }
155         popup.setBean(model);
156 
157         if (model != null) {
158 
159             ((AbstractSerializableBean) model).addPropertyChangeListener(AttachmentModelAware.PROPERTY_ATTACHMENT, getListenAttachmentsChanged());
160 //            List<Attachment> attachment = model.getAttachment();
161 //            setText(ButtonAttachment.getButtonText(attachment));
162         }
163     }
164 
165     protected PropertyChangeListener getListenAttachmentsChanged() {
166         if (listenAttachmentsChanged == null) {
167             listenAttachmentsChanged = evt -> {
168 //                    List<Attachment> attachment = (List<Attachment>) evt.getNewValue();
169 //                    setText(getButtonText(attachment));
170                 if (!popup.isVisible()) {
171                     init();
172                 }
173             };
174         }
175         return listenAttachmentsChanged;
176     }
177 }