001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.editors.xmlbeans;
006:
007: import org.apache.xmlbeans.XmlObject;
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.events.FocusAdapter;
010: import org.eclipse.swt.events.FocusEvent;
011: import org.eclipse.swt.events.KeyAdapter;
012: import org.eclipse.swt.events.KeyEvent;
013: import org.eclipse.swt.widgets.Spinner;
014: import org.terracotta.dso.editors.ConfigurationEditorPanel;
015: import org.terracotta.ui.util.SWTUtil;
016:
017: public class XmlIntegerSpinner implements XmlObjectHolder {
018: private XmlObjectHolderHelper m_helper;
019: private Spinner m_spinner;
020: private boolean m_listening;
021:
022: public XmlIntegerSpinner(Spinner spinner) {
023: m_spinner = spinner;
024: m_helper = new XmlObjectHolderHelper();
025: spinner.addFocusListener(new FocusAdapter() {
026: public void focusLost(FocusEvent e) {
027: if (m_listening) {
028: set();
029: }
030: }
031: });
032: spinner.addKeyListener(new KeyAdapter() {
033: public void keyPressed(KeyEvent e) {
034: if (!m_listening)
035: return;
036: switch (e.keyCode) {
037: case SWT.Selection: {
038: set();
039: break;
040: }
041: case SWT.F5: {
042: unset();
043: break;
044: }
045: }
046: }
047: });
048: }
049:
050: protected void ensureXmlObject() {
051: ConfigurationEditorPanel parent = (ConfigurationEditorPanel) SWTUtil
052: .getAncestorOfClass(ConfigurationEditorPanel.class,
053: m_spinner);
054:
055: if (parent != null) {
056: parent.ensureXmlObject();
057: }
058: }
059:
060: public void setup(XmlObject parent) {
061: m_listening = false;
062: m_helper.setup(parent);
063: setSelection(integerValue());
064: m_listening = true;
065: }
066:
067: public void init(Class parentClass, String elementName) {
068: m_helper.init(parentClass, elementName);
069: setSelection(m_helper.defaultIntegerValue());
070: m_spinner.setMinimum(m_helper.minInclusive());
071: m_spinner.setMaximum(m_helper.maxInclusive());
072: m_spinner.setPageIncrement(1);
073: }
074:
075: public void tearDown() {
076: m_helper.tearDown();
077: m_listening = false;
078: setSelection(m_helper.defaultIntegerValue());
079: }
080:
081: public Integer integerValue() {
082: return isSet() ? m_helper.getIntegerValue() : m_helper
083: .defaultIntegerValue();
084: }
085:
086: public boolean isRequired() {
087: return m_helper.isRequired();
088: }
089:
090: public boolean isSet() {
091: return m_helper.isSet();
092: }
093:
094: public void set() {
095: m_listening = false;
096: int iVal = getSelection();
097: if (m_helper.hasDefault()
098: && m_helper.defaultIntegerValue().equals(iVal)) {
099: unset();
100: } else {
101: ensureXmlObject();
102: m_helper.set(Integer.toString(iVal));
103: setSelection(iVal);
104: }
105: m_listening = true;
106: }
107:
108: public void unset() {
109: if (!isRequired()) {
110: m_listening = false;
111: m_helper.unset();
112: Integer iVal = m_helper.defaultIntegerValue();
113: setSelection(iVal);
114: m_listening = true;
115: }
116: }
117:
118: public synchronized void addXmlObjectStructureListener(
119: XmlObjectStructureListener listener) {
120: m_helper.addXmlObjectStructureListener(listener);
121: }
122:
123: public synchronized void removeXmlObjectStructureListener(
124: XmlObjectStructureListener listener) {
125: m_helper.removeXmlObjectStructureListener(listener);
126: }
127:
128: public int getSelection() {
129: return m_spinner.getSelection();
130: }
131:
132: public void setSelection(int value) {
133: m_spinner.setSelection(value);
134: }
135: }
|