View Javadoc

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