001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.editors.xmlbeans;
005:
006: import org.apache.xmlbeans.XmlObject;
007: import org.eclipse.swt.events.SelectionAdapter;
008: import org.eclipse.swt.events.SelectionEvent;
009: import org.eclipse.swt.widgets.Button;
010: import org.terracotta.dso.editors.ConfigurationEditorPanel;
011: import org.terracotta.ui.util.SWTUtil;
012:
013: public class XmlBooleanToggle implements XmlObjectHolder {
014: private XmlObjectHolderHelper m_helper;
015: private Button m_button;
016: private boolean m_listening;
017:
018: public XmlBooleanToggle(Button button) {
019: m_helper = new XmlObjectHolderHelper();
020: m_button = button;
021: button.addSelectionListener(new SelectionAdapter() {
022: public void widgetSelected(SelectionEvent e) {
023: if (m_listening) {
024: set();
025: }
026: }
027: });
028: }
029:
030: protected void ensureXmlObject() {
031: ConfigurationEditorPanel parent = (ConfigurationEditorPanel) SWTUtil
032: .getAncestorOfClass(ConfigurationEditorPanel.class,
033: m_button);
034:
035: if (parent != null) {
036: parent.ensureXmlObject();
037: }
038: }
039:
040: public void init(Class parentClass, String elementName) {
041: m_helper.init(parentClass, elementName);
042: }
043:
044: public void setup(XmlObject parent) {
045: m_listening = false;
046: m_helper.setup(parent);
047: m_button.setSelection(booleanValue());
048: m_listening = true;
049: }
050:
051: public void tearDown() {
052: m_helper.tearDown();
053: m_listening = false;
054: m_button.setSelection(false);
055: }
056:
057: public boolean booleanValue() {
058: return isSet() ? m_helper.getBoolean() : m_helper
059: .defaultBoolean();
060: }
061:
062: public boolean isRequired() {
063: return m_helper.isRequired();
064: }
065:
066: public boolean isSet() {
067: return m_helper.isSet();
068: }
069:
070: public void set() {
071: m_listening = false;
072: boolean isSelected = m_button.getSelection();
073:
074: if (isSelected == m_helper.defaultBoolean()) {
075: unset();
076: } else {
077: String s = Boolean.toString(isSelected);
078:
079: ensureXmlObject();
080: m_helper.set(s);
081: m_button.setSelection(isSelected);
082: }
083: m_listening = true;
084: }
085:
086: public void unset() {
087: if (!isRequired()) {
088: m_listening = false;
089: m_helper.unset();
090: m_button.setSelection(m_helper.defaultBoolean());
091: m_listening = true;
092: }
093: }
094:
095: public synchronized void addXmlObjectStructureListener(
096: XmlObjectStructureListener listener) {
097: m_helper.addXmlObjectStructureListener(listener);
098: }
099:
100: public synchronized void removeXmlObjectStructureListener(
101: XmlObjectStructureListener listener) {
102: m_helper.removeXmlObjectStructureListener(listener);
103: }
104:
105: public void setSelection(boolean value) {
106: m_button.setSelection(value);
107: }
108:
109: public boolean getSelection() {
110: return m_button.getSelection();
111: }
112:
113: public void setText(String text) {
114: m_button.setText(text);
115: }
116:
117: public String getText() {
118: return m_button.getText();
119: }
120:
121: public void setEnabled(boolean enabled) {
122: m_button.setEnabled(enabled);
123: }
124:
125: public boolean isEnabled() {
126: return m_button.isEnabled();
127: }
128: }
|