1 4 package org.terracotta.dso.editors.xmlbeans; 5 6 import org.apache.xmlbeans.XmlObject; 7 8 import org.dijon.Separator; 9 10 import com.tc.admin.common.XAbstractAction; 11 import com.tc.admin.common.XTextField; 12 import org.terracotta.dso.editors.ConfigurationEditorPanel; 13 14 import java.awt.event.ActionEvent ; 15 16 import javax.swing.JPopupMenu ; 17 18 public class XmlStringField extends XTextField 19 implements XmlObjectHolder 20 { 21 private XmlObjectHolderHelper m_helper; 22 private boolean m_listening; 23 24 public XmlStringField() { 25 super(); 26 m_helper = new XmlObjectHolderHelper(); 27 getActionMap().put(RESET, new ResetAction()); 28 getInputMap().put(RESET_STROKE, RESET); 29 } 30 31 protected JPopupMenu createPopup() { 32 JPopupMenu popup = super.createPopup(); 33 34 if(popup == null) { 35 popup = new JPopupMenu (); 36 } 37 else { 38 popup.add(new Separator()); 39 } 40 41 popup.add(new ResetAction()); 42 43 return popup; 44 } 45 46 protected void ensureXmlObject() { 47 ConfigurationEditorPanel parent = (ConfigurationEditorPanel) 48 getAncestorOfClass(ConfigurationEditorPanel.class, this); 49 50 if(parent != null) { 51 parent.ensureXmlObject(); 52 } 53 } 54 55 public void init(Class parentType, String elementName) { 56 m_helper.init(parentType, elementName); 57 } 58 59 public void setup(XmlObject parent) { 60 m_listening = false; 61 m_helper.setup(parent); 62 setText(stringValue()); 63 if(isSet()) { 64 m_helper.validateXmlObject(this); 65 } 66 m_listening = true; 67 } 68 69 protected void fireActionPerformed() { 70 if(m_listening) { 71 set(); 72 } 73 super.fireActionPerformed(); 74 } 75 76 public void tearDown() { 77 m_helper.tearDown(); 78 m_listening = false; 79 setText(null); 80 } 81 82 public String stringValue() { 83 return isSet() ? m_helper.getStringValue() : m_helper.defaultStringValue(); 84 } 85 86 public boolean isRequired() { 87 return m_helper.isRequired(); 88 } 89 90 public boolean isSet() { 91 return m_helper.isSet(); 92 } 93 94 public void set() { 95 String s = getText(); 96 97 ensureXmlObject(); 98 m_helper.set(s); 99 setText(s); 100 m_helper.validateXmlObject(this); 101 } 102 103 public void unset() { 104 if(!isRequired()) { 105 m_listening = false; 106 m_helper.unset(); 107 setText(m_helper.defaultStringValue()); 108 m_listening = true; 109 } 110 } 111 112 public synchronized void addXmlObjectStructureListener(XmlObjectStructureListener listener) { 113 m_helper.addXmlObjectStructureListener(listener); 114 } 115 116 public synchronized void removeXmlObjectStructureListener(XmlObjectStructureListener listener) { 117 m_helper.removeXmlObjectStructureListener(listener); 118 } 119 120 class ResetAction extends XAbstractAction { 121 ResetAction() { 122 super("Reset"); 123 setShortDescription("Reset to default value"); 124 setAccelerator(RESET_STROKE); 125 } 126 127 public void actionPerformed(ActionEvent ae) { 128 unset(); 129 } 130 } 131 } 132 133 | Popular Tags |