1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.javaeditor.saveparticipant;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.preferences.DefaultScope;
21 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22 import org.eclipse.core.runtime.preferences.IScopeContext;
23 import org.eclipse.core.runtime.preferences.InstanceScope;
25 import org.eclipse.core.resources.ProjectScope;
27 import org.eclipse.jface.preference.IPreferencePageContainer;
29 import org.eclipse.jdt.ui.JavaUI;
31 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
32 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
33 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
35 public abstract class AbstractSaveParticipantPreferenceConfiguration implements ISaveParticipantPreferenceConfiguration {
38 * Preference prefix that is prepended to the id of {@link SaveParticipantDescriptor save participants}.
41 * Value is of type <code>Boolean</code>.
44 * @see SaveParticipantDescriptor
47 public static final String EDITOR_SAVE_PARTICIPANT_PREFIX= "editor_save_participant_"; //$NON-NLS-1$
49 private SelectionButtonDialogField fEnableField;
50 private IScopeContext fContext;
53 * @return id of the post save listener managed by this configuration block
55 protected abstract String getPostSaveListenerId();
58 * @return human readable name of the post save listener managed by this configuration block
60 protected abstract String getPostSaveListenerName();
63 * Subclasses can add specific controls
65 * @param parent the parent to use to add the control to
66 * @param container the container showing the preferences
68 protected void createConfigControl(Composite parent, IPreferencePageContainer container) {
69 //Default has no specific controls
75 public Control createControl(Composite parent, IPreferencePageContainer container) {
76 Composite composite= new Composite(parent, SWT.NONE);
77 GridData gridData= new GridData(SWT.FILL, SWT.FILL, true, true);
78 composite.setLayoutData(gridData);
79 GridLayout layout= new GridLayout();
80 layout.marginHeight= 0;
81 layout.marginWidth= 0;
82 composite.setLayout(layout);
84 fEnableField= new SelectionButtonDialogField(SWT.CHECK);
85 fEnableField.setLabelText(getPostSaveListenerName());
86 fEnableField.doFillIntoGrid(composite, 1);
88 createConfigControl(composite, container);
96 public void initialize(final IScopeContext context, IAdaptable element) {
97 boolean enabled= isEnabled(context);
98 fEnableField.setSelection(enabled);
100 fEnableField.setDialogFieldListener(new IDialogFieldListener() {
101 public void dialogFieldChanged(DialogField field) {
102 fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(getPreferenceKey(), fEnableField.isSelected());
103 enabled(fEnableField.isSelected());
116 public void dispose() {}
121 public void performDefaults() {
122 String key= getPreferenceKey();
123 boolean defaultEnabled;
124 if (ProjectScope.SCOPE.equals(fContext.getName())) {
125 defaultEnabled= InstanceScope.INSTANCE.getNode(JavaUI.ID_PLUGIN).getBoolean(key, false);
127 defaultEnabled= DefaultScope.INSTANCE.getNode(JavaUI.ID_PLUGIN).getBoolean(key, false);
129 fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(key, defaultEnabled);
130 fEnableField.setSelection(defaultEnabled);
132 enabled(defaultEnabled);
138 public void performOk() {}
143 public void enableProjectSettings() {
144 fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(getPreferenceKey(), fEnableField.isSelected());
150 public void disableProjectSettings() {
151 fContext.getNode(JavaUI.ID_PLUGIN).remove(getPreferenceKey());
157 public boolean hasSettingsInScope(IScopeContext context) {
158 return context.getNode(JavaUI.ID_PLUGIN).get(getPreferenceKey(), null) != null;
164 public boolean isEnabled(IScopeContext context) {
165 IEclipsePreferences node;
166 if (hasSettingsInScope(context)) {
167 node= context.getNode(JavaUI.ID_PLUGIN);
169 node= InstanceScope.INSTANCE.getNode(JavaUI.ID_PLUGIN);
171 IEclipsePreferences defaultNode= DefaultScope.INSTANCE.getNode(JavaUI.ID_PLUGIN);
173 String key= getPreferenceKey();
174 return node.getBoolean(key, defaultNode.getBoolean(key, false));
178 * @param enabled true if this save action has been enabled by user, false otherwise
180 protected void enabled(boolean enabled) {
183 private String getPreferenceKey() {
184 return EDITOR_SAVE_PARTICIPANT_PREFIX + getPostSaveListenerId();