View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 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         }
136     }
137 
138     public MagnoliaTab getActiveTab() {
139         return (MagnoliaTab) getState(false).activeTab;
140     }
141 
142     public MagnoliaTab getNextTab(final MagnoliaTab tab) {
143         return CollectionUtil.getNext(tabs, tab);
144     }
145 
146     @Override
147     public void removeComponent(final Component c) {
148         if (c instanceof MagnoliaTab) {
149             final MagnoliaTab tab = (MagnoliaTab) c;
150             super.removeComponent(c);
151             tabs.remove(tab);
152             markAsDirty();
153         }
154     }
155 
156     public void setActiveTab(final MagnoliaTab tab) {
157         getState().activeTab = tab;
158         updateTabContentVisibility();
159     }
160 
161     private void updateTabContentVisibility() {
162         Iterator<Component> it = iterator();
163         while (it.hasNext()) {
164             MagnoliaTab tabIt = (MagnoliaTab) it.next();
165             if (tabIt.getContent() != null) {
166                 tabIt.getContent().setVisible(tabIt == getState().activeTab);
167             }
168         }
169     }
170 
171     @Override
172     public void replaceComponent(Component oldComponent, Component newComponent) {
173     }
174 
175 
176     @Override
177     public int getComponentCount() {
178         return tabs.size();
179     }
180 
181     @Override
182     public Iterator<Component> iterator() {
183         return new ComponentIterator<MagnoliaTab>(tabs.iterator());
184     }
185 
186     /**
187      * Send a rpc call to the client, which will remove the tab related views and call back to the server.
188      * @see #closeTab(MagnoliaTab)
189      */
190     public void closeTabFromServer(MagnoliaTab tab) {
191         getRpcProxy(MagnoliaTabSheetClientRpc.class).closeTab(tab);
192     }
193 
194     public void setLogo(String logo, String bgcolor) {
195         getState().logo = logo;
196         getState().logoBgColor = bgcolor;
197     }
198 
199     public void setName(String name) {
200         getState().name = name;
201     }
202 }