1 11 package org.eclipse.ltk.internal.ui.refactoring.history; 12 13 import java.io.IOException ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Set ; 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 56 public final class RefactoringHistoryEditHelper { 57 58 59 public interface IRefactoringHistoryProvider { 60 61 68 public RefactoringHistory getRefactoringHistory(IProgressMonitor monitor); 69 } 70 71 79 private static IProject[] getAffectedProjects(final RefactoringDescriptorProxy[] descriptors) { 80 final Set set= new HashSet (); 81 for (int index= 0; index < descriptors.length; index++) { 82 final String project= descriptors[index].getProject(); 83 if (project == null || "".equals(project)) 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 iterator= set.iterator(); iterator.hasNext(); index++) { 91 result[index]= root.getProject((String ) iterator.next()); 92 } 93 return result; 94 } 95 96 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 throwable= exception.getStatus().getException(); 133 if (throwable instanceof IOException ) { 134 shell.getDisplay().syncExec(new Runnable () { 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 () { 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 exception) { 159 RefactoringUIPlugin.log(exception); 160 } catch (InterruptedException exception) { 161 } 163 } finally { 164 service.disconnect(); 165 } 166 } 167 168 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 [] details= { ""}; final String 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 current= resolved.getComment(); 194 if (current != null) 195 details[0]= current; 196 } 197 } 198 }, project == null ? root : (IResource) root.getProject(project))); 199 } catch (InvocationTargetException exception) { 200 RefactoringUIPlugin.log(exception); 201 } catch (InterruptedException 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 exception) { 215 RefactoringUIPlugin.log(exception); 216 } catch (InterruptedException exception) { 217 } 219 } 220 } finally { 221 service.disconnect(); 222 } 223 } 224 225 228 private RefactoringHistoryEditHelper() { 229 } 231 } | Popular Tags |