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