1 package fr.ifremer.tutti.ui.swing.util.comment;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
42
43
44
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
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 }