KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > swing > CardPanel


1 package com.opensymphony.workflow.designer.swing;
2
3 /*
4  * Copyright (c) Sun Microsystems.
5  */

6
7 import java.awt.*;
8
9 import javax.swing.*;
10
11 /**
12  * A simpler alternative to a JPanel with a CardLayout. The AWT CardLayout
13  * layout manager can be inconvenient to use because the special "stack of
14  * cards" operations it supports require a cast to use. For example to show
15  * the card named "myCard" given a JPanel with a CardLayout one would write:
16  * <pre>
17  * ((CardLayout)(myJPanel.getLayout())).show(myJPanel, "myCard");
18  * </pre>
19  * This doesn't work well with Swing - all of the CardLayout display
20  * operations, like <code>show</code> call validate directly. Swing supports
21  * automatic validation (see JComponent.revalidate()); this direct call to
22  * validate is inefficient.
23  * <p>
24  * The CardPane JPanel subclass is intended to support a layout with a modest
25  * number of cards, on the order of 100 or less. A cards name is it's
26  * component name, as in java.awt.Component.getName(), which is set when the
27  * component is added to the CardPanel:
28  * <pre>
29  * myCardPanel.add(myChild, "MyChildName");
30  * myChild.getName() <i>=> "MyChildName"</i>
31  * </pre>
32  * As with CardLayout, the first child added to a CardPanel is made visible
33  * and there's only one child visible at a time. The <code>showCard</code>
34  * method accepts either a childs name or the child itself:
35  * <pre>
36  * myCardPanel.show("MyChildName");
37  * myCardPanel.show(myChild);
38  * </pre>
39  * <p>
40  * The CardPanel class doesn't support the vgap/hgap CardLayout properties
41  * since one can add a Border, see JComponent.setBorder().
42  *
43  * @author Sun Microsystems
44  */

