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: * Alex Blewitt - Bug 133277 Allow Sort Members to be performed on package and project levels
011: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
012:
013: import java.lang.reflect.InvocationTargetException;
014:
015: import org.eclipse.core.runtime.preferences.DefaultScope;
016: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
017: import org.eclipse.core.runtime.preferences.InstanceScope;
018: import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
019: import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
020:
021: import org.eclipse.ui.IWorkbenchSite;
022:
023: import org.eclipse.jdt.core.ICompilationUnit;
024: import org.eclipse.jdt.core.JavaModelException;
025:
026: import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
027: import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
028: import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
029:
030: import org.eclipse.jdt.ui.JavaUI;
031:
032: import org.eclipse.jdt.internal.ui.fix.ICleanUp;
033: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
034:
035: public class AllCleanUpsAction extends CleanUpAction {
036:
037: private IPreferenceChangeListener fPreferenceChangeListener;
038:
039: public AllCleanUpsAction(IWorkbenchSite site) {
040: super (site);
041: setToolTipText(ActionMessages.CleanUpAction_tooltip);
042: setDescription(ActionMessages.CleanUpAction_description);
043: installPreferenceListener();
044: updateActionLabel();
045:
046: // PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ORGANIZE_IMPORTS_ACTION);
047: }
048:
049: public AllCleanUpsAction(JavaEditor editor) {
050: super (editor);
051: setToolTipText(ActionMessages.CleanUpAction_tooltip);
052: setDescription(ActionMessages.CleanUpAction_description);
053: installPreferenceListener();
054: updateActionLabel();
055: }
056:
057: /**
058: * {@inheritDoc}
059: */
060: protected ICleanUp[] createCleanUps(ICompilationUnit[] units) {
061: return CleanUpRefactoring.createCleanUps();
062: }
063:
064: /**
065: * {@inheritDoc}
066: */
067: protected String getActionName() {
068: return ActionMessages.CleanUpAction_actionName;
069: }
070:
071: /**
072: * {@inheritDoc}
073: */
074: protected void performRefactoring(ICompilationUnit[] cus,
075: ICleanUp[] cleanUps) throws JavaModelException,
076: InvocationTargetException {
077: RefactoringExecutionStarter.startCleanupRefactoring(cus,
078: cleanUps, getShell(), showWizard(), getActionName());
079: }
080:
081: private boolean showWizard() {
082: InstanceScope instanceScope = new InstanceScope();
083: IEclipsePreferences instanceNode = instanceScope
084: .getNode(JavaUI.ID_PLUGIN);
085: if (instanceNode.get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD,
086: null) != null)
087: return instanceNode.getBoolean(
088: CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true);
089:
090: DefaultScope defaultScope = new DefaultScope();
091: IEclipsePreferences defaultNode = defaultScope
092: .getNode(JavaUI.ID_PLUGIN);
093: return defaultNode.getBoolean(
094: CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true);
095: }
096:
097: private void updateActionLabel() {
098: if (showWizard()) {
099: setText(ActionMessages.CleanUpAction_labelWizard);
100: } else {
101: setText(ActionMessages.CleanUpAction_label);
102: }
103: }
104:
105: private void installPreferenceListener() {
106: fPreferenceChangeListener = new IPreferenceChangeListener() {
107: public void preferenceChange(PreferenceChangeEvent event) {
108: if (event.getKey().equals(
109: CleanUpConstants.SHOW_CLEAN_UP_WIZARD)) {
110: updateActionLabel();
111: }
112: }
113: };
114: new InstanceScope().getNode(JavaUI.ID_PLUGIN)
115: .addPreferenceChangeListener(fPreferenceChangeListener);
116: }
117:
118: public void dispose() {
119: if (fPreferenceChangeListener != null) {
120: new InstanceScope().getNode(JavaUI.ID_PLUGIN)
121: .removePreferenceChangeListener(
122: fPreferenceChangeListener);
123: fPreferenceChangeListener = null;
124: }
125: }
126: }
|