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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.fix;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.UnsupportedEncodingException;
18 import java.util.Hashtable;
19 import java.util.Iterator;
20 import java.util.List;
23 import org.xml.sax.InputSource;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Link;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.swt.widgets.Table;
37 import org.eclipse.core.runtime.CoreException;
38 import org.eclipse.core.runtime.IStatus;
39 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
40 import org.eclipse.core.runtime.preferences.InstanceScope;
42 import org.eclipse.core.resources.ProjectScope;
44 import org.eclipse.jface.dialogs.ControlEnableState;
45 import org.eclipse.jface.dialogs.Dialog;
46 import org.eclipse.jface.viewers.ColumnLayoutData;
47 import org.eclipse.jface.viewers.ColumnWeightData;
48 import org.eclipse.jface.viewers.ITableLabelProvider;
49 import org.eclipse.jface.viewers.LabelProvider;
50 import org.eclipse.jface.viewers.ViewerComparator;
51 import org.eclipse.jface.window.Window;
52 import org.eclipse.jface.wizard.IWizardPage;
54 import org.eclipse.ui.dialogs.PreferencesUtil;
56 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
57 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
59 import org.eclipse.jdt.core.IJavaProject;
61 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
62 import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil;
63 import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
64 import org.eclipse.jdt.internal.corext.fix.CleanUpRegistry.CleanUpTabPageDescriptor;
65 import org.eclipse.jdt.internal.corext.util.Messages;
67 import org.eclipse.jdt.ui.JavaUI;
68 import org.eclipse.jdt.ui.cleanup.CleanUpOptions;
69 import org.eclipse.jdt.ui.cleanup.ICleanUp;
71 import org.eclipse.jdt.internal.ui.JavaPlugin;
72 import org.eclipse.jdt.internal.ui.JavaPluginImages;
73 import org.eclipse.jdt.internal.ui.preferences.BulletListBlock;
74 import org.eclipse.jdt.internal.ui.preferences.CleanUpPreferencePage;
75 import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpProfileVersioner;
76 import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpTabPage;
77 import org.eclipse.jdt.internal.ui.preferences.formatter.IModifyDialogTabPage;
78 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager;
79 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
80 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
81 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore;
82 import org.eclipse.jdt.internal.ui.util.SWTUtil;
83 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
84 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
85 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
86 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
87 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
89 public class CleanUpRefactoringWizard extends RefactoringWizard {
91 private static final String USE_CUSTOM_PROFILE_KEY= "org.eclipse.jdt.ui.cleanup.use_dialog_profile"; //$NON-NLS-1$
92 private static final String CUSTOM_PROFILE_KEY= "org.eclipse.jdt.ui.cleanup.custom_profile"; //$NON-NLS-1$
94 public static class ProjectProfileLableProvider extends LabelProvider implements ITableLabelProvider {
96 private Hashtable<String, Profile> fProfileIdsTable;
101 public Image getColumnImage(Object element, int columnIndex) {
108 public String getColumnText(Object element, int columnIndex) {
109 if (columnIndex == 0) {
110 return ((IJavaProject)element).getProject().getName();
111 } else if (columnIndex == 1) {
113 if (fProfileIdsTable == null)
114 fProfileIdsTable= loadProfiles();
116 IEclipsePreferences instancePreferences= InstanceScope.INSTANCE.getNode(JavaUI.ID_PLUGIN);
118 final String workbenchProfileId;
119 if (instancePreferences.get(CleanUpConstants.CLEANUP_PROFILE, null) != null) {
120 workbenchProfileId= instancePreferences.get(CleanUpConstants.CLEANUP_PROFILE, null);
122 workbenchProfileId= CleanUpConstants.DEFAULT_PROFILE;
125 return getProjectProfileName((IJavaProject)element, fProfileIdsTable, workbenchProfileId);
130 private Hashtable<String, Profile> loadProfiles() {
131 List<Profile> list= CleanUpPreferenceUtil.loadProfiles(InstanceScope.INSTANCE);
132 Hashtable<String, Profile> profileIdsTable= new Hashtable<String, Profile>();
133 for (Iterator<Profile> iterator= list.iterator(); iterator.hasNext();) {
134 Profile profile= iterator.next();
135 profile.generated_2203140287459843041(profileIdsTable);
138 return profileIdsTable;
141 private String getProjectProfileName(final IJavaProject project, Hashtable<String, Profile> profileIdsTable, String workbenchProfileId) {
142 ProjectScope projectScope= new ProjectScope(project.getProject());
143 IEclipsePreferences node= projectScope.getNode(JavaUI.ID_PLUGIN);
144 String id= node.get(CleanUpConstants.CLEANUP_PROFILE, null);
146 Profile profile= profileIdsTable.get(workbenchProfileId);
147 if (profile != null) {
148 return profile.getName();
150 return MultiFixMessages.CleanUpRefactoringWizard_unknownProfile_Name;
153 Profile profile= profileIdsTable.get(id);
154 if (profile != null) {
155 return profile.getName();
157 return Messages.format(MultiFixMessages.CleanUpRefactoringWizard_UnmanagedProfileWithName_Name, id.substring(ProfileManager.ID_PREFIX.length()));
162 public void reset() {
163 fProfileIdsTable= null;
167 public static class CleanUpConfigurationPage extends UserInputWizardPage implements IModifyDialogTabPage.IModificationListener {
169 public static final class WizardCleanUpSelectionDialog extends CleanUpSelectionDialog {
171 private static final String CLEAN_UP_SELECTION_PREFERENCE_KEY= "clean_up_selection_dialog"; //$NON-NLS-1$
173 private WizardCleanUpSelectionDialog(Shell parent, Map<String, String> settings) {
174 super(parent, settings, MultiFixMessages.CleanUpRefactoringWizard_CustomCleanUpsDialog_title);
178 protected NamedCleanUpTabPage[] createTabPages(Map<String, String> workingValues) {
179 CleanUpTabPageDescriptor[] descriptors= JavaPlugin.getDefault().getCleanUpRegistry().getCleanUpTabPageDescriptors(CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS);
181 NamedCleanUpTabPage[] result= new NamedCleanUpTabPage[descriptors.length];
183 for (int i= 0; i < descriptors.length; i++) {
184 String name= descriptors[i].getName();
185 CleanUpTabPage page= descriptors[i].createTabPage();
187 page.generated_5422806669659892960(workingValues, WizardCleanUpSelectionDialog.this);
189 result[i]= new NamedCleanUpTabPage(name, page);
196 protected String getPreferenceKeyPrefix() {
197 return CLEAN_UP_SELECTION_PREFERENCE_KEY;
201 protected String getSelectionCountMessage(int selectionCount, int size) {
202 return Messages.format(MultiFixMessages.CleanUpRefactoringWizard_XofYCleanUpsSelected_message, new Object[] {new Integer(selectionCount), new Integer(size)});
206 protected String getEmptySelectionMessage() {
207 return MultiFixMessages.CleanUpRefactoringWizard_EmptySelection_message;
211 public static final class ProfileTableAdapter implements IListAdapter<IJavaProject> {
212 public final ProjectProfileLableProvider fProvider;
213 public final Shell fShell;
215 private ProfileTableAdapter(ProjectProfileLableProvider provider, Shell shell) {
220 public void customButtonPressed(ListDialogField<IJavaProject> field, int index) {
221 openPropertyDialog(field);
224 public void doubleClicked(ListDialogField<IJavaProject> field) {
225 openPropertyDialog(field);
228 private void openPropertyDialog(ListDialogField<IJavaProject> field) {
229 field.generated_5707197636312538521(ProfileTableAdapter.this);
232 public void selectionChanged(ListDialogField<IJavaProject> field) {
233 field.generated_805087378320485276();
237 public static final String ENCODING= "UTF-8"; //$NON-NLS-1$
239 public final CleanUpRefactoring fCleanUpRefactoring;
240 public Map<String, String> fCustomSettings;
241 public SelectionButtonDialogField fUseCustomField;
243 public ControlEnableState fEnableState;
245 public CleanUpConfigurationPage(CleanUpRefactoring refactoring) {
246 super(MultiFixMessages.CleanUpRefactoringWizard_CleanUpConfigurationPage_title);
247 fCleanUpRefactoring= refactoring;
248 fCleanUpRefactoring.generated_6419108774172304623(this);
254 public void createControl(Composite parent) {
255 initializeDialogUnits(parent);
257 boolean isCustom= getDialogSettings().getBoolean(USE_CUSTOM_PROFILE_KEY);
259 final Composite composite= new Composite(parent, SWT.NONE);
260 composite.setLayout(new GridLayout(2, false));
261 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
262 composite.setFont(parent.getFont());
264 SelectionButtonDialogField useProfile= new SelectionButtonDialogField(SWT.RADIO);
265 useProfile.setLabelText(MultiFixMessages.CleanUpRefactoringWizard_use_configured_radio);
266 useProfile.setSelection(!isCustom);
267 useProfile.doFillIntoGrid(composite, 2);
269 ProjectProfileLableProvider tableLabelProvider= new ProjectProfileLableProvider();
270 IListAdapter<IJavaProject> listAdapter= new ProfileTableAdapter(tableLabelProvider, getShell());
271 String[] buttons= new String[] {
272 MultiFixMessages.CleanUpRefactoringWizard_Configure_Button
274 final ListDialogField<IJavaProject> settingsField= new ListDialogField<IJavaProject>(listAdapter, buttons, tableLabelProvider) {
279 protected int getListStyle() {
280 return super.getListStyle() | SWT.SINGLE;
284 String[] headerNames= new String[] {
285 MultiFixMessages.CleanUpRefactoringWizard_Project_TableHeader,
286 MultiFixMessages.CleanUpRefactoringWizard_Profile_TableHeader
288 ColumnLayoutData[] columns = new ColumnLayoutData[] {
289 new ColumnWeightData(2, true),
290 new ColumnWeightData(1, true)
292 settingsField.setTableColumns(new ListDialogField.ColumnsDescription(columns , headerNames, true));
293 settingsField.setViewerComparator(new ViewerComparator());
295 settingsField.doFillIntoGrid(composite, 3);
297 Table table= settingsField.getTableViewer().getTable();
298 GridData data= (GridData)settingsField.getListControl(null).getLayoutData();
299 data.horizontalIndent= 15;
300 data.grabExcessVerticalSpace= false;
301 data.heightHint= SWTUtil.getTableHeightHint(table, Math.min(5, fCleanUpRefactoring.getProjects().length + 2));
302 data.grabExcessHorizontalSpace= true;
303 data.verticalAlignment= GridData.BEGINNING;
305 data= (GridData)settingsField.getButtonBox(null).getLayoutData();
306 data.grabExcessVerticalSpace= false;
307 data.verticalAlignment= GridData.BEGINNING;
309 data= (GridData)settingsField.getLabelControl(null).getLayoutData();
312 settingsField.generated_2131102452012913709(CleanUpConfigurationPage.this);
314 fUseCustomField= new SelectionButtonDialogField(SWT.RADIO);
315 fUseCustomField.setLabelText(MultiFixMessages.CleanUpRefactoringWizard_use_custom_radio);
316 fUseCustomField.setSelection(isCustom);
317 fUseCustomField.doFillIntoGrid(composite, 2);
319 String settings= getDialogSettings().get(CUSTOM_PROFILE_KEY);
320 if (settings == null) {
321 fCustomSettings= JavaPlugin.getDefault().getCleanUpRegistry().getDefaultOptions(CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS).getMap();
324 fCustomSettings= decodeSettings(settings);
325 } catch (CoreException e) {
327 fCustomSettings= JavaPlugin.getDefault().getCleanUpRegistry().getDefaultOptions(CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS).getMap();
331 final BulletListBlock bulletListBlock= new BulletListBlock(composite, SWT.NONE);
332 GridData layoutData= new GridData(SWT.FILL, SWT.FILL, true, true);
333 layoutData.horizontalIndent= 15;
334 layoutData.grabExcessVerticalSpace= true;
335 bulletListBlock.setLayoutData(layoutData);
337 final Button configure= new Button(composite, SWT.NONE);
338 configure.setText(MultiFixMessages.CleanUpRefactoringWizard_ConfigureCustomProfile_button);
340 data= new GridData(SWT.TOP, SWT.LEAD, false, false);
341 data.widthHint= SWTUtil.getButtonWidthHint(configure);
342 configure.setLayoutData(data);
344 showCustomSettings(bulletListBlock);
345 configure.addSelectionListener(new SelectionAdapter() {
350 public void widgetSelected(SelectionEvent e) {
351 Hashtable<String, String> workingValues= new Hashtable<String, String>(fCustomSettings);
352 CleanUpSelectionDialog dialog= new WizardCleanUpSelectionDialog(getShell(), workingValues);
353 if (dialog.open() == Window.OK) {
354 fCustomSettings= workingValues;
355 showCustomSettings(bulletListBlock);
360 updateEnableState(isCustom, settingsField, configure, bulletListBlock);
362 fUseCustomField.setDialogFieldListener(new IDialogFieldListener() {
363 public void dialogFieldChanged(DialogField field) {
364 updateEnableState(fUseCustomField.isSelected(), settingsField, configure, bulletListBlock);
368 Link preferencePageLink= new Link(composite, SWT.WRAP);
369 preferencePageLink.setText(MultiFixMessages.CleanUpRefactoringWizard_HideWizard_Link);
370 preferencePageLink.setFont(parent.getFont());
371 GridData gridData= new GridData(SWT.FILL, SWT.FILL, true, false);
372 gridData.widthHint= convertWidthInCharsToPixels(50);
373 gridData.horizontalSpan= 2;
374 preferencePageLink.setLayoutData(gridData);
375 preferencePageLink.addSelectionListener(new SelectionAdapter() {
380 public void widgetSelected(SelectionEvent e) {
381 PreferencesUtil.createPreferenceDialogOn(composite.getShell(), CleanUpPreferencePage.PREF_ID, null, null).open();
385 setControl(composite);
387 Dialog.applyDialogFont(composite);
390 private void updateEnableState(boolean isCustom, final ListDialogField<IJavaProject> settingsField, Button configureCustom, BulletListBlock bulletListBlock) {
391 settingsField.generated_8006197945996197760(isCustom, CleanUpConfigurationPage.this, bulletListBlock);
392 configureCustom.setEnabled(isCustom);
395 private void showCustomSettings(BulletListBlock bulletListBlock) {
396 StringBuffer buf= new StringBuffer();
398 final ICleanUp[] cleanUps= JavaPlugin.getDefault().getCleanUpRegistry().createCleanUps();
399 CleanUpOptions options= new MapCleanUpOptions(fCustomSettings);
400 for (int i= 0; i < cleanUps.length; i++) {
401 cleanUps[i].setOptions(options);
402 String[] descriptions= cleanUps[i].getStepDescriptions();
403 if (descriptions != null) {
404 for (int j= 0; j < descriptions.length; j++) {
405 if (buf.length() > 0) {
408 buf.append(descriptions[j]);
412 bulletListBlock.setText(buf.toString());
416 protected boolean performFinish() {
417 initializeRefactoring();
419 return super.performFinish();
423 public IWizardPage getNextPage() {
424 initializeRefactoring();
426 return super.getNextPage();
429 private void storeSettings() {
430 getDialogSettings().put(USE_CUSTOM_PROFILE_KEY, fUseCustomField.isSelected());
432 getDialogSettings().put(CUSTOM_PROFILE_KEY, encodeSettings(fCustomSettings));
433 } catch (CoreException e) {
438 private void initializeRefactoring() {
439 CleanUpRefactoring refactoring= (CleanUpRefactoring)getRefactoring();
441 refactoring.generated_680054145197122767(CleanUpConfigurationPage.this);
444 public String encodeSettings(Map<String, String> settings) throws CoreException {
445 ByteArrayOutputStream stream= new ByteArrayOutputStream(2000);
447 CleanUpProfileVersioner versioner= new CleanUpProfileVersioner();
448 versioner.generated_3820833480962819227(settings, stream);
450 return stream.toString(ENCODING);
451 } catch (UnsupportedEncodingException e) {
452 return stream.toString();
455 try { stream.close(); } catch (IOException e) { /* ignore */ }
459 public Map<String, String> decodeSettings(String settings) throws CoreException {
462 bytes= settings.getBytes(ENCODING);
463 } catch (UnsupportedEncodingException e) {
464 bytes= settings.getBytes();
466 InputStream is= new ByteArrayInputStream(bytes);
468 List<Profile> res= ProfileStore.readProfilesFromStream(new InputSource(is));
469 if (res == null || res.size() == 0)
470 return JavaPlugin.getDefault().getCleanUpRegistry().getDefaultOptions(CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS).getMap();
472 CustomProfile profile= (CustomProfile)res.get(0);
473 return profile.generated_1844878289878542955();
475 try { is.close(); } catch (IOException e) { /* ignore */ }
482 public void updateStatus(IStatus status) {}
487 public void valuesModified() {}
490 public CleanUpRefactoringWizard(CleanUpRefactoring refactoring, int flags) {
491 super(refactoring, flags);
492 setDefaultPageTitle(MultiFixMessages.CleanUpRefactoringWizard_PageTitle);
493 setWindowTitle(MultiFixMessages.CleanUpRefactoringWizard_WindowTitle);
494 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_CLEAN_UP);
498 * @see org.eclipse.ltk.ui.refactoring.RefactoringWizard#addUserInputPages()
501 protected void addUserInputPages() {
502 addPage(new CleanUpConfigurationPage((CleanUpRefactoring)getRefactoring()));