1 21 package proguard.gui; 22 23 import java.awt.*; 24 import java.awt.event.*; 25 26 import javax.swing.*; 27 28 29 37 public class TabbedPane 38 extends JPanel 39 { 40 private CardLayout cardLayout = new CardLayout(); 41 private JPanel cardPanel = new JPanel(cardLayout); 42 private ButtonGroup buttonGroup = new ButtonGroup(); 43 44 45 48 public TabbedPane() 49 { 50 GridBagLayout layout = new GridBagLayout(); 51 setLayout(layout); 52 53 GridBagConstraints cardConstraints = new GridBagConstraints(); 54 cardConstraints.gridx = 1; 55 cardConstraints.gridy = 0; 56 cardConstraints.gridheight = GridBagConstraints.REMAINDER; 57 cardConstraints.fill = GridBagConstraints.BOTH; 58 cardConstraints.weightx = 1.0; 59 cardConstraints.weighty = 1.0; 60 cardConstraints.anchor = GridBagConstraints.NORTHWEST; 61 62 add(cardPanel, cardConstraints); 63 } 64 65 66 72 public Component add(final String title, Component component) 73 { 74 GridBagConstraints buttonConstraints = new GridBagConstraints(); 75 buttonConstraints.gridx = 0; 76 buttonConstraints.fill = GridBagConstraints.HORIZONTAL; 77 buttonConstraints.anchor = GridBagConstraints.NORTHWEST; 78 buttonConstraints.ipadx = 10; 79 buttonConstraints.ipady = 4; 80 81 JToggleButton button = new JToggleButton(title); 82 83 button.setModel(new JToggleButton.ToggleButtonModel() 86 { 87 public void setPressed(boolean b) 88 { 89 if ((isPressed() == b) || !isEnabled()) 90 { 91 return; 92 } 93 94 if (b == false && isArmed()) 95 { 96 setSelected(!this.isSelected()); 97 } 98 99 if (b) 100 { 101 stateMask |= PRESSED; 102 } 103 else 104 { 105 stateMask &= ~PRESSED; 106 } 107 108 fireStateChanged(); 109 110 if (isPressed()) 111 { 112 fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand())); 113 } 114 } 115 116 }); 117 118 button.addActionListener(new ActionListener() 120 { 121 public void actionPerformed(ActionEvent e) 122 { 123 cardLayout.show(cardPanel, title); 124 } 125 }); 126 127 buttonGroup.add(button); 129 130 if (cardPanel.getComponentCount() == 0) 132 { 133 button.setSelected(true); 134 } 135 136 add(button, buttonConstraints); 138 cardPanel.add(title, component); 139 140 return component; 141 } 142 143 144 151 public Component addImage(final Image image) 152 { 153 GridBagConstraints imageConstraints = new GridBagConstraints(); 154 imageConstraints.gridx = 0; 155 imageConstraints.weighty = 1.0; 156 imageConstraints.fill = GridBagConstraints.BOTH; 157 imageConstraints.anchor = GridBagConstraints.SOUTHWEST; 158 159 JPanel component = new JPanel() 160 { 161 public void paintComponent(Graphics graphics) 162 { 163 graphics.drawImage(image, 0, getHeight() - image.getHeight(null), this); 164 } 165 }; 166 component.setBorder(BorderFactory.createEtchedBorder()); 167 168 add(component, imageConstraints); 169 170 return component; 171 } 172 173 174 177 public void first() 178 { 179 cardLayout.first(cardPanel); 180 updateButtonSelection(); 181 } 182 183 184 187 public void last() 188 { 189 cardLayout.last(cardPanel); 190 updateButtonSelection(); 191 } 192 193 194 197 public void previous() 198 { 199 cardLayout.previous(cardPanel); 200 updateButtonSelection(); 201 } 202 203 204 207 public void next() 208 { 209 cardLayout.next(cardPanel); 210 updateButtonSelection(); 211 } 212 213 214 217 private void updateButtonSelection() 218 { 219 int count = cardPanel.getComponentCount(); 220 for (int index = 0 ; index < count ; index++) { 221 Component card = cardPanel.getComponent(index); 222 if (card.isShowing()) 223 { 224 JToggleButton button = (JToggleButton)getComponent(index+1); 225 button.setSelected(true); 226 } 227 } 228 } 229 } 230 | Popular Tags |