Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3e4fce86159fb3465deeb5442487542c349193f5 (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
/*******************************************************************************
 * 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 implementation
 ******************************************************************************/
package org.eclipse.team.internal.ui.target;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.target.IRemoteTargetResource;
import org.eclipse.team.core.target.Site;
import org.eclipse.team.internal.core.target.UrlUtil;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.ui.model.WorkbenchLabelProvider;

public class MappingSelectionPage extends TargetWizardPage {
	private IPath path = Path.EMPTY;
	private Site site;
	private TreeViewer viewer;
	private Text textPath;
	
	public MappingSelectionPage(String pageName, String title, ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
		setDescription(Policy.bind("MappingSelectionPage.description")); //$NON-NLS-1$
	}

	public void setSite(Site site) {
		this.site = site;
	}
	
	public Site getSite() {
		return site;
	}
	
	public void createControl(Composite p) {
		Composite composite = createComposite(p, 1);
		
		createLabel(composite, Policy.bind("MappingSelectionPage.label")); //$NON-NLS-1$
		
		viewer = new TreeViewer(composite, SWT.BORDER | SWT.SINGLE);
		
		GridData data = new GridData (GridData.FILL_BOTH);
		viewer.getTree().setLayoutData(data);
		viewer.setContentProvider(new SiteLazyContentProvider());
		viewer.setLabelProvider(new WorkbenchLabelProvider());
		viewer.setSorter(new SiteViewSorter());
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				updateTextPath();
			}
		});
		
		// include only folders in view
		viewer.addFilter(new ViewerFilter() {
			public boolean select(Viewer viewer, Object parentElement, Object element) {
				if(element instanceof RemoteResourceElement) {
					return ((RemoteResourceElement)element).getRemoteResource().isContainer();
				}
				return false;
			}
		});
		
		Button newFolderButton = new Button(composite, SWT.PUSH);
		newFolderButton.setText(Policy.bind("MappingSelectionPage.newFolderLabel")); //$NON-NLS-1$
		newFolderButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent (Event event) {
				Shell shell = getShell();
				try {
					// assume that only one folder is selected in the folder tree
					IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
					Object currentSelection = selection.getFirstElement();
					final IRemoteTargetResource selectedFolder = getSelectedRemoteFolder(selection);					
					String defaultName = ((ConfigureTargetWizard) getWizard()).project.getName();

					IRemoteTargetResource newFolder = CreateNewFolderAction.createDir(getShell(), selectedFolder, defaultName);
					if (newFolder == null)
						return;

					RemoteResourceElement newFolderUIElement = new RemoteResourceElement(newFolder);

					((RemoteResourceElement)currentSelection).setCachedChildren(null);
					viewer.refresh(currentSelection);
					viewer.setExpandedState(currentSelection, true);
					viewer.setSelection(new StructuredSelection(newFolderUIElement));
				} catch (TeamException e) {
					TeamUIPlugin.handle(e);
					return;
				}
			}			
		});
		setViewerInput();
		setControl(composite);
		setPageComplete(true);
	}
	
	private IRemoteTargetResource getSelectedRemoteFolder(IStructuredSelection selection) {		
		if (!selection.isEmpty()) {
			final List filesSelection = new ArrayList();
			Iterator it = selection.iterator();
			while(it.hasNext()) {
				Object o = it.next();
				if(o instanceof RemoteResourceElement) {
					return ((RemoteResourceElement)o).getRemoteResource();
				} else if(o instanceof SiteElement) {
					try {
						return ((SiteElement)o).getSite().getRemoteResource();
					} catch (TeamException e) {
						return null;
					}
				}
			}
		}
		return null;
	}
	
	/**
	 * Method updateTextPath.
	 */
	private void updateTextPath() {
		IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
		if (!selection.isEmpty()) {
			final List filesSelection = new ArrayList();
			Iterator it = selection.iterator();
			while(it.hasNext()) {
				Object o = it.next();
				if(o instanceof RemoteResourceElement) {
						RemoteResourceElement element = (RemoteResourceElement) o;
						URL remoteResourceURL;
						remoteResourceURL = element.getRemoteResource().getURL();
						this.path = UrlUtil.getTrailingPath(
							remoteResourceURL,
							this.site.getURL());
					return;
				}
			}
		}
	}

	public IPath getMapping() {
		return this.path;
	}
	
	/**
	 * Attempt to set the viewer input.
	 * Do nothing if we don't have enough info yet to set it.
	 */
	private void setViewerInput() {
		if(this.site == null || viewer == null)
			return;
		viewer.setInput(new SiteRootsElement(new Site[] {site}));		
	}

	/**
	 * @see IDialogPage#setVisible(boolean)
	 */
	public void setVisible(boolean visible) {
		if(visible) {
			setViewerInput();
			viewer.setSelection(new StructuredSelection(new SiteElement(site)));
		}
		super.setVisible(visible);
	}
}

Back to the top