summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/MorphingTabPanel.java
blob: 8c6fe071988456a2ed847e93b05fb5077c7a83e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (C) 2011 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.client.ui;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;

import java.util.ArrayList;
import java.util.List;

/** A TabPanel which allows entries to be hidden.  This class is not yet
 *  designed to handle removes or any other add methods than the one
 *  overridden here.  It is also not designed to handle anything other
 *  than text for the tab.
 */
public class MorphingTabPanel extends TabPanel {
  // Keep track of the order the widgets/texts should be in when not hidden.
  private List<Widget> widgets = new ArrayList<Widget>();
  private List<String> texts = new ArrayList<String>();

  // currently visible widgets
  private List<Widget> visibles = new ArrayList<Widget>();

  private int selection;

  public MorphingTabPanel() {
    addSelectionHandler(new SelectionHandler<Integer>() {
        @Override
        public void onSelection(SelectionEvent<Integer> ev) {
          selection = ev.getSelectedItem();
        }
      });
  }

  public int getSelectedIndex() {
    return selection;
  }

  public Widget getSelectedWidget() {
    return getWidget(getSelectedIndex());
  }

  @Override
  public void clear() {
    super.clear();
    widgets.clear();
    texts.clear();
    visibles.clear();
  }

  @Override
  public void add(Widget w, String tabText) {
    addInvisible(w, tabText);
    visibles.add(w);
    super.add(w, tabText);
  }

  public void addInvisible(Widget w, String tabText) {
    widgets.add(w);
    texts.add(tabText);
  }

  public void setVisible(Widget w, boolean visible) {
    if (visible) {
      if (visibles.indexOf(w) == -1) {
        int origPos = widgets.indexOf(w);

        /* Re-insert the widget right after the first visible widget found
           when scanning backwards from the current widget */
        for (int pos = origPos -1; pos >=0 ; pos--) {
          int visiblePos = visibles.indexOf(widgets.get(pos));
          if (visiblePos != -1) {
            visibles.add(visiblePos + 1, w);
            insert(w, texts.get(origPos), visiblePos + 1);
            break;
          }
        }
      }
    } else {
      int i = visibles.indexOf(w);
      if (i != -1) {
        visibles.remove(i);
        remove(i);
      }
    }
  }
}