1 14 package wingset; 15 16 import org.wings.SAbstractButton; 17 import org.wings.SButton; 18 import org.wings.SCheckBox; 19 import org.wings.SComponent; 20 import org.wings.SConstants; 21 import org.wings.SContainer; 22 import org.wings.SForm; 23 import org.wings.SGridLayout; 24 import org.wings.SIcon; 25 import org.wings.SLabel; 26 import org.wings.SPanel; 27 import org.wings.SURLIcon; 28 import org.wings.border.SEmptyBorder; 29 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.util.Iterator ; 33 34 38 public class ButtonExample extends WingSetPane { 39 private static final SIcon icon = new SURLIcon("../icons/ButtonIcon.gif"); 41 private static final SIcon disabledIcon = new SURLIcon("../icons/ButtonDisabledIcon.gif"); 42 private static final SIcon pressedIcon = new SURLIcon("../icons/ButtonPressedIcon.gif"); 43 private static final SIcon rolloverIcon = new SURLIcon("../icons/ButtonRolloverIcon.gif"); 44 45 private final SLabel buttonPressedLabel = new SLabel("No button pressed"); 47 private final ActionListener reportButtonPressedAction = new ActionListener () { 48 public void actionPerformed(ActionEvent e) { 49 buttonPressedLabel.setText("<html>Button <b>'" + e.getActionCommand() + "'</b> pressed"); 50 } 51 }; 52 53 private ButtonControls controls; 55 56 public SComponent createExample() { 57 controls = new ButtonControls(); 58 controls.getApplyButton().addActionListener(reportButtonPressedAction); 59 60 final SForm form = new SForm(new SGridLayout(1)); 61 final SContainer buttonExamplePanel = createButtonExample(form); 62 63 form.add(controls); 64 form.add(buttonExamplePanel); 65 return form; 66 } 67 68 private SContainer createButtonExample(SForm containingForm) { 69 final SButton[] buttons = new SButton[9]; 70 final int[] textHPos = new int[] {SConstants.LEFT, SConstants.CENTER, SConstants.RIGHT}; 71 final int[] textVPos = new int[] {SConstants.TOP, SConstants.CENTER, SConstants.BOTTOM}; 72 73 for (int i = 0; i < buttons.length; i++) { 74 final String buttonName = "Text " + (i + 1); 75 buttons[i] = new SButton(buttonName); 76 buttons[i].setActionCommand(buttons[i].getText()); 77 if (i != 4) { 78 buttons[i].setIcon(icon); 79 buttons[i].setDisabledIcon(disabledIcon); 80 buttons[i].setRolloverIcon(rolloverIcon); 81 buttons[i].setPressedIcon(pressedIcon); 82 } 83 84 buttons[i].setToolTipText("Button " + (i + 1)); 85 buttons[i].setName("button" + (i + 1)); 86 buttons[i].setShowAsFormComponent(false); 87 buttons[i].setVerticalTextPosition(textVPos[(i / 3)% 3]); 88 buttons[i].setHorizontalTextPosition(textHPos[i % 3]); 89 buttons[i].setActionCommand(buttonName); 90 controls.addSizable(buttons[i]); 91 } 92 93 final SGridLayout grid = new SGridLayout(3); 94 final SPanel buttonGrid = new SPanel(grid); 95 grid.setBorder(1); 96 grid.setHgap(10); 97 grid.setVgap(10); 98 buttonGrid.setHorizontalAlignment(CENTER); 99 buttonGrid.setBorder(new SEmptyBorder(10, 0, 10, 0)); 100 101 for (int i = 0; i < buttons.length; i++) { 102 buttons[i].addActionListener(reportButtonPressedAction); 103 buttonGrid.add(buttons[i]); 104 } 105 106 final SButton defaultButton = new SButton(); 108 defaultButton.addActionListener(reportButtonPressedAction); 109 defaultButton.setActionCommand("Default Button (Enter key)"); 110 containingForm.setDefaultButton(defaultButton); 111 112 final SPanel panel = new SPanel(new SGridLayout(1)); 113 final SLabel description = new SLabel("Click on the buttons and see how The label changes\n" + 114 "Click into a input box and press apply or enter to see the effect of the enter button capturing."); 115 panel.setBorder(new SEmptyBorder(10, 10, 10, 10)); 116 panel.add(description); 117 panel.add(buttonGrid); 118 panel.add(buttonPressedLabel); 119 120 return panel; 121 } 122 123 private class ButtonControls extends ComponentControls { 124 public ButtonControls() { 125 final SCheckBox showAsFormComponent = new SCheckBox("Show as Form Component"); 126 showAsFormComponent.addActionListener(new ActionListener () { 127 public void actionPerformed(ActionEvent e) { 128 for (Iterator iterator = components.iterator(); iterator.hasNext();) { 129 SComponent component = (SComponent) iterator.next(); 130 component.setShowAsFormComponent(showAsFormComponent.isSelected()); 131 } 132 } 133 }); 134 add(showAsFormComponent); 135 136 final SCheckBox useImages = new SCheckBox("Use Icons"); 137 useImages.setSelected(true); 138 useImages.addActionListener(new ActionListener () { 139 public void actionPerformed(ActionEvent e) { 140 boolean use = useImages.isSelected(); 141 142 for (Iterator iterator = components.iterator(); iterator.hasNext();) { 143 SAbstractButton component = (SAbstractButton) iterator.next(); 144 component.setIcon(use ? icon : null); 145 component.setDisabledIcon(use ? disabledIcon : null); 146 component.setRolloverIcon(use ? rolloverIcon : null); 147 component.setPressedIcon(use ? pressedIcon : null); 148 } 149 } 150 }); 151 add(useImages); 152 } 153 154 public SButton getApplyButton() { 155 return super.applyButton; 156 } 157 } 158 } 159 | Popular Tags |