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