001: package org.osbl.client.wings;
002:
003: import org.wings.*;
004: import org.wings.event.*;
005: import org.osbl.client.wings.shell.*;
006:
007: import java.awt.*;
008: import java.util.Map;
009: import java.util.HashMap;
010:
011: /**
012: * @author hengels
013: * @version $Revision$
014: */
015: public class XTabbedPane extends STabbedPane {
016: private final static String SVG_BOTTOM = "svg/tabbed_pane_bottom.svg";
017: private final static String SVG_BOTTOM_SELECTED = "svg/tabbed_pane_bottom_selected.svg";
018: private final static String SVG_LEFT = "svg/tabbed_pane_left.svg";
019: private final static String SVG_LEFT_SELECTED = "svg/tabbed_pane_left_selected.svg";
020: private final static String SVG_RIGHT = "svg/tabbed_pane_right.svg";
021: private final static String SVG_RIGHT_SELECTED = "svg/tabbed_pane_right_selected.svg";
022: private final static String SVG_TOP = "svg/tabbed_pane_top.svg";
023: private final static String SVG_TOP_SELECTED = "svg/tabbed_pane_top_selected.svg";
024:
025: private Map<String, SIcon> icons = new HashMap<String, SIcon>();
026: private boolean first = true;
027:
028: public XTabbedPane() {
029: setPreferredSize(SDimension.FULLAREA);
030: setStyle("XTabbedPane");
031: }
032:
033: public XTabbedPane(int tabPlacement) {
034: super (tabPlacement);
035: setPreferredSize(SDimension.FULLAREA);
036: setStyle("XTabbedPane");
037: }
038:
039: public void addTab(SComponent component) {
040: addTab(null, component);
041: }
042:
043: public void addTab(String code, SComponent component) {
044: int tabCount = getTabCount();
045: String title = code != null ? Client.getInstance()
046: .getResourceProvider().getMessage(code) : "Tab "
047: + (tabCount + 1);
048: String tip = Client.getInstance().getResourceProvider()
049: .getMessage(code + ".help");
050: super .addTab(title, null, component, tip);
051:
052: component.setPreferredSize(SDimension.FULLAREA);
053: component.addComponentListener(new SComponentAdapter() {
054: public void componentShown(SComponentEvent e) {
055: SComponent component = e.getComponent();
056: if (component instanceof LazyTab
057: && component.getClientProperty("initialized") == null) {
058: ((LazyTab) component).initTab();
059: component.putClientProperty("initialized",
060: Boolean.TRUE);
061: }
062: }
063: });
064: if (getTabCount() == 1 && component instanceof LazyTab) {
065: ((LazyTab) component).initTab();
066: }
067: }
068:
069: public SIcon getIconAt(int i) {
070: String title = super .getTitleAt(i);
071: if (title == null)
072: return super .getIconAt(i);
073:
074: String resource = null;
075: Dimension dimension = null;
076: boolean selected = i == getSelectedIndex();
077: String key = title + (selected ? "_selected" : "_deselected");
078:
079: SIcon icon = icons.get(key);
080: if (icon == null) {
081: switch (getTabPlacement()) {
082: case SConstants.TOP:
083: resource = selected ? XTabbedPane.SVG_TOP_SELECTED
084: : XTabbedPane.SVG_TOP;
085: dimension = SVGButtonRenderer.getInstance()
086: .calculateButtonImageSize(super .getTitleAt(i));
087: // use fixed width
088: //dimension = new Dimension(120, 22);
089: break;
090: case SConstants.LEFT:
091: resource = selected ? XTabbedPane.SVG_LEFT_SELECTED
092: : XTabbedPane.SVG_LEFT;
093: dimension = SVGButtonRenderer.getInstance()
094: .calculateButtonImageSize(title);
095: dimension = new Dimension(22, dimension.width + 10);
096: break;
097: case SConstants.RIGHT:
098: resource = selected ? XTabbedPane.SVG_RIGHT_SELECTED
099: : XTabbedPane.SVG_RIGHT;
100: dimension = SVGButtonRenderer.getInstance()
101: .calculateButtonImageSize(title);
102: dimension = new Dimension(22, dimension.width + 10);
103: break;
104: case SConstants.BOTTOM:
105: resource = selected ? XTabbedPane.SVG_BOTTOM_SELECTED
106: : XTabbedPane.SVG_BOTTOM;
107: dimension = SVGButtonRenderer.getInstance()
108: .calculateButtonImageSize(title);
109: dimension = new Dimension(dimension.width + 10, 22);
110: break;
111: }
112: icon = SVGButtonRenderer.getInstance()
113: .retrieveRenderedButtonIcon(title, resource,
114: dimension);
115: icons.put(key, icon);
116: }
117: return icon;
118: }
119:
120: public String getTitleAt(int i) {
121: return null;
122: }
123:
124: public void requestFocus() {
125: }
126:
127: public void setEnabledAt(int index, boolean enabled) {
128: super .setEnabledAt(index, enabled);
129: if (!enabled && index == super .getSelectedIndex()) {
130: setSelectedIndex(getNearestEnabledTab(index));
131: }
132: }
133:
134: private int getNearestEnabledTab(int index) {
135: for (int i = index; i > -1; i--) {
136: if (isEnabledAt(i)) {
137: return i;
138: }
139: }
140: return getFirstEnabledTab();
141: }
142:
143: @Override
144: public int getSelectedIndex() {
145: if (first) {
146: int firstSelectedTab = getFirstEnabledTab();
147: if (firstSelectedTab == -1)
148: firstSelectedTab = 0;
149: if (super .getSelectedIndex() == -1)
150: super .setSelectedIndex(firstSelectedTab);
151: first = false;
152: }
153: return super .getSelectedIndex();
154: }
155:
156: public SComponent getSelectedComponent() {
157: int firstSelectedTab = getFirstEnabledTab();
158: if (firstSelectedTab == -1) {
159: // the user has no rights to view any tab
160: SPanel panel = new SPanel();
161: panel.setHorizontalAlignment(SConstants.CENTER);
162: panel.setVerticalAlignment(SConstants.CENTER);
163: panel
164: .add(new SLabel(
165: Client
166: .getInstance()
167: .getResourceProvider()
168: .getMessage(
169: "org.osbl.client.wings.XTabbedPane.insufficient_rights")));
170: return panel;
171: }
172: return super .getSelectedComponent();
173: }
174:
175: private int getFirstEnabledTab() {
176: for (int i = 0; i < getTabCount(); i++) {
177: if (isEnabledAt(i)) {
178: return i;
179: }
180: }
181: return -1;
182: }
183: }
|