45
46 public class CardPanel extends JPanel
47 {
48
49   private static class Layout implements LayoutManager
50   {
51     /**
52      * Set the childs name (if non-null) and and make it visible
53      * iff it's the only CardPanel child.
54      * @see java.awt.Component#setName
55      */

56     public void addLayoutComponent(String JavaDoc name, Component child)
57     {
58       if(name != null)
59       {
60         child.setName(name);
61       }
62       child.setVisible(child.getParent().getComponentCount() == 1);
63     }
64
65     /**
66      * If this child was visible, then make the first remaining
67      * child visible.
68      */

69     public void removeLayoutComponent(Component child)
70     {
71       if(child.isVisible())
72       {
73         Container parent = child.getParent();
74         if(parent.getComponentCount() > 0)
75         {
76           parent.getComponent(0).setVisible(true);
77         }
78       }
79     }
80
81     /**
82      * @return the maximum preferred width/height + the parents insets
83      */

84     public Dimension preferredLayoutSize(Container parent)
85     {
86       int nChildren = parent.getComponentCount();
87       Insets insets = parent.getInsets();
88       int width = insets.left + insets.right;
89       int height = insets.top + insets.bottom;
90       for(int i = 0; i < nChildren; i++)
91       {
92         Dimension d = parent.getComponent(i).getPreferredSize();
93         if(d.width > width)
94         {
95           width = d.width;
96         }
97         if(d.height > height)
98         {
99           height = d.height;
100         }
101       }
102       return new Dimension(width, height);
103     }
104
105     /**
106      * @return the maximum minimum width/height + the parents insets
107      */

108     public Dimension minimumLayoutSize(Container parent)
109     {
110       int nChildren = parent.getComponentCount();
111       Insets insets = parent.getInsets();
112       int width = insets.left + insets.right;
113       int height = insets.top + insets.bottom;
114       for(int i = 0; i < nChildren; i++)
115       {
116         if(!parent.getComponent(i).isVisible()) continue;
117         Dimension d = parent.getComponent(i).getMinimumSize();
118         if(d.width > width)
119         {
120           width = d.width;
121         }
122         if(d.height > height)
123         {
124           height = d.height;
125         }
126       }
127       //return new Dimension(width, height);
128
return new Dimension(100, height);
129     }
130
131     public void layoutContainer(Container parent)
132     {
133       int nChildren = parent.getComponentCount();
134       Insets insets = parent.getInsets();
135       for(int i = 0; i < nChildren; i++)
136       {
137         Component child = parent.getComponent(i);
138         if(child.isVisible())
139         {
140           Rectangle r = parent.getBounds();
141           int width = r.width - insets.left + insets.right;
142           int height = r.height - insets.top + insets.bottom;
143           child.setBounds(insets.left, insets.top, width, height);
144           break;
145         }
146       }
147     }
148   }
149
150   /**
151    * Creates a CardPanel. Children, called "cards" in this API, should be
152    * added with add(). The first child we be made visible, subsequent
153    * children will be hidden. To show a card, use one of the show*Card
154    * methods.
155    */

156   public CardPanel()
157   {
158     super(new Layout());
159   }
160
161   /**
162    * Hide the currently visible child "card" and show the
163    * specified card. If the specified card isn't a child
164    * of the CardPanel then we add it here.
165    */

166   public Component getVisibleCard()
167   {
168     int index = getVisibleChildIndex();
169     return index != -1 ? getComponent(index) : null;
170   }
171
172   /**
173    * Return the index of the first (and one would hope - only)
174    * visible child. If a visible child can't be found,
175    * perhaps the caller has inexlicably hidden all of the
176    * children, then return -1.
177    */

178   public int getVisibleChildIndex()
179   {
180     int nChildren = getComponentCount();
181     for(int i = 0; i < nChildren; i++)
182     {
183       Component child = getComponent(i);
184       if(child.isVisible())
185       {
186         return i;
187       }
188     }
189     return -1;
190   }
191
192   /**
193    * Return the name of the visible child.
194    */

195   public String JavaDoc getVisibleChildName()
196   {
197     int i = getVisibleChildIndex();
198     return -1 == i ? null : getComponent(i).getName();
199   }
200
201   /**
202    * Hide the currently visible child "card" and show the
203    * specified card. If the specified card isn't a child
204    * of the CardPanel then we add it here.
205    */

206   public void showCard(Component card)
207   {
208     if(card.getParent() != this)
209     {
210       add(card);
211     }
212     Component visibleComponent = getVisibleCard();
213     if(visibleComponent == card)
214       return;
215     visibleComponent.setVisible(false);
216     card.setVisible(true);
217     revalidate();
218     repaint();
219   }
220
221   /**
222    * Show the card with the specified name.
223    * @see java.awt.Component#getName
224    */

225   public Component showCard(String JavaDoc name)
226   {
227     if(getVisibleCard()!=null && name.equals(getVisibleCard().getName())) return getVisibleCard();
228     int nChildren = getComponentCount();
229     for(int i = 0; i < nChildren; i++)
230     {
231       Component child = getComponent(i);
232       if(child.getName().equals(name) && !child.isVisible())
233       {
234         showCard(child);
235         return child;
236       }
237     }
238     return null;
239   }
240
241   /**
242    * Show the first card that was added to this CardPanel.
243    */

244   public void showFirstCard()
245   {
246     if(getComponentCount() <= 0)
247     {
248       return;
249     }
250     showCard(getComponent(0));
251   }
252
253   /**
254    * Show the last card that was added to this CardPanel.
255    */

256   public void showLastCard()
257   {
258     if(getComponentCount() <= 0)
259     {
260       return;
261     }
262     showCard(getComponent(getComponentCount() - 1));
263   }
264
265   /**
266    * Show the card that was added to this CardPanel after the currently
267    * visible card. If the currently visible card was added last, then
268    * show the first card.
269    */

270   public void showNextCard()
271   {
272     if(getComponentCount() <= 0)
273     {
274       return;
275     }
276     int index = getVisibleChildIndex();
277     if(index == -1)
278     {
279       showCard(getComponent(0));
280     }
281     else if(index == (getComponentCount() - 1))
282     {
283       showCard(getComponent(0));
284     }
285     else
286     {
287       showCard(getComponent(index + 1));
288     }
289   }
290
291   /**
292    * Show the card that was added to this CardPanel before the currently
293    * visible card. If the currently visible card was added first, then
294    * show the last card.
295    */

296   public void showPreviousCard()
297   {
298     if(getComponentCount() <= 0)
299     {
300       return;
301     }
302     int index = getVisibleChildIndex();
303     if(index == -1)
304     {
305       showCard(getComponent(0));
306     }
307     else if(index == 0)
308     {
309       showCard(getComponent(getComponentCount() - 1));
310     }
311     else
312     {
313       showCard(getComponent(index - 1));
314     }
315   }
316
317 }
318
Popular Tags