project upload is functional!

This commit is contained in:
Branislav Stojkovic
2016-10-22 12:31:32 +02:00
parent f949fa5087
commit 7c6bd1804b
5 changed files with 231 additions and 47 deletions
@@ -8,4 +8,8 @@ public class CloudException extends Exception {
super(e);
}
public CloudException(String string) {
super(string);
}
}
@@ -1,7 +1,15 @@
package org.diylc.swing.plugins.cloud.presenter;
import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ComboBoxModel;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager;
@@ -15,7 +23,10 @@ public class CloudPresenter {
private static String USERNAME_KEY = "cloud.Username";
private static String TOKEN_KEY = "cloud.token";
private static String ERROR = "Error";
private final static Logger LOG = Logger.getLogger(CloudPresenter.class);
private static final Object SUCCESS = "Success";
private ServiceAPI service;
private String serviceUrl;
@@ -27,7 +38,7 @@ public class CloudPresenter {
public CloudPresenter(CloudListener listener) {
serviceUrl = ConfigurationManager.getInstance().readString(
ServiceAPI.URL_KEY, "http://www.diy-fever.com/diylc/api");
ServiceAPI.URL_KEY, "http://www.diy-fever.com/diylc/api/v1");
ProxyFactory factory = new ProxyFactory(new PhpFlatProxy());
service = factory.createProxy(ServiceAPI.class, serviceUrl);
if (service == null)
@@ -48,7 +59,7 @@ public class CloudPresenter {
throw new CloudException(e);
}
if (res == null || res.equals("Error")) {
if (res == null || res.equals(ERROR)) {
LOG.info("Login failed");
return false;
} else {
@@ -79,7 +90,7 @@ public class CloudPresenter {
} catch (Exception e) {
throw new CloudException(e);
}
if (res == null || res.equals("Error")) {
if (res == null || res.equals(ERROR)) {
LOG.info("Login failed");
return false;
} else {
@@ -103,6 +114,45 @@ public class CloudPresenter {
return loggedIn;
}
public String[] getCategories() throws CloudException {
Object res;
try {
res = service.getCategories();
if (res != null && res.equals(ERROR))
throw new CloudException(
"Could not fetch categories from the server.");
List<Object> cats = (List<Object>) res;
return cats.toArray(new String[0]);
} catch (Exception e) {
throw new CloudException(e);
}
}
public void upload(String projectName, String category, String description,
String keywords, String diylcVersion, RenderedImage thumbnail,
File project) throws IOException, CloudException {
String username = ConfigurationManager.getInstance().readString(
USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY,
null);
if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again.");
File thumbnailFile = File.createTempFile("upload-thumbnail", ".png");
if (ImageIO.write(thumbnail, "png", thumbnailFile)) {
try {
String res = service.upload(username, token, getMachineId(),
projectName, category, description, diylcVersion,
keywords, thumbnailFile, project);
if (!res.equals(SUCCESS))
throw new CloudException(res);
} catch (Exception e) {
throw new CloudException(e);
}
}
}
private String getMachineId() {
if (machineId == null) {
try {
@@ -151,8 +151,8 @@ public class DialogFactory {
return dialog;
}
public UploadDialog createUploadDialog(IPlugInPort plugInPort) {
UploadDialog dialog = new UploadDialog(mainFrame, plugInPort);
public UploadDialog createUploadDialog(IPlugInPort plugInPort, CloudPresenter cloudPresenter) {
UploadDialog dialog = new UploadDialog(mainFrame, plugInPort, cloudPresenter);
return dialog;
}
@@ -288,21 +288,41 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
FileFilterEnum.DIY.getFilter(), null,
FileFilterEnum.DIY.getExtensions()[0], null);
if (file != null) {
swingUI.executeBackgroundTask(new ITask<Void>() {
swingUI.executeBackgroundTask(new ITask<File>() {
@Override
public Void doInBackground() throws Exception {
public File doInBackground() throws Exception {
LOG.debug("Uploading from " + file.getAbsolutePath());
thumbnailPresenter.loadProjectFromFile(file
.getAbsolutePath());
return null;
return file;
}
@Override
public void complete(Void result) {
public void complete(File result) {
UploadDialog dialog = DialogFactory.getInstance()
.createUploadDialog(thumbnailPresenter);
.createUploadDialog(thumbnailPresenter,
cloudPresenter);
dialog.setVisible(true);
if (ButtonDialog.OK.equals(dialog
.getSelectedButtonCaption())) {
try {
cloudPresenter.upload(dialog.getName(), dialog
.getCategory(),
dialog.getDescription(), dialog
.getKeywords(), plugInPort
.getCurrentVersionNumber()
.toString(), dialog
.getThumbnail(), result);
swingUI.showMessage(
"The project has been uploaded to the cloud successfully. Thank you for your contribution!",
"Upload Success",
IView.INFORMATION_MESSAGE);
} catch (Exception e) {
swingUI.showMessage(e.getMessage(),
"Upload Error", IView.ERROR_MESSAGE);
}
}
}
@Override
@@ -8,6 +8,7 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.EnumSet;
import javax.swing.BorderFactory;
@@ -25,6 +26,7 @@ import javax.swing.event.ChangeListener;
import org.diylc.common.DrawOption;
import org.diylc.common.IPlugInPort;
import org.diylc.swing.plugins.cloud.presenter.CloudException;
import org.diylc.swing.plugins.cloud.presenter.CloudPresenter;
import org.diylc.swingframework.ButtonDialog;
@@ -37,9 +39,13 @@ public class UploadDialog extends ButtonDialog {
+ BULLET
+ "All content uploaded to DIY Cloud is shared under Creative Commons 3.0 Licence.<br>"
+ BULLET
+ "Only finished and resonably verified projects may be uploaded to the library.<br>"
+ "Only finished and resonably verified projects may be uploaded to the DIY Cloud.<br>"
+ BULLET
+ "Uploaded content may be reviewed and/or removed by the administrators.<br>"
+ "Please provide a meaningful project description to make it possible for others to find the project.<br>"
+ BULLET
+ "If you are building on top of someone else's work, please leave credits in the description.<br>"
+ BULLET
+ "Uploaded content may be reviewed, edited and removed by the administrators if it is not up to standards.<br>"
+ "</html>";
private JPanel mainPanel;
@@ -52,12 +58,15 @@ public class UploadDialog extends ButtonDialog {
private JTextField keywordsField;
private IPlugInPort plugInPort;
private CloudPresenter cloudPresenter;
public UploadDialog(JFrame owner, IPlugInPort plugInPort) {
public UploadDialog(JFrame owner, IPlugInPort plugInPort,
CloudPresenter cloudPresenter) {
super(owner, "Upload A Project", new String[] { OK, CANCEL });
this.plugInPort = plugInPort;
this.cloudPresenter = cloudPresenter;
layoutGui();
refreshState();
getButton(OK).setEnabled(false);
}
@Override
@@ -67,20 +76,65 @@ public class UploadDialog extends ButtonDialog {
mainPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 4;
mainPanel.add(getThumbnailPanel(), gbc);
gbc.gridheight = 1;
gbc.gridx = 1;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(new JLabel("Project Name: "), gbc);
gbc.gridx = 2;
gbc.weightx = 1;
mainPanel.add(getNameField(), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 0;
mainPanel.add(new JLabel("Category:"), gbc);
gbc.gridx = 2;
gbc.weightx = 1;
mainPanel.add(getCategoryBox(), gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 0;
mainPanel.add(new JLabel("Description: "), gbc);
gbc.gridx = 2;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(getDescriptionArea(), gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(new JLabel("Keywords: "), gbc);
gbc.gridx = 2;
gbc.weightx = 1;
mainPanel.add(getKeywordsField(), gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(getTermsLabel(), gbc);
gbc.gridy = 2;
gbc.gridy = 5;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
@@ -90,10 +144,6 @@ public class UploadDialog extends ButtonDialog {
return mainPanel;
}
private void refreshState() {
getButton(OK).setEnabled(getAgreeBox().isSelected());
}
private JCheckBox getAgreeBox() {
if (agreeBox == null) {
agreeBox = new JCheckBox("I agree to these terms");
@@ -101,7 +151,7 @@ public class UploadDialog extends ButtonDialog {
@Override
public void stateChanged(ChangeEvent e) {
refreshState();
getButton(OK).setEnabled(getAgreeBox().isSelected());
}
});
}
@@ -121,7 +171,7 @@ public class UploadDialog extends ButtonDialog {
return termsLabel;
}
public JPanel getThumbnailPanel() {
private JPanel getThumbnailPanel() {
if (thumbnailPanel == null) {
thumbnailPanel = new JPanel(new GridBagLayout());
@@ -141,27 +191,7 @@ public class UploadDialog extends ButtonDialog {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
Dimension d = UploadDialog.this.plugInPort
.getCanvasDimensions(false);
Rectangle rect = getBounds();
g2d.setColor(Color.white);
g2d.fill(rect);
double projectRatio = d.getWidth() / d.getHeight();
double actualRatio = rect.getWidth() / rect.getHeight();
double zoomRatio;
if (projectRatio > actualRatio) {
zoomRatio = rect.getWidth() / d.getWidth();
} else {
zoomRatio = rect.getHeight() / d.getHeight();
}
g2d.scale(zoomRatio, zoomRatio);
UploadDialog.this.plugInPort.draw(g2d,
EnumSet.noneOf(DrawOption.class), null);
paintThumbnail(g, getBounds());
}
}, gbc);
Dimension d = UploadDialog.this.plugInPort
@@ -177,19 +207,99 @@ public class UploadDialog extends ButtonDialog {
return thumbnailPanel;
}
public JTextField getNameField() {
private JTextField getNameField() {
if (nameField == null) {
nameField = new JTextField();
// set default from the project
for (int i = 0; i < plugInPort.getProjectProperties().size(); i++)
if (plugInPort.getProjectProperties().get(i).getName()
.equals("Title"))
nameField.setText(plugInPort.getProjectProperties().get(i)
.getValue().toString());
}
return nameField;
}
public JComboBox getCategoryBox() {
private JComboBox getCategoryBox() {
if (categoryBox == null) {
try {
categoryBox = new JComboBox(cloudPresenter.getCategories());
} catch (CloudException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return categoryBox;
}
public JTextArea getDescriptionArea() {
private JTextArea getDescriptionArea() {
if (descriptionArea == null) {
descriptionArea = new JTextArea();
// set default from the project
for (int i = 0; i < plugInPort.getProjectProperties().size(); i++)
if (plugInPort.getProjectProperties().get(i).getName()
.equals("Description"))
descriptionArea.setText(plugInPort.getProjectProperties()
.get(i).getValue().toString());
descriptionArea.setBorder(getNameField().getBorder());
descriptionArea.setFont(getNameField().getFont());
}
return descriptionArea;
}
public JTextField getKeywordsField() {
private JTextField getKeywordsField() {
if (keywordsField == null) {
keywordsField = new JTextField();
}
return keywordsField;
}
public String getName() {
return getNameField().getText();
}
public String getDescription() {
return getDescriptionArea().getText();
}
public String getCategory() {
return getCategoryBox().getSelectedItem().toString();
}
public BufferedImage getThumbnail() {
BufferedImage thumbnailImage = new BufferedImage(getThumbnailPanel()
.getWidth(), getThumbnailPanel().getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D cg = thumbnailImage.createGraphics();
paintThumbnail(cg, new Rectangle(thumbnailImage.getWidth(), thumbnailImage.getHeight()));
return thumbnailImage;
}
private void paintThumbnail(Graphics g, Rectangle rect) {
Graphics2D g2d = (Graphics2D) g;
Dimension d = UploadDialog.this.plugInPort
.getCanvasDimensions(false);
g2d.setColor(Color.white);
g2d.fill(rect);
double projectRatio = d.getWidth() / d.getHeight();
double actualRatio = rect.getWidth() / rect.getHeight();
double zoomRatio;
if (projectRatio > actualRatio) {
zoomRatio = rect.getWidth() / d.getWidth();
} else {
zoomRatio = rect.getHeight() / d.getHeight();
}
g2d.scale(zoomRatio, zoomRatio);
UploadDialog.this.plugInPort.draw(g2d,
EnumSet.noneOf(DrawOption.class), null);
}
public String getKeywords() {
return getKeywordsField().getText();
}
}