Skip to main content
summaryrefslogtreecommitdiffstats
blob: 80ce5e052e41324ce7f7df378142b3f25d18c1ef (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package org.eclipse.team.internal.ui.target;

/*
 * (c) Copyright IBM Corp. 2000, 2002.
 * All Rights Reserved.
 */

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.target.TargetManager;
import org.eclipse.team.core.target.TargetProvider;
import org.eclipse.team.internal.core.InfiniteSubProgressMonitor;
import org.eclipse.team.internal.core.TeamPlugin;
import org.eclipse.team.internal.ui.DetailsDialog;
import org.eclipse.team.internal.ui.IPromptCondition;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.PromptingDialog;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

/**
 * Action for getting the contents of the selected resources
 */
public class GetAction extends TargetAction {	
	
	private class OutgoingChangesDialog extends DetailsDialog {
		private IResource[] outgoingChanges;
		private org.eclipse.swt.widgets.List detailsList;
		
		public OutgoingChangesDialog(Shell shell, IResource[] outgoingChanges) {
			super(shell, Policy.bind("GetAction.confirmFileOverwriteTitle")); //$NON-NLS-1$
			this.outgoingChanges = outgoingChanges;
		}
			
		protected Composite createDropDownDialogArea(Composite parent) {
			// create a composite with standard margins and spacing
			Composite composite = new Composite(parent, SWT.NONE);
			GridLayout layout = new GridLayout();
			layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
			layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
			layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
			layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
			composite.setLayout(layout);
			composite.setLayoutData(new GridData(GridData.FILL_BOTH));
			composite.setFont(parent.getFont());
			
			detailsList = new org.eclipse.swt.widgets.List(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);	 
			GridData data = new GridData ();		
			data.heightHint = 75;
			data.horizontalAlignment = GridData.FILL;
			data.grabExcessHorizontalSpace = true;
			detailsList.setLayoutData(data);
			
			for (int i = 0; i < outgoingChanges.length; i++) {
				detailsList.add(outgoingChanges[i].getFullPath().toString()); //$NON-NLS-1$
			}			
			return composite;
		}

		protected void createMainDialogArea(Composite top) {
			Composite parent = new Composite(top, SWT.NONE);
			GridLayout layout = new GridLayout();
			layout.marginHeight = 0;
			layout.marginWidth = 0;
			layout.numColumns = 2;
			parent.setLayout(layout);
			parent.setLayoutData(new GridData(GridData.FILL_BOTH));
			parent.setFont(parent.getFont());
			
			// create image
			Image image = getImage(DLG_IMG_QUESTION);
			if (image != null) {
				Label label = new Label(parent, 0);
				image.setBackground(label.getBackground());
				label.setImage(image);
				label.setLayoutData(new GridData(
					GridData.HORIZONTAL_ALIGN_CENTER |
					GridData.VERTICAL_ALIGN_BEGINNING));
			}
			
			Label label = new Label(parent, SWT.WRAP);
			label.setText(Policy.bind("GetAction.confirmFileOverwrite")); //$NON-NLS-1$
			GridData data = new GridData(
				GridData.GRAB_HORIZONTAL |
				GridData.GRAB_VERTICAL |
				GridData.HORIZONTAL_ALIGN_FILL |
				GridData.VERTICAL_ALIGN_CENTER);
			data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
			label.setLayoutData(data);
			label.setFont(parent.getFont());				
			setPageComplete(true);
		}

		protected void updateEnablements() {
		}
	}
	
	public void run(IAction action) {	
		if(promptForOutgoingChanges()) {
			run(new WorkspaceModifyOperation() {
				public void execute(IProgressMonitor monitor) throws InterruptedException, InvocationTargetException {
					try {
						Hashtable table = getTargetProviderMapping();
						Set keySet = table.keySet();
						monitor.beginTask(null, keySet.size() * 1000);
						
						// perform the get on each provider
						Iterator iterator = keySet.iterator();
						while (iterator.hasNext()) {					
							TargetProvider provider = (TargetProvider)iterator.next();
							monitor.setTaskName(Policy.bind("GetAction.working", provider.getURL().toExternalForm()));  //$NON-NLS-1$
							List list = (List)table.get(provider);
							IResource[] providerResources = (IResource[])list.toArray(new IResource[list.size()]);
							provider.get(providerResources, Policy.subInfiniteMonitorFor(monitor, 1000));
						}
					} catch (TeamException e) {
						throw new InvocationTargetException(e);
					} finally {
						monitor.done();
					}
				}
			}, Policy.bind("GetAction.title"), this.PROGRESS_DIALOG); //$NON-NLS-1$
		}
	}
	
	private boolean promptForOutgoingChanges() {
		try {
			// find any outgoing changes that will be overwritten and prompt
			Hashtable table = getTargetProviderMapping();
			Set keySet = table.keySet();
	
			Iterator iterator = keySet.iterator();
			List outgoingChanges = new ArrayList();
			while (iterator.hasNext()) {					
				TargetProvider provider = (TargetProvider)iterator.next();
				List list = (List)table.get(provider);
				IResource[] providerResources = (IResource[])list.toArray(new IResource[list.size()]);
				outgoingChanges.addAll(Arrays.asList(findResourcesWithOutgoingChanges(providerResources)));
			}
			if(! outgoingChanges.isEmpty()) {
				final OutgoingChangesDialog dialog = new OutgoingChangesDialog(getShell(), 
														(IResource[]) outgoingChanges.toArray(new IResource[outgoingChanges.size()]));
				final boolean okToContinue[] = {true};
				getShell().getDisplay().syncExec(new Runnable() {
					public void run() {
						if(dialog.open() != dialog.OK) {
							okToContinue[0] = false;
						}
					}
				});
				return okToContinue[0];
			}
			return true;
		} catch(CoreException e) {
			TeamUIPlugin.handle(e);
		} catch(TeamException e) {
			TeamUIPlugin.handle(e);
		}		
		return false;
	}
	
	/**
	 * @see TeamAction#isEnabled()
	 */
	protected boolean isEnabled() {
		IResource[] resources = getSelectedResources();
		if (resources.length == 0) return false;
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			try {
				TargetProvider provider = TargetManager.getProvider(resource.getProject());			
				if(provider == null)
					return false;
				if(! provider.canPut(resource))
					return false;	//if one can't don't allow for any
			} catch (TeamException e) {
				TeamPlugin.log(IStatus.ERROR, Policy.bind("GetAction.Exception_getting_provider_2"), e); //$NON-NLS-1$
				return false;
			}
		}
		return true;
	}
}

Back to the top