Advertisement
maccaches

Input (zigzby)

Nov 25th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. /**
  2.  * BlueJPastebin
  3.  * Connects BlueJ to the Pastebin service
  4.  * @author revoked
  5.  * @version Alpha 0.1
  6.  * Code adapted from BlueJ source code
  7.  
  8. import bluej.extensions.*;
  9. import bluej.extensions.event.*;
  10. import bluej.extensions.editor.*;
  11.  
  12. import java.net.URL;
  13. import javax.swing.*;
  14. import java.awt.event.*;
  15. import java.net.*;
  16. import java.io.*;
  17. import java.util.Scanner;
  18. public class SimpleExtension2 extends Extension implements PackageListener {
  19.     public void startup (BlueJ bluej) {
  20.         bluej.setMenuGenerator(new MenuBuilder());
  21.         bluej.addPackageListener(this);
  22.     }
  23.     public void packageOpened ( PackageEvent ev ) {
  24.         try {
  25.             System.out.println ("Project " + ev.getPackage().getProject().getName() + " opened.");
  26.         } catch (ExtensionException e) {
  27.             System.out.println("Project closed by BlueJ");
  28.         }
  29.     }
  30.     public void packageClosing ( PackageEvent ev ) {
  31.     }  
  32.     public boolean isCompatible () {
  33.         return true;
  34.     }
  35.     public String  getVersion () {
  36.         return ("0.1");  
  37.     }
  38.     public String  getName () {
  39.         return ("BlueJPastebin");  
  40.     }
  41.     public void terminate() {
  42.         System.out.println ("BlueJPastebin exiting");
  43.     }
  44.     public String getDescription () {
  45.         return ("Allows users to directly upload code from BlueJ to the Pastebin service");
  46.     }
  47.     public URL getURL () {
  48.         try {
  49.             return new URL("http://www.bluej.org/doc/writingextensions.html");
  50.         } catch ( Exception eee ) {
  51.             System.out.println ("BlueJPasebin: getURL: Exception="+eee.getMessage());
  52.             return null;
  53.         }
  54.     }
  55. }
  56. class MenuBuilder extends MenuGenerator {
  57.     private BPackage curPackage;
  58.     private BClass curClass;
  59.     private BObject curObject;
  60.     public JMenuItem getClassMenuItem(BClass aClass) {
  61.         JMenu jm = new JMenu("Pastebin");
  62.         jm.add(new JMenuItem(new EditAction()));
  63.         return jm;
  64.     }
  65.     public void notifyPostClassMenu(BClass bc, JMenuItem jmi) {
  66.         System.out.println("Post on Class menu by BlueJPastebin");
  67.         curPackage = null ; curClass = bc ; curObject = null;
  68.     }
  69.    
  70.     public void addCode() {
  71.         Editor classEditor = null;
  72.         try {
  73.             classEditor = curClass.getEditor();
  74.         } catch (Exception e) { }
  75.         if(classEditor == null) {
  76.             System.out.println("Can't create Editor for " + curClass);
  77.             return;
  78.         }
  79.        
  80.         int textLen = classEditor.getTextLength();
  81.         TextLocation beginningLocation = classEditor.getSelectionBegin();
  82.         TextLocation endingLocation = classEditor.getSelectionEnd();
  83.         TextLocation lastLine = classEditor.getTextLocationFromOffset(textLen);
  84.         try {
  85.     Scanner input = new Scanner(System.in);
  86.     System.out.println("Enter your pastebin code:");
  87.     String urlParameters = "api_option=paste&api_paste_code="ha"&api_dev_key="revokedforso";
  88.     URL myURL = new URL("http://pastebin.com/api/api_post.php");
  89.     URLConnection myURLConnection = myURL.openConnection();
  90.     myURLConnection.setDoOutput(true);
  91.  
  92.     myURLConnection.connect();
  93.    
  94.     OutputStreamWriter writer = new OutputStreamWriter(myURLConnection.getOutputStream());
  95.  
  96.     writer.write(urlParameters);
  97.     writer.flush();
  98.  
  99.     String line;
  100.     BufferedReader reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
  101.  
  102.     while ((line = reader.readLine()) != null) {
  103.         JOptionPane.showMessageDialog(null, line);
  104.     }
  105.     writer.close();
  106.     reader.close();  
  107. }
  108. catch (MalformedURLException e) {
  109.     // new URL() failed
  110.     // ...
  111.     System.out.println("Enter a valid URL");
  112. }
  113. catch (IOException e) {  
  114.     // openConnection() failed
  115.     // ...
  116.     System.out.println("INPUT OUTPUT ERROR");
  117. }
  118.     }
  119.     class EditAction extends AbstractAction {
  120.         public EditAction() {
  121.             putValue(AbstractAction.NAME, "Upload to Patebin");
  122.         }
  123.         public void actionPerformed(ActionEvent e) {
  124.             addCode();
  125.         }
  126.     }
  127. }
  128. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement