View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.blossom.dialog;
35  
36  import java.lang.reflect.Method;
37  import java.util.Collections;
38  import java.util.Comparator;
39  
40  import org.apache.commons.lang.ArrayUtils;
41  import org.apache.commons.lang.StringUtils;
42  
43  import info.magnolia.module.blossom.annotation.TabFactory;
44  import info.magnolia.ui.dialog.config.DialogBuilder;
45  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
46  import info.magnolia.ui.form.config.TabBuilder;
47  import info.magnolia.ui.framework.config.UiConfig;
48  import info.magnolia.module.blossom.support.MethodInvocationUtils;
49  import info.magnolia.module.blossom.support.ParameterResolver;
50  import info.magnolia.ui.dialog.definition.ConfiguredDialogDefinition;
51  import info.magnolia.ui.form.definition.ConfiguredFormDefinition;
52  import info.magnolia.ui.form.definition.TabDefinition;
53  
54  /**
55   * Default implementation of {@link info.magnolia.module.blossom.dialog.DialogCreator}.
56   *
57   * @since 0.5
58   */
59  public class DefaultDialogCreator implements DialogCreator {
60  
61      @Override
62      public void createDialog(DialogFactoryMetaData metaData, final DialogCreationContext context) throws Exception {
63  
64          DialogBuilder dialogBuilder = new DialogBuilder(context.getId());
65          context.setDialog(dialogBuilder.definition());
66          context.getDialog().setForm(new ConfiguredFormDefinition());
67  
68          if (metaData.getFactoryMethod() != null) {
69  
70              ParameterResolver parameters = getDialogFactoryParameters(metaData, context, dialogBuilder);
71              MethodInvocationUtils.invoke(metaData.getFactoryMethod(), metaData.getFactoryObject(), parameters);
72          } else {
73  
74              invokeTabFactories(metaData, context, dialogBuilder);
75          }
76  
77          if (StringUtils.isNotEmpty(metaData.getLabel())) {
78              ConfiguredFormDefinition form = (ConfiguredFormDefinition) dialogBuilder.definition().getForm();
79              form.setLabel(metaData.getLabel());
80              dialogBuilder.label(metaData.getLabel());
81          }
82  
83          if (StringUtils.isNotEmpty(metaData.getI18nBasename())) {
84              dialogBuilder.i18nBasename(metaData.getI18nBasename());
85          }
86  
87          if (metaData.getTabOrder() != null) {
88              sortTabs(context.getDialog(), metaData.getTabOrder());
89          }
90      }
91  
92      private void sortTabs(FormDialogDefinition dialog, final String[] order) {
93          Collections.sort(dialog.getForm().getTabs(), new Comparator<Object>() {
94  
95              @Override
96              public int compare(Object o1, Object o2) {
97                  switch (((o1 instanceof TabDefinition) ? 2 : 0) + ((o2 instanceof TabDefinition) ? 1 : 0)) {
98                      case 0:
99                          return 0;
100                     case 1:
101                         return -1;
102                     case 2:
103                         return 1;
104                     case 3:
105                         return ArrayUtils.indexOf(order, ((TabDefinition) o1).getLabel()) - ArrayUtils.indexOf(order, ((TabDefinition) o2).getLabel());
106                 }
107                 return 0; // Will never happen
108             }
109         });
110     }
111 
112     private void invokeTabFactories(final DialogFactoryMetaData metaData, final DialogCreationContext context, DialogBuilder dialogBuilder) {
113 
114         for (Method tabFactory : metaData.getTabFactories()) {
115 
116             TabFactory annotation = tabFactory.getAnnotation(TabFactory.class);
117             TabBuilder tabBuilder = new TabBuilder(annotation.value()).label(annotation.value());
118             dialogBuilder.form().tabs(tabBuilder);
119 
120             ParameterResolver parameters = getTabFactoryParameters(metaData, context, tabBuilder);
121             MethodInvocationUtils.invoke(tabFactory, metaData.getFactoryObject(), parameters);
122         }
123     }
124 
125     protected ParameterResolver getDialogFactoryParameters(DialogFactoryMetaData metaData, final DialogCreationContext context, final DialogBuilder dialogBuilder) {
126         return new ParameterResolver() {
127 
128             @Override
129             public Object resolveParameter(Class<?> parameterType) {
130                 if (parameterType.equals(DialogBuilder.class)) {
131                     return dialogBuilder;
132                 }
133                 if (parameterType.isAssignableFrom(ConfiguredDialogDefinition.class)) {
134                     return context.getDialog();
135                 }
136                 if (parameterType.equals(DialogCreationContext.class)) {
137                     return context;
138                 }
139                 if (parameterType.equals(UiConfig.class)) {
140                     return new UiConfig();
141                 }
142                 return super.resolveParameter(parameterType);
143             }
144         };
145     }
146 
147     protected ParameterResolver getTabFactoryParameters(DialogFactoryMetaData metaData, final DialogCreationContext context, final TabBuilder tabBuilder) {
148         return new ParameterResolver() {
149 
150             @Override
151             public Object resolveParameter(Class<?> parameterType) {
152                 if (parameterType.equals(TabBuilder.class)) {
153                     return tabBuilder;
154                 }
155                 if (parameterType.isAssignableFrom(ConfiguredDialogDefinition.class)) {
156                     return context.getDialog();
157                 }
158                 if (parameterType.equals(DialogCreationContext.class)) {
159                     return context;
160                 }
161                 if (parameterType.equals(UiConfig.class)) {
162                     return new UiConfig();
163                 }
164                 return super.resolveParameter(parameterType);
165             }
166         };
167     }
168 }