View Javadoc
1   package fr.ifremer.tutti.ui.swing;
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.ui.swing.content.actions.AbstractMainUITuttiAction;
26  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
27  import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
28  import jaxx.runtime.JAXXObject;
29  import org.apache.commons.lang3.reflect.ConstructorUtils;
30  import org.nuiton.jaxx.application.ApplicationTechnicalException;
31  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
32  import org.nuiton.jaxx.application.swing.action.AbstractApplicationAction;
33  import org.nuiton.jaxx.application.swing.action.ApplicationActionFactory;
34  
35  import javax.swing.AbstractButton;
36  import javax.swing.Action;
37  
38  import static org.nuiton.i18n.I18n.t;
39  
40  /**
41   * Created on 11/24/13.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 3.0
45   */
46  public class TuttiActionFactory extends ApplicationActionFactory {
47  
48      @Override
49      public <A extends AbstractApplicationAction> A createLogicAction(AbstractApplicationUIHandler handler,
50                                                                       Class<A> actionName) {
51          TuttiUIContext context = (TuttiUIContext) handler.getContext();
52          if (AbstractMainUITuttiAction.class.isAssignableFrom(actionName) &&
53              context.getMainUI() != null) {
54              handler = context.getMainUI().getHandler();
55          }
56  
57          try {
58              // create action
59              return ConstructorUtils.invokeConstructor(actionName, (AbstractTuttiUIHandler) handler);
60          } catch (Exception e) {
61              throw new ApplicationTechnicalException(t("application.action.create.error", actionName), e);
62          }
63      }
64  
65      public <A extends Action> A createSimpleAction(AbstractApplicationUIHandler handler,
66                                                     AbstractButton abstractButton,
67                                                     Class<A> actionType) {
68          try {
69  
70              A action = ConstructorUtils.invokeConstructor(actionType, (JAXXObject) handler.getUI());
71              TuttiUIUtil.prepareAction(abstractButton, action, abstractButton.getName());
72  
73              return action;
74  
75          } catch (Exception e) {
76              throw new ApplicationTechnicalException(t("jaxx.application.action.create.error", actionType), e);
77          }
78  
79      }
80  
81      public <A extends Action> A createSimpleAction(JAXXObject ui,
82                                                     AbstractButton abstractButton,
83                                                     Class<A> actionType) {
84          try {
85  
86              A action = ConstructorUtils.invokeConstructor(actionType,  ui);
87              TuttiUIUtil.prepareAction(abstractButton, action, abstractButton.getName());
88  
89              return action;
90  
91          } catch (Exception e) {
92              throw new ApplicationTechnicalException(t("jaxx.application.action.create.error", actionType), e);
93          }
94  
95      }
96  }