View Javadoc
1   /**
2    * This file Copyright (c) 2010-2017 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.vaadin.tabsheet;
35  
36  import info.magnolia.ui.vaadin.common.ComponentIterator;
37  import info.magnolia.ui.vaadin.gwt.client.tabsheet.connector.MagnoliaTabSheetState;
38  import info.magnolia.ui.vaadin.gwt.client.tabsheet.rpc.MagnoliaTabSheetClientRpc;
39  import info.magnolia.ui.vaadin.gwt.client.tabsheet.rpc.MagnoliaTabSheetServerRpc;
40  import info.magnolia.ui.vaadin.gwt.client.tabsheet.util.CollectionUtil;
41  
42  import java.util.Iterator;
43  import java.util.LinkedList;
44  import java.util.List;
45  
46  import com.vaadin.shared.Connector;
47  import com.vaadin.ui.AbstractComponentContainer;
48  import com.vaadin.ui.Component;
49  
50  /**
51   * Simple lightweight tabsheet component.
52   */
53  public class MagnoliaTabSheet extends AbstractComponentContainer {
54  
55      private final List<MagnoliaTab> tabs = new LinkedList<MagnoliaTab>();
56  
57      public MagnoliaTabSheet() {
58          super();
59          setImmediate(true);
60          registerRpc(new MagnoliaTabSheetServerRpc() {
61              @Override
62              public void setActiveTab(Connector tabConnector) {
63                  MagnoliaTabSheet.this.setActiveTab((MagnoliaTab) tabConnector);
64              }
65  
66              @Override
67              public void closeTab(Connector tabConnector) {
68                  MagnoliaTabSheet.this.closeTab((MagnoliaTab) tabConnector);
69              }
70  
71              @Override
72              public void setShowAll() {
73                  MagnoliaTabSheet.this.showAll();
74              }
75          });
76      }
77  
78      protected void showAll() {
79          Iterator<Component> it = iterator();
80          while (it.hasNext()) {
81              MagnoliaTab tabIt = (MagnoliaTab) it.next();
82              if (tabIt.getContent() != null) {
83                  tabIt.getContent().setVisible(true);
84              }
85          }
86          getState().activeTab = null;
87          getState().showAllEnabled = true;
88      }
89  
90      @Override
91      public void addComponent(final Component c) {
92          if (c instanceof MagnoliaTab) {
93              doAddTab((MagnoliaTab) c);
94          } else {
95              addTab("", c);
96          }
97      }
98  
99      public MagnoliaTab addTab(final String caption, final Component c) {
100         final MagnoliaTab tab = new MagnoliaTab(caption, c);
101         doAddTab(tab);
102         return tab;
103     }
104 
105     @Override
106     protected MagnoliaTabSheetState getState() {
107         return (MagnoliaTabSheetState) super.getState();
108     }
109 
110     @Override
111     protected MagnoliaTabSheetState getState(boolean markDirty) {
112         return (MagnoliaTabSheetState) super.getState(markDirty);
113     }
114 
115     public void showAllTab(boolean showAll, String label) {
116         getState().showAllEnabled = showAll;
117         getState().showAllLabel = label;
118     }
119 
120     protected void closeTab(MagnoliaTab tab) {
121         if (getState().activeTab == tab) {
122             final MagnoliaTab nextTab = getNextTab(tab);
123             if (nextTab != null && nextTab != tab) {
124                 setActiveTab(nextTab);
125             }
126         }
127         removeComponent(tab);
128     }
129 
130     protected void doAddTab(final MagnoliaTab tab) {
131         super.addComponent(tab);
132         tabs.add(tab);
133         if (getState().activeTab == null) {
134             setActiveTab(tab);
135         } else {
136             updateTabContentVisibility();
137         }
138     }
139 
140     public MagnoliaTab getActiveTab() {
141         return (MagnoliaTab) getState(false).activeTab;
142     }
143 
144     public MagnoliaTab getNextTab(final MagnoliaTab tab) {
145         return CollectionUtil.getNext(tabs, tab);
146     }
147 
148     @Override
149     public void removeComponent(final Component c) {
150         if (c instanceof MagnoliaTab) {
151             final MagnoliaTab tab = (MagnoliaTab) c;
152             super.removeComponent(c);
153             tabs.remove(tab);
154             markAsDirty();
155         }
156     }
157 
158     public void setActiveTab(final MagnoliaTab tab) {
159         getState().activeTab = tab;
160         updateTabContentVisibility();
161     }
162 
163     private void updateTabContentVisibility() {
164         Iterator<Component> it = iterator();
165         while (it.hasNext()) {
166             MagnoliaTab tabIt = (MagnoliaTab) it.next();
167             if (tabIt.getContent() != null) {
168                 tabIt.getContent().setVisible(tabIt == getState().activeTab);
169             }
170         }
171     }
172 
173     @Override
174     public void replaceComponent(Component oldComponent, Component newComponent) {
175     }
176 
177 
178     @Override
179     public int getComponentCount() {
180         return tabs.size();
181     }
182 
183     @Override
184     public Iterator<Component> iterator() {
185         return new ComponentIterator<MagnoliaTab>(tabs.iterator());
186     }
187 
188     /**
189      * Send a rpc call to the client, which will remove the tab related views and call back to the server.
190      * @see #closeTab(MagnoliaTab)
191      */
192     public void closeTabFromServer(MagnoliaTab tab) {
193         getRpcProxy(MagnoliaTabSheetClientRpc.class).closeTab(tab);
194     }
195 
196     public void setLogo(String logo, String bgcolor) {
197         getState().logo = logo;
198         getState().logoBgColor = bgcolor;
199     }
200 
201     public void setName(String name) {
202         getState().name = name;
203     }
204 }