KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > AllCleanUpsAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Alex Blewitt - Bug 133277 Allow Sort Members to be performed on package and project levels
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.actions;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
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         // PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ORGANIZE_IMPORTS_ACTION);
48
}
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     /**
59      * {@inheritDoc}
60      */

61     protected ICleanUp[] createCleanUps(ICompilationUnit[] units) {
62         return CleanUpRefactoring.createCleanUps();
63     }
64
65     /**
66      * {@inheritDoc}
67      */

68     protected String JavaDoc getActionName() {
69         return ActionMessages.CleanUpAction_actionName;
70     }
71
72     /**
73      * {@inheritDoc}
74      */

75     protected void performRefactoring(ICompilationUnit[] cus, ICleanUp[] cleanUps) throws JavaModelException, InvocationTargetException JavaDoc {
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