Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b665a5fa6ebd3109263ec0ce3c08e44fb34fec9a (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
/*******************************************************************************
 * Copyright (c) 2011 Red Hat 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:
 *     Chris Aniszczyk <caniszczyk@gmail.com> - initial contribution
 *******************************************************************************/
package org.eclipse.mylyn.internal.github.ui.gist;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.GistService;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.internal.github.core.GitHub;
import org.eclipse.mylyn.internal.github.ui.GitHubUi;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.ISources;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Create Gist handler class.
 */
public class CreateGistHandler extends AbstractHandler {

	/**
	 * PUBLIC_GIST
	 */
	public static final String PUBLIC_GIST = "publicGist"; //$NON-NLS-1$

	/**
	 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
	 */
	public boolean isEnabled() {
		return !GistConnectorUi.getRepositories().isEmpty();
	}

	/**
	 * TODO replace this with HandlerUtil.getActiveEditorInput(ExecutionEvent)
	 * as soon as we don't support Eclipse 3.6 anymore copied from HandlerUtil
	 * in 3.7 to be able to run this on 3.6
	 * 
	 * Return the input of the active editor.
	 * 
	 * @param event
	 *            The execution event that contains the application context
	 * @return the input of the active editor, or <code>null</code>.
	 */
	private static IEditorInput getActiveEditorInput(ExecutionEvent event) {
		Object o = HandlerUtil.getVariable(event,
				ISources.ACTIVE_EDITOR_INPUT_NAME);
		if (o instanceof IEditorInput) {
			return (IEditorInput) o;
		}
		return null;
	}

	public Object execute(ExecutionEvent event) throws ExecutionException {
		// TODO replace this with
		// HandlerUtil.getActiveEditorInput(ExecutionEvent) as soon
		// as we don't support Eclipse 3.6 anymore
		IEditorInput input = getActiveEditorInput(event);
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection == null || selection.isEmpty())
			selection = HandlerUtil.getActiveMenuSelection(event);
		if (selection instanceof ITextSelection
				&& input instanceof IFileEditorInput) {
			ITextSelection text = (ITextSelection) selection;
			IFile file = ((IFileEditorInput) input).getFile();
			createGistJob(file.getName(), file.getFileExtension(),
					text.getText(),
					Boolean.parseBoolean(event.getParameter(PUBLIC_GIST)));
		} else if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			Object obj = structuredSelection.getFirstElement();
			if (obj instanceof IFile)
				createGistJob((IFile) obj,
						Boolean.parseBoolean(event.getParameter(PUBLIC_GIST)));
		}
		return null;
	}

	private void createGistJob(String name, String extension, String contents,
			boolean isPublic) {
		Set<TaskRepository> repositories = GistConnectorUi.getRepositories();

		// only use the first repository, in the future provide a
		// selection if multiple exist
		TaskRepository repository = repositories.iterator().next();
		GitHubClient client = GitHub.configureClient(new GitHubClient());
		GitHub.addCredentials(client, repository);
		GistService service = new GistService(client);
		CreateGistJob job = new CreateGistJob(
				Messages.CreateGistHandler_CreateGistJobName, name, contents,
				service, isPublic);
		job.setSystem(true);
		job.schedule();
	}

	private void createGistJob(IFile file, boolean isPublic) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(file.getContents()));
			String line;
			StringBuilder result = new StringBuilder();
			while ((line = br.readLine()) != null)
				result.append(line).append('\n');

			String contents = result.toString();
			createGistJob(file.getName(), file.getFileExtension(), contents,
					isPublic);
		} catch (CoreException e) {
			GitHubUi.logError(e);
		} catch (IOException e) {
			GitHubUi.logError(e);
		} finally {
			if (br != null)
				try {
					br.close();
				} catch (IOException e) {
					GitHubUi.logError(e);
				}
		}
	}

}

Back to the top