001: /*
002: * This file is not part of the ItsNat framework.
003: *
004: * Original source code use and closed source derivatives are authorized
005: * to third parties with no restriction or fee.
006: * The original source code is owned by the author.
007: *
008: * This program is distributed AS IS in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: *
012: * Author: Jose Maria Arranz Santamaria
013: * (C) Innowhere Software Services S.L., Spanish company, year 2007
014: */
015:
016: package org.itsnat.feashow.features.components.buttons.toggle;
017:
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import java.awt.event.ItemEvent;
021: import java.awt.event.ItemListener;
022: import javax.swing.ButtonGroup;
023: import javax.swing.JToggleButton.ToggleButtonModel;
024: import org.itsnat.comp.ItsNatButtonGroup;
025: import org.itsnat.comp.ItsNatComponentManager;
026: import org.itsnat.comp.free.ItsNatFreeRadioButton;
027: import org.itsnat.core.ItsNatDocument;
028: import org.itsnat.feashow.FeatureTreeNode;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.events.Event;
031: import org.w3c.dom.events.EventListener;
032: import org.w3c.dom.events.EventTarget;
033:
034: public class FreeRadioButtonTreeNode extends FeatureTreeNode implements
035: EventListener, ActionListener, ItemListener {
036: protected ItsNatFreeRadioButton buttonComp1;
037: protected ItsNatFreeRadioButton buttonComp2;
038:
039: public FreeRadioButtonTreeNode() {
040: }
041:
042: public void startExamplePanel() {
043: ItsNatDocument itsNatDoc = getItsNatDocument();
044: ItsNatComponentManager componentMgr = itsNatDoc
045: .getItsNatComponentManager();
046:
047: this .buttonComp1 = (ItsNatFreeRadioButton) componentMgr
048: .createItsNatComponentById("buttonId1",
049: "freeRadioButton", null);
050: buttonComp1.addEventListener("click", this );
051:
052: this .buttonComp2 = (ItsNatFreeRadioButton) componentMgr
053: .createItsNatComponentById("buttonId2",
054: "freeRadioButton", null);
055: buttonComp2.addEventListener("click", this );
056:
057: ButtonGroup group = new ButtonGroup();
058: ItsNatButtonGroup compGroup = componentMgr
059: .getItsNatButtonGroup(group);
060: compGroup.addButton(buttonComp1);
061: compGroup.addButton(buttonComp2);
062:
063: ToggleButtonModel dataModel1 = buttonComp1
064: .getToggleButtonModel();
065: dataModel1.addActionListener(this );
066: dataModel1.addItemListener(this );
067:
068: ToggleButtonModel dataModel2 = buttonComp2
069: .getToggleButtonModel();
070: dataModel2.addActionListener(this );
071: dataModel2.addItemListener(this );
072: }
073:
074: public void endExamplePanel() {
075: this .buttonComp1.dispose();
076: this .buttonComp1 = null;
077:
078: this .buttonComp2.dispose();
079: this .buttonComp2 = null;
080: }
081:
082: public void handleEvent(Event evt) {
083: EventTarget currentTarget = evt.getCurrentTarget();
084: String button;
085: if (currentTarget == buttonComp1.getElement())
086: button = "button 1";
087: else
088: button = "button 2";
089:
090: log(evt.getCurrentTarget() + " " + evt.getType() + " " + button);
091: }
092:
093: public void actionPerformed(ActionEvent e) {
094: String button;
095: if (e.getSource() == buttonComp1.getToggleButtonModel())
096: button = "button 1";
097: else
098: button = "button 2";
099:
100: log(e.getClass().toString() + " " + button);
101: }
102:
103: public void itemStateChanged(ItemEvent e) {
104: String fact;
105: int state = e.getStateChange();
106: if (state == ItemEvent.SELECTED)
107: fact = "selected";
108: else
109: fact = "deselected";
110:
111: if (e.getItem() == buttonComp1.getToggleButtonModel())
112: fact += " 1";
113: else
114: fact += " 2";
115:
116: log(e.getClass().toString() + " " + fact);
117:
118: if (e.getItem() == buttonComp1.getToggleButtonModel())
119: updateDecoration(buttonComp1);
120: else
121: updateDecoration(buttonComp2);
122: }
123:
124: public void updateDecoration(ItsNatFreeRadioButton buttonComp) {
125: Element buttonElem = buttonComp.getElement();
126: ToggleButtonModel model = buttonComp.getToggleButtonModel();
127: if (model.isSelected())
128: buttonElem.setAttribute("style",
129: "background: rgb(253,147,173);"); // weak red
130: else
131: buttonElem.removeAttribute("style");
132: }
133: }
|