001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.plaf.ToggleButtonCG;
016:
017: import javax.swing.*;
018:
019: /**
020: * Knows and displays two different states.
021: *
022: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
023: */
024: public class SToggleButton extends SAbstractButton {
025:
026: /**
027: * Creates a toggle button with an initial text.
028: *
029: * @param label Text to display
030: */
031: public SToggleButton(String label) {
032: super (label);
033: }
034:
035: /**
036: * Creates a toggle button with an icon.
037: *
038: * @param icon Icon to display
039: */
040: public SToggleButton(SIcon icon) {
041: super ();
042: setIcon(icon);
043: }
044:
045: /**
046: * Creates a button where properties are taken from the
047: * Action supplied.
048: *
049: * @param action the Action used to specify the new button
050: */
051: public SToggleButton(Action action) {
052: super (action);
053: }
054:
055: /**
056: * Creates a toggle button.
057: *
058: */
059: public SToggleButton() {
060: }
061:
062: public void setCG(ToggleButtonCG cg) {
063: super .setCG(cg);
064: }
065:
066: public void processLowLevelEvent(String action, String[] values) {
067: processKeyEvents(values);
068: if (action.endsWith("_keystroke"))
069: return;
070:
071: delayEvents(true);
072:
073: boolean origSelected = isSelected();
074:
075: if (getGroup() != null) {
076: getGroup().setDelayEvents(true);
077: setSelected(parseSelectionToggle(values[0]));
078: getGroup().setDelayEvents(false);
079: } else {
080: setSelected(parseSelectionToggle(values[0]));
081: } // end of else
082:
083: if (isSelected() != origSelected) {
084: // got an event, that is a select...
085: SForm.addArmedComponent(this );
086: } // end of if ()
087:
088: delayEvents(false);
089: }
090:
091: /**
092: * in form components the parameter value of an button is the button
093: * text. So just toggle selection, in process request, if it is a request
094: * for me.
095: */
096: protected boolean parseSelectionToggle(String toggleParameter) {
097: // a button/image in a form has no value, so just toggle selection...
098: if (getShowAsFormComponent()) {
099: return !isSelected();
100: } // end of if ()
101:
102: if ("1".equals(toggleParameter))
103: return true;
104: else if ("0".equals(toggleParameter))
105: return false;
106:
107: // don't change...
108: return isSelected();
109: }
110:
111: }
|