001: package com.xoetrope.awt;
002:
003: import java.awt.CardLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.GridLayout;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import java.util.Hashtable;
010: import net.xoetrope.builder.XuiBuilder;
011: import net.xoetrope.awt.XButton;
012: import net.xoetrope.awt.XPanel;
013: import net.xoetrope.xui.helper.XuiUtilities;
014:
015: /**
016: * An outlook style roll-up bar. The methods are modelled on the JTabbedPane
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: * $Revision: 1.3 $
022: */
023: public class XRollupBar extends XPanel implements ActionListener {
024: protected XPanel contentPane;
025: protected int selIdx;
026: protected CardLayout cardManager;
027:
028: /**
029: * Create a new button panel
030: */
031: public XRollupBar() {
032: setLayout(new GridLayout(0, 1));
033:
034: contentPane = new XPanel();
035: contentPane.setLayout(cardManager = new CardLayout());
036: add(contentPane);
037:
038: selIdx = 0;
039: }
040:
041: /**
042: * Add a new button.
043: * The add method could not be overloaded so this method adds does the
044: * equivalent.
045: * @param comp the new button component
046: * @return the new component
047: */
048: public Component add(Component comp) {
049: String title = null;
050: try {
051: Hashtable attribs = XuiBuilder.getCurrentAttributes();
052: title = XuiUtilities.translate(currentProject,
053: (String) attribs.get("title"));
054: } catch (Exception ex) {
055: }
056: addbutton(title, comp);
057: return comp;
058: }
059:
060: /**
061: * Add a new button.
062: * The add method could not be overloaded so this method adds does the
063: * equivalent.
064: * @param name the name/caption of the button component
065: * @param panel the content
066: */
067: public void addbutton(String name, Component panel) {
068: XButton newBtn = new XButton();
069: newBtn.setText(name);
070: add(newBtn);
071: contentPane.add(panel, name);
072: newBtn.addActionListener(this );
073: }
074:
075: /**
076: * Get the button component at the specified index, skips over the expanded panel
077: * @param index the button index
078: * @return the button component
079: */
080: public XButton getButton(int index) {
081: if (index >= (getComponentCount() - 1))
082: return null;
083:
084: Component comp = getComponent(index);
085: if (comp instanceof XPanel)
086: comp = getComponent(++index);
087: return (XButton) comp;
088: }
089:
090: /**
091: * Get a button's background color
092: * @param index the button index
093: * @return the color
094: */
095: public Color getBackgroundAt(int index) {
096: return getButton(index).getBackground();
097: }
098:
099: /**
100: * Get a button's foreground color
101: * @param index the button index
102: * @return the color
103: */
104: public Color getForegroundAt(int index) {
105: return getButton(index).getForeground();
106: }
107:
108: /**
109: * Get the number of buttons
110: * @return the number of buttons
111: */
112: public int getButtonCount() {
113: return getComponentCount() - 1;
114: }
115:
116: /**
117: * Get the index of the selected button
118: * @return the selection index
119: */
120: public int getSelectedIndex() {
121: return selIdx;
122: }
123:
124: /**
125: * Get the title of the button at the specified index
126: * @param index the button index
127: * @return the button text/title
128: */
129: public String getTitleAt(int index) {
130: return getButton(index).getText();
131: }
132:
133: /**
134: * Set the button background color
135: * @param index the button index
136: * @param clr the new colr
137: */
138: public void setBackgroundAt(int index, Color clr) {
139: getButton(index).setBackground(clr);
140: }
141:
142: /**
143: * Set the foreground color
144: * @param index the button index
145: * @param clr the new color
146: */
147: public void setForegroundAt(int index, Color clr) {
148: getComponent(index).setForeground(clr);
149: }
150:
151: /**
152: * Select a button
153: * @param index the button index
154: */
155: public void setSelectedIndex(int index) {
156: selIdx = index;
157: XButton comp = getButton(selIdx);
158: cardManager.show(contentPane, comp.getText());
159: repaint();
160: }
161:
162: /**
163: * Set a button's title
164: * @param index the button index
165: * @param str the text for the button
166: */
167: public void setTitleAt(int index, String str) {
168: XButton btn = getButton(index);
169: String oldText = btn.getText();
170: int numComponents = contentPane.getComponentCount();
171: for (int i = 0; i < numComponents; i++) {
172: if (contentPane.getComponent(i).getName().equals(oldText))
173: contentPane.getComponent(i).setName(str);
174: }
175: btn.setText(str);
176: }
177:
178: //-Action listener methods-----------------------------------------------------
179: /**
180: * Invoked when a mouse button has been pressed on a component.
181: * @param e the event
182: */
183: public void actionPerformed(ActionEvent e) {
184: XButton comp = (XButton) e.getSource();
185: int numComps = getComponentCount();
186: int contentIdx = -1;
187: for (int i = 0; i < numComps; i++) {
188: Component compi = getComponent(i);
189: if (compi == comp)
190: selIdx = i;
191: else if (compi == contentPane)
192: contentIdx = i;
193: }
194:
195: // Reduce the index as the content pane is about to be moved down.
196: if (contentIdx < selIdx)
197: selIdx--;
198:
199: remove(contentPane);
200: add(contentPane, selIdx + 1);
201:
202: cardManager.show(contentPane, comp.getText());
203: doLayout();
204: }
205: }
|