]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
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 | |
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.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; | |
18 | ||
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; | |
24 | ||
25 | import org.eclipse.core.resources.ProjectScope; | |
26 | ||
27 | import org.eclipse.jface.preference.IPreferencePageContainer; | |
28 | ||
29 | import org.eclipse.jdt.ui.JavaUI; | |
30 | ||
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; | |
34 | ||
35 | public abstract class AbstractSaveParticipantPreferenceConfiguration implements ISaveParticipantPreferenceConfiguration { | |
36 | ||
37 | /** | |
38 | * Preference prefix that is prepended to the id of {@link SaveParticipantDescriptor save participants}. | |
39 | * | |
40 | * <p> | |
41 | * Value is of type <code>Boolean</code>. | |
42 | * </p> | |
43 | * | |
44 | * @see SaveParticipantDescriptor | |
45 | * @since 3.3 | |
46 | */ | |
47 | public static final String EDITOR_SAVE_PARTICIPANT_PREFIX= "editor_save_participant_"; //$NON-NLS-1$ | |
48 | ||
49 | private SelectionButtonDialogField fEnableField; | |
50 | private IScopeContext fContext; | |
51 | ||
52 | /** | |
53 | * @return id of the post save listener managed by this configuration block | |
54 | */ | |
55 | protected abstract String getPostSaveListenerId(); | |
56 | ||
57 | /** | |
58 | * @return human readable name of the post save listener managed by this configuration block | |
59 | */ | |
60 | protected abstract String getPostSaveListenerName(); | |
61 | ||
62 | /** | |
63 | * Subclasses can add specific controls | |
64 | * | |
65 | * @param parent the parent to use to add the control to | |
66 | * @param container the container showing the preferences | |
67 | */ | |
68 | protected void createConfigControl(Composite parent, IPreferencePageContainer container) { | |
69 | //Default has no specific controls | |
70 | } | |
71 | ||
72 | /** | |
73 | * {@inheritDoc} | |
74 | */ | |
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); | |
83 | ||
84 | fEnableField= new SelectionButtonDialogField(SWT.CHECK); | |
85 | fEnableField.setLabelText(getPostSaveListenerName()); | |
86 | fEnableField.doFillIntoGrid(composite, 1); | |
87 | ||
88 | createConfigControl(composite, container); | |
89 | ||
90 | return composite; | |
91 | } | |
92 | ||
93 | /** | |
94 | * {@inheritDoc} | |
95 | */ | |
96 | public void initialize(final IScopeContext context, IAdaptable element) { | |
97 | boolean enabled= isEnabled(context); | |
98 | fEnableField.setSelection(enabled); | |
99 | ||
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()); | |
104 | } | |
105 | ||
106 | }); | |
107 | ||
108 | fContext= context; | |
109 | ||
110 | enabled(enabled); | |
111 | } | |
112 | ||
113 | /** | |
114 | * {@inheritDoc} | |
115 | */ | |
116 | public void dispose() {} | |
117 | ||
118 | /** | |
119 | * {@inheritDoc} | |
120 | */ | |
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); | |
126 | } else { | |
127 | defaultEnabled= DefaultScope.INSTANCE.getNode(JavaUI.ID_PLUGIN).getBoolean(key, false); | |
128 | } | |
129 | fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(key, defaultEnabled); | |
130 | fEnableField.setSelection(defaultEnabled); | |
131 | ||
132 | enabled(defaultEnabled); | |
133 | } | |
134 | ||
135 | /** | |
136 | * {@inheritDoc} | |
137 | */ | |
138 | public void performOk() {} | |
139 | ||
140 | /** | |
141 | * {@inheritDoc} | |
142 | */ | |
143 | public void enableProjectSettings() { | |
144 | fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(getPreferenceKey(), fEnableField.isSelected()); | |
145 | } | |
146 | ||
147 | /** | |
148 | * {@inheritDoc} | |
149 | */ | |
150 | public void disableProjectSettings() { | |
151 | fContext.getNode(JavaUI.ID_PLUGIN).remove(getPreferenceKey()); | |
152 | } | |
153 | ||
154 | /** | |
155 | * {@inheritDoc} | |
156 | */ | |
157 | public boolean hasSettingsInScope(IScopeContext context) { | |
158 | return context.getNode(JavaUI.ID_PLUGIN).get(getPreferenceKey(), null) != null; | |
159 | } | |
160 | ||
161 | /** | |
162 | * {@inheritDoc} | |
163 | */ | |
164 | public boolean isEnabled(IScopeContext context) { | |
165 | IEclipsePreferences node; | |
166 | if (hasSettingsInScope(context)) { | |
167 | node= context.getNode(JavaUI.ID_PLUGIN); | |
168 | } else { | |
169 | node= InstanceScope.INSTANCE.getNode(JavaUI.ID_PLUGIN); | |
170 | } | |
171 | IEclipsePreferences defaultNode= DefaultScope.INSTANCE.getNode(JavaUI.ID_PLUGIN); | |
172 | ||
173 | String key= getPreferenceKey(); | |
174 | return node.getBoolean(key, defaultNode.getBoolean(key, false)); | |
175 | } | |
176 | ||
177 | /** | |
178 | * @param enabled true if this save action has been enabled by user, false otherwise | |
179 | */ | |
180 | protected void enabled(boolean enabled) { | |
181 | } | |
182 | ||
183 | private String getPreferenceKey() { | |
184 | return EDITOR_SAVE_PARTICIPANT_PREFIX + getPostSaveListenerId(); | |
185 | } | |
186 | } |