]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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 | * Aaron Luchko, aluchko@redhat.com - 105926 [Formatter] Exporting Unnamed profile fails silently | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.jdt.internal.ui.preferences.cleanup; | |
13 | ||
14 | import java.util.Hashtable; | |
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
17 | import java.util.Map; | |
18 | import java.util.Observable; | |
19 | import java.util.Observer; | |
20 | ||
21 | import org.eclipse.swt.SWT; | |
22 | import org.eclipse.swt.layout.GridData; | |
23 | import org.eclipse.swt.widgets.Composite; | |
24 | import org.eclipse.swt.widgets.Shell; | |
25 | ||
26 | import org.eclipse.core.runtime.CoreException; | |
27 | import org.eclipse.core.runtime.preferences.DefaultScope; | |
28 | import org.eclipse.core.runtime.preferences.IEclipsePreferences; | |
29 | import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; | |
30 | import org.eclipse.core.runtime.preferences.IScopeContext; | |
31 | ||
32 | import org.eclipse.core.resources.IProject; | |
33 | ||
34 | import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; | |
35 | import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil; | |
36 | ||
37 | import org.eclipse.jdt.ui.JavaUI; | |
38 | import org.eclipse.jdt.ui.cleanup.CleanUpOptions; | |
39 | import org.eclipse.jdt.ui.cleanup.ICleanUp; | |
40 | ||
41 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
42 | import org.eclipse.jdt.internal.ui.fix.MapCleanUpOptions; | |
43 | import org.eclipse.jdt.internal.ui.preferences.BulletListBlock; | |
44 | import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess; | |
45 | import org.eclipse.jdt.internal.ui.preferences.formatter.IProfileVersioner; | |
46 | import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialog; | |
47 | import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileConfigurationBlock; | |
48 | import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager; | |
49 | import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile; | |
50 | import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile; | |
51 | import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore; | |
52 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; | |
53 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; | |
54 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; | |
55 | ||
56 | ||
57 | /** | |
58 | * The clean up configuration block for the clean up preference page. | |
59 | */ | |
60 | public class CleanUpConfigurationBlock extends ProfileConfigurationBlock { | |
61 | ||
62 | private static final String CLEANUP_PAGE_SETTINGS_KEY= "cleanup_page"; //$NON-NLS-1$ | |
63 | private static final String DIALOGSTORE_LASTSAVELOADPATH= JavaUI.ID_PLUGIN + ".cleanup"; //$NON-NLS-1$ | |
64 | ||
65 | private final IScopeContext fCurrContext; | |
66 | private SelectionButtonDialogField fShowCleanUpWizardDialogField; | |
67 | private CleanUpProfileManager fProfileManager; | |
68 | private ProfileStore fProfileStore; | |
69 | ||
70 | public CleanUpConfigurationBlock(IProject project, PreferencesAccess access) { | |
71 | super(project, access, DIALOGSTORE_LASTSAVELOADPATH); | |
72 | ||
73 | if (project != null) { | |
74 | fCurrContext= null; | |
75 | } else { | |
76 | fCurrContext= access.getInstanceScope(); | |
77 | } | |
78 | } | |
79 | ||
80 | @Override | |
81 | protected IProfileVersioner createProfileVersioner() { | |
82 | return new CleanUpProfileVersioner(); | |
83 | } | |
84 | ||
85 | @Override | |
86 | protected ProfileStore createProfileStore(IProfileVersioner versioner) { | |
87 | fProfileStore= new ProfileStore(CleanUpConstants.CLEANUP_PROFILES, versioner); | |
88 | return fProfileStore; | |
89 | } | |
90 | ||
91 | @Override | |
92 | protected ProfileManager createProfileManager(List<Profile> profiles, IScopeContext context, PreferencesAccess access, IProfileVersioner profileVersioner) { | |
93 | profiles.addAll(CleanUpPreferenceUtil.getBuiltInProfiles()); | |
94 | fProfileManager= new CleanUpProfileManager(profiles, context, access, profileVersioner); | |
95 | return fProfileManager; | |
96 | } | |
97 | ||
98 | /** | |
99 | * {@inheritDoc} | |
100 | */ | |
101 | @Override | |
102 | protected void configurePreview(Composite composite, int numColumns, final ProfileManager profileManager) { | |
103 | Map<String, String> settings= profileManager.getSelected().getSettings(); | |
104 | final Map<String, String> sharedSettings= new Hashtable<String, String>(); | |
105 | fill(settings, sharedSettings); | |
106 | ||
107 | final ICleanUp[] cleanUps= JavaPlugin.getDefault().getCleanUpRegistry().createCleanUps(); | |
108 | CleanUpOptions options= new MapCleanUpOptions(sharedSettings); | |
109 | for (int i= 0; i < cleanUps.length; i++) { | |
110 | cleanUps[i].setOptions(options); | |
111 | } | |
112 | ||
113 | createLabel(composite, CleanUpMessages.CleanUpConfigurationBlock_SelectedCleanUps_label, numColumns); | |
114 | ||
115 | final BulletListBlock cleanUpListBlock= new BulletListBlock(composite, SWT.NONE); | |
116 | GridData gridData= new GridData(SWT.FILL, SWT.FILL, true, true); | |
117 | gridData.horizontalSpan= numColumns; | |
118 | cleanUpListBlock.setLayoutData(gridData); | |
119 | cleanUpListBlock.setText(getSelectedCleanUpsInfo(cleanUps)); | |
120 | ||
121 | profileManager.addObserver(new Observer() { | |
122 | ||
123 | public void update(Observable o, Object arg) { | |
124 | final int value= ((Integer)arg).intValue(); | |
125 | switch (value) { | |
126 | case ProfileManager.PROFILE_CREATED_EVENT: | |
127 | case ProfileManager.PROFILE_DELETED_EVENT: | |
128 | case ProfileManager.SELECTION_CHANGED_EVENT: | |
129 | case ProfileManager.SETTINGS_CHANGED_EVENT: | |
130 | fill(profileManager.getSelected().getSettings(), sharedSettings); | |
131 | cleanUpListBlock.setText(getSelectedCleanUpsInfo(cleanUps)); | |
132 | } | |
133 | } | |
134 | ||
135 | }); | |
136 | } | |
137 | ||
138 | private String getSelectedCleanUpsInfo(ICleanUp[] cleanUps) { | |
139 | if (cleanUps.length == 0) | |
140 | return ""; //$NON-NLS-1$ | |
141 | ||
142 | StringBuffer buf= new StringBuffer(); | |
143 | ||
144 | boolean first= true; | |
145 | for (int i= 0; i < cleanUps.length; i++) { | |
146 | String[] descriptions= cleanUps[i].getStepDescriptions(); | |
147 | if (descriptions != null) { | |
148 | for (int j= 0; j < descriptions.length; j++) { | |
149 | if (first) { | |
150 | first= false; | |
151 | } else { | |
152 | buf.append('\n'); | |
153 | } | |
154 | buf.append(descriptions[j]); | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | return buf.toString(); | |
160 | } | |
161 | ||
162 | private void fill(Map<String, String> settings, Map<String, String> sharedSettings) { | |
163 | sharedSettings.clear(); | |
164 | for (Iterator<String> iterator= settings.keySet().iterator(); iterator.hasNext();) { | |
165 | String key= iterator.next(); | |
166 | sharedSettings.put(key, settings.get(key)); | |
167 | } | |
168 | } | |
169 | ||
170 | @Override | |
171 | protected ModifyDialog createModifyDialog(Shell shell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile) { | |
172 | return new CleanUpModifyDialog(shell, profile, profileManager, profileStore, newProfile, CLEANUP_PAGE_SETTINGS_KEY, DIALOGSTORE_LASTSAVELOADPATH); | |
173 | } | |
174 | ||
175 | /** | |
176 | * {@inheritDoc} | |
177 | */ | |
178 | @Override | |
179 | public Composite createContents(Composite parent) { | |
180 | Composite composite= super.createContents(parent); | |
181 | ||
182 | if (fCurrContext == null) | |
183 | return composite; | |
184 | ||
185 | fShowCleanUpWizardDialogField= new SelectionButtonDialogField(SWT.CHECK); | |
186 | fShowCleanUpWizardDialogField.setLabelText(CleanUpMessages.CleanUpConfigurationBlock_ShowCleanUpWizard_checkBoxLabel); | |
187 | fShowCleanUpWizardDialogField.doFillIntoGrid(composite, 5); | |
188 | ||
189 | IEclipsePreferences node= fCurrContext.getNode(JavaUI.ID_PLUGIN); | |
190 | boolean showWizard; | |
191 | if (node.get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, null) != null) { | |
192 | showWizard= node.getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true); | |
193 | } else { | |
194 | showWizard= DefaultScope.INSTANCE.getNode(JavaUI.ID_PLUGIN).getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true); | |
195 | } | |
196 | if (showWizard) | |
197 | fShowCleanUpWizardDialogField.setSelection(true); | |
198 | ||
199 | fShowCleanUpWizardDialogField.setDialogFieldListener(new IDialogFieldListener() { | |
200 | public void dialogFieldChanged(DialogField field) { | |
201 | doShowCleanUpWizard(fShowCleanUpWizardDialogField.isSelected()); | |
202 | } | |
203 | }); | |
204 | ||
205 | return composite; | |
206 | } | |
207 | ||
208 | private void doShowCleanUpWizard(boolean showWizard) { | |
209 | IEclipsePreferences preferences= fCurrContext.getNode(JavaUI.ID_PLUGIN); | |
210 | if (preferences.get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, null) != null && | |
211 | preferences.getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true) == showWizard) | |
212 | return; | |
213 | ||
214 | preferences.putBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, showWizard); | |
215 | } | |
216 | ||
217 | /** | |
218 | * {@inheritDoc} | |
219 | */ | |
220 | @Override | |
221 | public void performDefaults() { | |
222 | super.performDefaults(); | |
223 | if (fCurrContext == null) | |
224 | return; | |
225 | ||
226 | fCurrContext.getNode(JavaUI.ID_PLUGIN).remove(CleanUpConstants.SHOW_CLEAN_UP_WIZARD); | |
227 | boolean showWizard= DefaultScope.INSTANCE.getNode(JavaUI.ID_PLUGIN).getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true); | |
228 | fShowCleanUpWizardDialogField.setDialogFieldListener(null); | |
229 | fShowCleanUpWizardDialogField.setSelection(showWizard); | |
230 | fShowCleanUpWizardDialogField.setDialogFieldListener(new IDialogFieldListener() { | |
231 | public void dialogFieldChanged(DialogField field) { | |
232 | doShowCleanUpWizard(fShowCleanUpWizardDialogField.isSelected()); | |
233 | } | |
234 | }); | |
235 | } | |
236 | ||
237 | /** | |
238 | * {@inheritDoc} | |
239 | */ | |
240 | @Override | |
241 | protected void preferenceChanged(PreferenceChangeEvent event) { | |
242 | if (CleanUpConstants.CLEANUP_PROFILES.equals(event.getKey())) { | |
243 | try { | |
244 | String id= fCurrContext.getNode(JavaUI.ID_PLUGIN).get(CleanUpConstants.CLEANUP_PROFILE, null); | |
245 | if (id == null) | |
246 | fProfileManager.getDefaultProfile().getID(); | |
247 | ||
248 | List<Profile> oldProfiles= fProfileManager.getSortedProfiles(); | |
249 | Profile[] oldProfilesArray= oldProfiles.toArray(new Profile[oldProfiles.size()]); | |
250 | for (int i= 0; i < oldProfilesArray.length; i++) { | |
251 | if (oldProfilesArray[i] instanceof CustomProfile) { | |
252 | fProfileManager.deleteProfile((CustomProfile)oldProfilesArray[i]); | |
253 | } | |
254 | } | |
255 | ||
256 | List<Profile> newProfiles= fProfileStore.readProfilesFromString((String)event.getNewValue()); | |
257 | for (Iterator<Profile> iterator= newProfiles.iterator(); iterator.hasNext();) { | |
258 | CustomProfile profile= (CustomProfile)iterator.next(); | |
259 | fProfileManager.addProfile(profile); | |
260 | } | |
261 | ||
262 | Profile profile= fProfileManager.getProfile(id); | |
263 | if (profile != null) { | |
264 | fProfileManager.setSelected(profile); | |
265 | } else { | |
266 | fProfileManager.setSelected(fProfileManager.getDefaultProfile()); | |
267 | } | |
268 | } catch (CoreException e) { | |
269 | JavaPlugin.log(e); | |
270 | } | |
271 | } else if (CleanUpConstants.CLEANUP_PROFILE.equals(event.getKey())) { | |
272 | if (event.getNewValue() == null) { | |
273 | fProfileManager.setSelected(fProfileManager.getDefaultProfile()); | |
274 | } else { | |
275 | Profile profile= fProfileManager.getProfile((String)event.getNewValue()); | |
276 | if (profile != null) { | |
277 | fProfileManager.setSelected(profile); | |
278 | } | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | } |