View Javadoc

1   /**
2    * This file Copyright (c) 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.googlesitemap.app.subapp.sitemapdetail;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.base.SiteMapContentView;
39  import info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.base.SiteMapDetailPresenter;
40  import info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.event.EntrySelectedEvent;
41  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
42  import info.magnolia.objectfactory.ComponentProvider;
43  import info.magnolia.ui.actionbar.ActionbarPresenter;
44  import info.magnolia.ui.api.action.ActionDefinition;
45  import info.magnolia.ui.api.action.ActionExecutionException;
46  import info.magnolia.ui.api.app.SubAppContext;
47  import info.magnolia.ui.api.app.SubAppEventBus;
48  import info.magnolia.ui.api.event.AdmincentralEventBus;
49  import info.magnolia.ui.api.event.ContentChangedEvent;
50  import info.magnolia.ui.contentapp.detail.DetailLocation;
51  import info.magnolia.ui.framework.app.SubAppActionExecutor;
52  import info.magnolia.ui.vaadin.actionbar.ActionbarView;
53  import info.magnolia.ui.vaadin.integration.NullItem;
54  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
55  
56  import javax.inject.Inject;
57  import javax.inject.Named;
58  import javax.jcr.Node;
59  import javax.jcr.RepositoryException;
60  
61  /**
62   * Presenter class, serves as a delegate for {@link SiteMapEditorSubApp}.
63   * Manages {@link SiteMapEditorView}.
64   */
65  public class SiteMapEditorPresenter implements EntrySelectedEvent.Handler, ActionbarPresenter.Listener {
66  
67      public static final String EDIT_ACTIONS_GROUP = "editActions";
68  
69      private SiteMapEditorView view;
70  
71      private SiteMapDetailPresenter contentPresenter;
72  
73      private ActionbarPresenter actionbarPresenter;
74  
75      private SubAppContext appContext;
76  
77      private EventBus eventBus;
78  
79      private EventBus adminCentralEventBus;
80  
81      private SubAppActionExecutor executor;
82  
83      private JcrNodeAdapter nodeAdapter;
84  
85      private ComponentProvider provider;
86  
87      private String targetWorkspace;
88  
89      @Inject
90      public SiteMapEditorPresenter(
91          SiteMapEditorView view,
92          ActionbarPresenter actionbarPresenter,
93          SubAppContext appContext,
94          @Named(SubAppEventBus.NAME) EventBus eventBus,
95          @Named(AdmincentralEventBus.NAME) final EventBus adminCentralEventBus,
96          SubAppActionExecutor executor,
97          ComponentProvider provider) {
98          this.view = view;
99          this.actionbarPresenter = actionbarPresenter;
100         this.appContext = appContext;
101         this.eventBus = eventBus;
102         this.adminCentralEventBus = adminCentralEventBus;
103         this.executor = executor;
104         this.provider = provider;
105         this.eventBus.addHandler(EntrySelectedEvent.class, this);
106         this.actionbarPresenter.setListener(this);
107     }
108 
109     public SiteMapEditorView start() {
110         SiteMapEditorSubAppDescriptor descriptor = (SiteMapEditorSubAppDescriptor) appContext.getSubAppDescriptor();
111         this.targetWorkspace = descriptor.getTargetWorkspace();
112         this.contentPresenter = provider.newInstance(descriptor.getPresenterClass());
113 
114         ActionbarView actionbarView = actionbarPresenter.start(descriptor.getActionbar());
115         view.setActionbarView(actionbarView);
116 
117         SiteMapContentView contentView = contentPresenter.start();
118         view.setContentView(contentView);
119 
120         if (contentPresenter.getCurrentSelection() == null) {
121             actionbarPresenter.disableGroup(EDIT_ACTIONS_GROUP);
122         }
123 
124         adminCentralEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
125             @Override
126             public void onContentChanged(ContentChangedEvent event) {
127                 if (targetWorkspace.equals(event.getWorkspace())) {
128                     contentPresenter.edit(nodeAdapter.getJcrItem());
129                 }
130             }
131         });
132         return view;
133     }
134 
135     public void setSiteMapNode(JcrNodeAdapter nodeAdapter) {
136         this.nodeAdapter = nodeAdapter;
137         contentPresenter.edit(nodeAdapter.getJcrItem());
138     }
139 
140     @Override
141     public void onEntriesSelected(SiteMapEntry entry) {
142         if (entry == null) {
143             actionbarPresenter.disableGroup(EDIT_ACTIONS_GROUP);
144         } else {
145             actionbarPresenter.enableGroup(EDIT_ACTIONS_GROUP);
146         }
147     }
148 
149     @Override
150     public void onActionbarItemClicked(String itemName) {
151         try {
152             SiteMapEntry entry = contentPresenter.getCurrentSelection();
153             if (entry != null) {
154                 Node pageNode = MgnlContext.getJCRSession(targetWorkspace).getNode(entry.getPath());
155                 executor.execute(itemName, new JcrNodeAdapter(pageNode));
156             } else {
157                 executor.execute(itemName, new NullItem());
158             }
159         } catch (ActionExecutionException e) {
160             e.printStackTrace();
161         } catch (RepositoryException e) {
162             e.printStackTrace();
163         }
164     }
165 
166     @Override
167     public String getLabel(String itemName) {
168         ActionDefinition actionDefinition = executor.getActionDefinition(itemName);
169         return actionDefinition != null ? actionDefinition.getLabel() : null;
170     }
171 
172     @Override
173     public String getIcon(String itemName) {
174         try {
175             ActionDefinition actionDefinition = executor.getActionDefinition(itemName);
176             return actionDefinition != null ? actionDefinition.getIcon() : null;
177         } catch (Exception e) {
178             e.printStackTrace();
179             return null;
180         }
181     }
182 
183     public String getCaptionForLocation(DetailLocation location) {
184         return contentPresenter.getLabel() + "-" + location.getNodePath();
185     }
186 }