KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > actions > AcceptRefactoringsAction


1 /*******************************************************************************
2  * Copyright (c) 2006 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.ltk.internal.ui.refactoring.actions;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.team.core.diff.IThreeWayDiff;
17 import org.eclipse.team.core.mapping.IMergeContext;
18 import org.eclipse.team.core.mapping.ISynchronizationContext;
19
20 import org.eclipse.core.runtime.Assert;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.ResourcesPlugin;
24
25 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
26
27 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryImplementation;
28 import org.eclipse.ltk.internal.ui.refactoring.IRefactoringHelpContextIds;
29 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
30 import org.eclipse.ltk.internal.ui.refactoring.model.ModelMessages;
31 import org.eclipse.ltk.internal.ui.refactoring.model.RefactoringDescriptorSynchronizationProxy;
32 import org.eclipse.ltk.internal.ui.refactoring.model.RefactoringHistoryMergeWizard;
33
34 import org.eclipse.swt.widgets.Shell;
35
36 import org.eclipse.jface.action.Action;
37 import org.eclipse.jface.window.Window;
38 import org.eclipse.jface.wizard.WizardDialog;
39
40 import org.eclipse.ui.PlatformUI;
41
42 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration;
43
44 /**
45  * Action to accept pending refactorings to execute them on the local workspace.
46  *
47  * @since 3.2
48  */

49 public final class AcceptRefactoringsAction extends Action {
50
51     /** Refactoring history accept configuration */
52     private static final class RefactoringHistoryAcceptConfiguration extends RefactoringHistoryControlConfiguration {
53
54         /**
55          * Creates a new refactoring history accept configuration.
56          *
57          * @param project
58          * the project, or <code>null</code>
59          */

60         public RefactoringHistoryAcceptConfiguration(final IProject project) {
61             super(project, false, false);
62         }
63
64         /**
65          * {@inheritDoc}
66          */

67         public String JavaDoc getProjectPattern() {
68             return ModelMessages.AcceptRefactoringsAction_wizard_project_pattern;
69         }
70
71         /**
72          * {@inheritDoc}
73          */

74         public String JavaDoc getWorkspaceCaption() {
75             return ModelMessages.AcceptRefactoringsAction_wizard_workspace_caption;
76         }
77     }
78
79     /** The refactoring history accept wizard */
80     private static final class RefactoringHistoryAcceptWizard extends RefactoringHistoryMergeWizard {
81
82         /**
83          * Creates a new refactoring history accept wizard.
84          */

85         public RefactoringHistoryAcceptWizard() {
86             super(RefactoringUIMessages.RefactoringWizard_refactoring, ModelMessages.AcceptRefactoringsAction_wizard_title, ModelMessages.AcceptRefactoringsAction_wizard_description);
87         }
88     }
89
90     /** The wizard height */
91     private static final int SIZING_WIZARD_HEIGHT= 520;
92
93     /** The wizard width */
94     private static final int SIZING_WIZARD_WIDTH= 470;
95
96     /** The synchronization context to use */
97     private final ISynchronizationContext fContext;
98
99     /** The refactoring descriptor proxies, or <code>null</code> */
100     private RefactoringDescriptorProxy[] fProxies= null;
101
102     /** The shell to use */
103     private final Shell fShell;
104
105     /**
106      * Creates a new accept refactorings action.
107      *
108      * @param context
109      * the synchronization context
110      * @param shell
111      * the shell to use
112      */

113     public AcceptRefactoringsAction(final ISynchronizationContext context, final Shell shell) {
114         Assert.isNotNull(context);
115         Assert.isNotNull(shell);
116         fContext= context;
117         fShell= shell;
118         setText(ModelMessages.AcceptRefactoringsAction_title);
119         setToolTipText(ModelMessages.AcceptRefactoringsAction_tool_tip);
120         setDescription(ModelMessages.AcceptRefactoringsAction_description);
121     }
122
123     /**
124      * {@inheritDoc}
125      */

126     public boolean isEnabled() {
127         if (fProxies != null && fProxies.length > 0) {
128             for (int index= 0; index < fProxies.length; index++) {
129                 if (fProxies[index] instanceof RefactoringDescriptorSynchronizationProxy) {
130                     final RefactoringDescriptorSynchronizationProxy proxy= (RefactoringDescriptorSynchronizationProxy) fProxies[index];
131                     if (proxy.getDirection() == IThreeWayDiff.INCOMING)
132                         return true;
133                 }
134             }
135         }
136         return false;
137     }
138
139     /**
140      * {@inheritDoc}
141      */

142     public void run() {
143         if (fProxies != null && fProxies.length > 0) {
144             final RefactoringHistoryMergeWizard wizard= new RefactoringHistoryAcceptWizard();
145             int result= Window.OK;
146             try {
147                 final WizardDialog dialog= new WizardDialog(fShell, wizard);
148                 IProject project= null;
149                 Set JavaDoc proxies= new HashSet JavaDoc();
150                 for (int index= 0; index < fProxies.length; index++) {
151                     if (fProxies[index] instanceof RefactoringDescriptorSynchronizationProxy) {
152                         final RefactoringDescriptorSynchronizationProxy proxy= (RefactoringDescriptorSynchronizationProxy) fProxies[index];
153                         if (proxy.getDirection() == IThreeWayDiff.INCOMING)
154                             proxies.add(proxy);
155                     }
156                     String JavaDoc name= fProxies[index].getProject();
157                     if (name != null && !"".equals(name)) //$NON-NLS-1$
158
project= ResourcesPlugin.getWorkspace().getRoot().getProject(name);
159                 }
160                 wizard.setConfiguration(new RefactoringHistoryAcceptConfiguration(project));
161                 wizard.setInput(new RefactoringHistoryImplementation((RefactoringDescriptorProxy[]) proxies.toArray(new RefactoringDescriptorProxy[proxies.size()])));
162                 dialog.create();
163                 dialog.getShell().setSize(Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x), SIZING_WIZARD_HEIGHT);
164                 PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), IRefactoringHelpContextIds.REFACTORING_ACCEPT_REFACTORING_PAGE);
165                 result= dialog.open();
166             } finally {
167                 if (result != Window.CANCEL && fContext instanceof IMergeContext) {
168                     final IMergeContext context= (IMergeContext) fContext;
169                     wizard.resolveConflicts(context);
170                 }
171             }
172         }
173     }
174
175     /**
176      * Sets the refactoring descriptor proxies to accept.
177      *
178      * @param proxies
179      * the refactoring descriptor proxies
180      */

181     public void setRefactoringDescriptors(final RefactoringDescriptorProxy[] proxies) {
182         Assert.isNotNull(proxies);
183         fProxies= proxies;
184     }
185 }
Popular Tags