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 import fr.ifremer.quadrige3.ui.core.dto.CommentAware;
25 import fr.ifremer.quadrige3.ui.swing.ApplicationUIContext;
26 import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
27 import jaxx.runtime.SwingUtil;
28 import jaxx.runtime.spi.UIHandler;
29 import jaxx.runtime.swing.ComponentMover;
30 import jaxx.runtime.swing.ComponentResizer;
31 import org.nuiton.jaxx.application.bean.JavaBeanObjectUtil;
32
33 import javax.swing.*;
34 import java.awt.Dialog;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.KeyEvent;
37
38 import static org.nuiton.i18n.I18n.t;
39
40
41
42
43 public class CommentEditorUIHandler implements UIHandler<CommentEditorUI> {
44
45 private static final int DEFAULT_EDITOR_WIDTH = 400;
46
47 private static final int DEFAULT_EDITOR_HEIGHT = 200;
48
49
50
51
52 private static final String CLOSE_DIALOG_ACTION = "closeDialog";
53
54
55
56
57 private static final String SHOW_DIALOG_ACTION = "showDialog";
58
59 protected ApplicationUIContext context;
60
61 private CommentEditorUI ui;
62
63
64
65
66 @Override
67 public void beforeInit(CommentEditorUI ui) {
68 this.ui = ui;
69 this.context = (ApplicationUIContext) ApplicationUIUtil.getApplicationContext(ui);
70 }
71
72
73
74
75 @Override
76 public void afterInit(CommentEditorUI ui) {
77
78 ui.pack();
79 ui.setResizable(true);
80 ui.setSize(DEFAULT_EDITOR_WIDTH, DEFAULT_EDITOR_HEIGHT);
81
82 ComponentResizer cr = new ComponentResizer();
83 cr.registerComponent(ui);
84 ComponentMover cm = new ComponentMover();
85 cm.setDragInsets(cr.getDragInsets());
86 cm.registerComponent(ui);
87
88 JRootPane rootPane = ui.getRootPane();
89
90 KeyStroke shortcutClosePopup
91 = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
92
93 rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
94 shortcutClosePopup, CLOSE_DIALOG_ACTION);
95
96 closeAction = new AbstractAction() {
97 private static final long serialVersionUID = 1L;
98
99 @Override
100 public void actionPerformed(ActionEvent e) {
101 CommentEditorUIHandler.this.ui.dispose();
102 }
103 };
104
105 openAction = new AbstractAction() {
106
107 private static final long serialVersionUID = 1L;
108
109 @Override
110 public void actionPerformed(ActionEvent e) {
111 CommentEditorUIHandler.this.ui.setVisible(true);
112 CommentEditorUIHandler.this.ui.getTextContent().requestFocus();
113 CommentEditorUIHandler.this.ui.getTextScrollPane().getVerticalScrollBar().setValue(0);
114 }
115 };
116
117 ImageIcon actionIcon = SwingUtil.createActionIcon("close-dialog");
118 closeAction.putValue(Action.SMALL_ICON, actionIcon);
119 closeAction.putValue(Action.LARGE_ICON_KEY, actionIcon);
120 closeAction.putValue(Action.ACTION_COMMAND_KEY, "close");
121 closeAction.putValue(Action.NAME, "close");
122 closeAction.putValue(Action.SHORT_DESCRIPTION, t("quadrige3.commentEditor.action.close.tip"));
123
124 rootPane.getActionMap().put(CLOSE_DIALOG_ACTION, closeAction);
125 rootPane.getActionMap().put(SHOW_DIALOG_ACTION, openAction);
126
127 JButton closeButton = new JButton(closeAction);
128 closeButton.setText(null);
129 closeButton.setFocusPainted(false);
130 closeButton.setRequestFocusEnabled(false);
131 closeButton.setFocusable(false);
132
133 JToolBar jToolBar = new JToolBar();
134 jToolBar.setOpaque(false);
135 jToolBar.add(closeAction);
136 jToolBar.setBorderPainted(false);
137 jToolBar.setFloatable(false);
138 ui.getCommentEditorTopPanel().setRightDecoration(jToolBar);
139 }
140
141 private Action closeAction;
142
143 private Action openAction;
144
145
146
147
148 public void closeEditor() {
149
150 closeAction.actionPerformed(null);
151 }
152
153
154
155
156
157
158 public void openEditor(JComponent component) {
159
160 if (component != null) {
161 place(component);
162 }
163 openAction.actionPerformed(null);
164 }
165
166
167
168
169
170
171 public void init(boolean editable) {
172
173 CommentAware bean = ui.getBean();
174 String content = bean == null ? null : (ui.getProperty() == null ? bean.getComment() : (String) JavaBeanObjectUtil.getProperty(bean, ui.getProperty()));
175 ui.getTextContent().setText(content);
176 ui.getTextContent().setEnabled(editable);
177 }
178
179
180
181
182
183
184 private void place(JComponent component) {
185
186 int x = component.getLocationOnScreen().x;
187 int y = component.getLocationOnScreen().y + component.getHeight();
188
189
190
191 if (x + ui.getWidth() > ui.getOwner().getX() + ui.getOwner().getWidth()) {
192 x = x - ui.getWidth() + component.getWidth();
193 }
194 ui.setModalityType(Dialog.ModalityType.MODELESS);
195
196
197
198
199
200
201
202
203 ui.setLocation(x, y);
204 }
205
206
207
208
209
210
211 public void setText(String value) {
212 CommentAware bean = ui.getBean();
213 if (ui.getProperty() == null) {
214 bean.setComment(value);
215 } else {
216 JavaBeanObjectUtil.setProperty(bean, ui.getProperty(), value);
217 }
218 }
219 }