1 14 package org.wings; 15 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 19 25 public class SCardLayout 26 extends SAbstractLayoutManager { 27 protected HashMap tab = new HashMap (); 28 29 32 public SCardLayout() { 33 } 34 35 public void addComponent(SComponent c, Object constraint, int index) { 36 if (tab.size() > 0) 37 c.setVisible(false); 38 tab.put((constraint != null) ? constraint : c.getName(), c); 39 } 40 41 public void removeComponent(SComponent c) { 42 for (Iterator e = tab.keySet().iterator(); e.hasNext();) { 43 Object key = e.next(); 44 if (tab.get(key) == c) { 45 tab.remove(key); 46 return; 47 } 48 } 49 } 50 51 55 void checkLayout(SContainer parent) { 56 if (parent.getLayout() != this) { 57 throw new IllegalArgumentException ("wrong parent for CardLayout"); 58 } 59 } 60 61 67 public void first(SContainer parent) { 68 checkLayout(parent); 69 for (int i = 0; i < parent.getComponentCount(); i++) 70 if (i > 0) 71 parent.getComponent(i).setVisible(false); 72 else 73 parent.getComponent(i).setVisible(true); 74 } 75 76 84 public void next(SContainer parent) { 85 checkLayout(parent); 86 int ncomponents = parent.getComponentCount(); 87 for (int i = 0; i < ncomponents; i++) { 88 SComponent comp = parent.getComponent(i); 89 if (comp.isVisible()) { 90 comp.setVisible(false); 91 comp = parent.getComponent((i + 1 < ncomponents) ? i + 1 : 0); 92 comp.setVisible(true); 93 return; 94 } 95 } 96 } 97 98 106 public void previous(SContainer parent) { 107 checkLayout(parent); 108 int ncomponents = parent.getComponentCount(); 109 for (int i = 0; i < ncomponents; i++) { 110 SComponent comp = parent.getComponent(i); 111 if (comp.isVisible()) { 112 comp.setVisible(false); 113 comp = parent.getComponent((i > 0) ? i - 1 : ncomponents - 1); 114 comp.setVisible(true); 115 return; 116 } 117 } 118 } 119 120 126 public void last(SContainer parent) { 127 checkLayout(parent); 128 int ncomponents = parent.getComponentCount(); 129 for (int i = 0; i < ncomponents; i++) { 130 if (i < ncomponents - 1) 131 parent.getComponent(i).setVisible(false); 132 else 133 parent.getComponent(i).setVisible(true); 134 } 135 } 136 137 138 141 public void show(SComponent comp) { 142 for (Iterator en = tab.values().iterator(); en.hasNext();) { 143 SComponent c = (SComponent) en.next(); 144 c.setVisible(false); 145 } 146 comp.setVisible(true); 147 } 148 149 152 public void show(Object constraint) { 153 SComponent visibleComponent = (SComponent) tab.get(constraint); 154 if (visibleComponent != null) { 155 for (Iterator en = tab.values().iterator(); en.hasNext();) { 156 SComponent c = (SComponent) en.next(); 157 c.setVisible(false); 158 } 159 visibleComponent.setVisible(true); 160 } 161 } 162 163 172 public void show(SContainer parent, Object name) { 173 checkLayout(parent); 174 SComponent next = (SComponent) tab.get(name); 175 if ((next != null) && !next.isVisible()) { 176 for (int i = 0; i < parent.getComponentCount(); i++) 177 parent.getComponent(i).setVisible(false); 178 next.setVisible(true); 179 } 180 } 181 182 183 public SComponent getVisibleComponent() { 184 for (Iterator en = tab.values().iterator(); en.hasNext();) { 185 SComponent c = (SComponent) en.next(); 186 if (c.isVisible()) 187 return c; 188 } 189 return null; 190 } 191 192 193 public Object getVisibleConstraint() { 194 for (Iterator en = tab.keySet().iterator(); en.hasNext();) { 195 Object constraint = en.next(); 196 SComponent c = (SComponent) tab.get(constraint); 197 if (c.isVisible()) 198 return constraint; 199 } 200 return null; 201 } 202 } 203 204 205 | Popular Tags |