View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.comment;
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.CommentAware;
26  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
27  import jaxx.runtime.SwingUtil;
28  
29  import javax.swing.JToggleButton;
30  import javax.swing.event.ChangeEvent;
31  import javax.swing.event.ChangeListener;
32  import java.awt.Point;
33  import java.awt.event.HierarchyBoundsAdapter;
34  import java.awt.event.HierarchyEvent;
35  import java.awt.event.WindowAdapter;
36  import java.awt.event.WindowEvent;
37  
38  import static org.nuiton.i18n.I18n.t;
39  
40  /**
41   * A toggleButton to show (or hide) comment editor.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 1.1
45   */
46  public class ButtonComment extends JToggleButton {
47  
48      private static final long serialVersionUID = 1L;
49  
50      protected final CommentEditorUI popup;
51  
52      protected Point popupPosition = null;
53  
54      protected boolean popupMoving;
55  
56      public ButtonComment(TuttiUIContext context,
57                           CommentAware model) {
58  
59          setIcon(SwingUtil.createActionIcon("edit-comment"));
60          setToolTipText(t("tutti.commentEditor.action.tip"));
61  
62          popup = new CommentEditorUI(context);
63  
64          popup.addWindowListener(new WindowAdapter() {
65  
66              @Override
67              public void windowOpened(WindowEvent e) {
68                  setSelected(true);
69              }
70  
71              @Override
72              public void windowClosing(WindowEvent e) {
73                  setSelected(false);
74              }
75  
76              @Override
77              public void windowClosed(WindowEvent e) {
78                  setSelected(false);
79              }
80          });
81  
82          addChangeListener(new ChangeListener() {
83              @Override
84              public void stateChanged(ChangeEvent e) {
85                  if (isSelected()) {
86                      popup.openEditor(ButtonComment.this);
87                  } else {
88                      popup.closeEditor();
89                  }
90              }
91          });
92  
93          addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
94  
95              @Override
96              public void ancestorMoved(HierarchyEvent e) {
97                  if (popup.isShowing()) {
98  
99                      // place dialog just under the button
100                     Point point = new Point(getLocationOnScreen());
101                     point.translate(-popup.getWidth() + getWidth(), getHeight());
102                     popupMoving = true;
103                     try {
104                         popup.setLocation(point);
105                     } finally {
106                         popupMoving = false;
107                     }
108                 }
109             }
110         });
111         setBean(model);
112     }
113 
114     public void init() {
115         popup.getHandler().init();
116     }
117 
118     public void init(CommentAware model) {
119         setBean(model);
120         init();
121     }
122 
123     public void onCloseUI() {
124         setSelected(false);
125     }
126 
127     public CommentAware getBean() {
128         return popup.getBean();
129     }
130 
131     protected void setBean(CommentAware model) {
132         popup.setBean(model);
133         init();
134 
135     }
136 
137 }