Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bluej.extensions.*;
- import bluej.extensions.event.*;
- import bluej.extensions.editor.*;
- import java.net.URL;
- import javax.swing.*;
- import java.awt.event.*;
- import java.net.*;
- import java.io.*;
- import java.util.Scanner;
- public class BlueJPastebin extends Extension implements PackageListener {
- public void startup (BlueJ bluej) {
- bluej.setMenuGenerator(new MenuBuilder());
- bluej.addPackageListener(this);
- }
- public void packageOpened ( PackageEvent ev ) {
- try {
- System.out.println ("Project " + ev.getPackage().getProject().getName() + " opened.");
- } catch (ExtensionException e) {
- System.out.println("Project closed by BlueJ");
- }
- }
- public void packageClosing ( PackageEvent ev ) {
- }
- public boolean isCompatible () {
- return true;
- }
- public String getVersion () {
- return ("0.1");
- }
- public String getName () {
- return ("BlueJPastebin");
- }
- public void terminate() {
- System.out.println ("BlueJPastebin exiting");
- }
- public String getDescription () {
- return ("Allows users to directly upload code from BlueJ to the Pastebin service");
- }
- public URL getURL () {
- try {
- return new URL("http://www.bluej.org/doc/writingextensions.html");
- } catch ( Exception eee ) {
- System.out.println ("BlueJPasebin: getURL: Exception="+eee.getMessage());
- return null;
- }
- }
- }
- class MenuBuilder extends MenuGenerator {
- public BClass curClass;
- public JMenuItem getClassMenuItem(BClass aClass) {
- JMenu jm = new JMenu("Pastebin");
- jm.add(new JMenuItem(new EditAction()));
- return jm;
- }
- public void notifyPostClassMenu(BClass bc, JMenuItem jmi) {
- System.out.println("Post on Class menu by BlueJPastebin");
- curClass = bc ;
- }
- private void addCode() {
- try {
- Editor classEditor = null;
- try {
- classEditor = curClass.getEditor();
- } catch (Exception edt){
- edt.printStackTrace();
- }
- if(classEditor == null) {
- System.out.println("Can't create Editor for " + curClass);
- return;
- }
- int textLen = classEditor.getTextLength();
- int lines = classEditor.getLineCount();
- TextLocation start = new TextLocation(0,0);
- TextLocation end = new TextLocation(134,1);
- classEditor.setSelection(start,end);
- TextLocation beginningLocation = classEditor.getSelectionBegin();
- TextLocation endingLocation = classEditor.getSelectionEnd();
- String zigzby = classEditor.getText(beginningLocation, endingLocation);
- Scanner input = new Scanner(System.in);
- String urlParameters = "api_option=paste&api_paste_code="+zigzby+"&api_dev_key=//removed dev key for privacy&api_paste_expire_date=1M";
- URL myURL = new URL("http://pastebin.com/api/api_post.php");
- URLConnection myURLConnection = myURL.openConnection();
- myURLConnection.setDoOutput(true);
- myURLConnection.connect();
- OutputStreamWriter writer = new OutputStreamWriter(myURLConnection.getOutputStream());
- writer.write(urlParameters);
- writer.flush();
- String line;
- BufferedReader reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
- while ((line = reader.readLine()) != null) {
- JOptionPane.showMessageDialog(null, line);
- }
- writer.close();
- reader.close();
- }
- catch (MalformedURLException e) {
- // new URL() failed
- // ...
- System.out.println("Enter a valid URL");
- }
- catch (IOException e) {
- // openConnection() failed
- // ...
- System.out.println("INPUT OUTPUT ERROR");
- }
- }
- class EditAction extends AbstractAction {
- public EditAction() {
- putValue(AbstractAction.NAME, "Upload to Patebin");
- }
- public void actionPerformed(ActionEvent e) {
- addCode();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement