KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > saveparticipant > AbstractSaveParticipantPreferenceConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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     /**
37      * Preference prefix that is appended to the id of {@link SaveParticipantDescriptor save participants}.
38      *
39      * <p>
40      * Value is of type <code>Boolean</code>.
41      * </p>
42      *
43      * @see SaveParticipantDescriptor
44      * @since 3.3
45      */

46     private static final String JavaDoc EDITOR_SAVE_PARTICIPANT_PREFIX= "editor_save_participant_"; //$NON-NLS-1$
47

48     private SelectionButtonDialogField fEnableField;
49     private Control fConfigControl;
50     private IScopeContext fContext;
51     private ControlEnableState fConfigControlEnabledState;
52
53     /**
54      * The id of the post save listener managed by this configuration block, not null
55      */

56     protected abstract String JavaDoc getPostSaveListenerId();
57     
58     /**
59      * The name of the post save listener managed by this configuration block, not null
60      */

61     protected abstract String JavaDoc getPostSaveListenerName();
62     
63     protected Control createConfigControl(Composite composite, IPreferencePageContainer container) {
64         //Default has no specific controls
65
return null;
66     }
67     
68     /**
69      * {@inheritDoc}
70      */

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     /**
90      * {@inheritDoc}
91      */

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     /**
110      * {@inheritDoc}
111      */

112     public void dispose() {}
113     
114     /**
115      * {@inheritDoc}
116      */

117     public void performDefaults() {
118         String JavaDoc 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     /**
125      * {@inheritDoc}
126      */

127     public void performOk() {}
128     
129     /**
130      * {@inheritDoc}
131      */

132     public void enableProjectSettings() {
133         fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(getPreferenceKey(), fEnableField.isSelected());
134     }
135     
136     /**
137      * {@inheritDoc}
138      */

139     public void disableProjectSettings() {
140         fContext.getNode(JavaUI.ID_PLUGIN).remove(getPreferenceKey());
141     }
142     
143     /**
144      * {@inheritDoc}
145      */

146     public boolean hasSettingsInScope(IScopeContext context) {
147         return context.getNode(JavaUI.ID_PLUGIN).get(getPreferenceKey(), null) != null;
148     }
149     
150     /**
151      * {@inheritDoc}
152      */

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 JavaDoc 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 JavaDoc getPreferenceKey() {
179         return EDITOR_SAVE_PARTICIPANT_PREFIX + getPostSaveListenerId();
180     }
181 }
182
Popular Tags