1 12 package org.eclipse.jdt.internal.ui.actions; 13 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.core.runtime.preferences.DefaultScope; 17 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 18 import org.eclipse.core.runtime.preferences.InstanceScope; 19 import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; 20 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; 21 22 import org.eclipse.ui.IWorkbenchSite; 23 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.JavaModelException; 26 27 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; 28 import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring; 29 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 30 31 import org.eclipse.jdt.ui.JavaUI; 32 33 import org.eclipse.jdt.internal.ui.fix.ICleanUp; 34 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 35 36 public class AllCleanUpsAction extends CleanUpAction { 37 38 private IPreferenceChangeListener fPreferenceChangeListener; 39 40 public AllCleanUpsAction(IWorkbenchSite site) { 41 super(site); 42 setToolTipText(ActionMessages.CleanUpAction_tooltip); 43 setDescription(ActionMessages.CleanUpAction_description); 44 installPreferenceListener(); 45 updateActionLabel(); 46 47 } 49 50 public AllCleanUpsAction(JavaEditor editor) { 51 super(editor); 52 setToolTipText(ActionMessages.CleanUpAction_tooltip); 53 setDescription(ActionMessages.CleanUpAction_description); 54 installPreferenceListener(); 55 updateActionLabel(); 56 } 57 58 61 protected ICleanUp[] createCleanUps(ICompilationUnit[] units) { 62 return CleanUpRefactoring.createCleanUps(); 63 } 64 65 68 protected String getActionName() { 69 return ActionMessages.CleanUpAction_actionName; 70 } 71 72 75 protected void performRefactoring(ICompilationUnit[] cus, ICleanUp[] cleanUps) throws JavaModelException, InvocationTargetException { 76 RefactoringExecutionStarter.startCleanupRefactoring(cus, cleanUps, getShell(), showWizard(), getActionName()); 77 } 78 79 private boolean showWizard() { 80 InstanceScope instanceScope= new InstanceScope(); 81 IEclipsePreferences instanceNode= instanceScope.getNode(JavaUI.ID_PLUGIN); 82 if (instanceNode.get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, null) != null) 83 return instanceNode.getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true); 84 85 DefaultScope defaultScope= new DefaultScope(); 86 IEclipsePreferences defaultNode= defaultScope.getNode(JavaUI.ID_PLUGIN); 87 return defaultNode.getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true); 88 } 89 90 private void updateActionLabel() { 91 if (showWizard()) { 92 setText(ActionMessages.CleanUpAction_labelWizard); 93 } else { 94 setText(ActionMessages.CleanUpAction_label); 95 } 96 } 97 98 private void installPreferenceListener() { 99 fPreferenceChangeListener= new IPreferenceChangeListener() { 100 public void preferenceChange(PreferenceChangeEvent event) { 101 if (event.getKey().equals(CleanUpConstants.SHOW_CLEAN_UP_WIZARD)) { 102 updateActionLabel(); 103 } 104 } 105 }; 106 new InstanceScope().getNode(JavaUI.ID_PLUGIN).addPreferenceChangeListener(fPreferenceChangeListener); 107 } 108 109 public void dispose() { 110 if (fPreferenceChangeListener != null) { 111 new InstanceScope().getNode(JavaUI.ID_PLUGIN).removePreferenceChangeListener(fPreferenceChangeListener); 112 fPreferenceChangeListener= null; 113 } 114 } 115 } 116 | Popular Tags |