From e0a13a15fe24906c7f60470e78512b57e32ff992 Mon Sep 17 00:00:00 2001 From: LukBukkit Date: Mon, 18 Jul 2016 15:05:42 +0200 Subject: [PATCH] Initial Commit --- .gitignore | 2 + HasteIt.iml | 13 +++ README.md | 4 + resources/META-INF/plugin.xml | 35 ++++++ resources/icons/hastebin.png | Bin 0 -> 438 bytes resources/icons/hastebin@2x.png | Bin 0 -> 863 bytes src/de/lukweb/hasteit/MenuHaste.java | 157 +++++++++++++++++++++++++++ src/icons/HasteItIcons.java | 11 ++ 8 files changed, 222 insertions(+) create mode 100644 .gitignore create mode 100644 HasteIt.iml create mode 100644 README.md create mode 100644 resources/META-INF/plugin.xml create mode 100644 resources/icons/hastebin.png create mode 100644 resources/icons/hastebin@2x.png create mode 100644 src/de/lukweb/hasteit/MenuHaste.java create mode 100644 src/icons/HasteItIcons.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf3cf0a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.jar +/.idea/ \ No newline at end of file diff --git a/HasteIt.iml b/HasteIt.iml new file mode 100644 index 0000000..e025b20 --- /dev/null +++ b/HasteIt.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e797300 --- /dev/null +++ b/README.md @@ -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/). diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml new file mode 100644 index 0000000..c1d5c71 --- /dev/null +++ b/resources/META-INF/plugin.xml @@ -0,0 +1,35 @@ + + de.lukweb.hasteit + HasteIt + 1.0 + LukWeb + + com.intellij.modules.platform + + + most HTML tags may be used + ]]> + + + most HTML tags may be used + ]]> + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/icons/hastebin.png b/resources/icons/hastebin.png new file mode 100644 index 0000000000000000000000000000000000000000..fa4b02564c1c3d52a1e9225c003ed62789550146 GIT binary patch literal 438 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`EX7WqAsj$Z!;#Vf2?- zLJ($@v2Q2`G9*h}BT9nv(@M${i&7cN%ggmL^RkPR6AM!H@{7`Ezq65IU|=-%ba4!^ z=)HTv&^yLa=e>Gi37*JjVo zn$-W%bm1*Y0rh}g%nRD)-sWFlx_ExdZsvq-{Go>!d)J%jWxh$XI>%n%SKV>VKWcxb zpBTf&XgaN)uFmn!srtIF245#H{;znC!{A!MflHo>2ln2{_fMYt zaQ{=26%nd+`Yq86aW=<93mc!Q9}4cdRL7WVtHpYm#t dRf+v8sZ!z4b!C%p>;r}|gQu&X%Q~loCII<;umb=9 literal 0 HcmV?d00001 diff --git a/resources/icons/hastebin@2x.png b/resources/icons/hastebin@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..30faf54b46150d84f08e21e229f3273532c038d2 GIT binary patch literal 863 zcmV-l1EBngP)8-G7k>)6IKVcj<;U^qsqM2uuo?&C0a5NQrk+6zG80rCjH#5y{ z*Zd*EOT=S@MLe%MrZLRM zo%p&0pmT>A@fl`6ECF090Q{pnwlG3=OEUJc>$r-9&oG_Nx`VDO*t-dLCF85=;n2FL zVEoz#JRGg&!iLIPE&v!RY{1xOiPXjlM(Le*7T7r^F7(d7d~?D0vjm_>!1#Ud0-YU^ zvGPNAC?{aN!UBpra)o%Z1fX`c4H&F#xih@GREp>M{lFtUmy{DcTAo0Dhv9GL&Q+5e z+mMg21aM&liGbnf-iLqkG{%$hM(#}jiUf?O6{ud2;)F_Fb`kI(&D+<^12vlai0>rF z7wkHcN7FMb5-{``VR1)<9s~^V3+}}kn#Z&k&nUK~4^I-FB6J$e_dub8tPhDgT}CeV zAz*kJb>fWfADN4oVfj@imy4MRz>Tx4yw}#ZmdTZ*{sgOah9Wcnlwy-)buL|Gvc;$K pWIM^W5YDr_SUz}N)@5CC`44YA#)V#3NErYC002ovPDHLkV1n(RhF|~y literal 0 HcmV?d00001 diff --git a/src/de/lukweb/hasteit/MenuHaste.java b/src/de/lukweb/hasteit/MenuHaste.java new file mode 100644 index 0000000..8a20230 --- /dev/null +++ b/src/de/lukweb/hasteit/MenuHaste.java @@ -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! Open in Browser ", + 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(); + } + } + +} diff --git a/src/icons/HasteItIcons.java b/src/icons/HasteItIcons.java new file mode 100644 index 0000000..3336788 --- /dev/null +++ b/src/icons/HasteItIcons.java @@ -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"); + +}