001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Aaron Luchko, aluchko@redhat.com - 105926 [Formatter] Exporting Unnamed profile fails silently
011: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences.cleanup;
012:
013: import java.util.Hashtable;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Map;
017: import java.util.Observable;
018: import java.util.Observer;
019:
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.preferences.DefaultScope;
022: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
023: import org.eclipse.core.runtime.preferences.IScopeContext;
024: import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
025:
026: import org.eclipse.core.resources.IProject;
027:
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.layout.GridData;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Shell;
033:
034: import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
035: import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil;
036: import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
037:
038: import org.eclipse.jdt.ui.JavaUI;
039:
040: import org.eclipse.jdt.internal.ui.JavaPlugin;
041: import org.eclipse.jdt.internal.ui.fix.ICleanUp;
042: import org.eclipse.jdt.internal.ui.preferences.BulletListBlock;
043: import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess;
044: import org.eclipse.jdt.internal.ui.preferences.formatter.IProfileVersioner;
045: import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialog;
046: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileConfigurationBlock;
047: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager;
048: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore;
049: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
050: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
051: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
052: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
053: import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
054:
055: /**
056: * The clean up configuration block for the clean up preference page.
057: */
058: public class CleanUpConfigurationBlock extends
059: ProfileConfigurationBlock {
060:
061: private static final String CLEANUP_PAGE_SETTINGS_KEY = "cleanup_page"; //$NON-NLS-1$
062: private static final String DIALOGSTORE_LASTSAVELOADPATH = JavaUI.ID_PLUGIN
063: + ".cleanup"; //$NON-NLS-1$
064:
065: private final IScopeContext fCurrContext;
066: private SelectionButtonDialogField fShowCleanUpWizardDialogField;
067: private CleanUpProfileManager fProfileManager;
068: private ProfileStore fProfileStore;
069: private BulletListBlock fBrowserBlock;
070:
071: public CleanUpConfigurationBlock(IProject project,
072: PreferencesAccess access) {
073: super (project, access, DIALOGSTORE_LASTSAVELOADPATH);
074:
075: if (project != null) {
076: fCurrContext = null;
077: } else {
078: fCurrContext = access.getInstanceScope();
079: }
080: }
081:
082: protected IProfileVersioner createProfileVersioner() {
083: return new CleanUpProfileVersioner();
084: }
085:
086: protected ProfileStore createProfileStore(
087: IProfileVersioner versioner) {
088: fProfileStore = new ProfileStore(
089: CleanUpConstants.CLEANUP_PROFILES, versioner);
090: return fProfileStore;
091: }
092:
093: protected ProfileManager createProfileManager(List profiles,
094: IScopeContext context, PreferencesAccess access,
095: IProfileVersioner profileVersioner) {
096: profiles.addAll(CleanUpPreferenceUtil.getBuiltInProfiles());
097: fProfileManager = new CleanUpProfileManager(profiles, context,
098: access, profileVersioner);
099: return fProfileManager;
100: }
101:
102: /**
103: * {@inheritDoc}
104: */
105: protected void configurePreview(Composite composite,
106: int numColumns, final ProfileManager profileManager) {
107: Map settings = profileManager.getSelected().getSettings();
108: final Map sharedSettings = new Hashtable();
109: fill(settings, sharedSettings);
110:
111: final ICleanUp[] cleanUps = CleanUpRefactoring
112: .createCleanUps(sharedSettings);
113:
114: createLabel(
115: composite,
116: CleanUpMessages.CleanUpConfigurationBlock_SelectedCleanUps_label,
117: numColumns);
118:
119: fBrowserBlock = new BulletListBlock();
120: Control control = fBrowserBlock.createControl(composite);
121: ((GridData) control.getLayoutData()).horizontalSpan = numColumns;
122: fBrowserBlock.setText(getSelectedCleanUpsInfo(cleanUps));
123:
124: profileManager.addObserver(new Observer() {
125:
126: public void update(Observable o, Object arg) {
127: final int value = ((Integer) arg).intValue();
128: switch (value) {
129: case ProfileManager.PROFILE_CREATED_EVENT:
130: case ProfileManager.PROFILE_DELETED_EVENT:
131: case ProfileManager.SELECTION_CHANGED_EVENT:
132: case ProfileManager.SETTINGS_CHANGED_EVENT:
133: fill(profileManager.getSelected().getSettings(),
134: sharedSettings);
135: fBrowserBlock
136: .setText(getSelectedCleanUpsInfo(cleanUps));
137: }
138: }
139:
140: });
141: }
142:
143: private String getSelectedCleanUpsInfo(ICleanUp[] cleanUps) {
144: if (cleanUps.length == 0)
145: return ""; //$NON-NLS-1$
146:
147: StringBuffer buf = new StringBuffer();
148:
149: boolean first = true;
150: for (int i = 0; i < cleanUps.length; i++) {
151: String[] descriptions = cleanUps[i].getDescriptions();
152: if (descriptions != null) {
153: for (int j = 0; j < descriptions.length; j++) {
154: if (first) {
155: first = false;
156: } else {
157: buf.append('\n');
158: }
159: buf.append(descriptions[j]);
160: }
161: }
162: }
163:
164: return buf.toString();
165: }
166:
167: private void fill(Map settings, Map sharedSettings) {
168: sharedSettings.clear();
169: for (Iterator iterator = settings.keySet().iterator(); iterator
170: .hasNext();) {
171: String key = (String) iterator.next();
172: sharedSettings.put(key, settings.get(key));
173: }
174: }
175:
176: protected ModifyDialog createModifyDialog(Shell shell,
177: Profile profile, ProfileManager profileManager,
178: ProfileStore profileStore, boolean newProfile) {
179: return new CleanUpModifyDialog(shell, profile, profileManager,
180: profileStore, newProfile, CLEANUP_PAGE_SETTINGS_KEY,
181: DIALOGSTORE_LASTSAVELOADPATH);
182: }
183:
184: /**
185: * {@inheritDoc}
186: */
187: public Composite createContents(Composite parent) {
188: Composite composite = super .createContents(parent);
189:
190: if (fCurrContext == null)
191: return composite;
192:
193: fShowCleanUpWizardDialogField = new SelectionButtonDialogField(
194: SWT.CHECK);
195: fShowCleanUpWizardDialogField
196: .setLabelText(CleanUpMessages.CleanUpConfigurationBlock_ShowCleanUpWizard_checkBoxLabel);
197: fShowCleanUpWizardDialogField.doFillIntoGrid(composite, 5);
198:
199: IEclipsePreferences node = fCurrContext
200: .getNode(JavaUI.ID_PLUGIN);
201: boolean showWizard;
202: if (node.get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, null) != null) {
203: showWizard = node.getBoolean(
204: CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true);
205: } else {
206: showWizard = new DefaultScope().getNode(JavaUI.ID_PLUGIN)
207: .getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD,
208: true);
209: }
210: if (showWizard)
211: fShowCleanUpWizardDialogField.setSelection(true);
212:
213: fShowCleanUpWizardDialogField
214: .setDialogFieldListener(new IDialogFieldListener() {
215: public void dialogFieldChanged(DialogField field) {
216: doShowCleanUpWizard(fShowCleanUpWizardDialogField
217: .isSelected());
218: }
219: });
220:
221: return composite;
222: }
223:
224: private void doShowCleanUpWizard(boolean showWizard) {
225: IEclipsePreferences preferences = fCurrContext
226: .getNode(JavaUI.ID_PLUGIN);
227: if (preferences
228: .get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, null) != null
229: && preferences.getBoolean(
230: CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true) == showWizard)
231: return;
232:
233: preferences.putBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD,
234: showWizard);
235: }
236:
237: /**
238: * {@inheritDoc}
239: */
240: public void performDefaults() {
241: super .performDefaults();
242: if (fCurrContext == null)
243: return;
244:
245: fCurrContext.getNode(JavaUI.ID_PLUGIN).remove(
246: CleanUpConstants.SHOW_CLEAN_UP_WIZARD);
247: boolean showWizard = new DefaultScope().getNode(
248: JavaUI.ID_PLUGIN).getBoolean(
249: CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true);
250: fShowCleanUpWizardDialogField.setDialogFieldListener(null);
251: fShowCleanUpWizardDialogField.setSelection(showWizard);
252: fShowCleanUpWizardDialogField
253: .setDialogFieldListener(new IDialogFieldListener() {
254: public void dialogFieldChanged(DialogField field) {
255: doShowCleanUpWizard(fShowCleanUpWizardDialogField
256: .isSelected());
257: }
258: });
259: }
260:
261: /**
262: * {@inheritDoc}
263: */
264: protected void preferenceChanged(PreferenceChangeEvent event) {
265: if (CleanUpConstants.CLEANUP_PROFILES.equals(event.getKey())) {
266: try {
267: String id = fCurrContext.getNode(JavaUI.ID_PLUGIN).get(
268: CleanUpConstants.CLEANUP_PROFILE, null);
269: if (id == null)
270: fProfileManager.getDefaultProfile().getID();
271:
272: List oldProfiles = fProfileManager.getSortedProfiles();
273: Profile[] oldProfilesArray = (Profile[]) oldProfiles
274: .toArray(new Profile[oldProfiles.size()]);
275: for (int i = 0; i < oldProfilesArray.length; i++) {
276: if (oldProfilesArray[i] instanceof CustomProfile) {
277: fProfileManager
278: .deleteProfile((CustomProfile) oldProfilesArray[i]);
279: }
280: }
281:
282: List newProfiles = fProfileStore
283: .readProfilesFromString((String) event
284: .getNewValue());
285: for (Iterator iterator = newProfiles.iterator(); iterator
286: .hasNext();) {
287: CustomProfile profile = (CustomProfile) iterator
288: .next();
289: fProfileManager.addProfile(profile);
290: }
291:
292: Profile profile = fProfileManager.getProfile(id);
293: if (profile != null) {
294: fProfileManager.setSelected(profile);
295: } else {
296: fProfileManager.setSelected(fProfileManager
297: .getDefaultProfile());
298: }
299: } catch (CoreException e) {
300: JavaPlugin.log(e);
301: }
302: } else if (CleanUpConstants.CLEANUP_PROFILE.equals(event
303: .getKey())) {
304: if (event.getNewValue() == null) {
305: fProfileManager.setSelected(fProfileManager
306: .getDefaultProfile());
307: } else {
308: Profile profile = fProfileManager
309: .getProfile((String) event.getNewValue());
310: if (profile != null) {
311: fProfileManager.setSelected(profile);
312: }
313: }
314: }
315: }
316:
317: public void enableProjectSpecificSettings(
318: boolean useProjectSpecificSettings) {
319: fBrowserBlock.setEnabled(useProjectSpecificSettings);
320: }
321: }
|