View Javadoc

1   /**
2    * This file Copyright (c) 2012-2014 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.ui.actionbar;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.objectfactory.Components;
38  import info.magnolia.ui.actionbar.builder.ActionbarFactory;
39  import info.magnolia.ui.actionbar.definition.ActionbarDefinition;
40  import info.magnolia.ui.vaadin.actionbar.Actionbar;
41  import info.magnolia.ui.vaadin.actionbar.ActionbarView;
42  
43  import javax.inject.Inject;
44  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import com.vaadin.server.Resource;
49  
50  /**
51   * Default presenter for an action bar.
52   */
53  public class ActionbarPresenter implements ActionbarView.Listener {
54  
55      /**
56       * Listener interface for the Actionbar.
57       */
58      public interface Listener {
59  
60          void onActionbarItemClicked(String itemName);
61  
62          String getLabel(String itemName);
63  
64          String getIcon(String itemName);
65  
66      }
67  
68      private static final Logger log = LoggerFactory.getLogger(ActionbarPresenter.class);
69  
70      private static final String PREVIEW_SECTION_NAME = "preview";
71  
72      private ActionbarDefinition definition;
73  
74      private ActionbarView actionbar;
75  
76      private Listener listener;
77  
78      private SimpleTranslator translator;
79  
80      @Inject
81      public ActionbarPresenter(SimpleTranslator translator) {
82          this.translator = translator;
83      }
84  
85      /**
86       * @deprecated since 5.2.1 - please use {@link #ActionbarPresenter(SimpleTranslator)}.
87       */
88      public ActionbarPresenter() {
89          this.translator = Components.getComponent(SimpleTranslator.class);
90      }
91  
92      public void setListener(Listener listener) {
93          this.listener = listener;
94      }
95  
96      /**
97       * Initializes an actionbar with given definition and returns the view for
98       * parent to add it.
99       */
100     public ActionbarView start(final ActionbarDefinition definition) {
101         this.definition = definition;
102         actionbar = ActionbarFactory.build(definition, listener);
103         actionbar.setListener(this);
104         return actionbar;
105     }
106 
107     public void setPreview(final Resource previewResource) {
108         if (previewResource != null) {
109             if (!((Actionbar) actionbar).getSections().containsKey(PREVIEW_SECTION_NAME)) {
110                 actionbar.addSection(PREVIEW_SECTION_NAME, translator.translate("actionbar.preview"));
111             }
112             actionbar.setSectionPreview(previewResource, PREVIEW_SECTION_NAME);
113         } else {
114             if (((Actionbar) actionbar).getSections().containsKey(PREVIEW_SECTION_NAME)) {
115                 actionbar.removeSection(PREVIEW_SECTION_NAME);
116             }
117         }
118     }
119 
120     // METHODS DELEGATING TO THE VIEW
121 
122     public void enable(String... actionNames) {
123         if (actionbar != null) {
124             for (String action : actionNames) {
125                 actionbar.setActionEnabled(action, true);
126             }
127         }
128     }
129 
130     public void disable(String... actionNames) {
131         if (actionbar != null) {
132             for (String action : actionNames) {
133                 actionbar.setActionEnabled(action, false);
134             }
135         }
136     }
137 
138     public void enableGroup(String groupName) {
139         if (actionbar != null) {
140             actionbar.setGroupEnabled(groupName, true);
141         }
142     }
143 
144     public void disableGroup(String groupName) {
145         if (actionbar != null) {
146             actionbar.setGroupEnabled(groupName, false);
147         }
148     }
149 
150     public void enableGroup(String groupName, String sectionName) {
151         if (actionbar != null) {
152             actionbar.setGroupEnabled(groupName, sectionName, true);
153         }
154     }
155 
156     public void disableGroup(String groupName, String sectionName) {
157         if (actionbar != null) {
158             actionbar.setGroupEnabled(groupName, sectionName, false);
159         }
160     }
161 
162     public void showSection(String... sectionNames) {
163         if (actionbar != null) {
164             for (String section : sectionNames) {
165                 actionbar.setSectionVisible(section, true);
166             }
167         }
168     }
169 
170     public void hideSection(String... sectionNames) {
171         if (actionbar != null) {
172             for (String section : sectionNames) {
173                 actionbar.setSectionVisible(section, false);
174             }
175         }
176     }
177 
178     // VIEW LISTENER
179 
180     @Override
181     public void onActionbarItemClicked(String actionToken) {
182         String actionName = getActionName(actionToken);
183         listener.onActionbarItemClicked(actionName);
184     }
185 
186     private String getActionName(String actionToken) {
187         final String[] chunks = actionToken.split(":");
188         if (chunks.length != 2) {
189             log.warn("Invalid actionToken [{}]: it is expected to be in the form sectionName:actionName. Action name cannot be resolved. Please check actionbar definition.", actionToken);
190             return null;
191         }
192         final String sectionName = chunks[0];
193         final String actionName = chunks[1];
194 
195         return actionName;
196     }
197 }