View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.comment;
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.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   * @author Tony Chemit - chemit@codelutin.com
49   * @since 1.1
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  //        ui.setSize(800, 300);
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         //TODO Use fr.ifremer.tutti.ui.swing.util.TuttiUIUtil.prepareAction()
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         // Computes the location of bottom left corner of the cell
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 //        ui.pack();
175         // if the editor is too big on the right,
176         // then align its right side to the right side of the cell
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     /** pushes the typed text in the property of the bean */
184     public void setText(String value) {
185         CommentAware bean = ui.getBean();
186         bean.setComment(value);
187     }
188 
189 }