1 package fr.ifremer.tutti.ui.swing.util.attachment;
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.data.Attachment;
26 import fr.ifremer.tutti.ui.swing.TuttiUIContext;
27 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
28 import fr.ifremer.tutti.ui.swing.util.attachment.actions.HideAttachmentUIAction;
29 import fr.ifremer.tutti.ui.swing.util.attachment.actions.ShowAttachmentUIAction;
30 import jaxx.runtime.swing.ComponentMover;
31 import jaxx.runtime.swing.ComponentResizer;
32 import jaxx.runtime.validator.swing.SwingValidator;
33
34 import javax.swing.Action;
35 import javax.swing.JButton;
36 import javax.swing.JComponent;
37 import javax.swing.JRootPane;
38 import javax.swing.JToolBar;
39 import javax.swing.KeyStroke;
40 import java.awt.Component;
41 import java.awt.event.KeyEvent;
42 import java.util.List;
43
44
45
46
47
48
49 public class AttachmentEditorUIHandler extends AbstractTuttiUIHandler<TuttiUIContext, AttachmentEditorUI> {
50
51 public static final String CLOSE_DIALOG_ACTION = "closeDialog";
52
53 public static final String SHOW_DIALOG_ACTION = "showDialog";
54
55 @Override
56 public void beforeInit(AttachmentEditorUI ui) {
57 super.beforeInit(ui);
58 ui.setContextValue(TuttiUIContext.getApplicationContext());
59
60 }
61
62 @Override
63 public void afterInit(AttachmentEditorUI ui) {
64
65
66
67 initButton(ui.getAddButton());
68
69 ui.getFile().setDialogOwner(ui);
70 ui.pack();
71 ui.setResizable(true);
72
73 ComponentResizer cr = new ComponentResizer();
74 cr.registerComponent(ui);
75 ComponentMover cm = new ComponentMover();
76 cm.setDragInsets(cr.getDragInsets());
77 cm.registerComponent(ui);
78
79 closeAction = new HideAttachmentUIAction(ui);
80 openAction = new ShowAttachmentUIAction(ui);
81
82 JRootPane rootPane = ui.getRootPane();
83 KeyStroke shortcutClosePopup = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
84 rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(shortcutClosePopup, CLOSE_DIALOG_ACTION);
85 rootPane.getActionMap().put(CLOSE_DIALOG_ACTION, closeAction);
86
87 rootPane.getActionMap().put(SHOW_DIALOG_ACTION, openAction);
88
89 JButton closeButton = new JButton(closeAction);
90 closeButton.setText(null);
91 closeButton.setFocusPainted(false);
92 closeButton.setRequestFocusEnabled(false);
93 closeButton.setFocusable(false);
94
95 JToolBar jToolBar = new JToolBar();
96 jToolBar.setOpaque(false);
97 jToolBar.add(closeAction);
98 jToolBar.setBorderPainted(false);
99 jToolBar.setFloatable(false);
100 ui.getAttachmentBody().setRightDecoration(jToolBar);
101
102 }
103
104 @Override
105 protected JComponent getComponentToFocus() {
106 return getUI().getFile();
107 }
108
109 @Override
110 public void onCloseUI() {
111 ui.dispose();
112 }
113
114 @Override
115 public SwingValidator<TuttiUIContext> getValidator() {
116 return null;
117 }
118
119 protected Action closeAction;
120
121 protected Action openAction;
122
123 public void closeEditor() {
124
125 closeAction.actionPerformed(null);
126 }
127
128 public void openEditor(JComponent component) {
129
130 if (component != null) {
131 place(component);
132 }
133 openAction.actionPerformed(null);
134 }
135
136 public void init() {
137 resetFields();
138 ui.getAttachments().removeAll();
139 AttachmentModelAware bean = ui.getBean();
140 if (bean != null) {
141 List<Attachment> list = bean.getAttachment();
142 if (list != null) {
143 for (Attachment attachment : list) {
144 addAttachment(attachment);
145 }
146 }
147 }
148 }
149
150 public void place(JComponent component) {
151
152 Component comp = component;
153 int x = 0;
154 int y = component.getHeight();
155 while (comp != null) {
156 x += comp.getX();
157 y += comp.getY();
158 comp = comp.getParent();
159 }
160
161 ui.pack();
162
163
164 if (x + ui.getWidth() > ui.getOwner().getX() + ui.getOwner().getWidth()) {
165 x = x - ui.getWidth() + component.getWidth();
166 }
167 ui.setLocation(x, y);
168 }
169
170 public void addAttachment(Attachment attachment) {
171
172 AttachmentItemModel model = new AttachmentItemModel();
173 model.fromEntity(attachment);
174
175 ui.setContextValue(model);
176 AttachmentItem item = new AttachmentItem(ui);
177 ui.getAttachments().add(item);
178
179 }
180
181 public void resetFields() {
182 ui.getFile().setSelectedFilePath(null);
183 ui.getFileName().setText("");
184 ui.getFileComment().setText("");
185 }
186
187 }