1 11 package org.eclipse.jdt.internal.ui.javaeditor.saveparticipant; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.preferences.DefaultScope; 15 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 16 import org.eclipse.core.runtime.preferences.IScopeContext; 17 import org.eclipse.core.runtime.preferences.InstanceScope; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.layout.GridData; 21 import org.eclipse.swt.layout.GridLayout; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 25 import org.eclipse.jface.dialogs.ControlEnableState; 26 import org.eclipse.jface.preference.IPreferencePageContainer; 27 28 import org.eclipse.jdt.ui.JavaUI; 29 30 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 31 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 32 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 33 34 public abstract class AbstractSaveParticipantPreferenceConfiguration implements ISaveParticipantPreferenceConfiguration { 35 36 46 private static final String EDITOR_SAVE_PARTICIPANT_PREFIX= "editor_save_participant_"; 48 private SelectionButtonDialogField fEnableField; 49 private Control fConfigControl; 50 private IScopeContext fContext; 51 private ControlEnableState fConfigControlEnabledState; 52 53 56 protected abstract String getPostSaveListenerId(); 57 58 61 protected abstract String getPostSaveListenerName(); 62 63 protected Control createConfigControl(Composite composite, IPreferencePageContainer container) { 64 return null; 66 } 67 68 71 public Control createControl(Composite parent, IPreferencePageContainer container) { 72 Composite composite= new Composite(parent, SWT.NONE); 73 GridData gridData= new GridData(SWT.FILL, SWT.TOP, true, false); 74 composite.setLayoutData(gridData); 75 GridLayout layout= new GridLayout(); 76 layout.marginHeight= 0; 77 layout.marginWidth= 0; 78 composite.setLayout(layout); 79 80 fEnableField= new SelectionButtonDialogField(SWT.CHECK); 81 fEnableField.setLabelText(getPostSaveListenerName()); 82 fEnableField.doFillIntoGrid(composite, 1); 83 84 fConfigControl= createConfigControl(composite, container); 85 86 return composite; 87 } 88 89 92 public void initialize(final IScopeContext context, IAdaptable element) { 93 boolean enabled= isEnabled(context); 94 fEnableField.setSelection(enabled); 95 96 if (fConfigControl != null && !enabled) { 97 fConfigControlEnabledState= ControlEnableState.disable(fConfigControl); 98 } 99 100 fEnableField.setDialogFieldListener(new IDialogFieldListener() { 101 public void dialogFieldChanged(DialogField field) { 102 enableConfigControl(fEnableField.isSelected()); 103 } 104 }); 105 106 fContext= context; 107 } 108 109 112 public void dispose() {} 113 114 117 public void performDefaults() { 118 String key= getPreferenceKey(); 119 boolean defaultEnabled= new DefaultScope().getNode(JavaUI.ID_PLUGIN).getBoolean(key, false); 120 fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(key, defaultEnabled); 121 fEnableField.setSelection(defaultEnabled); 122 } 123 124 127 public void performOk() {} 128 129 132 public void enableProjectSettings() { 133 fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(getPreferenceKey(), fEnableField.isSelected()); 134 } 135 136 139 public void disableProjectSettings() { 140 fContext.getNode(JavaUI.ID_PLUGIN).remove(getPreferenceKey()); 141 } 142 143 146 public boolean hasSettingsInScope(IScopeContext context) { 147 return context.getNode(JavaUI.ID_PLUGIN).get(getPreferenceKey(), null) != null; 148 } 149 150 153 public boolean isEnabled(IScopeContext context) { 154 IEclipsePreferences node; 155 if (hasSettingsInScope(context)) { 156 node= context.getNode(JavaUI.ID_PLUGIN); 157 } else { 158 node= new InstanceScope().getNode(JavaUI.ID_PLUGIN); 159 } 160 IEclipsePreferences defaultNode= new DefaultScope().getNode(JavaUI.ID_PLUGIN); 161 162 String key= getPreferenceKey(); 163 return node.getBoolean(key, defaultNode.getBoolean(key, false)); 164 } 165 166 protected void enableConfigControl(boolean isEnabled) { 167 fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(getPreferenceKey(), isEnabled); 168 if (fConfigControl != null) { 169 if (fConfigControlEnabledState != null) { 170 fConfigControlEnabledState.restore(); 171 fConfigControlEnabledState= null; 172 } else { 173 fConfigControlEnabledState= ControlEnableState.disable(fConfigControl); 174 } 175 } 176 } 177 178 private String getPreferenceKey() { 179 return EDITOR_SAVE_PARTICIPANT_PREFIX + getPostSaveListenerId(); 180 } 181 } 182 | Popular Tags |