View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.attachment;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (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 General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * @author Kevin Morin - kmorin@codelutin.com
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 0.2
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  //        super.initUI(ui);
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         // Computes the location of bottom left corner of the cell
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         // if the editor is too big on the right,
163         // then align its right side to the right side of the cell
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 }