Commit 69b39e94 authored by Markus Bauer's avatar Markus Bauer
Browse files

Export to pseuco.com

parent 3c10d1b3
package gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import com.pseuco.PseuCoShare;
public class ExportOnlineDialog extends JDialog {
private static final long serialVersionUID = 1L;
private final JPanel contentPanel = new JPanel();
private final String filename;
private final String code;
private URI uri;
/**
* Create the dialog.
*/
public ExportOnlineDialog(String filename, String code) {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
close();
}
});
this.filename = filename;
this.code = code;
setTitle("Export to pseuco.com");
setBounds(100, 100, 349, 139);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
{
JLabel lblSubmittingYourFile = new JLabel("Submitting your file to pseuco.com ...");
lblSubmittingYourFile.setFont(new Font("Tahoma", Font.PLAIN, 12));
contentPanel.add(lblSubmittingYourFile);
}
{
Component verticalStrut = Box.createVerticalStrut(20);
verticalStrut.setMaximumSize(new Dimension(32767, 10));
verticalStrut.setMinimumSize(new Dimension(0, 10));
verticalStrut.setPreferredSize(new Dimension(0, 10));
contentPanel.add(verticalStrut);
}
{
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
contentPanel.add(progressBar);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
close();
dispose();
}
});
cancelButton.setPreferredSize(new Dimension(80, 28));
cancelButton.setFont(new Font("Tahoma", Font.PLAIN, 12));
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
private Thread exportThread;
@SuppressWarnings("deprecation")
private void close() {
if (exportThread != null){
exportThread.stop();
}
}
/**
* Should be called in EDT
*/
public void startExport(){
setVisible(true);
exportThread = new ExportThread();
exportThread.start();
}
private class ExportThread extends Thread{
@Override
public void run(){
try{
PseuCoShare pcs = new PseuCoShare();
uri = pcs.submitFile("pseuco", filename, code, true);
pcs.openInBrowser(uri);
}catch(Exception e){
final Exception e2 = e;
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(ExportOnlineDialog.this, "Error: "+e2.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
});
} catch (InvocationTargetException | InterruptedException e1) {
}
}
}
}
}
......@@ -6,6 +6,7 @@
package gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.KeyEventDispatcher;
......@@ -22,6 +23,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
......@@ -31,21 +33,22 @@ import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.JToolBar;
import javax.swing.JSplitPane;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;
import jsyntaxpane.DefaultSyntaxKit;
import start.Start;
import compiler.Pseuco;
import jsyntaxpane.DefaultSyntaxKit;
public class GUI extends JFrame {
......@@ -57,7 +60,7 @@ public class GUI extends JFrame {
private JEditorPane input;
private JPopupMenu popupMenu;
private JMenuItem popUpRefresh, popUpOpen, popUpDelete, popUpRun, popUpNew, popUpRename;
private JButton menuOpen, menuRun, menuDebug, menuNew, menuSave, menuExport, menuStop, menuHelp;
private JButton menuOpen, menuRun, menuDebug, menuNew, menuSave, menuExport, menuStop, menuHelp, menuExportOnline;
private JLabel threadCount;
private FileList list;
......@@ -141,6 +144,8 @@ public class GUI extends JFrame {
debugBtn();
stopBtn();
toolBar.addSeparator();
exportOnlineBtn();
toolBar.addSeparator();
helpBtn();
Pseuco.setOnProgramFinished(new Runnable() {
......@@ -389,6 +394,24 @@ public class GUI extends JFrame {
toolBar.add(item);
}
private void exportOnlineBtn() {
Action action = new AbstractAction(){
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent arg0) {
String filename = currentProject;
if (!filename.isEmpty())
filename = new File(filename).getName();
new ExportOnlineDialog(filename, input.getText()).startExport();
}
};
action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control shift F5"));
menuExportOnline = new JButton(action);
helpItem(menuExportOnline, "Sends the file to pseuco.com (Ctrl+Shift+F5)", "/resources/resumebtnbig"); //TODO
}
/**
* Code View
*/
......
package com.pseuco;
import java.awt.Desktop;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import javax.json.Json;
import javax.json.JsonObject;
/**
* Submits files to the pseuCo.com-Sharing-API.
*
* @author Felix Freiberger
*
*/
public class PseuCoShare {
String apiUrl;
/**
* Initializes the object with a custom API endpoint.
*
* @param apiUrl
* The base URL. Must end with a slash.
*/
public PseuCoShare(String apiUrl) {
this.apiUrl = apiUrl;
}
/**
* Initializes the object with the default API endpoint.
*/
public PseuCoShare() {
this("http://pseuco.com/api/");
}
/**
* Opens a URL in the local Browser.
*
* @param uri
* The URL to open.
* @throws Exception
*/
public void openInBrowser(URI uri) throws Exception {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e) {
throw new Exception("Cannot open the Browser.", e);
}
}
/**
* Submits a file to the sharing server.
*
* @param file
* The file, as a JSON object.
* @param temporary
* Whether the sharing link should expire after a few minutes.
* Please use this if just want to open the link once.
* @return A URL which show the shared file when opened.
* @throws Exception
*/
public URI submitFile(JsonObject file, boolean temporary) throws Exception {
try {
URLConnection connection = new URL(apiUrl + "paste/add")
.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded);charset=UTF-8");
OutputStream output = connection.getOutputStream();
JsonObject data = Json.createObjectBuilder().add("file", file)
.add("temporary", temporary).build();
output.write(data.toString().getBytes(Charset.forName("UTF-8")));
InputStream response = connection.getInputStream();
JsonObject responseData = Json.createReader(response).readObject();
URI shareLink = new URI(responseData.getString("url"));
return shareLink;
} catch (Exception e) {
throw new Exception("Could not paste the file.", e);
}
}
/**
* Submits a file to the sharing server.
*
* @param fileType
* The type of the file, e.g. "pseuco", "ccs" or "lts".
* @param fileName
* The file name. Must not be null, but may be empty.
* @param fileContent
* The actual file content. Should be a valid file of the
* fileType specified.
* @param temporary
* Whether the sharing link should expire after a few minutes.
* Please use this if just want to open the link once.
* @return
* @throws Exception
*/
public URI submitFile(String fileType, String fileName, String fileContent,
boolean temporary) throws Exception {
JsonObject file = Json.createObjectBuilder().add("type", fileType)
.add("name", fileName).add("content", fileContent).build();
return submitFile(file, temporary);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment