001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.him.window.outlookbar;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Container;
025: import java.awt.Dimension;
026: import java.awt.Font;
027: import java.awt.IllegalComponentStateException;
028: import java.awt.Insets;
029: import java.awt.LayoutManager;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.util.Iterator;
033: import java.util.Vector;
034:
035: import javax.swing.Icon;
036: import javax.swing.JButton;
037: import javax.swing.JComponent;
038: import javax.swing.SwingConstants;
039:
040: import org.openharmonise.vfs.context.*;
041:
042: public class FloatingBarPane extends JComponent implements
043: LayoutManager, ActionListener {
044:
045: private Vector bars;
046: private Vector components;
047: private Component selectedComponent;
048: private Component previousComponent;
049: private int barHeight;
050: private int yInset;
051: private boolean animated;
052: private boolean reEntrant;
053: static int nameLessIndex = 0;
054:
055: private JComponent m_parent = null;
056:
057: public FloatingBarPane(JComponent parent) {
058: reEntrant = false;
059: bars = new Vector();
060: components = new Vector();
061: yInset = 0;
062: animated = false;
063: setLayout(this );
064: m_parent = parent;
065: }
066:
067: public Component add(Component component) {
068: if (!reEntrant) {
069: reEntrant = true;
070: return super .add("Bar-" + nameLessIndex++, component);
071: } else {
072: return super .add(component);
073: }
074: }
075:
076: public Component add(Component component, int i) {
077: if (i < bars.size()) {
078: Vector vector = new Vector();
079: Vector vector1 = new Vector();
080: for (int j = i; j < components.size(); j++) {
081: vector.addElement(components.elementAt(j));
082: vector1.addElement(((JButton) bars.elementAt(j))
083: .getText());
084: }
085:
086: for (int k = 0; k < vector.size(); k++)
087: remove((Component) vector.elementAt(k));
088:
089: add(component);
090: for (int l = 0; l < vector.size(); l++)
091: add((String) vector1.elementAt(l), (Component) vector
092: .elementAt(l));
093:
094: } else {
095: add(component);
096: }
097: return component;
098: }
099:
100: public void add(Component component, Object obj) {
101: add(component);
102: }
103:
104: public void add(Component component, Object obj, int i) {
105: add(component, i);
106: }
107:
108: public Component add(String s, Component component) {
109: reEntrant = true;
110: return super .add(s, component);
111: }
112:
113: public void add(String s, Icon icon, Component component) {
114: add(s, component);
115: ((JButton) bars.elementAt(bars.size() - 1)).setIcon(icon);
116: }
117:
118: public void add(String s, Icon icon, Component component, String s1) {
119: add(s, icon, component);
120: ((JComponent) bars.elementAt(bars.size() - 1))
121: .setToolTipText(s1);
122: }
123:
124: public Component getComponent(String sTitle) {
125: Component retn = null;
126:
127: Iterator itor = bars.iterator();
128: while (itor.hasNext()) {
129: JButton button = (JButton) itor.next();
130: if (button.getText().equals(sTitle)) {
131: retn = (Component) this .components.get(bars
132: .indexOf(button));
133: }
134: }
135:
136: return retn;
137: }
138:
139: public boolean isAnimated() {
140: return animated;
141: }
142:
143: public void setAnimated(boolean flag) {
144: animated = flag;
145: }
146:
147: public void setYInset(int i) {
148: yInset = i;
149: doLayout();
150: }
151:
152: public void setSelectedComponent(Component component) {
153: int i = components.indexOf(component);
154: if (component != selectedComponent && i > 0) {
155: previousComponent = selectedComponent;
156: selectedComponent = component;
157: if (animated)
158: (new MoveThread()).start();
159: else
160: doLayout();
161:
162: ContextEvent ce = new ContextEvent(
163: ContextType.CONTEXT_TABS, ((JButton) this .bars
164: .get(i)).getText().trim());
165: ContextHandler.getInstance().fireContextEvent(ce);
166: }
167: }
168:
169: public void setSelectedIndex(int i) {
170: if (i < 0 || i >= components.size())
171: return;
172: Component component = (Component) components.elementAt(i);
173: if (component != selectedComponent) {
174: previousComponent = selectedComponent;
175: selectedComponent = component;
176: if (animated)
177: (new MoveThread()).start();
178: else
179: doLayout();
180:
181: ContextEvent ce = new ContextEvent(
182: ContextType.CONTEXT_TABS, ((JButton) this .bars
183: .get(i)).getText().trim());
184: ContextHandler.getInstance().fireContextEvent(ce);
185: }
186: }
187:
188: public void setLayout(LayoutManager layoutmanager) {
189: if (layoutmanager != this ) {
190: throw new IllegalComponentStateException(
191: "Invalid LayoutManager change");
192: } else {
193: super .setLayout(layoutmanager);
194: return;
195: }
196: }
197:
198: public void setBackground(Color color) {
199: super .setBackground(color);
200: int i = bars.size();
201: for (int j = 0; j < i; j++)
202: ((Component) bars.elementAt(j)).setBackground(color);
203:
204: }
205:
206: public void setForeground(Color color) {
207: super .setForeground(color);
208: int i = bars.size();
209: for (int j = 0; j < i; j++)
210: ((Component) bars.elementAt(j)).setForeground(color);
211:
212: }
213:
214: public void addLayoutComponent(String s, Component component) {
215: JButton jbutton = new JButton(s);
216:
217: String fontName = "Dialog";
218: int fontSize = 11;
219: Font font = new Font(fontName, Font.PLAIN, fontSize);
220: jbutton.setFont(font);
221:
222: jbutton.setHorizontalAlignment(SwingConstants.LEFT);
223: jbutton.addActionListener(this );
224: Insets margin = jbutton.getMargin();
225: margin.left = 1;
226: jbutton.setMargin(margin);
227: add(jbutton);
228: bars.addElement(jbutton);
229: components.addElement(component);
230: selectedComponent = component;
231: doLayout();
232: reEntrant = false;
233: }
234:
235: public void removeLayoutComponent(Component component) {
236: int i = components.indexOf(component);
237: if (i >= 0) {
238: bars.removeElementAt(i);
239: components.removeElementAt(i);
240: }
241: }
242:
243: public Dimension preferredLayoutSize(Container container) {
244: if (selectedComponent != null) {
245: Dimension dimension = selectedComponent.getPreferredSize();
246: return new Dimension(dimension.width, dimension.height
247: + bars.size() * barHeight);
248: } else {
249: return new Dimension(0, 0);
250: }
251: }
252:
253: public Dimension minimumLayoutSize(Container container) {
254: if (selectedComponent != null) {
255: Dimension dimension = selectedComponent.getMinimumSize();
256: return new Dimension(dimension.width, dimension.height
257: + bars.size() * barHeight);
258: } else {
259: return new Dimension(0, 0);
260: }
261: }
262:
263: public void layoutContainer(Container container) {
264: Dimension dim = new Dimension(m_parent.getWidth(), m_parent
265: .getHeight());
266: this .setSize(dim);
267: Dimension dimension = getSize();
268: int i = bars.size();
269: if (i > 0) {
270: JButton jbutton = (JButton) bars.elementAt(0);
271: //jbutton.setMargin(new Insets(yInset, 0, yInset, 0));
272: barHeight = jbutton.getMinimumSize().height;
273: }
274: int j = 0;
275: for (int k = 0; k < i; k++) {
276: ((Component) bars.elementAt(k)).setBounds(0, j,
277: dimension.width - 1, barHeight);
278: j += barHeight;
279: if (components.elementAt(k) == selectedComponent) {
280: int l = dimension.height - barHeight * i;
281: selectedComponent.setBounds(0, j, dimension.width - 1,
282: l);
283: selectedComponent.doLayout();
284: selectedComponent.requestFocus();
285: selectedComponent.setVisible(true);
286: j += l;
287: } else {
288: ((Component) components.elementAt(k)).setVisible(false);
289: }
290: }
291:
292: }
293:
294: public void actionPerformed(ActionEvent actionevent) {
295: Component component = (Component) actionevent.getSource();
296: int i = bars.size();
297: for (int j = 0; j < i; j++)
298: if (component == bars.elementAt(j)) {
299: if (selectedComponent != components.elementAt(j)) {
300: previousComponent = selectedComponent;
301: selectedComponent = (Component) components
302: .elementAt(j);
303: if (animated)
304: (new MoveThread()).start();
305: else
306: doLayout();
307: ContextEvent ce = new ContextEvent(
308: ContextType.CONTEXT_TABS,
309: ((JButton) this .bars.get(j)).getText()
310: .trim());
311: ContextHandler.getInstance().fireContextEvent(ce);
312: } else {
313: selectedComponent.requestFocus();
314: }
315: return;
316: }
317:
318: }
319:
320: class MoveThread extends Thread {
321:
322: MoveThread() {
323: }
324:
325: public void run() {
326: Dimension dimension = getSize();
327: int i = bars.size();
328: int j = components.indexOf(previousComponent);
329: int k = components.indexOf(selectedComponent);
330: int l = j >= k ? k : j;
331: int i1 = j <= k ? k : j;
332: int j1 = previousComponent.getSize().height;
333: for (double d = 0.0D; d < 1.0D; d += 0.20000000000000001D) {
334: int k1 = barHeight * l;
335: for (int j2 = l; j2 <= i1; j2++) {
336: ((Component) bars.elementAt(j2)).setBounds(0, k1,
337: dimension.width - 1, barHeight);
338: k1 += barHeight;
339: if (components.elementAt(j2) == selectedComponent) {
340: int l1 = (int) ((double) j1 * d);
341: selectedComponent.setBounds(0, k1,
342: dimension.width - 1, l1);
343: selectedComponent.setVisible(true);
344: k1 += l1;
345: } else if (components.elementAt(j2) == previousComponent) {
346: int i2 = (int) ((double) j1 * (1.0D - d));
347: previousComponent.setBounds(0, k1,
348: dimension.width - 1, i2);
349: k1 += i2;
350: }
351: }
352:
353: try {
354: Thread.sleep(20L);
355: } catch (Exception exception) {
356: }
357: }
358:
359: doLayout();
360: }
361: }
362:
363: }
|