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.StringEnumAbstractBase;
007: import org.apache.xmlbeans.XmlObject;
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.events.KeyAdapter;
010: import org.eclipse.swt.events.KeyEvent;
011: import org.eclipse.swt.events.ModifyEvent;
012: import org.eclipse.swt.events.ModifyListener;
013: import org.eclipse.swt.widgets.Combo;
014: import org.terracotta.dso.editors.ConfigurationEditorPanel;
015: import org.terracotta.ui.util.SWTUtil;
016:
017: public class XmlStringEnumCombo implements XmlObjectHolder {
018: private XmlObjectHolderHelper m_helper;
019: private Combo m_combo;
020: private boolean m_listening;
021: private StringEnumAbstractBase[] m_enumValues;
022:
023: public XmlStringEnumCombo(Combo combo) {
024: m_combo = combo;
025: m_helper = new XmlObjectHolderHelper();
026: combo.addModifyListener(new ModifyListener() {
027: public void modifyText(ModifyEvent e) {
028: if (m_listening) {
029: set();
030: }
031: }
032: });
033: combo.addKeyListener(new KeyAdapter() {
034: public void keyPressed(KeyEvent e) {
035: if (!m_listening)
036: return;
037: switch (e.keyCode) {
038: case SWT.Selection: {
039: set();
040: break;
041: }
042: case SWT.F5: {
043: unset();
044: break;
045: }
046: }
047: }
048: });
049: }
050:
051: protected void ensureXmlObject() {
052: ConfigurationEditorPanel parent = (ConfigurationEditorPanel) SWTUtil
053: .getAncestorOfClass(ConfigurationEditorPanel.class,
054: m_combo);
055:
056: if (parent != null) {
057: parent.ensureXmlObject();
058: }
059: }
060:
061: public void init(Class parentType, String elementName) {
062: m_helper.init(parentType, elementName);
063: m_enumValues = m_helper.getEnumValues();
064: for (int i = 0; i < m_enumValues.length; i++) {
065: m_combo.add(m_enumValues[i].toString());
066: }
067: }
068:
069: private int valueIndex(StringEnumAbstractBase enumValue) {
070: for (int i = 0; i < m_enumValues.length; i++) {
071: if (enumValue.equals(m_enumValues[i]))
072: return i;
073: }
074: return -1;
075: }
076:
077: public void setup(XmlObject parent) {
078: m_listening = false;
079: m_helper.setup(parent);
080: m_combo.select(valueIndex(enumValue()));
081: m_listening = true;
082: }
083:
084: public void tearDown() {
085: m_helper.tearDown();
086: m_listening = false;
087: }
088:
089: public String getSelectedEnumString() {
090: return m_enumValues[m_combo.getSelectionIndex()].toString();
091: }
092:
093: public int getSelectedEnumInt() {
094: return m_enumValues[m_combo.getSelectionIndex()].intValue();
095: }
096:
097: public StringEnumAbstractBase enumValue() {
098: return isSet() ? m_helper.getEnumValue() : m_helper
099: .defaultEnumValue();
100: }
101:
102: public boolean isRequired() {
103: return m_helper.isRequired();
104: }
105:
106: public boolean isSet() {
107: return m_helper.isSet();
108: }
109:
110: public void set() {
111: String s = getSelectedEnumString();
112:
113: ensureXmlObject();
114: m_helper.set(s);
115: }
116:
117: public void unset() {
118: if (!isRequired()) {
119: m_listening = false;
120: m_helper.unset();
121: m_combo.select(valueIndex(m_helper.defaultEnumValue()));
122: m_listening = true;
123: }
124: }
125:
126: public synchronized void addXmlObjectStructureListener(
127: XmlObjectStructureListener listener) {
128: m_helper.addXmlObjectStructureListener(listener);
129: }
130:
131: public synchronized void removeXmlObjectStructureListener(
132: XmlObjectStructureListener listener) {
133: m_helper.removeXmlObjectStructureListener(listener);
134: }
135: }
|