View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.action;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
28  import fr.ifremer.quadrige2.ui.swing.common.ApplicationUIContext;
29  import fr.ifremer.quadrige2.ui.swing.common.AbstractUIHandler;
30  import org.apache.commons.lang3.reflect.ConstructorUtils;
31  import org.nuiton.i18n.I18n;
32  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
33  import org.nuiton.jaxx.application.swing.action.AbstractApplicationAction;
34  import org.nuiton.jaxx.application.swing.action.ApplicationActionFactory;
35  
36  import javax.swing.AbstractButton;
37  import java.util.concurrent.*;
38  
39  /**
40   * Action Factory
41   */
42  public class ActionFactory extends ApplicationActionFactory {
43  
44      /** {@inheritDoc} */
45      @Override
46      public <A extends AbstractApplicationAction> A createLogicAction(AbstractApplicationUIHandler handler, Class<A> actionName) {
47          ApplicationUIContext context = (ApplicationUIContext) handler.getContext();
48          if (AbstractMainUIAction.class.isAssignableFrom(actionName) && context.getMainUI() != null) {
49              handler = context.getMainUI().getHandler();
50          }
51  
52          return super.createLogicAction(handler, actionName);
53      }
54  
55      /**
56       * <p>createNonBlockingUIAction.</p>
57       *
58       * @param handler a {@link AbstractUIHandler} object.
59       * @param actionName a {@link Class} object.
60       * @param <A> a A object.
61       * @return a A object.
62       */
63      public <A extends AbstractWorkerAction> A createNonBlockingUIAction(AbstractUIHandler handler, Class<A> actionName) {
64          return createNonBlockingUIAction(handler, actionName, null);
65      }
66  
67      /**
68       * <p>createNonBlockingUIAction.</p>
69       *
70       * @param handler a {@link AbstractUIHandler} object.
71       * @param actionName a {@link Class} object.
72       * @param button a {@link AbstractButton} object.
73       * @param <A> a A object.
74       * @return a A object.
75       */
76      public <A extends AbstractWorkerAction> A createNonBlockingUIAction(AbstractUIHandler handler, Class<A> actionName, AbstractButton button) {
77          try {
78              // create action
79              A action = ConstructorUtils.invokeConstructor(actionName, handler);
80  
81              if (button != null) {
82                  action.setActionIcon(button.getIcon());
83                  action.setActionName(button.getText());
84                  action.setActionDescription(button.getToolTipText());
85                  action.setActionMnemonic(button.getMnemonic());
86              }
87  
88              return action;
89          } catch (Exception e) {
90              throw new Quadrige2TechnicalException(I18n.t("quadrige2.error.create.action", actionName.getName()), e);
91          }
92  
93      }
94  
95      /**
96       * Create a ThreadPoolExecutor with pool active option
97       *
98       * @param isPoolActive if true, create a poolable executor that allows multiple calls but always executes the last.
99       *                     if false, an exception is raised if a second call happens
100      * @return a ThreadPoolExecutor
101      */
102     public static ThreadPoolExecutor createThreadExecutor(boolean isPoolActive) {
103         int capacity = isPoolActive ? 2 : 1;
104         BlockingQueue<Runnable> queue = new LinkedBlockingDeque<>(capacity);
105 
106         RejectedExecutionHandler rejectedExecutionHandler = isPoolActive
107                 ? new ThreadPoolExecutor.DiscardPolicy()
108                 : new ThreadPoolExecutor.AbortPolicy();
109 
110         return new ThreadPoolExecutor(1, 1, 1, TimeUnit.MINUTES, queue, rejectedExecutionHandler);
111     }
112 }