Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2011-09-01 23:08:34 +0000
committerKevin Sawicki2011-09-01 23:08:34 +0000
commit5f0cea57654c5d01bd14c0fc40cf0bfaf71ac2ae (patch)
tree5e7f87d7426cee8d289adb1a3412a2f42c79dd8e /org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn
parent8d3500530ba8a233505bea6c8df4fa9972c7fde1 (diff)
downloadegit-github-5f0cea57654c5d01bd14c0fc40cf0bfaf71ac2ae.tar.gz
egit-github-5f0cea57654c5d01bd14c0fc40cf0bfaf71ac2ae.tar.xz
egit-github-5f0cea57654c5d01bd14c0fc40cf0bfaf71ac2ae.zip
Support more types of editor inputs when creating Gists.
Adds explicit support for path and uri editor inputs as well as structured selections that can adapt to an IFile handle. Change-Id: I7f91be696e0def8a1c6c1bde8454662ee5b2acea Signed-off-by: Kevin Sawicki <kevin@github.com>
Diffstat (limited to 'org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn')
-rw-r--r--org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/internal/github/ui/gist/CreateGistHandler.java69
1 files changed, 51 insertions, 18 deletions
diff --git a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/internal/github/ui/gist/CreateGistHandler.java b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/internal/github/ui/gist/CreateGistHandler.java
index b665a5fa..624e62d1 100644
--- a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/internal/github/ui/gist/CreateGistHandler.java
+++ b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/internal/github/ui/gist/CreateGistHandler.java
@@ -13,6 +13,7 @@ package org.eclipse.mylyn.internal.github.ui.gist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.net.URI;
import java.util.Set;
import org.eclipse.core.commands.AbstractHandler;
@@ -20,6 +21,8 @@ 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.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IPath;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.GistService;
import org.eclipse.jface.text.ITextSelection;
@@ -30,7 +33,9 @@ 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.IPathEditorInput;
import org.eclipse.ui.ISources;
+import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.handlers.HandlerUtil;
/**
@@ -44,6 +49,11 @@ public class CreateGistHandler extends AbstractHandler {
public static final String PUBLIC_GIST = "publicGist"; //$NON-NLS-1$
/**
+ * DEFAULT_FILENAME
+ */
+ private static final String DEFAULT_FILENAME = "file.txt"; //$NON-NLS-1$
+
+ /**
* @see org.eclipse.core.commands.AbstractHandler#isEnabled()
*/
public boolean isEnabled() {
@@ -62,12 +72,9 @@ public class CreateGistHandler extends AbstractHandler {
* @return the input of the active editor, or <code>null</code>.
*/
private static IEditorInput getActiveEditorInput(ExecutionEvent event) {
- Object o = HandlerUtil.getVariable(event,
+ Object var = HandlerUtil.getVariable(event,
ISources.ACTIVE_EDITOR_INPUT_NAME);
- if (o instanceof IEditorInput) {
- return (IEditorInput) o;
- }
- return null;
+ return var instanceof IEditorInput ? (IEditorInput) var : null;
}
public Object execute(ExecutionEvent event) throws ExecutionException {
@@ -78,25 +85,53 @@ public class CreateGistHandler extends AbstractHandler {
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection == null || selection.isEmpty())
selection = HandlerUtil.getActiveMenuSelection(event);
- if (selection instanceof ITextSelection
- && input instanceof IFileEditorInput) {
+ if (selection == null || selection.isEmpty())
+ return null;
+
+ boolean isPublic = Boolean
+ .parseBoolean(event.getParameter(PUBLIC_GIST));
+ if (selection instanceof ITextSelection) {
ITextSelection text = (ITextSelection) selection;
- IFile file = ((IFileEditorInput) input).getFile();
- createGistJob(file.getName(), file.getFileExtension(),
- text.getText(),
- Boolean.parseBoolean(event.getParameter(PUBLIC_GIST)));
+ String name = null;
+ if (input instanceof IFileEditorInput) {
+ IFile file = ((IFileEditorInput) input).getFile();
+ if (file != null)
+ name = file.getName();
+ }
+ if (name == null && input instanceof IPathEditorInput) {
+ IPath path = ((IPathEditorInput) input).getPath();
+ if (path != null)
+ name = path.lastSegment();
+ }
+ if (name == null && input instanceof IURIEditorInput) {
+ URI uri = ((IURIEditorInput) input).getURI();
+ if (uri != null) {
+ String rawPath = uri.getRawPath();
+ if (rawPath != null) {
+ int lastSlash = rawPath.lastIndexOf('/') + 1;
+ if (lastSlash > 0 && lastSlash < rawPath.length())
+ name = rawPath.substring(lastSlash);
+ }
+ }
+ }
+ if (name == null)
+ name = DEFAULT_FILENAME;
+ createGistJob(name, text.getText(), isPublic);
} else if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
Object obj = structuredSelection.getFirstElement();
+ IFile file = null;
if (obj instanceof IFile)
- createGistJob((IFile) obj,
- Boolean.parseBoolean(event.getParameter(PUBLIC_GIST)));
+ file = (IFile) obj;
+ else if (obj instanceof IAdaptable)
+ file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
+ if (file != null)
+ createGistJob(file, isPublic);
}
return null;
}
- private void createGistJob(String name, String extension, String contents,
- boolean isPublic) {
+ private void createGistJob(String name, String contents, boolean isPublic) {
Set<TaskRepository> repositories = GistConnectorUi.getRepositories();
// only use the first repository, in the future provide a
@@ -108,7 +143,6 @@ public class CreateGistHandler extends AbstractHandler {
CreateGistJob job = new CreateGistJob(
Messages.CreateGistHandler_CreateGistJobName, name, contents,
service, isPublic);
- job.setSystem(true);
job.schedule();
}
@@ -122,8 +156,7 @@ public class CreateGistHandler extends AbstractHandler {
result.append(line).append('\n');
String contents = result.toString();
- createGistJob(file.getName(), file.getFileExtension(), contents,
- isPublic);
+ createGistJob(file.getName(), contents, isPublic);
} catch (CoreException e) {
GitHubUi.logError(e);
} catch (IOException e) {

Back to the top