1 package fr.ifremer.quadrige3.ui.swing.table.comment;
2
3 /*-
4 * #%L
5 * Quadrige3 Core :: UI Swing Common
6 * %%
7 * Copyright (C) 2017 - 2019 Ifremer
8 * %%
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (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 Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * #L%
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 * A toggleButton to show (or hide) comment editor.
41 */
42 public class ButtonComment extends JToggleButton {
43
44 private static final long serialVersionUID = 1L;
45
46 protected final CommentEditorUI popup;
47
48 /**
49 * <p>Constructor for ButtonComment.</p>
50 *
51 * @param applicationUI a {@link ApplicationUI} object.
52 * @param model a {@link CommentAware} object.
53 * @param property a {@link String} object.
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 // place dialog just under the button
95 Point point = new Point(getLocationOnScreen());
96 point.translate(-popup.getWidth() + getWidth(), getHeight());
97 popup.setLocation(point);
98 }
99 }
100 });
101
102 // set the property, if null, 'comment' will be used (default behaviour of a CommentAware bean)
103 popup.setProperty(property);
104
105 // set title
106 popup.setTitleI18n(Optional.ofNullable(titleI18n).orElse("quadrige3.commentEditor.title"));
107
108 setBean(model);
109 }
110
111 /**
112 * <p>init.</p>
113 *
114 * @param model a {@link CommentAware} object.
115 * @param editable a boolean.
116 */
117 public void init(CommentAware model, boolean editable) {
118 setBean(model);
119 popup.getHandler().init(editable);
120 }
121
122 /**
123 * <p>getBean.</p>
124 *
125 * @return a {@link CommentAware} object.
126 */
127 public CommentAware getBean() {
128 return popup.getBean();
129 }
130
131 /**
132 * <p>setBean.</p>
133 *
134 * @param model a {@link CommentAware} object.
135 */
136 protected void setBean(CommentAware model) {
137 popup.setBean(model);
138 }
139
140 /**
141 * <p>getProperty.</p>
142 *
143 * @return a {@link String} object.
144 */
145 public String getProperty() {
146 return popup.getProperty();
147 }
148 }