blob: f618d2cdd817e7e4b261573f72dd1604ad569dff [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2016 Chalmers | University of Gothenburg, rt-labs 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:
* Chalmers | University of Gothenburg and rt-labs - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.capra.ui.drive;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import com.google.api.services.drive.model.File;
/**
* A handler for displaying the selected Google sheet in the default browser.
*
* @author Dusan Kalanj
*
*/
public class OpenInBrowserHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage activePage = window.getActivePage();
ISelection selection = activePage.getSelection();
if (selection instanceof IStructuredSelection) {
File driveFile = (File) ((IStructuredSelection) selection).getFirstElement();
try {
Desktop.getDesktop().browse(new URI("https://docs.google.com/spreadsheets/d/" + driveFile.getId()));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}
return null;
}
}