1 4 package org.terracotta.dso.editors; 5 6 import org.apache.xmlbeans.XmlObject; 7 import org.dijon.Container; 8 import org.eclipse.core.resources.IProject; 9 import org.eclipse.ui.PlatformUI; 10 import org.terracotta.dso.TcPlugin; 11 import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureChangeEvent; 12 import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureListener; 13 14 import java.util.ArrayList ; 15 16 import javax.swing.JComponent ; 17 import javax.swing.SwingUtilities ; 18 19 public class ConfigurationEditorPanel extends Container { 20 private transient ArrayList <XmlObjectStructureListener> m_listenerList; 21 private transient XmlObjectStructureChangeEvent m_changeEvent; 22 23 public ConfigurationEditorPanel() { 24 super(); 25 26 30 setOpaque(true); 31 } 32 33 public static void ensureXmlObject(JComponent comp) { 34 ConfigurationEditorPanel parent = (ConfigurationEditorPanel) 35 SwingUtilities.getAncestorOfClass(ConfigurationEditorPanel.class, comp); 36 37 if(parent != null) { 38 parent.ensureXmlObject(); 39 } 40 } 41 42 public void ensureXmlObject() { 43 ConfigurationEditorPanel parent = (ConfigurationEditorPanel) 44 SwingUtilities.getAncestorOfClass(ConfigurationEditorPanel.class, this); 45 46 if(parent != null) { 47 parent.ensureXmlObject(); 48 } 49 } 50 51 public void setDirty() { 52 ConfigurationEditorRoot editorRoot = getConfigurationEditorRoot(); 53 final IProject project = editorRoot.getProject(); 54 55 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable () { 56 public void run(){ 57 TcPlugin plugin = TcPlugin.getDefault(); 58 ConfigurationEditor editor = plugin.getConfigurationEditor(project); 59 60 if(editor != null) { 61 editor.setDirty(); 62 } 63 } 64 }); 65 } 66 67 70 private ConfigurationEditorRoot getConfigurationEditorRoot() { 71 if(this instanceof ConfigurationEditorRoot) { 72 return (ConfigurationEditorRoot)this; 73 } 74 else { 75 return (ConfigurationEditorRoot)getAncestorOfClass(ConfigurationEditorRoot.class); 76 } 77 } 78 79 public synchronized void addXmlObjectStructureListener(XmlObjectStructureListener listener) { 80 if(listener != null) { 81 if(m_listenerList == null) { 82 m_listenerList = new ArrayList <XmlObjectStructureListener>(); 83 } 84 m_listenerList.add(listener); 85 } 86 } 87 88 public synchronized void removeXmlObjectStructureListener(XmlObjectStructureListener listener) { 89 if(listener != null) { 90 if(m_listenerList != null) { 91 m_listenerList.remove(listener); 92 } 93 } 94 } 95 96 private XmlObjectStructureChangeEvent getChangeEvent(XmlObject source) { 97 if(m_changeEvent == null) { 98 m_changeEvent = new XmlObjectStructureChangeEvent(source); 99 } 100 else { 101 m_changeEvent.setXmlObject(source); 102 } 103 104 return m_changeEvent; 105 } 106 107 private XmlObjectStructureListener[] getListenerArray() { 108 return m_listenerList.toArray(new XmlObjectStructureListener[0]); 109 } 110 111 protected void fireXmlObjectStructureChanged(XmlObjectStructureChangeEvent e) { 112 if(m_listenerList != null) { 113 XmlObjectStructureListener[] listeners = getListenerArray(); 114 115 for(int i = 0; i < listeners.length; i++) { 116 listeners[i].structureChanged(e); 117 } 118 } 119 } 120 121 protected void fireXmlObjectStructureChanged(XmlObject source) { 122 fireXmlObjectStructureChanged(getChangeEvent(source)); 123 } 124 125 protected int parseInt(String s) { 126 try { 127 return Integer.parseInt(s); 128 } catch(Exception e) {return 0;} 129 } 130 } 131 | Popular Tags |