View Javadoc
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  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   * <p>CommentEditorUIHandler class.</p>
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       * Constant <code>CLOSE_DIALOG_ACTION="closeDialog"</code>
51       */
52      private static final String CLOSE_DIALOG_ACTION = "closeDialog";
53  
54      /**
55       * Constant <code>SHOW_DIALOG_ACTION="showDialog"</code>
56       */
57      private static final String SHOW_DIALOG_ACTION = "showDialog";
58  
59      protected ApplicationUIContext context;
60  
61      private CommentEditorUI ui;
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public void beforeInit(CommentEditorUI ui) {
68          this.ui = ui;
69          this.context = (ApplicationUIContext) ApplicationUIUtil.getApplicationContext(ui);
70      }
71  
72      /**
73       * {@inheritDoc}
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      * <p>closeEditor.</p>
147      */
148     public void closeEditor() {
149 
150         closeAction.actionPerformed(null);
151     }
152 
153     /**
154      * <p>openEditor.</p>
155      *
156      * @param component a {@link JComponent} object.
157      */
158     public void openEditor(JComponent component) {
159 
160         if (component != null) {
161             place(component);
162         }
163         openAction.actionPerformed(null);
164     }
165 
166     /**
167      * <p>init.</p>
168      *
169      * @param editable a boolean.
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      * <p>place.</p>
181      *
182      * @param component a {@link JComponent} object.
183      */
184     private void place(JComponent component) {
185         // Computes the location of bottom left corner of the cell
186         int x = component.getLocationOnScreen().x;
187         int y = component.getLocationOnScreen().y + component.getHeight();
188 
189         // if the editor is too big on the right,
190         // then align its right side to the right side of the cell
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         // try to change modality if parent is already a JDialog, but fails...
197 //        ui.setModalityType(
198 //            SwingUtilities.getRoot(component) instanceof JDialog
199 //                ? Dialog.ModalityType.TOOLKIT_MODAL
200 //                : Dialog.ModalityType.MODELESS
201 //        );
202 
203         ui.setLocation(x, y);
204     }
205 
206     /**
207      * pushes the typed text in the property of the bean
208      *
209      * @param value a {@link String} object.
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 }