Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 47254d51f429369f8e20015d00851de36929fed8 (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
/*******************************************************************************
 * Copyright (c) 2015 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.ui.internal.handlers;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.runtime.IFSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.runtime.IRuntimeModel;
import org.eclipse.tcf.te.tcf.filesystem.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.filesystem.ui.nls.Messages;
import org.eclipse.ui.handlers.HandlerUtil;
/**
 * The handler that downloads the selected files or folders to a local destination folder.
 */
public class DownloadFilesHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Shell shell = HandlerUtil.getActiveShellChecked(event);
		DirectoryDialog dlg = new DirectoryDialog(shell);
		dlg.setFilterPath(getFilterPath());
		dlg.setText(Messages.DownloadFilesHandler_folderDlg_text);
		dlg.setMessage(Messages.DownloadFilesHandler_folderDlg_message);

		final String destination = dlg.open();
		if (destination == null)
			return null;

		final File destinationFile = new File(destination);
		saveFilterPath(destinationFile);

		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
		List<IFSTreeNode> nodes = selection.toList();
		IRuntimeModel peer = nodes.get(0).getRuntimeModel();
		peer.operationDownload(nodes, destinationFile, new MoveCopyCallback()).runInUserJob(new Callback() {
			@Override
			protected void internalDone(Object caller, IStatus status) {
				for (IContainer container : ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(destinationFile.toURI())) {
					if (container != null) {
						try {
							container.refreshLocal(IResource.DEPTH_ONE, null);
						}
						catch (CoreException e) {
							e.printStackTrace();
						}
					}
				}
			}
		});

		return null;
	}

	private String getFilterPath() {
		String lastFolder = UIPlugin.getDefault().getDialogSettings().get(getClass().getName() + ".lastFolder"); //$NON-NLS-1$
		if (lastFolder != null)
			return lastFolder;
		Location loc = Platform.getInstanceLocation();
		if (loc != null) {
			URL url = loc.getURL();
			try {
				File file = URIUtil.toFile(url.toURI());
				if (file != null)
					return file.getPath();
            } catch (URISyntaxException e) {
            }
		}
	    return null;
    }

	private void saveFilterPath(File destinationFile) {
		if (destinationFile != null) {
			UIPlugin.getDefault().getDialogSettings().put(getClass().getName() + ".lastFolder", destinationFile.getPath()); //$NON-NLS-1$
		}
    }

}

Back to the top