1 package fr.ifremer.quadrige3.ui.swing.table.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.quadrige3.ui.core.dto.CommentAware;
26 import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
27 import jaxx.runtime.SwingUtil;
28
29 import javax.swing.JToggleButton;
30 import java.awt.Point;
31 import java.awt.event.HierarchyBoundsAdapter;
32 import java.awt.event.HierarchyEvent;
33 import java.awt.event.WindowAdapter;
34 import java.awt.event.WindowEvent;
35 import java.util.Optional;
36
37 import static org.nuiton.i18n.I18n.t;
38
39
40
41
42 public class ButtonComment extends JToggleButton {
43
44 private static final long serialVersionUID = 1L;
45
46 protected final CommentEditorUI popup;
47
48
49
50
51
52
53
54
55 public ButtonComment(ApplicationUI applicationUI, CommentAware model, String property, String titleI18n) {
56
57 setIcon(SwingUtil.createActionIcon("edit-no-comment"));
58 setToolTipText(t("quadrige3.commentEditor.action.tip"));
59
60 popup = new CommentEditorUI(applicationUI);
61
62 popup.addWindowListener(new WindowAdapter() {
63
64 @Override
65 public void windowOpened(WindowEvent e) {
66 setSelected(true);
67 }
68
69 @Override
70 public void windowClosing(WindowEvent e) {
71 setSelected(false);
72 }
73
74 @Override
75 public void windowClosed(WindowEvent e) {
76 setSelected(false);
77 }
78 });
79
80 addChangeListener(e -> {
81 if (isSelected()) {
82 popup.openEditor(ButtonComment.this);
83 } else {
84 popup.closeEditor();
85 }
86 });
87
88 addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
89
90 @Override
91 public void ancestorMoved(HierarchyEvent e) {
92 if (popup.isShowing()) {
93
94
95 Point point = new Point(getLocationOnScreen());
96 point.translate(-popup.getWidth() + getWidth(), getHeight());
97 popup.setLocation(point);
98 }
99 }
100 });
101
102
103 popup.setProperty(property);
104
105
106 popup.setTitleI18n(Optional.ofNullable(titleI18n).orElse("quadrige3.commentEditor.title"));
107
108 setBean(model);
109 }
110
111
112
113
114
115
116
117 public void init(CommentAware model, boolean editable) {
118 setBean(model);
119 popup.getHandler().init(editable);
120 }
121
122
123
124
125
126
127 public CommentAware getBean() {
128 return popup.getBean();
129 }
130
131
132
133
134
135
136 protected void setBean(CommentAware model) {
137 popup.setBean(model);
138 }
139
140
141
142
143
144
145 public String getProperty() {
146 return popup.getProperty();
147 }
148 }