1 11 package org.eclipse.compare.internal; 12 13 import java.util.ResourceBundle ; 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.swt.widgets.Shell; 17 18 import org.eclipse.jface.dialogs.MessageDialog; 19 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 20 import org.eclipse.jface.viewers.ISelection; 21 22 import org.eclipse.ui.actions.WorkspaceModifyOperation; 23 import org.eclipse.core.resources.*; 24 import org.eclipse.core.runtime.*; 25 26 27 public class AddFromHistoryAction extends BaseCompareAction { 28 29 private static final String BUNDLE_NAME= "org.eclipse.compare.internal.AddFromHistoryAction"; 31 public AddFromHistoryAction() { 32 } 34 35 protected boolean isEnabled(ISelection selection) { 36 return Utilities.getResources(selection).length == 1; 37 } 38 39 protected void run(ISelection selection) { 40 41 ResourceBundle bundle= ResourceBundle.getBundle(BUNDLE_NAME); 42 String title= Utilities.getString(bundle, "title"); 44 Shell parentShell= CompareUIPlugin.getShell(); 45 AddFromHistoryDialog dialog= null; 46 47 Object [] s= Utilities.getResources(selection); 48 49 for (int i= 0; i < s.length; i++) { 50 Object o= s[i]; 51 if (o instanceof IContainer) { 52 IContainer container= (IContainer) o; 53 54 ProgressMonitorDialog pmdialog= new ProgressMonitorDialog(parentShell); 55 IProgressMonitor pm= pmdialog.getProgressMonitor(); 56 IFile[] states= null; 57 try { 58 states= container.findDeletedMembersWithHistory(IResource.DEPTH_INFINITE, pm); 59 } catch (CoreException ex) { 60 pm.done(); 61 } 62 63 if (states == null || states.length <= 0) { 64 String msg= Utilities.getString(bundle, "noLocalHistoryError"); MessageDialog.openInformation(parentShell, title, msg); 66 return; 67 } 68 69 if (dialog == null) { 70 dialog= new AddFromHistoryDialog(parentShell, bundle); 71 dialog.setHelpContextId(ICompareContextIds.ADD_FROM_HISTORY_DIALOG); 72 } 73 74 if (dialog.select(container, states)) { 75 76 AddFromHistoryDialog.HistoryInput[] selected= dialog.getSelected(); 77 78 if (selected != null && selected.length > 0) { 79 try { 80 updateWorkspace(bundle, parentShell, selected); 81 82 } catch (InterruptedException x) { 83 85 } catch (InvocationTargetException x) { 86 String reason= x.getTargetException().getMessage(); 87 MessageDialog.openError(parentShell, title, Utilities.getFormattedString(bundle, "replaceError", reason)); } 89 } 90 } 91 } 92 } 93 } 94 95 void createContainers(IResource resource) throws CoreException { 96 IContainer container= resource.getParent(); 97 if (container instanceof IFolder) { 98 IFolder parent= (IFolder) container; 99 if (parent != null && !parent.exists()) { 100 createContainers(parent); 101 parent.create(false, true, null); 102 } 103 } 104 } 105 106 private void updateWorkspace(final ResourceBundle bundle, Shell shell, 107 final AddFromHistoryDialog.HistoryInput[] selected) 108 throws InvocationTargetException , InterruptedException { 109 110 WorkspaceModifyOperation operation= new WorkspaceModifyOperation() { 111 public void execute(IProgressMonitor pm) throws InvocationTargetException { 112 try { 113 String taskName= Utilities.getString(bundle, "taskName"); pm.beginTask(taskName, selected.length); 115 116 for (int i= 0; i < selected.length; i++) { 117 IFile file= selected[i].fFile; 118 IFileState fileState= selected[i].fFileState; 119 createContainers(file); 120 121 SubProgressMonitor subMonitor= new SubProgressMonitor(pm, 1); 122 try { 123 file.create(fileState.getContents(), false, subMonitor); 124 } finally { 125 subMonitor.done(); 126 } 127 } 128 } catch (CoreException e) { 129 throw new InvocationTargetException (e); 130 } finally { 131 pm.done(); 132 } 133 } 134 }; 135 136 ProgressMonitorDialog pmdialog= new ProgressMonitorDialog(shell); 137 pmdialog.run(false, true, operation); 138 } 139 } 140 | Popular Tags |