1 package com.opensymphony.workflow.designer.swing; 2 3 6 7 import java.awt.*; 8 9 import javax.swing.*; 10 11 45 46 public class CardPanel extends JPanel 47 { 48 49 private static class Layout implements LayoutManager 50 { 51 56 public void addLayoutComponent(String name, Component child) 57 { 58 if(name != null) 59 { 60 child.setName(name); 61 } 62 child.setVisible(child.getParent().getComponentCount() == 1); 63 } 64 65 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 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 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(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 156 public CardPanel() 157 { 158 super(new Layout()); 159 } 160 161 166 public Component getVisibleCard() 167 { 168 int index = getVisibleChildIndex(); 169 return index != -1 ? getComponent(index) : null; 170 } 171 172 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 195 public String getVisibleChildName() 196 { 197 int i = getVisibleChildIndex(); 198 return -1 == i ? null : getComponent(i).getName(); 199 } 200 201 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 225 public Component showCard(String 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 244 public void showFirstCard() 245 { 246 if(getComponentCount() <= 0) 247 { 248 return; 249 } 250 showCard(getComponent(0)); 251 } 252 253 256 public void showLastCard() 257 { 258 if(getComponentCount() <= 0) 259 { 260 return; 261 } 262 showCard(getComponent(getComponentCount() - 1)); 263 } 264 265 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 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 |