Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lukbukkit committed Jul 18, 2016
1 parent 044f5ee commit e0a13a1
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.jar
/.idea/
13 changes: 13 additions & 0 deletions HasteIt.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PLUGIN_MODULE" version="4">
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/resources/META-INF/plugin.xml" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# HasteIt

HasteIt is a plugin for [IDEA](https://www.jetbrains.com/idea/)-based IDEs.
You can quickly share your current file to [hastebin](http://hastebin.com/).
35 changes: 35 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<idea-plugin version="2">
<id>de.lukweb.hasteit</id>
<name>HasteIt</name>
<version>1.0</version>
<vendor email="[email protected]" url="http://www.lukweb.de">LukWeb</vendor>

<depends>com.intellij.modules.platform</depends>

<description><![CDATA[
Enter short description for your plugin here.<br>
<em>most HTML tags may be used</em>
]]></description>

<change-notes><![CDATA[
Add change notes here.<br>
<em>most HTML tags may be used</em>
]]>
</change-notes>

<idea-version since-build="141.0"/>

<extensions defaultExtensionNs="com.intellij">

</extensions>

<actions>
<action id="de.lukweb.hasteit.MenuHaste" class="de.lukweb.hasteit.MenuHaste" text="Haste it!"
description="Share your selected text to hastebin!" icon="HasteItIcons.UPLOAD_ACTION">
<add-to-group group-id="ToolsMenu" anchor="last"/>
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
<add-to-group group-id="FileMenu" anchor="last"/>
</action>
</actions>

</idea-plugin>
Binary file added resources/icons/hastebin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
157 changes: 157 additions & 0 deletions src/de/lukweb/hasteit/MenuHaste.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package de.lukweb.hasteit;

import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

import javax.swing.event.HyperlinkEvent.EventType;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;

public class MenuHaste extends AnAction {

private static final NotificationGroup notificationGroup = new NotificationGroup("HasteIt", NotificationDisplayType.BALLOON, false);

@Override
public void actionPerformed(AnActionEvent event) {

PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE);

if (psiFile == null) {
err("No file is selected!");
return;
}

Editor editor = event.getData(PlatformDataKeys.EDITOR);

if (editor == null) {
err("There's no editor!");
return;
}

SelectionModel selectionModel = editor.getSelectionModel();

if (!selectionModel.hasSelection()) {
err("No text is selected!");
return;
}

String text = selectionModel.getSelectedText();

ProgressManager.getInstance().run(new Task.Backgroundable(event.getProject(), "Uploading to Hastebin") {
@Override
public void run(@NotNull ProgressIndicator progressIndicator) {
progressIndicator.setIndeterminate(true);
String hasteCode = saveTextToHastebin(text);

if (hasteCode == null) {
progressIndicator.cancel();
return;
}

String fullHastebinUrl = "http://hastebin.com/" + hasteCode + "." + psiFile.getFileType().getDefaultExtension();

copyToClipboard(fullHastebinUrl);

notificationGroup.createNotification(
"HasteIt",
"Upload successful! Copied to clipboard! <a href=\"" + fullHastebinUrl + "\">Open in Browser</a> ",
NotificationType.INFORMATION,
(notification, hyperlinkEvent) -> {
if (!hyperlinkEvent.getEventType().equals(EventType.ACTIVATED)) return;
openURL(hyperlinkEvent.getURL());
}

).notify(null);

progressIndicator.cancel();
}
});

}

private void show(String text) {
show(text, NotificationType.INFORMATION);
}

private void err(String text) {
show(text, NotificationType.ERROR);
}

private void show(String text, NotificationType type) {
notificationGroup.createNotification(text, type).notify(null);
}

private String saveTextToHastebin(String text) {
try {
String url = "http://www.hastebin.com/documents";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(text);
wr.flush();
wr.close();

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

//print result

String[] split = response.toString().split(":");
return split[1].replaceAll("}", "").replace("{", "").replaceAll("\"", "");

} catch (IOException e) {
err("Error while uploading: " + e.getLocalizedMessage());
}
return null;
}

private void copyToClipboard(String text) {
StringSelection selection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
}

private void openURL(URL url) {
try {
Desktop.getDesktop().browse(url.toURI());
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
}

}
11 changes: 11 additions & 0 deletions src/icons/HasteItIcons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package icons;

import com.intellij.openapi.util.IconLoader;

import javax.swing.*;

public interface HasteItIcons {

Icon UPLOAD_ACTION = IconLoader.getIcon("hastebin.png");

}

0 comments on commit e0a13a1

Please sign in to comment.