View Javadoc
1   /**
2    * This file Copyright (c) 2018 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.admincentral.apps.messages.view.detail;
35  
36  import info.magnolia.admincentral.apps.messages.view.column.MessageTypeValueProvider;
37  import info.magnolia.icons.MagnoliaIcons;
38  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
39  import info.magnolia.ui.api.app.SubAppContext;
40  import info.magnolia.ui.api.app.SubAppDescriptor;
41  import info.magnolia.ui.api.location.Location;
42  import info.magnolia.ui.api.message.Message;
43  import info.magnolia.ui.contentapp.browser.actionbar.ActionbarView;
44  import info.magnolia.ui.contentapp.browser.context.ActionConfigurationContext;
45  import info.magnolia.ui.contentapp.browser.context.ValueContext;
46  import info.magnolia.ui.contentapp.detail.ContentDetailSubApp;
47  import info.magnolia.ui.framework.ViewProvider;
48  import info.magnolia.ui.framework.app.BaseSubApp;
49  import info.magnolia.ui.framework.databinding.ItemProviderStrategy;
50  import info.magnolia.ui.framework.databinding.view.EditorView;
51  import info.magnolia.ui.framework.databinding.view.FormView;
52  import info.magnolia.ui.framework.datasource.components.SelectedItems;
53  import info.magnolia.ui.framework.message.MessagesManager;
54  
55  import java.util.Locale;
56  import java.util.Optional;
57  
58  import javax.inject.Inject;
59  
60  /**
61   * Getting closer to what could be a generic MessageDetailSubApp.
62   * @param <T>
63   */
64  public class MessageDetailSubApp<T> extends BaseSubApp<MessageFormView> {
65  
66      private final MessageDetailDescriptor<T, ?> subAppDescriptor;
67      private final I18NAuthoringSupport i18NAuthoringSupport;
68      private final MessageTypeValueProvider messageTypeValueProvider;
69      private ContentDetailSubApp.LocationContext locationContext;
70      private ItemProviderStrategy<T> itemProviderStrategy;
71      private ValueContext<T> valueContext;
72      private MessagesManager messagesManager;
73  
74      @Inject
75      protected MessageDetailSubApp(SubAppContext subAppContext, ViewProvider viewProvider, I18NAuthoringSupport i18NAuthoringSupport, MessagesManager messagesManager) {
76          super(subAppContext, viewProvider.create(subAppContext.getSubAppDescriptor().getLabel(), MessageFormView.class));
77          this.messagesManager = messagesManager;
78          this.subAppDescriptor = (MessageDetailDescriptor<T, ?>) getSubAppContext().getSubAppDescriptor();
79          this.i18NAuthoringSupport = i18NAuthoringSupport;
80          this.messageTypeValueProvider = new MessageTypeValueProvider();
81      }
82  
83      @Override
84      public MessageFormView start(Location location) {
85          super.start(location);
86  
87          final MessageFormView view = getView();
88  
89          view.bindContext(ActionConfigurationContext.class).actions().set(subAppDescriptor.getActions());
90          view.bindDatasourceDefinition(subAppDescriptor.getDatasource());
91          valueContext = view.bindContext(ValueContext.class);
92  
93          final ContentDetailSubApp.LocaleContext localeContext = view.bindContext(ContentDetailSubApp.LocaleContext.class);
94          final Locale currentLocale = getSubAppContext().getAuthoringLocale() != null ?
95                  getSubAppContext().getAuthoringLocale() :
96                  i18NAuthoringSupport.getDefaultLocale();
97          localeContext.current().set(currentLocale);
98  
99          this.locationContext = view.bindContext(ContentDetailSubApp.LocationContext.class);
100         this.locationContext.location().set(ContentDetailSubApp.DetailLocation.wrap(location));
101         this.itemProviderStrategy = view.create(subAppDescriptor.getItemProvider(), subAppDescriptor.getDatasource());
102 
103         view.bindInstance(SubAppDescriptor.class, this.subAppDescriptor);
104         view.bindInstance(ItemProviderStrategy.class, itemProviderStrategy);
105 
106         Optional<T> itemProvider = itemProviderStrategy.read();
107         if (itemProvider.isPresent()) {
108             Message message = (Message) itemProvider.get();
109             valueContext.current().set(SelectedItems.of(itemProvider.get()));
110             messagesManager.clearMessage(message.getSender(), message.getId());
111 
112             final FormView<T> form = (FormView<T>) view.getViewProvider().create(subAppDescriptor.getForm(), subAppDescriptor.getDatasource());
113 
114             view.bindInstance(EditorView.class, form);
115             view.setCaption(message.getSubject());
116             subAppDescriptor.setLabel(message.getSubject());
117             messageTypeValueProvider.getIcon(message).ifPresent(this::updateViewAndSubAppDescriptorIcons);
118 
119             view.setSizeFull();
120             view.addStyleName("detail");
121             view.setMargin(false);
122             view.setSpacing(false);
123             view.setActionBar(view.getViewProvider().create("actionbar", ActionbarView.class, subAppDescriptor.getActionbar()));
124             view.setContent(form.asVaadinComponent());
125 
126             locationContext.location().observeNullable(l -> itemProviderStrategy.read().ifPresent(msg -> {
127                 valueContext.current().set(SelectedItems.of(msg));
128                 form.populate(msg);
129                 Message castedMsg = (Message) msg;
130                 view.setCaption(castedMsg.getSubject());
131                 subAppDescriptor.setLabel(castedMsg.getSubject());
132                 messageTypeValueProvider.getIcon(castedMsg).ifPresent(this::updateViewAndSubAppDescriptorIcons);
133             }));
134             localeContext.current().observeNullable(locale -> view.setContent(form.getLayout(locale)));
135         }
136 
137         return view;
138     }
139 
140     private void updateViewAndSubAppDescriptorIcons(MagnoliaIcons icon) {
141         getView().setIcon(icon);
142         subAppDescriptor.setIcon(icon.getCssClass());
143     }
144 
145 
146     @Override
147     public void locationChanged(Location location) {
148         super.locationChanged(location);
149         this.locationContext.location().set(ContentDetailSubApp.DetailLocation.wrap(location));
150     }
151 
152     @Override
153     public void stop() {
154         getView().destroy();
155     }
156 }