| TDIViewLayout.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: TDIViewLayout.java 2085 2011-03-03 03:41:41Z dmsmith $
21 */
22 package org.crosswire.common.swing.desktop;
23
24 import java.awt.Component;
25 import java.awt.Dimension;
26 import java.awt.event.MouseListener;
27
28 import javax.swing.JPopupMenu;
29 import javax.swing.JTabbedPane;
30 import javax.swing.UIManager;
31 import javax.swing.plaf.TabbedPaneUI;
32
33 import org.crosswire.common.swing.GuiUtil;
34
35 /**
36 * TDI (Tabbed Document Interface) manager of how we layout views as tabs.
37 *
38 * @see gnu.lgpl.License for license details.<br>
39 * The copyright to this program is held by it's authors.
40 * @author Joe Walker [joe at eireneh dot com]
41 * @author DM Smith [dmsmith555 at yahoo dot com]
42 */
43 public class TDIViewLayout extends AbstractViewLayout {
44 /**
45 * Build a TDI layout
46 */
47 public TDIViewLayout() {
48 super();
49 tabs = new JTabbedPane();
50 if (UIManager.getDefaults().containsKey("BibleViewPane.TabbedPaneUI")) {
51 tabs.setUI((TabbedPaneUI) UIManager.get("BibleViewPane.TabbedPaneUI"));
52 }
53 tabs.setMinimumSize(new Dimension(0, 0));
54 GuiUtil.applyDefaultOrientation(tabs);
55 }
56
57 /*
58 * (non-Javadoc)
59 *
60 * @see
61 * org.crosswire.common.swing.desktop.AbstractViewLayout#addView(java.awt
62 * .Component)
63 */
64 @Override
65 public void addView(Component component) {
66 int viewCount = getViewCount();
67
68 if (viewCount > 0) {
69 if (viewCount == 1) {
70 Component first = getView(0);
71 getPanel().remove(first);
72 tabs.add(first, getTitle(first));
73 getPanel().add(tabs, getConstraint());
74 }
75
76 tabs.add(component, getTitle(component));
77 tabs.setSelectedComponent(component);
78 } else {
79 getPanel().add(component, getConstraint());
80 }
81
82 super.addView(component);
83 }
84
85 /*
86 * (non-Javadoc)
87 *
88 * @see
89 * org.crosswire.common.swing.desktop.AbstractViewLayout#removeView(java
90 * .awt.Component)
91 */
92 @Override
93 public void removeView(Component component) {
94 int viewCount = getViewCount();
95
96 if (viewCount == 1) {
97 if (component instanceof Clearable) {
98 ((Clearable) component).clear();
99 }
100 return;
101 }
102
103 tabs.remove(component);
104
105 // There were two tabs and now there is one
106 // We migrate from tabs to just the component
107 if (viewCount == 2) {
108 Component remaining = tabs.getComponentAt(0);
109 // remove both tabs, because 0 will be reparented
110 tabs.removeTabAt(0);
111 getPanel().remove(tabs);
112 getPanel().add(remaining, getConstraint());
113 }
114
115 super.removeView(component);
116 }
117
118 /*
119 * (non-Javadoc)
120 *
121 * @see
122 * org.crosswire.common.swing.desktop.AbstractViewLayout#forceRemoveView
123 * (java.awt.Component)
124 */
125 @Override
126 protected void forceRemoveView(Component component) {
127 int viewCount = getViewCount();
128
129 if (viewCount == 1) {
130 getPanel().remove(component);
131 } else {
132 tabs.remove(component);
133
134 // There were two tabs and now there is one
135 // We migrate from tabs to just the component
136 if (viewCount == 2) {
137 Component remaining = tabs.getComponentAt(0);
138 // remove both tabs, because 0 will be reparented
139 tabs.removeTabAt(0);
140 getPanel().remove(tabs);
141 getPanel().add(remaining, getConstraint());
142 }
143 }
144 super.forceRemoveView(component);
145 }
146
147 /*
148 * (non-Javadoc)
149 *
150 * @seeorg.crosswire.bibledesktop.desktop.ViewLayout#update(org.crosswire.
151 * bibledesktop.book.BibleViewPane)
152 */
153 @Override
154 public void updateTitle(Component component) {
155 if (getViewCount() > 1) {
156 int index = tabs.indexOfComponent(component);
157 tabs.setTitleAt(index, getTitle(component));
158 }
159 }
160
161 /*
162 * (non-Javadoc)
163 *
164 * @see org.crosswire.bibledesktop.desktop.ViewLayout#getSelected()
165 */
166 @Override
167 public Component getSelected() {
168 if (getViewCount() == 1) {
169 return getView(0);
170 }
171 return tabs.getSelectedComponent();
172 }
173
174 /*
175 * (non-Javadoc)
176 *
177 * @see
178 * org.crosswire.common.swing.desktop.AbstractViewLayout#select(java.awt
179 * .Component)
180 */
181 @Override
182 public void select(Component component) {
183 // If we don't have tabs then it is selected.
184 if (getViewCount() > 1) {
185 tabs.setSelectedComponent(component);
186 }
187 }
188
189 /**
190 * Bind a popup to the tabbed page
191 *
192 * @param popup
193 */
194 public void addPopup(JPopupMenu popup) {
195 MouseListener ml = new TabPopupListener(tabs, popup);
196 tabs.addMouseListener(ml);
197 }
198
199 /**
200 * The tabbed view pane
201 */
202 private JTabbedPane tabs;
203 }
204