Skip to main content
summaryrefslogtreecommitdiffstats
blob: c38784003039d6bdd2adf0e1329342a6358e086c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 * IBM - Initial API and implementation
 ******************************************************************************/

package org.eclipse.team.internal.ccvs.ui;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFileState;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSTeamProvider;
import org.eclipse.team.internal.ccvs.core.ICVSFile;
import org.eclipse.team.internal.ccvs.core.client.Command;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.syncinfo.MutableResourceSyncInfo;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
import org.eclipse.team.internal.ccvs.core.util.AddDeleteMoveListener;
import org.eclipse.ui.IMarkerResolution;

/**
 * Generate marker resoultions for a cvs remove marker
 */
public class CVSRemoveResolutionGenerator extends CVSAbstractResolutionGenerator {
	IMarkerResolution commitDeletion =new IMarkerResolution() {
		public String getLabel() {
			return Policy.bind("CVSRemoveResloutionGenerator.Commit_Deletion_to_CVS_1"); //$NON-NLS-1$
		}
		public void run(IMarker marker) {
			try {
				final IContainer parent = (IContainer)marker.getResource();
				final String childName = (String)marker.getAttribute(AddDeleteMoveListener.NAME_ATTRIBUTE);
				ICVSFile mFile = CVSWorkspaceRoot.getCVSFileFor(parent.getFile(new Path(childName)));
				final TeamException[] exception = new TeamException[] {null};
				CVSRemoveResolutionGenerator.this.run(new IRunnableWithProgress() {
					public void run(IProgressMonitor monitor)throws InvocationTargetException, InterruptedException {
						try {
							((CVSTeamProvider)RepositoryProvider.getProvider(parent.getProject())).checkin(new IResource[] {parent.getFile(new Path(childName))}, IResource.DEPTH_ZERO, monitor);
						} catch (TeamException e) {
							exception[0] = e;
						}
					}
				});
				if (exception[0] != null) {
					throw exception[0];
				}
				marker.delete();
			} catch (TeamException e) {
				handle(e, null, null);
			} catch (CoreException e) {
				handle(e, null, null);
			} catch (InvocationTargetException e) {
				handle(e, null, null);
			}  catch (InterruptedException e) {
				// do nothing
			}
		}
	};

	IMarkerResolution undoDeletionLocal = new IMarkerResolution() {
		public String getLabel() {
			return Policy.bind("CVSRemoveResloutionGenerator.Undo_Deletion_from_Local_History_2"); //$NON-NLS-1$
		}
		public void run(IMarker marker) {
			try {
				final IContainer parent = (IContainer)marker.getResource();
				final String childName = (String)marker.getAttribute(AddDeleteMoveListener.NAME_ATTRIBUTE);
				final IFile file = parent.getFile(new Path(childName));
				final ICVSFile mFile = CVSWorkspaceRoot.getCVSFileFor(parent.getFile(new Path(childName)));
				
				boolean recreated = false;
				IFileState[] history = file.getHistory(null);
				for (int i = 0; i < history.length; i++) {
					IFileState state = history[i];
					if (state.exists()) {
						file.create(state.getContents(), false, null);
						mFile.setTimeStamp(new Date(state.getModificationTime()));
						recreated = true;
						break;
					}
				}
				
				if (recreated) {
					if (mFile.isManaged()) {
						ResourceSyncInfo info = mFile.getSyncInfo();
						if (info.isDeleted()) {
							MutableResourceSyncInfo deletedInfo = info.cloneMutable();
							deletedInfo.setDeleted(false);
							mFile.setSyncInfo(deletedInfo);
						}
					}
					marker.delete();
				} else {
					throw new CVSException(Policy.bind("CVSRemoveResloutionGenerator.No_local_history_available._Try_undoing_from_the_server_3")); //$NON-NLS-1$
				}
			} catch (TeamException e) {
				handle(e, null, null);
			} catch (CoreException e) {
				handle(e, null, null);
			}
		}
	};
		
	IMarkerResolution undoDeletion = new IMarkerResolution() {
		public String getLabel() {
			return Policy.bind("CVSRemoveResloutionGenerator.Undo_Deletion_from_CVS_Server_4"); //$NON-NLS-1$
		}
		public void run(IMarker marker) {
			try {
				final IContainer parent = (IContainer)marker.getResource();
				final String childName = (String)marker.getAttribute(AddDeleteMoveListener.NAME_ATTRIBUTE);
				final IFile file = parent.getFile(new Path(childName));
				final ICVSFile mFile = CVSWorkspaceRoot.getCVSFileFor(parent.getFile(new Path(childName)));
				
				if (mFile.isManaged()) {
					ResourceSyncInfo info = mFile.getSyncInfo();
					if (info.isDeleted()) {
						MutableResourceSyncInfo deletedInfo = info.cloneMutable();
						deletedInfo.setDeleted(false);
						mFile.setSyncInfo(deletedInfo);
					}
				}
				
				final TeamException[] exception = new TeamException[] {null};
				CVSRemoveResolutionGenerator.this.run(new IRunnableWithProgress() {
					public void run(IProgressMonitor monitor)throws InvocationTargetException, InterruptedException {
						try {
							CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(parent.getProject());
							provider.update(new IResource[] { parent.getFile(new Path(childName)) },
								Command.NO_LOCAL_OPTIONS, null, true /*createBackups*/, monitor);
						} catch (TeamException e) {
							exception[0] = e;
						}
					}
				});
				if (exception[0] != null) {
					throw exception[0];
				}			
				marker.delete();
			} catch (TeamException e) {
				handle(e, null, null);
			} catch (CoreException e) {
				handle(e, null, null);
			} catch (InvocationTargetException e) {
				handle(e, null, null);
			}  catch (InterruptedException e) {
				// do nothing
			}
		}
	};
	
	/*
	 * @see IMarkerResolutionGenerator#getResolutions(IMarker)
	 */
	public IMarkerResolution[] getResolutions(IMarker marker) {
		return new IMarkerResolution[] {
			commitDeletion, undoDeletionLocal, undoDeletion
		};
	}
}

Back to the top