001: package net.xoetrope.awt;
002:
003: import java.awt.BorderLayout;
004: import java.awt.CardLayout;
005: import java.awt.Color;
006: import java.awt.Component;
007: import java.awt.FlowLayout;
008: import java.awt.event.MouseEvent;
009: import java.awt.event.MouseListener;
010: import java.util.Hashtable;
011: import net.xoetrope.builder.XuiBuilder;
012:
013: /**
014: * A tab panel. The methods are modelled on the JTabbedPane
015: * <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
016: * License: see license.txt
017: * $Revision: 1.10 $
018: */
019: public class XTabPanel extends XPanel implements MouseListener {
020: protected XPanel tabPane;
021: protected XPanel contentPane;
022: protected int selIdx;
023: protected CardLayout cardManager;
024:
025: public XTabPanel() {
026: setLayout(new BorderLayout());
027: tabPane = new XPanel();
028:
029: FlowLayout fl = new FlowLayout();
030: fl.setAlignment(fl.LEFT);
031: fl.setHgap(1);
032: fl.setVgap(0);
033: tabPane.setLayout(fl);
034: add(tabPane, BorderLayout.NORTH);
035:
036: contentPane = new XPanel();
037: contentPane.setLayout(cardManager = new CardLayout());
038: add(contentPane, BorderLayout.CENTER);
039:
040: selIdx = 0;
041: }
042:
043: /**
044: * Add a new tab.
045: * The add method could not be overloaded so this method adds does the
046: * equivalent.
047: * @param name the name/caption of the tab component
048: * @param panel the content
049: */
050: public Component add(Component comp) {
051: String title = null;
052: try {
053: Hashtable attribs = XuiBuilder.getCurrentAttributes();
054: title = (String) attribs.get("title");
055: } catch (Exception ex) {
056: }
057: addTab(title, comp);
058: return comp;
059: }
060:
061: /**
062: * Add a new tab.
063: * The add method could not be overloaded so this method adds does the
064: * equivalent.
065: * @param name the name/caption of the tab component
066: * @param panel the content
067: */
068: public void addTab(String name, Component panel) {
069: XTab tab = new XTab(name);
070: tabPane.add(tab);
071: contentPane.add(panel, name);
072: tab.addMouseListener(this );
073: }
074:
075: public Component getComponentAt(int index) {
076: return contentPane.getComponent(index);
077: }
078:
079: public Color getBackgroundAt(int index) {
080: return tabPane.getComponent(index).getBackground();
081: }
082:
083: public Color getForegroundAt(int index) {
084: return tabPane.getComponent(index).getForeground();
085: }
086:
087: public int getTabCount() {
088: return tabPane.getComponentCount();
089: }
090:
091: public int getSelectedIndex() {
092: return selIdx;
093: }
094:
095: public String getTitleAt(int index) {
096: return ((XTab) tabPane.getComponent(index)).getText();
097: }
098:
099: public void setBackgroundAt(int index, Color clr) {
100: ((XTab) tabPane.getComponent(index)).setBackground(clr);
101: }
102:
103: public void setForegroundAt(int index, Color clr) {
104: ((XTab) tabPane.getComponent(index)).setForeground(clr);
105: }
106:
107: public void setSelectedIndex(int index) {
108: if (index != selIdx) {
109: XTab oldSelection = (XTab) tabPane.getComponent(selIdx);
110: oldSelection.setSelected(false);
111: oldSelection.repaint();
112: }
113:
114: selIdx = index;
115: XTab newSelection = (XTab) tabPane.getComponent(selIdx);
116: newSelection.setSelected(true);
117: newSelection.repaint();
118: cardManager.show(contentPane, ((XTab) tabPane
119: .getComponent(selIdx)).getText());
120: repaint();
121: }
122:
123: public void setTitleAt(int index, String str) {
124: ((XTab) tabPane.getComponent(index)).setText(str);
125: }
126:
127: /**
128: * Toggle the frame display. The frame is drawn around the content pane
129: * @param val 0 for no frame, 1 for a bevel, 2 for a flat frame
130: */
131: public void setDrawFrame(int value) {
132: int gap = Math.min(value, 2);
133: cardManager.setHgap(gap);
134: cardManager.setVgap(gap);
135: contentPane.setDrawFrame(value);
136: }
137:
138: //-Mouse listener methods-----------------------------------------------------
139: /**
140: * Invoked when the mouse button has been clicked (pressed
141: * and released) on a component.
142: */
143: public void mouseClicked(MouseEvent e) {
144: }
145:
146: /**
147: * Invoked when a mouse button has been pressed on a component.
148: */
149: public void mousePressed(MouseEvent e) {
150: XTab oldSelection = null;
151: XTab comp = (XTab) e.getSource();
152:
153: int numComponents = tabPane.getComponentCount();
154: for (int i = 0; i < numComponents; i++) {
155: XTab tabObj = (XTab) tabPane.getComponent(i);
156: if (tabObj.selected) {
157: oldSelection = (XTab) tabPane.getComponent(i);
158: oldSelection.setSelected(false);
159: oldSelection.repaint();
160: } else if (comp == tabObj)
161: selIdx = i;
162: }
163:
164: cardManager.show(contentPane, comp.getText());
165: comp.setSelected(true);
166: comp.repaint();
167: }
168:
169: /**
170: * Invoked when a mouse button has been released on a component.
171: */
172: public void mouseReleased(MouseEvent e) {
173: }
174:
175: /**
176: * Invoked when the mouse enters a component.
177: */
178: public void mouseEntered(MouseEvent e) {
179: }
180:
181: /**
182: * Invoked when the mouse exits a component.
183: */
184: public void mouseExited(MouseEvent e) {
185: }
186: }
|