Advertisement
maccaches

Pastebin

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