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;
006:
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.layout.GridData;
009: import org.eclipse.swt.layout.GridLayout;
010: import org.eclipse.swt.widgets.Button;
011: import org.eclipse.swt.widgets.Composite;
012: import org.eclipse.swt.widgets.Group;
013: import org.eclipse.swt.widgets.Label;
014: import org.eclipse.swt.widgets.Spinner;
015: import org.terracotta.dso.editors.xmlbeans.XmlBooleanToggle;
016: import org.terracotta.dso.editors.xmlbeans.XmlIntegerSpinner;
017: import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureChangeEvent;
018: import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureListener;
019: import org.terracotta.ui.util.SWTLayout;
020: import org.terracotta.ui.util.SWTUtil;
021:
022: import com.terracottatech.config.DsoServerData;
023: import com.terracottatech.config.GarbageCollection;
024:
025: public class GarbageCollectionPanel extends ConfigurationEditorPanel
026: implements XmlObjectStructureListener {
027: private DsoServerData m_dsoServerData;
028: private GarbageCollection m_garbageCollection;
029: private Layout m_layout;
030:
031: public GarbageCollectionPanel(Composite parent, int style) {
032: super (parent, style);
033: m_layout = new Layout(this );
034: SWTUtil.setBGColorRecurse(this .getDisplay().getSystemColor(
035: SWT.COLOR_WHITE), this );
036: }
037:
038: public void ensureXmlObject() {
039: super .ensureXmlObject();
040:
041: if (m_garbageCollection == null) {
042: removeListeners();
043: m_garbageCollection = m_dsoServerData
044: .addNewGarbageCollection();
045: updateChildren();
046: addListeners();
047: }
048: }
049:
050: public boolean hasAnySet() {
051: return m_garbageCollection != null
052: && (m_garbageCollection.isSetEnabled()
053: || m_garbageCollection.isSetVerbose() || m_garbageCollection
054: .isSetInterval());
055: }
056:
057: public void structureChanged(XmlObjectStructureChangeEvent e) {
058: testUnsetGarbageCollection();
059: }
060:
061: private void testUnsetGarbageCollection() {
062: if (!hasAnySet()
063: && m_dsoServerData.getGarbageCollection() != null) {
064: m_dsoServerData.unsetGarbageCollection();
065: m_garbageCollection = null;
066: fireXmlObjectStructureChanged();
067: updateChildren();
068: } else {
069: fireServerChanged();
070: }
071: }
072:
073: void fireServerChanged() {
074: DsoServerDataPanel panel = (DsoServerDataPanel) SWTUtil
075: .getAncestorOfClass(DsoServerDataPanel.class, this );
076: panel.fireServerChanged();
077: }
078:
079: private void fireXmlObjectStructureChanged() {
080: fireXmlObjectStructureChanged(m_dsoServerData);
081: }
082:
083: private void addListeners() {
084: ((XmlBooleanToggle) m_layout.m_gcCheck.getData())
085: .addXmlObjectStructureListener(this );
086: ((XmlIntegerSpinner) m_layout.m_gcIntervalSpinner.getData())
087: .addXmlObjectStructureListener(this );
088: ((XmlBooleanToggle) m_layout.m_verboseCheck.getData())
089: .addXmlObjectStructureListener(this );
090: }
091:
092: private void removeListeners() {
093: ((XmlBooleanToggle) m_layout.m_gcCheck.getData())
094: .removeXmlObjectStructureListener(this );
095: ((XmlIntegerSpinner) m_layout.m_gcIntervalSpinner.getData())
096: .removeXmlObjectStructureListener(this );
097: ((XmlBooleanToggle) m_layout.m_verboseCheck.getData())
098: .removeXmlObjectStructureListener(this );
099: }
100:
101: private void updateChildren() {
102: m_layout.setGarbageCollection(m_garbageCollection);
103: }
104:
105: public void setup(DsoServerData dsoServerData) {
106: setEnabled(true);
107: removeListeners();
108:
109: m_dsoServerData = dsoServerData;
110: m_garbageCollection = m_dsoServerData != null ? m_dsoServerData
111: .getGarbageCollection() : null;
112:
113: updateChildren();
114: addListeners();
115: }
116:
117: public void tearDown() {
118: removeListeners();
119:
120: m_dsoServerData = null;
121: m_garbageCollection = null;
122:
123: m_layout.tearDown();
124:
125: setEnabled(false);
126: }
127:
128: private class Layout implements SWTLayout {
129: private static final String GARBAGE_COLLECTION = "Garbage Collection";
130: private static final String GC_ENABLED = "Enabled";
131: private static final String VERBOSE = "Verbose";
132: private static final String GC_INTERVAL = "GC Interval (seconds)";
133:
134: private Button m_gcCheck;
135: private Spinner m_gcIntervalSpinner;
136: private Button m_verboseCheck;
137:
138: public void reset() {
139: setGarbageCollection(null);
140: }
141:
142: private void setGarbageCollection(GarbageCollection gc) {
143: ((XmlBooleanToggle) m_gcCheck.getData()).setup(gc);
144: ((XmlIntegerSpinner) m_gcIntervalSpinner.getData())
145: .setup(gc);
146: ((XmlBooleanToggle) m_verboseCheck.getData()).setup(gc);
147: }
148:
149: void tearDown() {
150: ((XmlBooleanToggle) m_gcCheck.getData()).tearDown();
151: ((XmlIntegerSpinner) m_gcIntervalSpinner.getData())
152: .tearDown();
153: ((XmlBooleanToggle) m_verboseCheck.getData()).tearDown();
154: }
155:
156: private Layout(Composite parent) {
157: GridLayout gridLayout = new GridLayout();
158: gridLayout.marginWidth = gridLayout.marginHeight = 0;
159: parent.setLayout(gridLayout);
160:
161: Group panel = new Group(parent, SWT.NONE);
162: panel.setText(GARBAGE_COLLECTION);
163: gridLayout = new GridLayout();
164: gridLayout.marginWidth = gridLayout.marginHeight = 10;
165: panel.setLayout(gridLayout);
166: GridData gridData = new GridData(GridData.FILL_BOTH);
167: panel.setLayoutData(gridData);
168:
169: m_gcCheck = new Button(panel, SWT.CHECK);
170: m_gcCheck.setText(GC_ENABLED);
171: initBooleanField(m_gcCheck, GarbageCollection.class,
172: "enabled");
173:
174: Composite intervalPanel = new Composite(panel, SWT.NONE);
175: gridLayout = new GridLayout(2, false);
176: intervalPanel.setLayout(gridLayout);
177: Label gcIntervalLabel = new Label(intervalPanel, SWT.NONE);
178: gcIntervalLabel.setText(GC_INTERVAL);
179:
180: m_gcIntervalSpinner = new Spinner(intervalPanel, SWT.BORDER);
181: gridData = new GridData();
182: m_gcIntervalSpinner.setLayoutData(gridData);
183: initIntegerSpinnerField(m_gcIntervalSpinner,
184: GarbageCollection.class, "interval");
185:
186: m_verboseCheck = new Button(panel, SWT.CHECK);
187: m_verboseCheck.setText(VERBOSE);
188: initBooleanField(m_verboseCheck, GarbageCollection.class,
189: "verbose");
190: }
191: }
192: }
|