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.SWT;
008: import org.eclipse.swt.events.FocusAdapter;
009: import org.eclipse.swt.events.FocusEvent;
010: import org.eclipse.swt.events.KeyAdapter;
011: import org.eclipse.swt.events.KeyEvent;
012: import org.eclipse.swt.widgets.Text;
013: import org.terracotta.dso.editors.ConfigurationEditorPanel;
014: import org.terracotta.ui.util.SWTUtil;
015:
016: public class XmlStringField implements XmlObjectHolder {
017: private XmlObjectHolderHelper m_helper;
018: private Text m_field;
019: private boolean m_listening;
020:
021: public XmlStringField(Text field) {
022: m_helper = new XmlObjectHolderHelper();
023: m_field = field;
024: field.addFocusListener(new FocusAdapter() {
025: public void focusLost(FocusEvent e) {
026: if (m_listening) {
027: set();
028: }
029: }
030: });
031: field.addKeyListener(new KeyAdapter() {
032: public void keyPressed(KeyEvent e) {
033: if (!m_listening)
034: return;
035: switch (e.keyCode) {
036: case SWT.Selection: {
037: set();
038: break;
039: }
040: case SWT.F5: {
041: unset();
042: break;
043: }
044: }
045: }
046: });
047: }
048:
049: protected void ensureXmlObject() {
050: ConfigurationEditorPanel parent = (ConfigurationEditorPanel) SWTUtil
051: .getAncestorOfClass(ConfigurationEditorPanel.class,
052: m_field);
053:
054: if (parent != null) {
055: parent.ensureXmlObject();
056: }
057: }
058:
059: public void init(Class parentType, String elementName) {
060: m_helper.init(parentType, elementName);
061: }
062:
063: public void setup(XmlObject parent) {
064: setListening(false);
065: m_helper.setup(parent);
066: setText(stringValue());
067: setListening(true);
068: }
069:
070: public void tearDown() {
071: m_helper.tearDown();
072: setListening(false);
073: setText("");
074: }
075:
076: public String stringValue() {
077: return isSet() ? m_helper.getStringValue() : m_helper
078: .defaultStringValue();
079: }
080:
081: public boolean isRequired() {
082: return m_helper.isRequired();
083: }
084:
085: public boolean isSet() {
086: return m_helper.isSet();
087: }
088:
089: public void set() {
090: setListening(false);
091: try {
092: String s = getText();
093: if (m_helper.hasDefault()
094: && m_helper.defaultStringValue().equals(s)) {
095: unset();
096: } else if (!s.equals(m_helper.getStringValue())) {
097: ensureXmlObject();
098: m_helper.set(s);
099: m_field.setText(s);
100: }
101: } finally {
102: setListening(true);
103: }
104: }
105:
106: public void unset() {
107: if (!isRequired()) {
108: setListening(false);
109: try {
110: m_helper.unset();
111: setText(m_helper.defaultStringValue());
112: } finally {
113: setListening(true);
114: }
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 String getText() {
129: return m_field.getText();
130: }
131:
132: public void setText(String text) {
133: m_field.setText(text != null ? text : "");
134: }
135:
136: private void setListening(boolean listening) {
137: if (m_listening != listening) {
138: m_listening = listening;
139: }
140: }
141: }
|