]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/fix/CleanUpRefactoringWizard.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / fix / CleanUpRefactoringWizard.java
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  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.fix;
12
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;
21 import java.util.Map;
22
23 import org.xml.sax.InputSource;
24
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;
36
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;
41
42 import org.eclipse.core.resources.ProjectScope;
43
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;
53
54 import org.eclipse.ui.dialogs.PreferencesUtil;
55
56 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
57 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
58
59 import org.eclipse.jdt.core.IJavaProject;
60
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;
66
67 import org.eclipse.jdt.ui.JavaUI;
68 import org.eclipse.jdt.ui.cleanup.CleanUpOptions;
69 import org.eclipse.jdt.ui.cleanup.ICleanUp;
70
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;
88
89 public class CleanUpRefactoringWizard extends RefactoringWizard {
90
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$
93
94         public static class ProjectProfileLableProvider extends LabelProvider implements ITableLabelProvider {
95
96                 private Hashtable<String, Profile> fProfileIdsTable;
97
98                 /**
99                  * {@inheritDoc}
100                  */
101                 public Image getColumnImage(Object element, int columnIndex) {
102                         return null;
103                 }
104
105                 /**
106                  * {@inheritDoc}
107                  */
108                 public String getColumnText(Object element, int columnIndex) {
109                         if (columnIndex == 0) {
110                                 return ((IJavaProject)element).getProject().getName();
111                         } else if (columnIndex == 1) {
112
113                                 if (fProfileIdsTable == null)
114                                 fProfileIdsTable= loadProfiles();
115
116                                 IEclipsePreferences instancePreferences= InstanceScope.INSTANCE.getNode(JavaUI.ID_PLUGIN);
117
118                         final String workbenchProfileId;
119                         if (instancePreferences.get(CleanUpConstants.CLEANUP_PROFILE, null) != null) {
120                                 workbenchProfileId= instancePreferences.get(CleanUpConstants.CLEANUP_PROFILE, null);
121                         } else {
122                                 workbenchProfileId= CleanUpConstants.DEFAULT_PROFILE;
123                         }
124
125                                 return getProjectProfileName((IJavaProject)element, fProfileIdsTable, workbenchProfileId);
126                         }
127                         return null;
128                 }
129
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);
136             }
137
138                 return profileIdsTable;
139         }
140
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);
145                         if (id == null) {
146                         Profile profile= profileIdsTable.get(workbenchProfileId);
147                         if (profile != null) {
148                                 return profile.getName();
149                         } else {
150                                 return MultiFixMessages.CleanUpRefactoringWizard_unknownProfile_Name;
151                         }
152                 } else {
153                         Profile profile= profileIdsTable.get(id);
154                         if (profile != null) {
155                                 return profile.getName();
156                         } else {
157                                 return Messages.format(MultiFixMessages.CleanUpRefactoringWizard_UnmanagedProfileWithName_Name, id.substring(ProfileManager.ID_PREFIX.length()));
158                         }
159                 }
160         }
161
162                 public void reset() {
163                         fProfileIdsTable= null;
164         }
165         }
166
167         public static class CleanUpConfigurationPage extends UserInputWizardPage implements IModifyDialogTabPage.IModificationListener {
168
169                 public static final class WizardCleanUpSelectionDialog extends CleanUpSelectionDialog {
170
171                         private static final String CLEAN_UP_SELECTION_PREFERENCE_KEY= "clean_up_selection_dialog"; //$NON-NLS-1$
172
173                         private WizardCleanUpSelectionDialog(Shell parent, Map<String, String> settings) {
174                                 super(parent, settings, MultiFixMessages.CleanUpRefactoringWizard_CustomCleanUpsDialog_title);
175                         }
176
177                         @Override
178                         protected NamedCleanUpTabPage[] createTabPages(Map<String, String> workingValues) {
179                                 CleanUpTabPageDescriptor[] descriptors= JavaPlugin.getDefault().getCleanUpRegistry().getCleanUpTabPageDescriptors(CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS);
180
181                                 NamedCleanUpTabPage[] result= new NamedCleanUpTabPage[descriptors.length];
182
183                                 for (int i= 0; i < descriptors.length; i++) {
184                                         String name= descriptors[i].getName();
185                                         CleanUpTabPage page= descriptors[i].createTabPage();
186
187                                         page.generated_5422806669659892960(workingValues, WizardCleanUpSelectionDialog.this);
188
189                                         result[i]= new NamedCleanUpTabPage(name, page);
190                                 }
191
192                                 return result;
193                         }
194
195                         @Override
196                         protected String getPreferenceKeyPrefix() {
197                                 return CLEAN_UP_SELECTION_PREFERENCE_KEY;
198                         }
199
200                         @Override
201                         protected String getSelectionCountMessage(int selectionCount, int size) {
202                                 return Messages.format(MultiFixMessages.CleanUpRefactoringWizard_XofYCleanUpsSelected_message, new Object[] {new Integer(selectionCount), new Integer(size)});
203                         }
204
205                         @Override
206                         protected String getEmptySelectionMessage() {
207                                 return MultiFixMessages.CleanUpRefactoringWizard_EmptySelection_message;
208                         }
209                 }
210
211                 public static final class ProfileTableAdapter implements IListAdapter<IJavaProject> {
212                 public final ProjectProfileLableProvider fProvider;
213                         public final Shell fShell;
214
215                 private ProfileTableAdapter(ProjectProfileLableProvider provider, Shell shell) {
216                         fProvider= provider;
217                                 fShell= shell;
218                 }
219
220                 public void customButtonPressed(ListDialogField<IJavaProject> field, int index) {
221                         openPropertyDialog(field);
222                 }
223
224                 public void doubleClicked(ListDialogField<IJavaProject> field) {
225                                 openPropertyDialog(field);
226                 }
227
228                 private void openPropertyDialog(ListDialogField<IJavaProject> field) {
229                     field.generated_5707197636312538521(ProfileTableAdapter.this);
230             }
231
232                         public void selectionChanged(ListDialogField<IJavaProject> field) {
233                         field.generated_805087378320485276();
234                 }
235         }
236
237                 public static final String ENCODING= "UTF-8"; //$NON-NLS-1$
238
239                 public final CleanUpRefactoring fCleanUpRefactoring;
240                 public Map<String, String> fCustomSettings;
241                 public SelectionButtonDialogField fUseCustomField;
242
243                 public ControlEnableState fEnableState;
244
245                 public CleanUpConfigurationPage(CleanUpRefactoring refactoring) {
246                         super(MultiFixMessages.CleanUpRefactoringWizard_CleanUpConfigurationPage_title);
247                         fCleanUpRefactoring= refactoring;
248                         fCleanUpRefactoring.generated_6419108774172304623(this);
249         }
250
251                 /**
252          * {@inheritDoc}
253          */
254         public void createControl(Composite parent) {
255                         initializeDialogUnits(parent);
256
257                 boolean isCustom= getDialogSettings().getBoolean(USE_CUSTOM_PROFILE_KEY);
258
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());
263
264                         SelectionButtonDialogField useProfile= new SelectionButtonDialogField(SWT.RADIO);
265                         useProfile.setLabelText(MultiFixMessages.CleanUpRefactoringWizard_use_configured_radio);
266                         useProfile.setSelection(!isCustom);
267                         useProfile.doFillIntoGrid(composite, 2);
268
269                         ProjectProfileLableProvider tableLabelProvider= new ProjectProfileLableProvider();
270                         IListAdapter<IJavaProject> listAdapter= new ProfileTableAdapter(tableLabelProvider, getShell());
271                         String[] buttons= new String[] {
272                                 MultiFixMessages.CleanUpRefactoringWizard_Configure_Button
273                         };
274                         final ListDialogField<IJavaProject> settingsField= new ListDialogField<IJavaProject>(listAdapter, buttons, tableLabelProvider) {
275                                 /**
276                                  * {@inheritDoc}
277                                  */
278                                 @Override
279                                 protected int getListStyle() {
280                                         return super.getListStyle() | SWT.SINGLE;
281                                 }
282                         };
283
284                         String[] headerNames= new String[] {
285                                         MultiFixMessages.CleanUpRefactoringWizard_Project_TableHeader,
286                                         MultiFixMessages.CleanUpRefactoringWizard_Profile_TableHeader
287                         };
288                         ColumnLayoutData[] columns = new ColumnLayoutData[] {
289                                         new ColumnWeightData(2, true),
290                                         new ColumnWeightData(1, true)
291                         };
292                         settingsField.setTableColumns(new ListDialogField.ColumnsDescription(columns , headerNames, true));
293                         settingsField.setViewerComparator(new ViewerComparator());
294
295                         settingsField.doFillIntoGrid(composite, 3);
296
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;
304
305                         data= (GridData)settingsField.getButtonBox(null).getLayoutData();
306                         data.grabExcessVerticalSpace= false;
307                         data.verticalAlignment= GridData.BEGINNING;
308
309                         data= (GridData)settingsField.getLabelControl(null).getLayoutData();
310                         data.exclude= true;
311
312                         settingsField.generated_2131102452012913709(CleanUpConfigurationPage.this);
313
314                         fUseCustomField= new SelectionButtonDialogField(SWT.RADIO);
315                         fUseCustomField.setLabelText(MultiFixMessages.CleanUpRefactoringWizard_use_custom_radio);
316                         fUseCustomField.setSelection(isCustom);
317                         fUseCustomField.doFillIntoGrid(composite, 2);
318
319                         String settings= getDialogSettings().get(CUSTOM_PROFILE_KEY);
320                         if (settings == null) {
321                                 fCustomSettings= JavaPlugin.getDefault().getCleanUpRegistry().getDefaultOptions(CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS).getMap();
322                         } else {
323                                 try {
324                         fCustomSettings= decodeSettings(settings);
325                 } catch (CoreException e) {
326                         JavaPlugin.log(e);
327                         fCustomSettings= JavaPlugin.getDefault().getCleanUpRegistry().getDefaultOptions(CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS).getMap();
328                 }
329                         }
330
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);
336
337                         final Button configure= new Button(composite, SWT.NONE);
338                         configure.setText(MultiFixMessages.CleanUpRefactoringWizard_ConfigureCustomProfile_button);
339
340                         data= new GridData(SWT.TOP, SWT.LEAD, false, false);
341                         data.widthHint= SWTUtil.getButtonWidthHint(configure);
342                         configure.setLayoutData(data);
343
344                         showCustomSettings(bulletListBlock);
345                         configure.addSelectionListener(new SelectionAdapter() {
346                                 /**
347                                  * {@inheritDoc}
348                                  */
349                                 @Override
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);
356                                         }
357                                 }
358                         });
359
360                         updateEnableState(isCustom, settingsField, configure, bulletListBlock);
361
362                         fUseCustomField.setDialogFieldListener(new IDialogFieldListener() {
363                                 public void dialogFieldChanged(DialogField field) {
364                                         updateEnableState(fUseCustomField.isSelected(), settingsField, configure, bulletListBlock);
365                 }
366                         });
367
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() {
376                                 /**
377                                  * {@inheritDoc}
378                                  */
379                                 @Override
380                                 public void widgetSelected(SelectionEvent e) {
381                                         PreferencesUtil.createPreferenceDialogOn(composite.getShell(), CleanUpPreferencePage.PREF_ID, null, null).open();
382                                 }
383                         });
384
385                         setControl(composite);
386
387                         Dialog.applyDialogFont(composite);
388         }
389
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);
393                 }
394
395                 private void showCustomSettings(BulletListBlock bulletListBlock) {
396                         StringBuffer buf= new StringBuffer();
397
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) {
406                                         buf.append('\n');
407                                 }
408                             buf.append(descriptions[j]);
409                         }
410                         }
411                 }
412                 bulletListBlock.setText(buf.toString());
413         }
414
415         @Override
416                 protected boolean performFinish() {
417                         initializeRefactoring();
418                         storeSettings();
419                         return super.performFinish();
420                 }
421
422                 @Override
423                 public IWizardPage getNextPage() {
424                         initializeRefactoring();
425                         storeSettings();
426                         return super.getNextPage();
427                 }
428
429                 private void storeSettings() {
430                         getDialogSettings().put(USE_CUSTOM_PROFILE_KEY, fUseCustomField.isSelected());
431                         try {
432                     getDialogSettings().put(CUSTOM_PROFILE_KEY, encodeSettings(fCustomSettings));
433             } catch (CoreException e) {
434                     JavaPlugin.log(e);
435             }
436         }
437
438                 private void initializeRefactoring() {
439                         CleanUpRefactoring refactoring= (CleanUpRefactoring)getRefactoring();
440
441                         refactoring.generated_680054145197122767(CleanUpConfigurationPage.this);
442         }
443
444                 public String encodeSettings(Map<String, String> settings) throws CoreException {
445                         ByteArrayOutputStream stream= new ByteArrayOutputStream(2000);
446                         try {
447                                 CleanUpProfileVersioner versioner= new CleanUpProfileVersioner();
448                                 versioner.generated_3820833480962819227(settings, stream);
449                                 try {
450                                         return stream.toString(ENCODING);
451                                 } catch (UnsupportedEncodingException e) {
452                                         return stream.toString();
453                                 }
454                         } finally {
455                                 try { stream.close(); } catch (IOException e) { /* ignore */ }
456                         }
457                 }
458
459                 public Map<String, String> decodeSettings(String settings) throws CoreException {
460                         byte[] bytes;
461                         try {
462                                 bytes= settings.getBytes(ENCODING);
463                         } catch (UnsupportedEncodingException e) {
464                                 bytes= settings.getBytes();
465                         }
466                         InputStream is= new ByteArrayInputStream(bytes);
467                         try {
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();
471
472                                 CustomProfile profile= (CustomProfile)res.get(0);
473                                 return profile.generated_1844878289878542955();
474                         } finally {
475                                 try { is.close(); } catch (IOException e) { /* ignore */ }
476                         }
477                 }
478
479                 /**
480          * {@inheritDoc}
481          */
482         public void updateStatus(IStatus status) {}
483
484                 /**
485          * {@inheritDoc}
486          */
487         public void valuesModified() {}
488         }
489
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);
495         }
496
497         /* (non-Javadoc)
498          * @see org.eclipse.ltk.ui.refactoring.RefactoringWizard#addUserInputPages()
499          */
500         @Override
501         protected void addUserInputPages() {
502                 addPage(new CleanUpConfigurationPage((CleanUpRefactoring)getRefactoring()));
503         }
504
505 }