KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > history > RefactoringHistoryEditHelper


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.history;
12
13 import java.io.IOException JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.core.runtime.jobs.ISchedulingRule;
24 import org.eclipse.core.runtime.jobs.MultiRule;
25
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.resources.IWorkspaceRoot;
29 import org.eclipse.core.resources.IWorkspaceRunnable;
30 import org.eclipse.core.resources.ResourcesPlugin;
31
32 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
33 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
34 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
35
36 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
37 import org.eclipse.ltk.internal.core.refactoring.history.IRefactoringDescriptorDeleteQuery;
38 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
39 import org.eclipse.ltk.internal.ui.refactoring.Messages;
40 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
41 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
42 import org.eclipse.ltk.internal.ui.refactoring.WorkbenchRunnableAdapter;
43
44 import org.eclipse.swt.widgets.Shell;
45
46 import org.eclipse.jface.dialogs.MessageDialog;
47 import org.eclipse.jface.operation.IRunnableContext;
48
49 import org.eclipse.ltk.ui.refactoring.history.IRefactoringHistoryControl;
50
51 /**
52  * Helper class to implement shared functionality to edit refactoring histories.
53  *
54  * @since 3.2
55  */

56 public final class RefactoringHistoryEditHelper {
57
58     /** Interface for refactoring history providers */
59     public interface IRefactoringHistoryProvider {
60
61         /**
62          * Returns the refactoring history to use.
63          *
64          * @param monitor
65          * the progress monitor to use
66          * @return the refactoring history
67          */

68         public RefactoringHistory getRefactoringHistory(IProgressMonitor monitor);
69     }
70
71     /**
72      * Returns the projects affected by the specified refactoring descriptors.
73      *
74      * @param descriptors
75      * the refactoring descriptors
76      * @return the affected projects, or <code>null</code> if the entire
77      * workspace is affected
78      */

79     private static IProject[] getAffectedProjects(final RefactoringDescriptorProxy[] descriptors) {
80         final Set JavaDoc set= new HashSet JavaDoc();
81         for (int index= 0; index < descriptors.length; index++) {
82             final String JavaDoc project= descriptors[index].getProject();
83             if (project == null || "".equals(project)) //$NON-NLS-1$
84
return null;
85             set.add(project);
86         }
87         final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
88         final IProject[] result= new IProject[set.size()];
89         int index= 0;
90         for (final Iterator JavaDoc iterator= set.iterator(); iterator.hasNext(); index++) {
91             result[index]= root.getProject((String JavaDoc) iterator.next());
92         }
93         return result;
94     }
95
96     /**
97      * Prompts the user to delete refactorings from the history.
98      *
99      * @param shell
100      * the shell to use
101      * @param context
102      * the runnable context to use
103      * @param control
104      * the refactoring history control
105      * @param query
106      * the refactoring descriptor delete query to use
107      * @param provider
108      * the refactoring history provider to use
109      * @param descriptors
110      * the refactoring descriptors to delete
111      */

112     public static void promptRefactoringDelete(final Shell shell, final IRunnableContext context, final IRefactoringHistoryControl control, final IRefactoringDescriptorDeleteQuery query, final IRefactoringHistoryProvider provider, final RefactoringDescriptorProxy[] descriptors) {
113         Assert.isNotNull(shell);
114         Assert.isNotNull(context);
115         Assert.isNotNull(control);
116         Assert.isNotNull(query);
117         Assert.isNotNull(provider);
118         Assert.isNotNull(descriptors);
119         final RefactoringHistoryService service= RefactoringHistoryService.getInstance();
120         try {
121             service.connect();
122             try {
123                 final IProject[] affected= getAffectedProjects(descriptors);
124                 context.run(false, true, new WorkbenchRunnableAdapter(new IWorkspaceRunnable() {
125
126                     public void run(final IProgressMonitor monitor) throws CoreException {
127                         try {
128                             monitor.beginTask(RefactoringCoreMessages.RefactoringHistoryService_deleting_refactorings, 300);
129                             try {
130                                 service.deleteRefactoringDescriptors(descriptors, query, new SubProgressMonitor(monitor, 280, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
131                             } catch (CoreException exception) {
132                                 final Throwable JavaDoc throwable= exception.getStatus().getException();
133                                 if (throwable instanceof IOException JavaDoc) {
134                                     shell.getDisplay().syncExec(new Runnable JavaDoc() {
135
136                                         public void run() {
137                                             MessageDialog.openError(shell, RefactoringUIMessages.ChangeExceptionHandler_refactoring, throwable.getLocalizedMessage());
138                                         }
139                                     });
140                                 } else
141                                     throw exception;
142                             }
143                             if (query.hasDeletions()) {
144                                 final RefactoringHistory history= provider.getRefactoringHistory(new SubProgressMonitor(monitor, 20, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
145                                 shell.getDisplay().syncExec(new Runnable JavaDoc() {
146
147                                     public void run() {
148                                         control.setInput(history);
149                                         control.setCheckedDescriptors(RefactoringPropertyPage.EMPTY_DESCRIPTORS);
150                                     }
151                                 });
152                             }
153                         } finally {
154                             monitor.done();
155                         }
156                     }
157                 }, affected == null ? ResourcesPlugin.getWorkspace().getRoot() : (ISchedulingRule) new MultiRule(affected)));
158             } catch (InvocationTargetException JavaDoc exception) {
159                 RefactoringUIPlugin.log(exception);
160             } catch (InterruptedException JavaDoc exception) {
161                 // Do nothing
162
}
163         } finally {
164             service.disconnect();
165         }
166     }
167
168     /**
169      * Prompts the user to edit the refactoring details.
170      *
171      * @param context
172      * the runnable context to use
173      * @param control
174      * the refactoring history control to update
175      * @param descriptor
176      * the refactoring descriptor to edit
177      */

178     public static void promptRefactoringDetails(final IRunnableContext context, final IRefactoringHistoryControl control, final RefactoringDescriptorProxy descriptor) {
179         Assert.isNotNull(context);
180         Assert.isNotNull(descriptor);
181         final RefactoringHistoryService service= RefactoringHistoryService.getInstance();
182         try {
183             service.connect();
184             final String JavaDoc[] details= { ""}; //$NON-NLS-1$
185
final String JavaDoc project= descriptor.getProject();
186             final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
187             try {
188                 context.run(false, true, new WorkbenchRunnableAdapter(new IWorkspaceRunnable() {
189
190                     public void run(final IProgressMonitor monitor) throws CoreException {
191                         final RefactoringDescriptor resolved= descriptor.requestDescriptor(monitor);
192                         if (resolved != null) {
193                             final String JavaDoc current= resolved.getComment();
194                             if (current != null)
195                                 details[0]= current;
196                         }
197                     }
198                 }, project == null ? root : (IResource) root.getProject(project)));
199             } catch (InvocationTargetException JavaDoc exception) {
200                 RefactoringUIPlugin.log(exception);
201             } catch (InterruptedException JavaDoc exception) {
202                 return;
203             }
204             final EditRefactoringDetailsDialog dialog= new EditRefactoringDetailsDialog(control.getControl().getShell(), RefactoringUIMessages.RefactoringPropertyPage_edit_caption, Messages.format(RefactoringUIMessages.RefactoringPropertyPage_edit_message, descriptor.getDescription()), details[0]);
205             if (dialog.open() == 0) {
206                 try {
207                     context.run(false, true, new WorkbenchRunnableAdapter(new IWorkspaceRunnable() {
208
209                         public void run(final IProgressMonitor monitor) throws CoreException {
210                             service.setRefactoringComment(descriptor, dialog.getDetails(), monitor);
211                             control.setSelectedDescriptors(new RefactoringDescriptorProxy[] { descriptor});
212                         }
213                     }, project == null ? root : (IResource) root.getProject(project)));
214                 } catch (InvocationTargetException JavaDoc exception) {
215                     RefactoringUIPlugin.log(exception);
216                 } catch (InterruptedException JavaDoc exception) {
217                     // Do nothing
218
}
219             }
220         } finally {
221             service.disconnect();
222         }
223     }
224
225     /**
226      * Creates a new refactoring history edit helper
227      */

228     private RefactoringHistoryEditHelper() {
229         // Not for instantiation
230
}
231 }
Popular Tags