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.Combo;
011: import org.eclipse.swt.widgets.Composite;
012: import org.eclipse.swt.widgets.Group;
013: import org.eclipse.swt.widgets.Label;
014: import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureChangeEvent;
015: import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureListener;
016: import org.terracotta.dso.editors.xmlbeans.XmlStringEnumCombo;
017: import org.terracotta.ui.util.SWTLayout;
018: import org.terracotta.ui.util.SWTUtil;
019:
020: import com.terracottatech.config.DsoServerData;
021: import com.terracottatech.config.Persistence;
022:
023: public class PersistencePanel extends ConfigurationEditorPanel
024: implements XmlObjectStructureListener {
025: private DsoServerData m_dsoServerData;
026: private Persistence m_persistence;
027: private Layout m_layout;
028:
029: public PersistencePanel(Composite parent, int style) {
030: super (parent, style);
031: m_layout = new Layout(this );
032: SWTUtil.setBGColorRecurse(this .getDisplay().getSystemColor(
033: SWT.COLOR_WHITE), this );
034: }
035:
036: public void ensureXmlObject() {
037: super .ensureXmlObject();
038:
039: if (m_persistence == null) {
040: removeListeners();
041: m_persistence = m_dsoServerData.addNewPersistence();
042: updateChildren();
043: addListeners();
044: }
045: }
046:
047: public boolean hasAnySet() {
048: return m_persistence != null && m_persistence.isSetMode();
049: }
050:
051: public void structureChanged(XmlObjectStructureChangeEvent e) {
052: syncModel();
053: }
054:
055: private void syncModel() {
056: if (!hasAnySet() && m_dsoServerData.getPersistence() != null) {
057: m_dsoServerData.unsetPersistence();
058: m_persistence = null;
059: // fireXmlObjectStructureChanged();
060: // updateChildren();
061: }
062: fireXmlObjectStructureChanged();
063: }
064:
065: private void fireXmlObjectStructureChanged() {
066: fireXmlObjectStructureChanged(m_dsoServerData);
067: }
068:
069: private void addListeners() {
070: ((XmlStringEnumCombo) m_layout.m_persistenceModeCombo.getData())
071: .addXmlObjectStructureListener(this );
072: }
073:
074: private void removeListeners() {
075: ((XmlStringEnumCombo) m_layout.m_persistenceModeCombo.getData())
076: .removeXmlObjectStructureListener(this );
077: }
078:
079: private void updateChildren() {
080: m_layout.setPersistence(m_persistence);
081: }
082:
083: public void setup(DsoServerData dsoServerData) {
084: setEnabled(true);
085: removeListeners();
086:
087: m_dsoServerData = dsoServerData;
088: m_persistence = m_dsoServerData != null ? m_dsoServerData
089: .getPersistence() : null;
090:
091: updateChildren();
092: addListeners();
093: }
094:
095: public void tearDown() {
096: removeListeners();
097:
098: m_dsoServerData = null;
099: m_persistence = null;
100:
101: ((XmlStringEnumCombo) m_layout.m_persistenceModeCombo.getData())
102: .tearDown();
103:
104: setEnabled(false);
105: }
106:
107: private class Layout implements SWTLayout {
108: private static final String PERSISTENCE_MODE = "Mode";
109:
110: private Combo m_persistenceModeCombo;
111:
112: public void reset() {
113: resetServerFields(false);
114: }
115:
116: private void setPersistence(Persistence persistence) {
117: ((XmlStringEnumCombo) m_persistenceModeCombo.getData())
118: .setup(persistence);
119: }
120:
121: private void resetServerFields(boolean enabled) {
122: m_persistenceModeCombo.deselectAll();
123: m_persistenceModeCombo.setEnabled(enabled);
124: }
125:
126: private Layout(Composite parent) {
127: GridLayout gridLayout = new GridLayout();
128: gridLayout.marginWidth = gridLayout.marginHeight = 0;
129: parent.setLayout(gridLayout);
130:
131: Group panel = new Group(parent, SWT.NONE);
132: panel.setText("Persistence");
133: gridLayout = new GridLayout(2, false);
134: gridLayout.marginWidth = gridLayout.marginHeight = 10;
135: panel.setLayout(gridLayout);
136: GridData gridData = new GridData(GridData.FILL_BOTH);
137: panel.setLayoutData(gridData);
138:
139: Label persistenceModeLabel = new Label(panel, SWT.NONE);
140: persistenceModeLabel.setText(PERSISTENCE_MODE);
141:
142: m_persistenceModeCombo = new Combo(panel, SWT.BORDER
143: | SWT.READ_ONLY);
144: gridData = new GridData(GridData.GRAB_HORIZONTAL);
145: m_persistenceModeCombo.setLayoutData(gridData);
146: initStringEnumCombo(m_persistenceModeCombo,
147: Persistence.class, "mode");
148: }
149: }
150: }
|